GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / gpu / drm / radeon / si.c
1 /*
2  * Copyright 2011 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Alex Deucher
23  */
24 #include <linux/firmware.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <drm/drmP.h>
28 #include "radeon.h"
29 #include "radeon_asic.h"
30 #include "radeon_audio.h"
31 #include <drm/radeon_drm.h>
32 #include "sid.h"
33 #include "atom.h"
34 #include "si_blit_shaders.h"
35 #include "clearstate_si.h"
36 #include "radeon_ucode.h"
37
38
39 /*(DEBLOBBED)*/
40
41 static u32 si_get_cu_active_bitmap(struct radeon_device *rdev, u32 se, u32 sh);
42 static void si_pcie_gen3_enable(struct radeon_device *rdev);
43 static void si_program_aspm(struct radeon_device *rdev);
44 extern void sumo_rlc_fini(struct radeon_device *rdev);
45 extern int sumo_rlc_init(struct radeon_device *rdev);
46 extern int r600_ih_ring_alloc(struct radeon_device *rdev);
47 extern void r600_ih_ring_fini(struct radeon_device *rdev);
48 extern void evergreen_fix_pci_max_read_req_size(struct radeon_device *rdev);
49 extern void evergreen_mc_stop(struct radeon_device *rdev, struct evergreen_mc_save *save);
50 extern void evergreen_mc_resume(struct radeon_device *rdev, struct evergreen_mc_save *save);
51 extern u32 evergreen_get_number_of_dram_channels(struct radeon_device *rdev);
52 extern void evergreen_print_gpu_status_regs(struct radeon_device *rdev);
53 extern bool evergreen_is_display_hung(struct radeon_device *rdev);
54 static void si_enable_gui_idle_interrupt(struct radeon_device *rdev,
55                                          bool enable);
56 static void si_init_pg(struct radeon_device *rdev);
57 static void si_init_cg(struct radeon_device *rdev);
58 static void si_fini_pg(struct radeon_device *rdev);
59 static void si_fini_cg(struct radeon_device *rdev);
60 static void si_rlc_stop(struct radeon_device *rdev);
61
62 static const u32 verde_rlc_save_restore_register_list[] =
63 {
64         (0x8000 << 16) | (0x98f4 >> 2),
65         0x00000000,
66         (0x8040 << 16) | (0x98f4 >> 2),
67         0x00000000,
68         (0x8000 << 16) | (0xe80 >> 2),
69         0x00000000,
70         (0x8040 << 16) | (0xe80 >> 2),
71         0x00000000,
72         (0x8000 << 16) | (0x89bc >> 2),
73         0x00000000,
74         (0x8040 << 16) | (0x89bc >> 2),
75         0x00000000,
76         (0x8000 << 16) | (0x8c1c >> 2),
77         0x00000000,
78         (0x8040 << 16) | (0x8c1c >> 2),
79         0x00000000,
80         (0x9c00 << 16) | (0x98f0 >> 2),
81         0x00000000,
82         (0x9c00 << 16) | (0xe7c >> 2),
83         0x00000000,
84         (0x8000 << 16) | (0x9148 >> 2),
85         0x00000000,
86         (0x8040 << 16) | (0x9148 >> 2),
87         0x00000000,
88         (0x9c00 << 16) | (0x9150 >> 2),
89         0x00000000,
90         (0x9c00 << 16) | (0x897c >> 2),
91         0x00000000,
92         (0x9c00 << 16) | (0x8d8c >> 2),
93         0x00000000,
94         (0x9c00 << 16) | (0xac54 >> 2),
95         0X00000000,
96         0x3,
97         (0x9c00 << 16) | (0x98f8 >> 2),
98         0x00000000,
99         (0x9c00 << 16) | (0x9910 >> 2),
100         0x00000000,
101         (0x9c00 << 16) | (0x9914 >> 2),
102         0x00000000,
103         (0x9c00 << 16) | (0x9918 >> 2),
104         0x00000000,
105         (0x9c00 << 16) | (0x991c >> 2),
106         0x00000000,
107         (0x9c00 << 16) | (0x9920 >> 2),
108         0x00000000,
109         (0x9c00 << 16) | (0x9924 >> 2),
110         0x00000000,
111         (0x9c00 << 16) | (0x9928 >> 2),
112         0x00000000,
113         (0x9c00 << 16) | (0x992c >> 2),
114         0x00000000,
115         (0x9c00 << 16) | (0x9930 >> 2),
116         0x00000000,
117         (0x9c00 << 16) | (0x9934 >> 2),
118         0x00000000,
119         (0x9c00 << 16) | (0x9938 >> 2),
120         0x00000000,
121         (0x9c00 << 16) | (0x993c >> 2),
122         0x00000000,
123         (0x9c00 << 16) | (0x9940 >> 2),
124         0x00000000,
125         (0x9c00 << 16) | (0x9944 >> 2),
126         0x00000000,
127         (0x9c00 << 16) | (0x9948 >> 2),
128         0x00000000,
129         (0x9c00 << 16) | (0x994c >> 2),
130         0x00000000,
131         (0x9c00 << 16) | (0x9950 >> 2),
132         0x00000000,
133         (0x9c00 << 16) | (0x9954 >> 2),
134         0x00000000,
135         (0x9c00 << 16) | (0x9958 >> 2),
136         0x00000000,
137         (0x9c00 << 16) | (0x995c >> 2),
138         0x00000000,
139         (0x9c00 << 16) | (0x9960 >> 2),
140         0x00000000,
141         (0x9c00 << 16) | (0x9964 >> 2),
142         0x00000000,
143         (0x9c00 << 16) | (0x9968 >> 2),
144         0x00000000,
145         (0x9c00 << 16) | (0x996c >> 2),
146         0x00000000,
147         (0x9c00 << 16) | (0x9970 >> 2),
148         0x00000000,
149         (0x9c00 << 16) | (0x9974 >> 2),
150         0x00000000,
151         (0x9c00 << 16) | (0x9978 >> 2),
152         0x00000000,
153         (0x9c00 << 16) | (0x997c >> 2),
154         0x00000000,
155         (0x9c00 << 16) | (0x9980 >> 2),
156         0x00000000,
157         (0x9c00 << 16) | (0x9984 >> 2),
158         0x00000000,
159         (0x9c00 << 16) | (0x9988 >> 2),
160         0x00000000,
161         (0x9c00 << 16) | (0x998c >> 2),
162         0x00000000,
163         (0x9c00 << 16) | (0x8c00 >> 2),
164         0x00000000,
165         (0x9c00 << 16) | (0x8c14 >> 2),
166         0x00000000,
167         (0x9c00 << 16) | (0x8c04 >> 2),
168         0x00000000,
169         (0x9c00 << 16) | (0x8c08 >> 2),
170         0x00000000,
171         (0x8000 << 16) | (0x9b7c >> 2),
172         0x00000000,
173         (0x8040 << 16) | (0x9b7c >> 2),
174         0x00000000,
175         (0x8000 << 16) | (0xe84 >> 2),
176         0x00000000,
177         (0x8040 << 16) | (0xe84 >> 2),
178         0x00000000,
179         (0x8000 << 16) | (0x89c0 >> 2),
180         0x00000000,
181         (0x8040 << 16) | (0x89c0 >> 2),
182         0x00000000,
183         (0x8000 << 16) | (0x914c >> 2),
184         0x00000000,
185         (0x8040 << 16) | (0x914c >> 2),
186         0x00000000,
187         (0x8000 << 16) | (0x8c20 >> 2),
188         0x00000000,
189         (0x8040 << 16) | (0x8c20 >> 2),
190         0x00000000,
191         (0x8000 << 16) | (0x9354 >> 2),
192         0x00000000,
193         (0x8040 << 16) | (0x9354 >> 2),
194         0x00000000,
195         (0x9c00 << 16) | (0x9060 >> 2),
196         0x00000000,
197         (0x9c00 << 16) | (0x9364 >> 2),
198         0x00000000,
199         (0x9c00 << 16) | (0x9100 >> 2),
200         0x00000000,
201         (0x9c00 << 16) | (0x913c >> 2),
202         0x00000000,
203         (0x8000 << 16) | (0x90e0 >> 2),
204         0x00000000,
205         (0x8000 << 16) | (0x90e4 >> 2),
206         0x00000000,
207         (0x8000 << 16) | (0x90e8 >> 2),
208         0x00000000,
209         (0x8040 << 16) | (0x90e0 >> 2),
210         0x00000000,
211         (0x8040 << 16) | (0x90e4 >> 2),
212         0x00000000,
213         (0x8040 << 16) | (0x90e8 >> 2),
214         0x00000000,
215         (0x9c00 << 16) | (0x8bcc >> 2),
216         0x00000000,
217         (0x9c00 << 16) | (0x8b24 >> 2),
218         0x00000000,
219         (0x9c00 << 16) | (0x88c4 >> 2),
220         0x00000000,
221         (0x9c00 << 16) | (0x8e50 >> 2),
222         0x00000000,
223         (0x9c00 << 16) | (0x8c0c >> 2),
224         0x00000000,
225         (0x9c00 << 16) | (0x8e58 >> 2),
226         0x00000000,
227         (0x9c00 << 16) | (0x8e5c >> 2),
228         0x00000000,
229         (0x9c00 << 16) | (0x9508 >> 2),
230         0x00000000,
231         (0x9c00 << 16) | (0x950c >> 2),
232         0x00000000,
233         (0x9c00 << 16) | (0x9494 >> 2),
234         0x00000000,
235         (0x9c00 << 16) | (0xac0c >> 2),
236         0x00000000,
237         (0x9c00 << 16) | (0xac10 >> 2),
238         0x00000000,
239         (0x9c00 << 16) | (0xac14 >> 2),
240         0x00000000,
241         (0x9c00 << 16) | (0xae00 >> 2),
242         0x00000000,
243         (0x9c00 << 16) | (0xac08 >> 2),
244         0x00000000,
245         (0x9c00 << 16) | (0x88d4 >> 2),
246         0x00000000,
247         (0x9c00 << 16) | (0x88c8 >> 2),
248         0x00000000,
249         (0x9c00 << 16) | (0x88cc >> 2),
250         0x00000000,
251         (0x9c00 << 16) | (0x89b0 >> 2),
252         0x00000000,
253         (0x9c00 << 16) | (0x8b10 >> 2),
254         0x00000000,
255         (0x9c00 << 16) | (0x8a14 >> 2),
256         0x00000000,
257         (0x9c00 << 16) | (0x9830 >> 2),
258         0x00000000,
259         (0x9c00 << 16) | (0x9834 >> 2),
260         0x00000000,
261         (0x9c00 << 16) | (0x9838 >> 2),
262         0x00000000,
263         (0x9c00 << 16) | (0x9a10 >> 2),
264         0x00000000,
265         (0x8000 << 16) | (0x9870 >> 2),
266         0x00000000,
267         (0x8000 << 16) | (0x9874 >> 2),
268         0x00000000,
269         (0x8001 << 16) | (0x9870 >> 2),
270         0x00000000,
271         (0x8001 << 16) | (0x9874 >> 2),
272         0x00000000,
273         (0x8040 << 16) | (0x9870 >> 2),
274         0x00000000,
275         (0x8040 << 16) | (0x9874 >> 2),
276         0x00000000,
277         (0x8041 << 16) | (0x9870 >> 2),
278         0x00000000,
279         (0x8041 << 16) | (0x9874 >> 2),
280         0x00000000,
281         0x00000000
282 };
283
284 static const u32 tahiti_golden_rlc_registers[] =
285 {
286         0xc424, 0xffffffff, 0x00601005,
287         0xc47c, 0xffffffff, 0x10104040,
288         0xc488, 0xffffffff, 0x0100000a,
289         0xc314, 0xffffffff, 0x00000800,
290         0xc30c, 0xffffffff, 0x800000f4,
291         0xf4a8, 0xffffffff, 0x00000000
292 };
293
294 static const u32 tahiti_golden_registers[] =
295 {
296         0x9a10, 0x00010000, 0x00018208,
297         0x9830, 0xffffffff, 0x00000000,
298         0x9834, 0xf00fffff, 0x00000400,
299         0x9838, 0x0002021c, 0x00020200,
300         0xc78, 0x00000080, 0x00000000,
301         0xd030, 0x000300c0, 0x00800040,
302         0xd830, 0x000300c0, 0x00800040,
303         0x5bb0, 0x000000f0, 0x00000070,
304         0x5bc0, 0x00200000, 0x50100000,
305         0x7030, 0x31000311, 0x00000011,
306         0x277c, 0x00000003, 0x000007ff,
307         0x240c, 0x000007ff, 0x00000000,
308         0x8a14, 0xf000001f, 0x00000007,
309         0x8b24, 0xffffffff, 0x00ffffff,
310         0x8b10, 0x0000ff0f, 0x00000000,
311         0x28a4c, 0x07ffffff, 0x4e000000,
312         0x28350, 0x3f3f3fff, 0x2a00126a,
313         0x30, 0x000000ff, 0x0040,
314         0x34, 0x00000040, 0x00004040,
315         0x9100, 0x07ffffff, 0x03000000,
316         0x8e88, 0x01ff1f3f, 0x00000000,
317         0x8e84, 0x01ff1f3f, 0x00000000,
318         0x9060, 0x0000007f, 0x00000020,
319         0x9508, 0x00010000, 0x00010000,
320         0xac14, 0x00000200, 0x000002fb,
321         0xac10, 0xffffffff, 0x0000543b,
322         0xac0c, 0xffffffff, 0xa9210876,
323         0x88d0, 0xffffffff, 0x000fff40,
324         0x88d4, 0x0000001f, 0x00000010,
325         0x1410, 0x20000000, 0x20fffed8,
326         0x15c0, 0x000c0fc0, 0x000c0400
327 };
328
329 static const u32 tahiti_golden_registers2[] =
330 {
331         0xc64, 0x00000001, 0x00000001
332 };
333
334 static const u32 pitcairn_golden_rlc_registers[] =
335 {
336         0xc424, 0xffffffff, 0x00601004,
337         0xc47c, 0xffffffff, 0x10102020,
338         0xc488, 0xffffffff, 0x01000020,
339         0xc314, 0xffffffff, 0x00000800,
340         0xc30c, 0xffffffff, 0x800000a4
341 };
342
343 static const u32 pitcairn_golden_registers[] =
344 {
345         0x9a10, 0x00010000, 0x00018208,
346         0x9830, 0xffffffff, 0x00000000,
347         0x9834, 0xf00fffff, 0x00000400,
348         0x9838, 0x0002021c, 0x00020200,
349         0xc78, 0x00000080, 0x00000000,
350         0xd030, 0x000300c0, 0x00800040,
351         0xd830, 0x000300c0, 0x00800040,
352         0x5bb0, 0x000000f0, 0x00000070,
353         0x5bc0, 0x00200000, 0x50100000,
354         0x7030, 0x31000311, 0x00000011,
355         0x2ae4, 0x00073ffe, 0x000022a2,
356         0x240c, 0x000007ff, 0x00000000,
357         0x8a14, 0xf000001f, 0x00000007,
358         0x8b24, 0xffffffff, 0x00ffffff,
359         0x8b10, 0x0000ff0f, 0x00000000,
360         0x28a4c, 0x07ffffff, 0x4e000000,
361         0x28350, 0x3f3f3fff, 0x2a00126a,
362         0x30, 0x000000ff, 0x0040,
363         0x34, 0x00000040, 0x00004040,
364         0x9100, 0x07ffffff, 0x03000000,
365         0x9060, 0x0000007f, 0x00000020,
366         0x9508, 0x00010000, 0x00010000,
367         0xac14, 0x000003ff, 0x000000f7,
368         0xac10, 0xffffffff, 0x00000000,
369         0xac0c, 0xffffffff, 0x32761054,
370         0x88d4, 0x0000001f, 0x00000010,
371         0x15c0, 0x000c0fc0, 0x000c0400
372 };
373
374 static const u32 verde_golden_rlc_registers[] =
375 {
376         0xc424, 0xffffffff, 0x033f1005,
377         0xc47c, 0xffffffff, 0x10808020,
378         0xc488, 0xffffffff, 0x00800008,
379         0xc314, 0xffffffff, 0x00001000,
380         0xc30c, 0xffffffff, 0x80010014
381 };
382
383 static const u32 verde_golden_registers[] =
384 {
385         0x9a10, 0x00010000, 0x00018208,
386         0x9830, 0xffffffff, 0x00000000,
387         0x9834, 0xf00fffff, 0x00000400,
388         0x9838, 0x0002021c, 0x00020200,
389         0xc78, 0x00000080, 0x00000000,
390         0xd030, 0x000300c0, 0x00800040,
391         0xd030, 0x000300c0, 0x00800040,
392         0xd830, 0x000300c0, 0x00800040,
393         0xd830, 0x000300c0, 0x00800040,
394         0x5bb0, 0x000000f0, 0x00000070,
395         0x5bc0, 0x00200000, 0x50100000,
396         0x7030, 0x31000311, 0x00000011,
397         0x2ae4, 0x00073ffe, 0x000022a2,
398         0x2ae4, 0x00073ffe, 0x000022a2,
399         0x2ae4, 0x00073ffe, 0x000022a2,
400         0x240c, 0x000007ff, 0x00000000,
401         0x240c, 0x000007ff, 0x00000000,
402         0x240c, 0x000007ff, 0x00000000,
403         0x8a14, 0xf000001f, 0x00000007,
404         0x8a14, 0xf000001f, 0x00000007,
405         0x8a14, 0xf000001f, 0x00000007,
406         0x8b24, 0xffffffff, 0x00ffffff,
407         0x8b10, 0x0000ff0f, 0x00000000,
408         0x28a4c, 0x07ffffff, 0x4e000000,
409         0x28350, 0x3f3f3fff, 0x0000124a,
410         0x28350, 0x3f3f3fff, 0x0000124a,
411         0x28350, 0x3f3f3fff, 0x0000124a,
412         0x30, 0x000000ff, 0x0040,
413         0x34, 0x00000040, 0x00004040,
414         0x9100, 0x07ffffff, 0x03000000,
415         0x9100, 0x07ffffff, 0x03000000,
416         0x8e88, 0x01ff1f3f, 0x00000000,
417         0x8e88, 0x01ff1f3f, 0x00000000,
418         0x8e88, 0x01ff1f3f, 0x00000000,
419         0x8e84, 0x01ff1f3f, 0x00000000,
420         0x8e84, 0x01ff1f3f, 0x00000000,
421         0x8e84, 0x01ff1f3f, 0x00000000,
422         0x9060, 0x0000007f, 0x00000020,
423         0x9508, 0x00010000, 0x00010000,
424         0xac14, 0x000003ff, 0x00000003,
425         0xac14, 0x000003ff, 0x00000003,
426         0xac14, 0x000003ff, 0x00000003,
427         0xac10, 0xffffffff, 0x00000000,
428         0xac10, 0xffffffff, 0x00000000,
429         0xac10, 0xffffffff, 0x00000000,
430         0xac0c, 0xffffffff, 0x00001032,
431         0xac0c, 0xffffffff, 0x00001032,
432         0xac0c, 0xffffffff, 0x00001032,
433         0x88d4, 0x0000001f, 0x00000010,
434         0x88d4, 0x0000001f, 0x00000010,
435         0x88d4, 0x0000001f, 0x00000010,
436         0x15c0, 0x000c0fc0, 0x000c0400
437 };
438
439 static const u32 oland_golden_rlc_registers[] =
440 {
441         0xc424, 0xffffffff, 0x00601005,
442         0xc47c, 0xffffffff, 0x10104040,
443         0xc488, 0xffffffff, 0x0100000a,
444         0xc314, 0xffffffff, 0x00000800,
445         0xc30c, 0xffffffff, 0x800000f4
446 };
447
448 static const u32 oland_golden_registers[] =
449 {
450         0x9a10, 0x00010000, 0x00018208,
451         0x9830, 0xffffffff, 0x00000000,
452         0x9834, 0xf00fffff, 0x00000400,
453         0x9838, 0x0002021c, 0x00020200,
454         0xc78, 0x00000080, 0x00000000,
455         0xd030, 0x000300c0, 0x00800040,
456         0xd830, 0x000300c0, 0x00800040,
457         0x5bb0, 0x000000f0, 0x00000070,
458         0x5bc0, 0x00200000, 0x50100000,
459         0x7030, 0x31000311, 0x00000011,
460         0x2ae4, 0x00073ffe, 0x000022a2,
461         0x240c, 0x000007ff, 0x00000000,
462         0x8a14, 0xf000001f, 0x00000007,
463         0x8b24, 0xffffffff, 0x00ffffff,
464         0x8b10, 0x0000ff0f, 0x00000000,
465         0x28a4c, 0x07ffffff, 0x4e000000,
466         0x28350, 0x3f3f3fff, 0x00000082,
467         0x30, 0x000000ff, 0x0040,
468         0x34, 0x00000040, 0x00004040,
469         0x9100, 0x07ffffff, 0x03000000,
470         0x9060, 0x0000007f, 0x00000020,
471         0x9508, 0x00010000, 0x00010000,
472         0xac14, 0x000003ff, 0x000000f3,
473         0xac10, 0xffffffff, 0x00000000,
474         0xac0c, 0xffffffff, 0x00003210,
475         0x88d4, 0x0000001f, 0x00000010,
476         0x15c0, 0x000c0fc0, 0x000c0400
477 };
478
479 static const u32 hainan_golden_registers[] =
480 {
481         0x9a10, 0x00010000, 0x00018208,
482         0x9830, 0xffffffff, 0x00000000,
483         0x9834, 0xf00fffff, 0x00000400,
484         0x9838, 0x0002021c, 0x00020200,
485         0xd0c0, 0xff000fff, 0x00000100,
486         0xd030, 0x000300c0, 0x00800040,
487         0xd8c0, 0xff000fff, 0x00000100,
488         0xd830, 0x000300c0, 0x00800040,
489         0x2ae4, 0x00073ffe, 0x000022a2,
490         0x240c, 0x000007ff, 0x00000000,
491         0x8a14, 0xf000001f, 0x00000007,
492         0x8b24, 0xffffffff, 0x00ffffff,
493         0x8b10, 0x0000ff0f, 0x00000000,
494         0x28a4c, 0x07ffffff, 0x4e000000,
495         0x28350, 0x3f3f3fff, 0x00000000,
496         0x30, 0x000000ff, 0x0040,
497         0x34, 0x00000040, 0x00004040,
498         0x9100, 0x03e00000, 0x03600000,
499         0x9060, 0x0000007f, 0x00000020,
500         0x9508, 0x00010000, 0x00010000,
501         0xac14, 0x000003ff, 0x000000f1,
502         0xac10, 0xffffffff, 0x00000000,
503         0xac0c, 0xffffffff, 0x00003210,
504         0x88d4, 0x0000001f, 0x00000010,
505         0x15c0, 0x000c0fc0, 0x000c0400
506 };
507
508 static const u32 hainan_golden_registers2[] =
509 {
510         0x98f8, 0xffffffff, 0x02010001
511 };
512
513 static const u32 tahiti_mgcg_cgcg_init[] =
514 {
515         0xc400, 0xffffffff, 0xfffffffc,
516         0x802c, 0xffffffff, 0xe0000000,
517         0x9a60, 0xffffffff, 0x00000100,
518         0x92a4, 0xffffffff, 0x00000100,
519         0xc164, 0xffffffff, 0x00000100,
520         0x9774, 0xffffffff, 0x00000100,
521         0x8984, 0xffffffff, 0x06000100,
522         0x8a18, 0xffffffff, 0x00000100,
523         0x92a0, 0xffffffff, 0x00000100,
524         0xc380, 0xffffffff, 0x00000100,
525         0x8b28, 0xffffffff, 0x00000100,
526         0x9144, 0xffffffff, 0x00000100,
527         0x8d88, 0xffffffff, 0x00000100,
528         0x8d8c, 0xffffffff, 0x00000100,
529         0x9030, 0xffffffff, 0x00000100,
530         0x9034, 0xffffffff, 0x00000100,
531         0x9038, 0xffffffff, 0x00000100,
532         0x903c, 0xffffffff, 0x00000100,
533         0xad80, 0xffffffff, 0x00000100,
534         0xac54, 0xffffffff, 0x00000100,
535         0x897c, 0xffffffff, 0x06000100,
536         0x9868, 0xffffffff, 0x00000100,
537         0x9510, 0xffffffff, 0x00000100,
538         0xaf04, 0xffffffff, 0x00000100,
539         0xae04, 0xffffffff, 0x00000100,
540         0x949c, 0xffffffff, 0x00000100,
541         0x802c, 0xffffffff, 0xe0000000,
542         0x9160, 0xffffffff, 0x00010000,
543         0x9164, 0xffffffff, 0x00030002,
544         0x9168, 0xffffffff, 0x00040007,
545         0x916c, 0xffffffff, 0x00060005,
546         0x9170, 0xffffffff, 0x00090008,
547         0x9174, 0xffffffff, 0x00020001,
548         0x9178, 0xffffffff, 0x00040003,
549         0x917c, 0xffffffff, 0x00000007,
550         0x9180, 0xffffffff, 0x00060005,
551         0x9184, 0xffffffff, 0x00090008,
552         0x9188, 0xffffffff, 0x00030002,
553         0x918c, 0xffffffff, 0x00050004,
554         0x9190, 0xffffffff, 0x00000008,
555         0x9194, 0xffffffff, 0x00070006,
556         0x9198, 0xffffffff, 0x000a0009,
557         0x919c, 0xffffffff, 0x00040003,
558         0x91a0, 0xffffffff, 0x00060005,
559         0x91a4, 0xffffffff, 0x00000009,
560         0x91a8, 0xffffffff, 0x00080007,
561         0x91ac, 0xffffffff, 0x000b000a,
562         0x91b0, 0xffffffff, 0x00050004,
563         0x91b4, 0xffffffff, 0x00070006,
564         0x91b8, 0xffffffff, 0x0008000b,
565         0x91bc, 0xffffffff, 0x000a0009,
566         0x91c0, 0xffffffff, 0x000d000c,
567         0x91c4, 0xffffffff, 0x00060005,
568         0x91c8, 0xffffffff, 0x00080007,
569         0x91cc, 0xffffffff, 0x0000000b,
570         0x91d0, 0xffffffff, 0x000a0009,
571         0x91d4, 0xffffffff, 0x000d000c,
572         0x91d8, 0xffffffff, 0x00070006,
573         0x91dc, 0xffffffff, 0x00090008,
574         0x91e0, 0xffffffff, 0x0000000c,
575         0x91e4, 0xffffffff, 0x000b000a,
576         0x91e8, 0xffffffff, 0x000e000d,
577         0x91ec, 0xffffffff, 0x00080007,
578         0x91f0, 0xffffffff, 0x000a0009,
579         0x91f4, 0xffffffff, 0x0000000d,
580         0x91f8, 0xffffffff, 0x000c000b,
581         0x91fc, 0xffffffff, 0x000f000e,
582         0x9200, 0xffffffff, 0x00090008,
583         0x9204, 0xffffffff, 0x000b000a,
584         0x9208, 0xffffffff, 0x000c000f,
585         0x920c, 0xffffffff, 0x000e000d,
586         0x9210, 0xffffffff, 0x00110010,
587         0x9214, 0xffffffff, 0x000a0009,
588         0x9218, 0xffffffff, 0x000c000b,
589         0x921c, 0xffffffff, 0x0000000f,
590         0x9220, 0xffffffff, 0x000e000d,
591         0x9224, 0xffffffff, 0x00110010,
592         0x9228, 0xffffffff, 0x000b000a,
593         0x922c, 0xffffffff, 0x000d000c,
594         0x9230, 0xffffffff, 0x00000010,
595         0x9234, 0xffffffff, 0x000f000e,
596         0x9238, 0xffffffff, 0x00120011,
597         0x923c, 0xffffffff, 0x000c000b,
598         0x9240, 0xffffffff, 0x000e000d,
599         0x9244, 0xffffffff, 0x00000011,
600         0x9248, 0xffffffff, 0x0010000f,
601         0x924c, 0xffffffff, 0x00130012,
602         0x9250, 0xffffffff, 0x000d000c,
603         0x9254, 0xffffffff, 0x000f000e,
604         0x9258, 0xffffffff, 0x00100013,
605         0x925c, 0xffffffff, 0x00120011,
606         0x9260, 0xffffffff, 0x00150014,
607         0x9264, 0xffffffff, 0x000e000d,
608         0x9268, 0xffffffff, 0x0010000f,
609         0x926c, 0xffffffff, 0x00000013,
610         0x9270, 0xffffffff, 0x00120011,
611         0x9274, 0xffffffff, 0x00150014,
612         0x9278, 0xffffffff, 0x000f000e,
613         0x927c, 0xffffffff, 0x00110010,
614         0x9280, 0xffffffff, 0x00000014,
615         0x9284, 0xffffffff, 0x00130012,
616         0x9288, 0xffffffff, 0x00160015,
617         0x928c, 0xffffffff, 0x0010000f,
618         0x9290, 0xffffffff, 0x00120011,
619         0x9294, 0xffffffff, 0x00000015,
620         0x9298, 0xffffffff, 0x00140013,
621         0x929c, 0xffffffff, 0x00170016,
622         0x9150, 0xffffffff, 0x96940200,
623         0x8708, 0xffffffff, 0x00900100,
624         0xc478, 0xffffffff, 0x00000080,
625         0xc404, 0xffffffff, 0x0020003f,
626         0x30, 0xffffffff, 0x0000001c,
627         0x34, 0x000f0000, 0x000f0000,
628         0x160c, 0xffffffff, 0x00000100,
629         0x1024, 0xffffffff, 0x00000100,
630         0x102c, 0x00000101, 0x00000000,
631         0x20a8, 0xffffffff, 0x00000104,
632         0x264c, 0x000c0000, 0x000c0000,
633         0x2648, 0x000c0000, 0x000c0000,
634         0x55e4, 0xff000fff, 0x00000100,
635         0x55e8, 0x00000001, 0x00000001,
636         0x2f50, 0x00000001, 0x00000001,
637         0x30cc, 0xc0000fff, 0x00000104,
638         0xc1e4, 0x00000001, 0x00000001,
639         0xd0c0, 0xfffffff0, 0x00000100,
640         0xd8c0, 0xfffffff0, 0x00000100
641 };
642
643 static const u32 pitcairn_mgcg_cgcg_init[] =
644 {
645         0xc400, 0xffffffff, 0xfffffffc,
646         0x802c, 0xffffffff, 0xe0000000,
647         0x9a60, 0xffffffff, 0x00000100,
648         0x92a4, 0xffffffff, 0x00000100,
649         0xc164, 0xffffffff, 0x00000100,
650         0x9774, 0xffffffff, 0x00000100,
651         0x8984, 0xffffffff, 0x06000100,
652         0x8a18, 0xffffffff, 0x00000100,
653         0x92a0, 0xffffffff, 0x00000100,
654         0xc380, 0xffffffff, 0x00000100,
655         0x8b28, 0xffffffff, 0x00000100,
656         0x9144, 0xffffffff, 0x00000100,
657         0x8d88, 0xffffffff, 0x00000100,
658         0x8d8c, 0xffffffff, 0x00000100,
659         0x9030, 0xffffffff, 0x00000100,
660         0x9034, 0xffffffff, 0x00000100,
661         0x9038, 0xffffffff, 0x00000100,
662         0x903c, 0xffffffff, 0x00000100,
663         0xad80, 0xffffffff, 0x00000100,
664         0xac54, 0xffffffff, 0x00000100,
665         0x897c, 0xffffffff, 0x06000100,
666         0x9868, 0xffffffff, 0x00000100,
667         0x9510, 0xffffffff, 0x00000100,
668         0xaf04, 0xffffffff, 0x00000100,
669         0xae04, 0xffffffff, 0x00000100,
670         0x949c, 0xffffffff, 0x00000100,
671         0x802c, 0xffffffff, 0xe0000000,
672         0x9160, 0xffffffff, 0x00010000,
673         0x9164, 0xffffffff, 0x00030002,
674         0x9168, 0xffffffff, 0x00040007,
675         0x916c, 0xffffffff, 0x00060005,
676         0x9170, 0xffffffff, 0x00090008,
677         0x9174, 0xffffffff, 0x00020001,
678         0x9178, 0xffffffff, 0x00040003,
679         0x917c, 0xffffffff, 0x00000007,
680         0x9180, 0xffffffff, 0x00060005,
681         0x9184, 0xffffffff, 0x00090008,
682         0x9188, 0xffffffff, 0x00030002,
683         0x918c, 0xffffffff, 0x00050004,
684         0x9190, 0xffffffff, 0x00000008,
685         0x9194, 0xffffffff, 0x00070006,
686         0x9198, 0xffffffff, 0x000a0009,
687         0x919c, 0xffffffff, 0x00040003,
688         0x91a0, 0xffffffff, 0x00060005,
689         0x91a4, 0xffffffff, 0x00000009,
690         0x91a8, 0xffffffff, 0x00080007,
691         0x91ac, 0xffffffff, 0x000b000a,
692         0x91b0, 0xffffffff, 0x00050004,
693         0x91b4, 0xffffffff, 0x00070006,
694         0x91b8, 0xffffffff, 0x0008000b,
695         0x91bc, 0xffffffff, 0x000a0009,
696         0x91c0, 0xffffffff, 0x000d000c,
697         0x9200, 0xffffffff, 0x00090008,
698         0x9204, 0xffffffff, 0x000b000a,
699         0x9208, 0xffffffff, 0x000c000f,
700         0x920c, 0xffffffff, 0x000e000d,
701         0x9210, 0xffffffff, 0x00110010,
702         0x9214, 0xffffffff, 0x000a0009,
703         0x9218, 0xffffffff, 0x000c000b,
704         0x921c, 0xffffffff, 0x0000000f,
705         0x9220, 0xffffffff, 0x000e000d,
706         0x9224, 0xffffffff, 0x00110010,
707         0x9228, 0xffffffff, 0x000b000a,
708         0x922c, 0xffffffff, 0x000d000c,
709         0x9230, 0xffffffff, 0x00000010,
710         0x9234, 0xffffffff, 0x000f000e,
711         0x9238, 0xffffffff, 0x00120011,
712         0x923c, 0xffffffff, 0x000c000b,
713         0x9240, 0xffffffff, 0x000e000d,
714         0x9244, 0xffffffff, 0x00000011,
715         0x9248, 0xffffffff, 0x0010000f,
716         0x924c, 0xffffffff, 0x00130012,
717         0x9250, 0xffffffff, 0x000d000c,
718         0x9254, 0xffffffff, 0x000f000e,
719         0x9258, 0xffffffff, 0x00100013,
720         0x925c, 0xffffffff, 0x00120011,
721         0x9260, 0xffffffff, 0x00150014,
722         0x9150, 0xffffffff, 0x96940200,
723         0x8708, 0xffffffff, 0x00900100,
724         0xc478, 0xffffffff, 0x00000080,
725         0xc404, 0xffffffff, 0x0020003f,
726         0x30, 0xffffffff, 0x0000001c,
727         0x34, 0x000f0000, 0x000f0000,
728         0x160c, 0xffffffff, 0x00000100,
729         0x1024, 0xffffffff, 0x00000100,
730         0x102c, 0x00000101, 0x00000000,
731         0x20a8, 0xffffffff, 0x00000104,
732         0x55e4, 0xff000fff, 0x00000100,
733         0x55e8, 0x00000001, 0x00000001,
734         0x2f50, 0x00000001, 0x00000001,
735         0x30cc, 0xc0000fff, 0x00000104,
736         0xc1e4, 0x00000001, 0x00000001,
737         0xd0c0, 0xfffffff0, 0x00000100,
738         0xd8c0, 0xfffffff0, 0x00000100
739 };
740
741 static const u32 verde_mgcg_cgcg_init[] =
742 {
743         0xc400, 0xffffffff, 0xfffffffc,
744         0x802c, 0xffffffff, 0xe0000000,
745         0x9a60, 0xffffffff, 0x00000100,
746         0x92a4, 0xffffffff, 0x00000100,
747         0xc164, 0xffffffff, 0x00000100,
748         0x9774, 0xffffffff, 0x00000100,
749         0x8984, 0xffffffff, 0x06000100,
750         0x8a18, 0xffffffff, 0x00000100,
751         0x92a0, 0xffffffff, 0x00000100,
752         0xc380, 0xffffffff, 0x00000100,
753         0x8b28, 0xffffffff, 0x00000100,
754         0x9144, 0xffffffff, 0x00000100,
755         0x8d88, 0xffffffff, 0x00000100,
756         0x8d8c, 0xffffffff, 0x00000100,
757         0x9030, 0xffffffff, 0x00000100,
758         0x9034, 0xffffffff, 0x00000100,
759         0x9038, 0xffffffff, 0x00000100,
760         0x903c, 0xffffffff, 0x00000100,
761         0xad80, 0xffffffff, 0x00000100,
762         0xac54, 0xffffffff, 0x00000100,
763         0x897c, 0xffffffff, 0x06000100,
764         0x9868, 0xffffffff, 0x00000100,
765         0x9510, 0xffffffff, 0x00000100,
766         0xaf04, 0xffffffff, 0x00000100,
767         0xae04, 0xffffffff, 0x00000100,
768         0x949c, 0xffffffff, 0x00000100,
769         0x802c, 0xffffffff, 0xe0000000,
770         0x9160, 0xffffffff, 0x00010000,
771         0x9164, 0xffffffff, 0x00030002,
772         0x9168, 0xffffffff, 0x00040007,
773         0x916c, 0xffffffff, 0x00060005,
774         0x9170, 0xffffffff, 0x00090008,
775         0x9174, 0xffffffff, 0x00020001,
776         0x9178, 0xffffffff, 0x00040003,
777         0x917c, 0xffffffff, 0x00000007,
778         0x9180, 0xffffffff, 0x00060005,
779         0x9184, 0xffffffff, 0x00090008,
780         0x9188, 0xffffffff, 0x00030002,
781         0x918c, 0xffffffff, 0x00050004,
782         0x9190, 0xffffffff, 0x00000008,
783         0x9194, 0xffffffff, 0x00070006,
784         0x9198, 0xffffffff, 0x000a0009,
785         0x919c, 0xffffffff, 0x00040003,
786         0x91a0, 0xffffffff, 0x00060005,
787         0x91a4, 0xffffffff, 0x00000009,
788         0x91a8, 0xffffffff, 0x00080007,
789         0x91ac, 0xffffffff, 0x000b000a,
790         0x91b0, 0xffffffff, 0x00050004,
791         0x91b4, 0xffffffff, 0x00070006,
792         0x91b8, 0xffffffff, 0x0008000b,
793         0x91bc, 0xffffffff, 0x000a0009,
794         0x91c0, 0xffffffff, 0x000d000c,
795         0x9200, 0xffffffff, 0x00090008,
796         0x9204, 0xffffffff, 0x000b000a,
797         0x9208, 0xffffffff, 0x000c000f,
798         0x920c, 0xffffffff, 0x000e000d,
799         0x9210, 0xffffffff, 0x00110010,
800         0x9214, 0xffffffff, 0x000a0009,
801         0x9218, 0xffffffff, 0x000c000b,
802         0x921c, 0xffffffff, 0x0000000f,
803         0x9220, 0xffffffff, 0x000e000d,
804         0x9224, 0xffffffff, 0x00110010,
805         0x9228, 0xffffffff, 0x000b000a,
806         0x922c, 0xffffffff, 0x000d000c,
807         0x9230, 0xffffffff, 0x00000010,
808         0x9234, 0xffffffff, 0x000f000e,
809         0x9238, 0xffffffff, 0x00120011,
810         0x923c, 0xffffffff, 0x000c000b,
811         0x9240, 0xffffffff, 0x000e000d,
812         0x9244, 0xffffffff, 0x00000011,
813         0x9248, 0xffffffff, 0x0010000f,
814         0x924c, 0xffffffff, 0x00130012,
815         0x9250, 0xffffffff, 0x000d000c,
816         0x9254, 0xffffffff, 0x000f000e,
817         0x9258, 0xffffffff, 0x00100013,
818         0x925c, 0xffffffff, 0x00120011,
819         0x9260, 0xffffffff, 0x00150014,
820         0x9150, 0xffffffff, 0x96940200,
821         0x8708, 0xffffffff, 0x00900100,
822         0xc478, 0xffffffff, 0x00000080,
823         0xc404, 0xffffffff, 0x0020003f,
824         0x30, 0xffffffff, 0x0000001c,
825         0x34, 0x000f0000, 0x000f0000,
826         0x160c, 0xffffffff, 0x00000100,
827         0x1024, 0xffffffff, 0x00000100,
828         0x102c, 0x00000101, 0x00000000,
829         0x20a8, 0xffffffff, 0x00000104,
830         0x264c, 0x000c0000, 0x000c0000,
831         0x2648, 0x000c0000, 0x000c0000,
832         0x55e4, 0xff000fff, 0x00000100,
833         0x55e8, 0x00000001, 0x00000001,
834         0x2f50, 0x00000001, 0x00000001,
835         0x30cc, 0xc0000fff, 0x00000104,
836         0xc1e4, 0x00000001, 0x00000001,
837         0xd0c0, 0xfffffff0, 0x00000100,
838         0xd8c0, 0xfffffff0, 0x00000100
839 };
840
841 static const u32 oland_mgcg_cgcg_init[] =
842 {
843         0xc400, 0xffffffff, 0xfffffffc,
844         0x802c, 0xffffffff, 0xe0000000,
845         0x9a60, 0xffffffff, 0x00000100,
846         0x92a4, 0xffffffff, 0x00000100,
847         0xc164, 0xffffffff, 0x00000100,
848         0x9774, 0xffffffff, 0x00000100,
849         0x8984, 0xffffffff, 0x06000100,
850         0x8a18, 0xffffffff, 0x00000100,
851         0x92a0, 0xffffffff, 0x00000100,
852         0xc380, 0xffffffff, 0x00000100,
853         0x8b28, 0xffffffff, 0x00000100,
854         0x9144, 0xffffffff, 0x00000100,
855         0x8d88, 0xffffffff, 0x00000100,
856         0x8d8c, 0xffffffff, 0x00000100,
857         0x9030, 0xffffffff, 0x00000100,
858         0x9034, 0xffffffff, 0x00000100,
859         0x9038, 0xffffffff, 0x00000100,
860         0x903c, 0xffffffff, 0x00000100,
861         0xad80, 0xffffffff, 0x00000100,
862         0xac54, 0xffffffff, 0x00000100,
863         0x897c, 0xffffffff, 0x06000100,
864         0x9868, 0xffffffff, 0x00000100,
865         0x9510, 0xffffffff, 0x00000100,
866         0xaf04, 0xffffffff, 0x00000100,
867         0xae04, 0xffffffff, 0x00000100,
868         0x949c, 0xffffffff, 0x00000100,
869         0x802c, 0xffffffff, 0xe0000000,
870         0x9160, 0xffffffff, 0x00010000,
871         0x9164, 0xffffffff, 0x00030002,
872         0x9168, 0xffffffff, 0x00040007,
873         0x916c, 0xffffffff, 0x00060005,
874         0x9170, 0xffffffff, 0x00090008,
875         0x9174, 0xffffffff, 0x00020001,
876         0x9178, 0xffffffff, 0x00040003,
877         0x917c, 0xffffffff, 0x00000007,
878         0x9180, 0xffffffff, 0x00060005,
879         0x9184, 0xffffffff, 0x00090008,
880         0x9188, 0xffffffff, 0x00030002,
881         0x918c, 0xffffffff, 0x00050004,
882         0x9190, 0xffffffff, 0x00000008,
883         0x9194, 0xffffffff, 0x00070006,
884         0x9198, 0xffffffff, 0x000a0009,
885         0x919c, 0xffffffff, 0x00040003,
886         0x91a0, 0xffffffff, 0x00060005,
887         0x91a4, 0xffffffff, 0x00000009,
888         0x91a8, 0xffffffff, 0x00080007,
889         0x91ac, 0xffffffff, 0x000b000a,
890         0x91b0, 0xffffffff, 0x00050004,
891         0x91b4, 0xffffffff, 0x00070006,
892         0x91b8, 0xffffffff, 0x0008000b,
893         0x91bc, 0xffffffff, 0x000a0009,
894         0x91c0, 0xffffffff, 0x000d000c,
895         0x91c4, 0xffffffff, 0x00060005,
896         0x91c8, 0xffffffff, 0x00080007,
897         0x91cc, 0xffffffff, 0x0000000b,
898         0x91d0, 0xffffffff, 0x000a0009,
899         0x91d4, 0xffffffff, 0x000d000c,
900         0x9150, 0xffffffff, 0x96940200,
901         0x8708, 0xffffffff, 0x00900100,
902         0xc478, 0xffffffff, 0x00000080,
903         0xc404, 0xffffffff, 0x0020003f,
904         0x30, 0xffffffff, 0x0000001c,
905         0x34, 0x000f0000, 0x000f0000,
906         0x160c, 0xffffffff, 0x00000100,
907         0x1024, 0xffffffff, 0x00000100,
908         0x102c, 0x00000101, 0x00000000,
909         0x20a8, 0xffffffff, 0x00000104,
910         0x264c, 0x000c0000, 0x000c0000,
911         0x2648, 0x000c0000, 0x000c0000,
912         0x55e4, 0xff000fff, 0x00000100,
913         0x55e8, 0x00000001, 0x00000001,
914         0x2f50, 0x00000001, 0x00000001,
915         0x30cc, 0xc0000fff, 0x00000104,
916         0xc1e4, 0x00000001, 0x00000001,
917         0xd0c0, 0xfffffff0, 0x00000100,
918         0xd8c0, 0xfffffff0, 0x00000100
919 };
920
921 static const u32 hainan_mgcg_cgcg_init[] =
922 {
923         0xc400, 0xffffffff, 0xfffffffc,
924         0x802c, 0xffffffff, 0xe0000000,
925         0x9a60, 0xffffffff, 0x00000100,
926         0x92a4, 0xffffffff, 0x00000100,
927         0xc164, 0xffffffff, 0x00000100,
928         0x9774, 0xffffffff, 0x00000100,
929         0x8984, 0xffffffff, 0x06000100,
930         0x8a18, 0xffffffff, 0x00000100,
931         0x92a0, 0xffffffff, 0x00000100,
932         0xc380, 0xffffffff, 0x00000100,
933         0x8b28, 0xffffffff, 0x00000100,
934         0x9144, 0xffffffff, 0x00000100,
935         0x8d88, 0xffffffff, 0x00000100,
936         0x8d8c, 0xffffffff, 0x00000100,
937         0x9030, 0xffffffff, 0x00000100,
938         0x9034, 0xffffffff, 0x00000100,
939         0x9038, 0xffffffff, 0x00000100,
940         0x903c, 0xffffffff, 0x00000100,
941         0xad80, 0xffffffff, 0x00000100,
942         0xac54, 0xffffffff, 0x00000100,
943         0x897c, 0xffffffff, 0x06000100,
944         0x9868, 0xffffffff, 0x00000100,
945         0x9510, 0xffffffff, 0x00000100,
946         0xaf04, 0xffffffff, 0x00000100,
947         0xae04, 0xffffffff, 0x00000100,
948         0x949c, 0xffffffff, 0x00000100,
949         0x802c, 0xffffffff, 0xe0000000,
950         0x9160, 0xffffffff, 0x00010000,
951         0x9164, 0xffffffff, 0x00030002,
952         0x9168, 0xffffffff, 0x00040007,
953         0x916c, 0xffffffff, 0x00060005,
954         0x9170, 0xffffffff, 0x00090008,
955         0x9174, 0xffffffff, 0x00020001,
956         0x9178, 0xffffffff, 0x00040003,
957         0x917c, 0xffffffff, 0x00000007,
958         0x9180, 0xffffffff, 0x00060005,
959         0x9184, 0xffffffff, 0x00090008,
960         0x9188, 0xffffffff, 0x00030002,
961         0x918c, 0xffffffff, 0x00050004,
962         0x9190, 0xffffffff, 0x00000008,
963         0x9194, 0xffffffff, 0x00070006,
964         0x9198, 0xffffffff, 0x000a0009,
965         0x919c, 0xffffffff, 0x00040003,
966         0x91a0, 0xffffffff, 0x00060005,
967         0x91a4, 0xffffffff, 0x00000009,
968         0x91a8, 0xffffffff, 0x00080007,
969         0x91ac, 0xffffffff, 0x000b000a,
970         0x91b0, 0xffffffff, 0x00050004,
971         0x91b4, 0xffffffff, 0x00070006,
972         0x91b8, 0xffffffff, 0x0008000b,
973         0x91bc, 0xffffffff, 0x000a0009,
974         0x91c0, 0xffffffff, 0x000d000c,
975         0x91c4, 0xffffffff, 0x00060005,
976         0x91c8, 0xffffffff, 0x00080007,
977         0x91cc, 0xffffffff, 0x0000000b,
978         0x91d0, 0xffffffff, 0x000a0009,
979         0x91d4, 0xffffffff, 0x000d000c,
980         0x9150, 0xffffffff, 0x96940200,
981         0x8708, 0xffffffff, 0x00900100,
982         0xc478, 0xffffffff, 0x00000080,
983         0xc404, 0xffffffff, 0x0020003f,
984         0x30, 0xffffffff, 0x0000001c,
985         0x34, 0x000f0000, 0x000f0000,
986         0x160c, 0xffffffff, 0x00000100,
987         0x1024, 0xffffffff, 0x00000100,
988         0x20a8, 0xffffffff, 0x00000104,
989         0x264c, 0x000c0000, 0x000c0000,
990         0x2648, 0x000c0000, 0x000c0000,
991         0x2f50, 0x00000001, 0x00000001,
992         0x30cc, 0xc0000fff, 0x00000104,
993         0xc1e4, 0x00000001, 0x00000001,
994         0xd0c0, 0xfffffff0, 0x00000100,
995         0xd8c0, 0xfffffff0, 0x00000100
996 };
997
998 static u32 verde_pg_init[] =
999 {
1000         0x353c, 0xffffffff, 0x40000,
1001         0x3538, 0xffffffff, 0x200010ff,
1002         0x353c, 0xffffffff, 0x0,
1003         0x353c, 0xffffffff, 0x0,
1004         0x353c, 0xffffffff, 0x0,
1005         0x353c, 0xffffffff, 0x0,
1006         0x353c, 0xffffffff, 0x0,
1007         0x353c, 0xffffffff, 0x7007,
1008         0x3538, 0xffffffff, 0x300010ff,
1009         0x353c, 0xffffffff, 0x0,
1010         0x353c, 0xffffffff, 0x0,
1011         0x353c, 0xffffffff, 0x0,
1012         0x353c, 0xffffffff, 0x0,
1013         0x353c, 0xffffffff, 0x0,
1014         0x353c, 0xffffffff, 0x400000,
1015         0x3538, 0xffffffff, 0x100010ff,
1016         0x353c, 0xffffffff, 0x0,
1017         0x353c, 0xffffffff, 0x0,
1018         0x353c, 0xffffffff, 0x0,
1019         0x353c, 0xffffffff, 0x0,
1020         0x353c, 0xffffffff, 0x0,
1021         0x353c, 0xffffffff, 0x120200,
1022         0x3538, 0xffffffff, 0x500010ff,
1023         0x353c, 0xffffffff, 0x0,
1024         0x353c, 0xffffffff, 0x0,
1025         0x353c, 0xffffffff, 0x0,
1026         0x353c, 0xffffffff, 0x0,
1027         0x353c, 0xffffffff, 0x0,
1028         0x353c, 0xffffffff, 0x1e1e16,
1029         0x3538, 0xffffffff, 0x600010ff,
1030         0x353c, 0xffffffff, 0x0,
1031         0x353c, 0xffffffff, 0x0,
1032         0x353c, 0xffffffff, 0x0,
1033         0x353c, 0xffffffff, 0x0,
1034         0x353c, 0xffffffff, 0x0,
1035         0x353c, 0xffffffff, 0x171f1e,
1036         0x3538, 0xffffffff, 0x700010ff,
1037         0x353c, 0xffffffff, 0x0,
1038         0x353c, 0xffffffff, 0x0,
1039         0x353c, 0xffffffff, 0x0,
1040         0x353c, 0xffffffff, 0x0,
1041         0x353c, 0xffffffff, 0x0,
1042         0x353c, 0xffffffff, 0x0,
1043         0x3538, 0xffffffff, 0x9ff,
1044         0x3500, 0xffffffff, 0x0,
1045         0x3504, 0xffffffff, 0x10000800,
1046         0x3504, 0xffffffff, 0xf,
1047         0x3504, 0xffffffff, 0xf,
1048         0x3500, 0xffffffff, 0x4,
1049         0x3504, 0xffffffff, 0x1000051e,
1050         0x3504, 0xffffffff, 0xffff,
1051         0x3504, 0xffffffff, 0xffff,
1052         0x3500, 0xffffffff, 0x8,
1053         0x3504, 0xffffffff, 0x80500,
1054         0x3500, 0xffffffff, 0x12,
1055         0x3504, 0xffffffff, 0x9050c,
1056         0x3500, 0xffffffff, 0x1d,
1057         0x3504, 0xffffffff, 0xb052c,
1058         0x3500, 0xffffffff, 0x2a,
1059         0x3504, 0xffffffff, 0x1053e,
1060         0x3500, 0xffffffff, 0x2d,
1061         0x3504, 0xffffffff, 0x10546,
1062         0x3500, 0xffffffff, 0x30,
1063         0x3504, 0xffffffff, 0xa054e,
1064         0x3500, 0xffffffff, 0x3c,
1065         0x3504, 0xffffffff, 0x1055f,
1066         0x3500, 0xffffffff, 0x3f,
1067         0x3504, 0xffffffff, 0x10567,
1068         0x3500, 0xffffffff, 0x42,
1069         0x3504, 0xffffffff, 0x1056f,
1070         0x3500, 0xffffffff, 0x45,
1071         0x3504, 0xffffffff, 0x10572,
1072         0x3500, 0xffffffff, 0x48,
1073         0x3504, 0xffffffff, 0x20575,
1074         0x3500, 0xffffffff, 0x4c,
1075         0x3504, 0xffffffff, 0x190801,
1076         0x3500, 0xffffffff, 0x67,
1077         0x3504, 0xffffffff, 0x1082a,
1078         0x3500, 0xffffffff, 0x6a,
1079         0x3504, 0xffffffff, 0x1b082d,
1080         0x3500, 0xffffffff, 0x87,
1081         0x3504, 0xffffffff, 0x310851,
1082         0x3500, 0xffffffff, 0xba,
1083         0x3504, 0xffffffff, 0x891,
1084         0x3500, 0xffffffff, 0xbc,
1085         0x3504, 0xffffffff, 0x893,
1086         0x3500, 0xffffffff, 0xbe,
1087         0x3504, 0xffffffff, 0x20895,
1088         0x3500, 0xffffffff, 0xc2,
1089         0x3504, 0xffffffff, 0x20899,
1090         0x3500, 0xffffffff, 0xc6,
1091         0x3504, 0xffffffff, 0x2089d,
1092         0x3500, 0xffffffff, 0xca,
1093         0x3504, 0xffffffff, 0x8a1,
1094         0x3500, 0xffffffff, 0xcc,
1095         0x3504, 0xffffffff, 0x8a3,
1096         0x3500, 0xffffffff, 0xce,
1097         0x3504, 0xffffffff, 0x308a5,
1098         0x3500, 0xffffffff, 0xd3,
1099         0x3504, 0xffffffff, 0x6d08cd,
1100         0x3500, 0xffffffff, 0x142,
1101         0x3504, 0xffffffff, 0x2000095a,
1102         0x3504, 0xffffffff, 0x1,
1103         0x3500, 0xffffffff, 0x144,
1104         0x3504, 0xffffffff, 0x301f095b,
1105         0x3500, 0xffffffff, 0x165,
1106         0x3504, 0xffffffff, 0xc094d,
1107         0x3500, 0xffffffff, 0x173,
1108         0x3504, 0xffffffff, 0xf096d,
1109         0x3500, 0xffffffff, 0x184,
1110         0x3504, 0xffffffff, 0x15097f,
1111         0x3500, 0xffffffff, 0x19b,
1112         0x3504, 0xffffffff, 0xc0998,
1113         0x3500, 0xffffffff, 0x1a9,
1114         0x3504, 0xffffffff, 0x409a7,
1115         0x3500, 0xffffffff, 0x1af,
1116         0x3504, 0xffffffff, 0xcdc,
1117         0x3500, 0xffffffff, 0x1b1,
1118         0x3504, 0xffffffff, 0x800,
1119         0x3508, 0xffffffff, 0x6c9b2000,
1120         0x3510, 0xfc00, 0x2000,
1121         0x3544, 0xffffffff, 0xfc0,
1122         0x28d4, 0x00000100, 0x100
1123 };
1124
1125 static void si_init_golden_registers(struct radeon_device *rdev)
1126 {
1127         switch (rdev->family) {
1128         case CHIP_TAHITI:
1129                 radeon_program_register_sequence(rdev,
1130                                                  tahiti_golden_registers,
1131                                                  (const u32)ARRAY_SIZE(tahiti_golden_registers));
1132                 radeon_program_register_sequence(rdev,
1133                                                  tahiti_golden_rlc_registers,
1134                                                  (const u32)ARRAY_SIZE(tahiti_golden_rlc_registers));
1135                 radeon_program_register_sequence(rdev,
1136                                                  tahiti_mgcg_cgcg_init,
1137                                                  (const u32)ARRAY_SIZE(tahiti_mgcg_cgcg_init));
1138                 radeon_program_register_sequence(rdev,
1139                                                  tahiti_golden_registers2,
1140                                                  (const u32)ARRAY_SIZE(tahiti_golden_registers2));
1141                 break;
1142         case CHIP_PITCAIRN:
1143                 radeon_program_register_sequence(rdev,
1144                                                  pitcairn_golden_registers,
1145                                                  (const u32)ARRAY_SIZE(pitcairn_golden_registers));
1146                 radeon_program_register_sequence(rdev,
1147                                                  pitcairn_golden_rlc_registers,
1148                                                  (const u32)ARRAY_SIZE(pitcairn_golden_rlc_registers));
1149                 radeon_program_register_sequence(rdev,
1150                                                  pitcairn_mgcg_cgcg_init,
1151                                                  (const u32)ARRAY_SIZE(pitcairn_mgcg_cgcg_init));
1152                 break;
1153         case CHIP_VERDE:
1154                 radeon_program_register_sequence(rdev,
1155                                                  verde_golden_registers,
1156                                                  (const u32)ARRAY_SIZE(verde_golden_registers));
1157                 radeon_program_register_sequence(rdev,
1158                                                  verde_golden_rlc_registers,
1159                                                  (const u32)ARRAY_SIZE(verde_golden_rlc_registers));
1160                 radeon_program_register_sequence(rdev,
1161                                                  verde_mgcg_cgcg_init,
1162                                                  (const u32)ARRAY_SIZE(verde_mgcg_cgcg_init));
1163                 radeon_program_register_sequence(rdev,
1164                                                  verde_pg_init,
1165                                                  (const u32)ARRAY_SIZE(verde_pg_init));
1166                 break;
1167         case CHIP_OLAND:
1168                 radeon_program_register_sequence(rdev,
1169                                                  oland_golden_registers,
1170                                                  (const u32)ARRAY_SIZE(oland_golden_registers));
1171                 radeon_program_register_sequence(rdev,
1172                                                  oland_golden_rlc_registers,
1173                                                  (const u32)ARRAY_SIZE(oland_golden_rlc_registers));
1174                 radeon_program_register_sequence(rdev,
1175                                                  oland_mgcg_cgcg_init,
1176                                                  (const u32)ARRAY_SIZE(oland_mgcg_cgcg_init));
1177                 break;
1178         case CHIP_HAINAN:
1179                 radeon_program_register_sequence(rdev,
1180                                                  hainan_golden_registers,
1181                                                  (const u32)ARRAY_SIZE(hainan_golden_registers));
1182                 radeon_program_register_sequence(rdev,
1183                                                  hainan_golden_registers2,
1184                                                  (const u32)ARRAY_SIZE(hainan_golden_registers2));
1185                 radeon_program_register_sequence(rdev,
1186                                                  hainan_mgcg_cgcg_init,
1187                                                  (const u32)ARRAY_SIZE(hainan_mgcg_cgcg_init));
1188                 break;
1189         default:
1190                 break;
1191         }
1192 }
1193
1194 /**
1195  * si_get_allowed_info_register - fetch the register for the info ioctl
1196  *
1197  * @rdev: radeon_device pointer
1198  * @reg: register offset in bytes
1199  * @val: register value
1200  *
1201  * Returns 0 for success or -EINVAL for an invalid register
1202  *
1203  */
1204 int si_get_allowed_info_register(struct radeon_device *rdev,
1205                                  u32 reg, u32 *val)
1206 {
1207         switch (reg) {
1208         case GRBM_STATUS:
1209         case GRBM_STATUS2:
1210         case GRBM_STATUS_SE0:
1211         case GRBM_STATUS_SE1:
1212         case SRBM_STATUS:
1213         case SRBM_STATUS2:
1214         case (DMA_STATUS_REG + DMA0_REGISTER_OFFSET):
1215         case (DMA_STATUS_REG + DMA1_REGISTER_OFFSET):
1216         case UVD_STATUS:
1217                 *val = RREG32(reg);
1218                 return 0;
1219         default:
1220                 return -EINVAL;
1221         }
1222 }
1223
1224 #define PCIE_BUS_CLK                10000
1225 #define TCLK                        (PCIE_BUS_CLK / 10)
1226
1227 /**
1228  * si_get_xclk - get the xclk
1229  *
1230  * @rdev: radeon_device pointer
1231  *
1232  * Returns the reference clock used by the gfx engine
1233  * (SI).
1234  */
1235 u32 si_get_xclk(struct radeon_device *rdev)
1236 {
1237         u32 reference_clock = rdev->clock.spll.reference_freq;
1238         u32 tmp;
1239
1240         tmp = RREG32(CG_CLKPIN_CNTL_2);
1241         if (tmp & MUX_TCLK_TO_XCLK)
1242                 return TCLK;
1243
1244         tmp = RREG32(CG_CLKPIN_CNTL);
1245         if (tmp & XTALIN_DIVIDE)
1246                 return reference_clock / 4;
1247
1248         return reference_clock;
1249 }
1250
1251 /* get temperature in millidegrees */
1252 int si_get_temp(struct radeon_device *rdev)
1253 {
1254         u32 temp;
1255         int actual_temp = 0;
1256
1257         temp = (RREG32(CG_MULT_THERMAL_STATUS) & CTF_TEMP_MASK) >>
1258                 CTF_TEMP_SHIFT;
1259
1260         if (temp & 0x200)
1261                 actual_temp = 255;
1262         else
1263                 actual_temp = temp & 0x1ff;
1264
1265         actual_temp = (actual_temp * 1000);
1266
1267         return actual_temp;
1268 }
1269
1270 #define TAHITI_IO_MC_REGS_SIZE 36
1271
1272 static const u32 tahiti_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
1273         {0x0000006f, 0x03044000},
1274         {0x00000070, 0x0480c018},
1275         {0x00000071, 0x00000040},
1276         {0x00000072, 0x01000000},
1277         {0x00000074, 0x000000ff},
1278         {0x00000075, 0x00143400},
1279         {0x00000076, 0x08ec0800},
1280         {0x00000077, 0x040000cc},
1281         {0x00000079, 0x00000000},
1282         {0x0000007a, 0x21000409},
1283         {0x0000007c, 0x00000000},
1284         {0x0000007d, 0xe8000000},
1285         {0x0000007e, 0x044408a8},
1286         {0x0000007f, 0x00000003},
1287         {0x00000080, 0x00000000},
1288         {0x00000081, 0x01000000},
1289         {0x00000082, 0x02000000},
1290         {0x00000083, 0x00000000},
1291         {0x00000084, 0xe3f3e4f4},
1292         {0x00000085, 0x00052024},
1293         {0x00000087, 0x00000000},
1294         {0x00000088, 0x66036603},
1295         {0x00000089, 0x01000000},
1296         {0x0000008b, 0x1c0a0000},
1297         {0x0000008c, 0xff010000},
1298         {0x0000008e, 0xffffefff},
1299         {0x0000008f, 0xfff3efff},
1300         {0x00000090, 0xfff3efbf},
1301         {0x00000094, 0x00101101},
1302         {0x00000095, 0x00000fff},
1303         {0x00000096, 0x00116fff},
1304         {0x00000097, 0x60010000},
1305         {0x00000098, 0x10010000},
1306         {0x00000099, 0x00006000},
1307         {0x0000009a, 0x00001000},
1308         {0x0000009f, 0x00a77400}
1309 };
1310
1311 static const u32 pitcairn_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
1312         {0x0000006f, 0x03044000},
1313         {0x00000070, 0x0480c018},
1314         {0x00000071, 0x00000040},
1315         {0x00000072, 0x01000000},
1316         {0x00000074, 0x000000ff},
1317         {0x00000075, 0x00143400},
1318         {0x00000076, 0x08ec0800},
1319         {0x00000077, 0x040000cc},
1320         {0x00000079, 0x00000000},
1321         {0x0000007a, 0x21000409},
1322         {0x0000007c, 0x00000000},
1323         {0x0000007d, 0xe8000000},
1324         {0x0000007e, 0x044408a8},
1325         {0x0000007f, 0x00000003},
1326         {0x00000080, 0x00000000},
1327         {0x00000081, 0x01000000},
1328         {0x00000082, 0x02000000},
1329         {0x00000083, 0x00000000},
1330         {0x00000084, 0xe3f3e4f4},
1331         {0x00000085, 0x00052024},
1332         {0x00000087, 0x00000000},
1333         {0x00000088, 0x66036603},
1334         {0x00000089, 0x01000000},
1335         {0x0000008b, 0x1c0a0000},
1336         {0x0000008c, 0xff010000},
1337         {0x0000008e, 0xffffefff},
1338         {0x0000008f, 0xfff3efff},
1339         {0x00000090, 0xfff3efbf},
1340         {0x00000094, 0x00101101},
1341         {0x00000095, 0x00000fff},
1342         {0x00000096, 0x00116fff},
1343         {0x00000097, 0x60010000},
1344         {0x00000098, 0x10010000},
1345         {0x00000099, 0x00006000},
1346         {0x0000009a, 0x00001000},
1347         {0x0000009f, 0x00a47400}
1348 };
1349
1350 static const u32 verde_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
1351         {0x0000006f, 0x03044000},
1352         {0x00000070, 0x0480c018},
1353         {0x00000071, 0x00000040},
1354         {0x00000072, 0x01000000},
1355         {0x00000074, 0x000000ff},
1356         {0x00000075, 0x00143400},
1357         {0x00000076, 0x08ec0800},
1358         {0x00000077, 0x040000cc},
1359         {0x00000079, 0x00000000},
1360         {0x0000007a, 0x21000409},
1361         {0x0000007c, 0x00000000},
1362         {0x0000007d, 0xe8000000},
1363         {0x0000007e, 0x044408a8},
1364         {0x0000007f, 0x00000003},
1365         {0x00000080, 0x00000000},
1366         {0x00000081, 0x01000000},
1367         {0x00000082, 0x02000000},
1368         {0x00000083, 0x00000000},
1369         {0x00000084, 0xe3f3e4f4},
1370         {0x00000085, 0x00052024},
1371         {0x00000087, 0x00000000},
1372         {0x00000088, 0x66036603},
1373         {0x00000089, 0x01000000},
1374         {0x0000008b, 0x1c0a0000},
1375         {0x0000008c, 0xff010000},
1376         {0x0000008e, 0xffffefff},
1377         {0x0000008f, 0xfff3efff},
1378         {0x00000090, 0xfff3efbf},
1379         {0x00000094, 0x00101101},
1380         {0x00000095, 0x00000fff},
1381         {0x00000096, 0x00116fff},
1382         {0x00000097, 0x60010000},
1383         {0x00000098, 0x10010000},
1384         {0x00000099, 0x00006000},
1385         {0x0000009a, 0x00001000},
1386         {0x0000009f, 0x00a37400}
1387 };
1388
1389 static const u32 oland_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
1390         {0x0000006f, 0x03044000},
1391         {0x00000070, 0x0480c018},
1392         {0x00000071, 0x00000040},
1393         {0x00000072, 0x01000000},
1394         {0x00000074, 0x000000ff},
1395         {0x00000075, 0x00143400},
1396         {0x00000076, 0x08ec0800},
1397         {0x00000077, 0x040000cc},
1398         {0x00000079, 0x00000000},
1399         {0x0000007a, 0x21000409},
1400         {0x0000007c, 0x00000000},
1401         {0x0000007d, 0xe8000000},
1402         {0x0000007e, 0x044408a8},
1403         {0x0000007f, 0x00000003},
1404         {0x00000080, 0x00000000},
1405         {0x00000081, 0x01000000},
1406         {0x00000082, 0x02000000},
1407         {0x00000083, 0x00000000},
1408         {0x00000084, 0xe3f3e4f4},
1409         {0x00000085, 0x00052024},
1410         {0x00000087, 0x00000000},
1411         {0x00000088, 0x66036603},
1412         {0x00000089, 0x01000000},
1413         {0x0000008b, 0x1c0a0000},
1414         {0x0000008c, 0xff010000},
1415         {0x0000008e, 0xffffefff},
1416         {0x0000008f, 0xfff3efff},
1417         {0x00000090, 0xfff3efbf},
1418         {0x00000094, 0x00101101},
1419         {0x00000095, 0x00000fff},
1420         {0x00000096, 0x00116fff},
1421         {0x00000097, 0x60010000},
1422         {0x00000098, 0x10010000},
1423         {0x00000099, 0x00006000},
1424         {0x0000009a, 0x00001000},
1425         {0x0000009f, 0x00a17730}
1426 };
1427
1428 static const u32 hainan_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
1429         {0x0000006f, 0x03044000},
1430         {0x00000070, 0x0480c018},
1431         {0x00000071, 0x00000040},
1432         {0x00000072, 0x01000000},
1433         {0x00000074, 0x000000ff},
1434         {0x00000075, 0x00143400},
1435         {0x00000076, 0x08ec0800},
1436         {0x00000077, 0x040000cc},
1437         {0x00000079, 0x00000000},
1438         {0x0000007a, 0x21000409},
1439         {0x0000007c, 0x00000000},
1440         {0x0000007d, 0xe8000000},
1441         {0x0000007e, 0x044408a8},
1442         {0x0000007f, 0x00000003},
1443         {0x00000080, 0x00000000},
1444         {0x00000081, 0x01000000},
1445         {0x00000082, 0x02000000},
1446         {0x00000083, 0x00000000},
1447         {0x00000084, 0xe3f3e4f4},
1448         {0x00000085, 0x00052024},
1449         {0x00000087, 0x00000000},
1450         {0x00000088, 0x66036603},
1451         {0x00000089, 0x01000000},
1452         {0x0000008b, 0x1c0a0000},
1453         {0x0000008c, 0xff010000},
1454         {0x0000008e, 0xffffefff},
1455         {0x0000008f, 0xfff3efff},
1456         {0x00000090, 0xfff3efbf},
1457         {0x00000094, 0x00101101},
1458         {0x00000095, 0x00000fff},
1459         {0x00000096, 0x00116fff},
1460         {0x00000097, 0x60010000},
1461         {0x00000098, 0x10010000},
1462         {0x00000099, 0x00006000},
1463         {0x0000009a, 0x00001000},
1464         {0x0000009f, 0x00a07730}
1465 };
1466
1467 /* ucode loading */
1468 int si_mc_load_microcode(struct radeon_device *rdev)
1469 {
1470         const __be32 *fw_data = NULL;
1471         const __le32 *new_fw_data = NULL;
1472         u32 running;
1473         u32 *io_mc_regs = NULL;
1474         const __le32 *new_io_mc_regs = NULL;
1475         int i, regs_size, ucode_size;
1476
1477         if (!rdev->mc_fw)
1478                 return -EINVAL;
1479
1480         if (rdev->new_fw) {
1481                 const struct mc_firmware_header_v1_0 *hdr =
1482                         (const struct mc_firmware_header_v1_0 *)rdev->mc_fw->data;
1483
1484                 radeon_ucode_print_mc_hdr(&hdr->header);
1485                 regs_size = le32_to_cpu(hdr->io_debug_size_bytes) / (4 * 2);
1486                 new_io_mc_regs = (const __le32 *)
1487                         (rdev->mc_fw->data + le32_to_cpu(hdr->io_debug_array_offset_bytes));
1488                 ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
1489                 new_fw_data = (const __le32 *)
1490                         (rdev->mc_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1491         } else {
1492                 ucode_size = rdev->mc_fw->size / 4;
1493
1494                 switch (rdev->family) {
1495                 case CHIP_TAHITI:
1496                         io_mc_regs = (u32 *)&tahiti_io_mc_regs;
1497                         regs_size = TAHITI_IO_MC_REGS_SIZE;
1498                         break;
1499                 case CHIP_PITCAIRN:
1500                         io_mc_regs = (u32 *)&pitcairn_io_mc_regs;
1501                         regs_size = TAHITI_IO_MC_REGS_SIZE;
1502                         break;
1503                 case CHIP_VERDE:
1504                 default:
1505                         io_mc_regs = (u32 *)&verde_io_mc_regs;
1506                         regs_size = TAHITI_IO_MC_REGS_SIZE;
1507                         break;
1508                 case CHIP_OLAND:
1509                         io_mc_regs = (u32 *)&oland_io_mc_regs;
1510                         regs_size = TAHITI_IO_MC_REGS_SIZE;
1511                         break;
1512                 case CHIP_HAINAN:
1513                         io_mc_regs = (u32 *)&hainan_io_mc_regs;
1514                         regs_size = TAHITI_IO_MC_REGS_SIZE;
1515                         break;
1516                 }
1517                 fw_data = (const __be32 *)rdev->mc_fw->data;
1518         }
1519
1520         running = RREG32(MC_SEQ_SUP_CNTL) & RUN_MASK;
1521
1522         if (running == 0) {
1523                 /* reset the engine and set to writable */
1524                 WREG32(MC_SEQ_SUP_CNTL, 0x00000008);
1525                 WREG32(MC_SEQ_SUP_CNTL, 0x00000010);
1526
1527                 /* load mc io regs */
1528                 for (i = 0; i < regs_size; i++) {
1529                         if (rdev->new_fw) {
1530                                 WREG32(MC_SEQ_IO_DEBUG_INDEX, le32_to_cpup(new_io_mc_regs++));
1531                                 WREG32(MC_SEQ_IO_DEBUG_DATA, le32_to_cpup(new_io_mc_regs++));
1532                         } else {
1533                                 WREG32(MC_SEQ_IO_DEBUG_INDEX, io_mc_regs[(i << 1)]);
1534                                 WREG32(MC_SEQ_IO_DEBUG_DATA, io_mc_regs[(i << 1) + 1]);
1535                         }
1536                 }
1537                 /* load the MC ucode */
1538                 for (i = 0; i < ucode_size; i++) {
1539                         if (rdev->new_fw)
1540                                 WREG32(MC_SEQ_SUP_PGM, le32_to_cpup(new_fw_data++));
1541                         else
1542                                 WREG32(MC_SEQ_SUP_PGM, be32_to_cpup(fw_data++));
1543                 }
1544
1545                 /* put the engine back into the active state */
1546                 WREG32(MC_SEQ_SUP_CNTL, 0x00000008);
1547                 WREG32(MC_SEQ_SUP_CNTL, 0x00000004);
1548                 WREG32(MC_SEQ_SUP_CNTL, 0x00000001);
1549
1550                 /* wait for training to complete */
1551                 for (i = 0; i < rdev->usec_timeout; i++) {
1552                         if (RREG32(MC_SEQ_TRAIN_WAKEUP_CNTL) & TRAIN_DONE_D0)
1553                                 break;
1554                         udelay(1);
1555                 }
1556                 for (i = 0; i < rdev->usec_timeout; i++) {
1557                         if (RREG32(MC_SEQ_TRAIN_WAKEUP_CNTL) & TRAIN_DONE_D1)
1558                                 break;
1559                         udelay(1);
1560                 }
1561         }
1562
1563         return 0;
1564 }
1565
1566 static int si_init_microcode(struct radeon_device *rdev)
1567 {
1568         const char *chip_name;
1569         const char *new_chip_name;
1570         size_t pfp_req_size, me_req_size, ce_req_size, rlc_req_size, mc_req_size;
1571         size_t smc_req_size, mc2_req_size;
1572         char fw_name[30];
1573         int err;
1574         int new_fw = 0;
1575         bool new_smc = false;
1576
1577         DRM_DEBUG("\n");
1578
1579         switch (rdev->family) {
1580         case CHIP_TAHITI:
1581                 chip_name = "TAHITI";
1582                 /* XXX: figure out which Tahitis need the new ucode */
1583                 if (0)
1584                         new_smc = true;
1585                 new_chip_name = "tahiti";
1586                 pfp_req_size = SI_PFP_UCODE_SIZE * 4;
1587                 me_req_size = SI_PM4_UCODE_SIZE * 4;
1588                 ce_req_size = SI_CE_UCODE_SIZE * 4;
1589                 rlc_req_size = SI_RLC_UCODE_SIZE * 4;
1590                 mc_req_size = SI_MC_UCODE_SIZE * 4;
1591                 mc2_req_size = TAHITI_MC_UCODE_SIZE * 4;
1592                 smc_req_size = ALIGN(TAHITI_SMC_UCODE_SIZE, 4);
1593                 break;
1594         case CHIP_PITCAIRN:
1595                 chip_name = "PITCAIRN";
1596                 if ((rdev->pdev->revision == 0x81) ||
1597                     (rdev->pdev->device == 0x6810) ||
1598                     (rdev->pdev->device == 0x6811) ||
1599                     (rdev->pdev->device == 0x6816) ||
1600                     (rdev->pdev->device == 0x6817) ||
1601                     (rdev->pdev->device == 0x6806))
1602                         new_smc = true;
1603                 new_chip_name = "pitcairn";
1604                 pfp_req_size = SI_PFP_UCODE_SIZE * 4;
1605                 me_req_size = SI_PM4_UCODE_SIZE * 4;
1606                 ce_req_size = SI_CE_UCODE_SIZE * 4;
1607                 rlc_req_size = SI_RLC_UCODE_SIZE * 4;
1608                 mc_req_size = SI_MC_UCODE_SIZE * 4;
1609                 mc2_req_size = PITCAIRN_MC_UCODE_SIZE * 4;
1610                 smc_req_size = ALIGN(PITCAIRN_SMC_UCODE_SIZE, 4);
1611                 break;
1612         case CHIP_VERDE:
1613                 chip_name = "VERDE";
1614                 if ((rdev->pdev->revision == 0x81) ||
1615                     (rdev->pdev->revision == 0x83) ||
1616                     (rdev->pdev->revision == 0x87) ||
1617                     (rdev->pdev->device == 0x6820) ||
1618                     (rdev->pdev->device == 0x6821) ||
1619                     (rdev->pdev->device == 0x6822) ||
1620                     (rdev->pdev->device == 0x6823) ||
1621                     (rdev->pdev->device == 0x682A) ||
1622                     (rdev->pdev->device == 0x682B))
1623                         new_smc = true;
1624                 new_chip_name = "verde";
1625                 pfp_req_size = SI_PFP_UCODE_SIZE * 4;
1626                 me_req_size = SI_PM4_UCODE_SIZE * 4;
1627                 ce_req_size = SI_CE_UCODE_SIZE * 4;
1628                 rlc_req_size = SI_RLC_UCODE_SIZE * 4;
1629                 mc_req_size = SI_MC_UCODE_SIZE * 4;
1630                 mc2_req_size = VERDE_MC_UCODE_SIZE * 4;
1631                 smc_req_size = ALIGN(VERDE_SMC_UCODE_SIZE, 4);
1632                 break;
1633         case CHIP_OLAND:
1634                 chip_name = "OLAND";
1635                 if ((rdev->pdev->revision == 0xC7) ||
1636                     (rdev->pdev->revision == 0x80) ||
1637                     (rdev->pdev->revision == 0x81) ||
1638                     (rdev->pdev->revision == 0x83) ||
1639                     (rdev->pdev->device == 0x6604) ||
1640                     (rdev->pdev->device == 0x6605))
1641                         new_smc = true;
1642                 new_chip_name = "oland";
1643                 pfp_req_size = SI_PFP_UCODE_SIZE * 4;
1644                 me_req_size = SI_PM4_UCODE_SIZE * 4;
1645                 ce_req_size = SI_CE_UCODE_SIZE * 4;
1646                 rlc_req_size = SI_RLC_UCODE_SIZE * 4;
1647                 mc_req_size = mc2_req_size = OLAND_MC_UCODE_SIZE * 4;
1648                 smc_req_size = ALIGN(OLAND_SMC_UCODE_SIZE, 4);
1649                 break;
1650         case CHIP_HAINAN:
1651                 chip_name = "HAINAN";
1652                 if ((rdev->pdev->revision == 0x81) ||
1653                     (rdev->pdev->revision == 0x83) ||
1654                     (rdev->pdev->revision == 0xC3) ||
1655                     (rdev->pdev->device == 0x6664) ||
1656                     (rdev->pdev->device == 0x6665) ||
1657                     (rdev->pdev->device == 0x6667))
1658                         new_smc = true;
1659                 new_chip_name = "hainan";
1660                 pfp_req_size = SI_PFP_UCODE_SIZE * 4;
1661                 me_req_size = SI_PM4_UCODE_SIZE * 4;
1662                 ce_req_size = SI_CE_UCODE_SIZE * 4;
1663                 rlc_req_size = SI_RLC_UCODE_SIZE * 4;
1664                 mc_req_size = mc2_req_size = OLAND_MC_UCODE_SIZE * 4;
1665                 smc_req_size = ALIGN(HAINAN_SMC_UCODE_SIZE, 4);
1666                 break;
1667         default: BUG();
1668         }
1669
1670         DRM_INFO("Loading %s Microcode\n", new_chip_name);
1671
1672         snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", new_chip_name);
1673         err = reject_firmware(&rdev->pfp_fw, fw_name, rdev->dev);
1674         if (err) {
1675                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", chip_name);
1676                 err = reject_firmware(&rdev->pfp_fw, fw_name, rdev->dev);
1677                 if (err)
1678                         goto out;
1679                 if (rdev->pfp_fw->size != pfp_req_size) {
1680                         printk(KERN_ERR
1681                                "si_cp: Bogus length %zu in firmware \"%s\"\n",
1682                                rdev->pfp_fw->size, fw_name);
1683                         err = -EINVAL;
1684                         goto out;
1685                 }
1686         } else {
1687                 err = radeon_ucode_validate(rdev->pfp_fw);
1688                 if (err) {
1689                         printk(KERN_ERR
1690                                "si_cp: validation failed for firmware \"%s\"\n",
1691                                fw_name);
1692                         goto out;
1693                 } else {
1694                         new_fw++;
1695                 }
1696         }
1697
1698         snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", new_chip_name);
1699         err = reject_firmware(&rdev->me_fw, fw_name, rdev->dev);
1700         if (err) {
1701                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", chip_name);
1702                 err = reject_firmware(&rdev->me_fw, fw_name, rdev->dev);
1703                 if (err)
1704                         goto out;
1705                 if (rdev->me_fw->size != me_req_size) {
1706                         printk(KERN_ERR
1707                                "si_cp: Bogus length %zu in firmware \"%s\"\n",
1708                                rdev->me_fw->size, fw_name);
1709                         err = -EINVAL;
1710                 }
1711         } else {
1712                 err = radeon_ucode_validate(rdev->me_fw);
1713                 if (err) {
1714                         printk(KERN_ERR
1715                                "si_cp: validation failed for firmware \"%s\"\n",
1716                                fw_name);
1717                         goto out;
1718                 } else {
1719                         new_fw++;
1720                 }
1721         }
1722
1723         snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", new_chip_name);
1724         err = reject_firmware(&rdev->ce_fw, fw_name, rdev->dev);
1725         if (err) {
1726                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", chip_name);
1727                 err = reject_firmware(&rdev->ce_fw, fw_name, rdev->dev);
1728                 if (err)
1729                         goto out;
1730                 if (rdev->ce_fw->size != ce_req_size) {
1731                         printk(KERN_ERR
1732                                "si_cp: Bogus length %zu in firmware \"%s\"\n",
1733                                rdev->ce_fw->size, fw_name);
1734                         err = -EINVAL;
1735                 }
1736         } else {
1737                 err = radeon_ucode_validate(rdev->ce_fw);
1738                 if (err) {
1739                         printk(KERN_ERR
1740                                "si_cp: validation failed for firmware \"%s\"\n",
1741                                fw_name);
1742                         goto out;
1743                 } else {
1744                         new_fw++;
1745                 }
1746         }
1747
1748         snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", new_chip_name);
1749         err = reject_firmware(&rdev->rlc_fw, fw_name, rdev->dev);
1750         if (err) {
1751                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", chip_name);
1752                 err = reject_firmware(&rdev->rlc_fw, fw_name, rdev->dev);
1753                 if (err)
1754                         goto out;
1755                 if (rdev->rlc_fw->size != rlc_req_size) {
1756                         printk(KERN_ERR
1757                                "si_rlc: Bogus length %zu in firmware \"%s\"\n",
1758                                rdev->rlc_fw->size, fw_name);
1759                         err = -EINVAL;
1760                 }
1761         } else {
1762                 err = radeon_ucode_validate(rdev->rlc_fw);
1763                 if (err) {
1764                         printk(KERN_ERR
1765                                "si_cp: validation failed for firmware \"%s\"\n",
1766                                fw_name);
1767                         goto out;
1768                 } else {
1769                         new_fw++;
1770                 }
1771         }
1772
1773         snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", new_chip_name);
1774         err = reject_firmware(&rdev->mc_fw, fw_name, rdev->dev);
1775         if (err) {
1776                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", chip_name);
1777                 err = reject_firmware(&rdev->mc_fw, fw_name, rdev->dev);
1778                 if (err) {
1779                         snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", chip_name);
1780                         err = reject_firmware(&rdev->mc_fw, fw_name, rdev->dev);
1781                         if (err)
1782                                 goto out;
1783                 }
1784                 if ((rdev->mc_fw->size != mc_req_size) &&
1785                     (rdev->mc_fw->size != mc2_req_size)) {
1786                         printk(KERN_ERR
1787                                "si_mc: Bogus length %zu in firmware \"%s\"\n",
1788                                rdev->mc_fw->size, fw_name);
1789                         err = -EINVAL;
1790                 }
1791                 DRM_INFO("%s: %zu bytes\n", fw_name, rdev->mc_fw->size);
1792         } else {
1793                 err = radeon_ucode_validate(rdev->mc_fw);
1794                 if (err) {
1795                         printk(KERN_ERR
1796                                "si_cp: validation failed for firmware \"%s\"\n",
1797                                fw_name);
1798                         goto out;
1799                 } else {
1800                         new_fw++;
1801                 }
1802         }
1803
1804         if (new_smc)
1805                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", new_chip_name);
1806         else
1807                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", new_chip_name);
1808         err = reject_firmware(&rdev->smc_fw, fw_name, rdev->dev);
1809         if (err) {
1810                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", chip_name);
1811                 err = reject_firmware(&rdev->smc_fw, fw_name, rdev->dev);
1812                 if (err) {
1813                         printk(KERN_ERR
1814                                "smc: error loading firmware \"%s\"\n",
1815                                fw_name);
1816                         release_firmware(rdev->smc_fw);
1817                         rdev->smc_fw = NULL;
1818                         err = 0;
1819                 } else if (rdev->smc_fw->size != smc_req_size) {
1820                         printk(KERN_ERR
1821                                "si_smc: Bogus length %zu in firmware \"%s\"\n",
1822                                rdev->smc_fw->size, fw_name);
1823                         err = -EINVAL;
1824                 }
1825         } else {
1826                 err = radeon_ucode_validate(rdev->smc_fw);
1827                 if (err) {
1828                         printk(KERN_ERR
1829                                "si_cp: validation failed for firmware \"%s\"\n",
1830                                fw_name);
1831                         goto out;
1832                 } else {
1833                         new_fw++;
1834                 }
1835         }
1836
1837         if (new_fw == 0) {
1838                 rdev->new_fw = false;
1839         } else if (new_fw < 6) {
1840                 printk(KERN_ERR "si_fw: mixing new and old firmware!\n");
1841                 err = -EINVAL;
1842         } else {
1843                 rdev->new_fw = true;
1844         }
1845 out:
1846         if (err) {
1847                 if (err != -EINVAL)
1848                         printk(KERN_ERR
1849                                "si_cp: Failed to load firmware \"%s\"\n",
1850                                fw_name);
1851                 release_firmware(rdev->pfp_fw);
1852                 rdev->pfp_fw = NULL;
1853                 release_firmware(rdev->me_fw);
1854                 rdev->me_fw = NULL;
1855                 release_firmware(rdev->ce_fw);
1856                 rdev->ce_fw = NULL;
1857                 release_firmware(rdev->rlc_fw);
1858                 rdev->rlc_fw = NULL;
1859                 release_firmware(rdev->mc_fw);
1860                 rdev->mc_fw = NULL;
1861                 release_firmware(rdev->smc_fw);
1862                 rdev->smc_fw = NULL;
1863         }
1864         return err;
1865 }
1866
1867 /* watermark setup */
1868 static u32 dce6_line_buffer_adjust(struct radeon_device *rdev,
1869                                    struct radeon_crtc *radeon_crtc,
1870                                    struct drm_display_mode *mode,
1871                                    struct drm_display_mode *other_mode)
1872 {
1873         u32 tmp, buffer_alloc, i;
1874         u32 pipe_offset = radeon_crtc->crtc_id * 0x20;
1875         /*
1876          * Line Buffer Setup
1877          * There are 3 line buffers, each one shared by 2 display controllers.
1878          * DC_LB_MEMORY_SPLIT controls how that line buffer is shared between
1879          * the display controllers.  The paritioning is done via one of four
1880          * preset allocations specified in bits 21:20:
1881          *  0 - half lb
1882          *  2 - whole lb, other crtc must be disabled
1883          */
1884         /* this can get tricky if we have two large displays on a paired group
1885          * of crtcs.  Ideally for multiple large displays we'd assign them to
1886          * non-linked crtcs for maximum line buffer allocation.
1887          */
1888         if (radeon_crtc->base.enabled && mode) {
1889                 if (other_mode) {
1890                         tmp = 0; /* 1/2 */
1891                         buffer_alloc = 1;
1892                 } else {
1893                         tmp = 2; /* whole */
1894                         buffer_alloc = 2;
1895                 }
1896         } else {
1897                 tmp = 0;
1898                 buffer_alloc = 0;
1899         }
1900
1901         WREG32(DC_LB_MEMORY_SPLIT + radeon_crtc->crtc_offset,
1902                DC_LB_MEMORY_CONFIG(tmp));
1903
1904         WREG32(PIPE0_DMIF_BUFFER_CONTROL + pipe_offset,
1905                DMIF_BUFFERS_ALLOCATED(buffer_alloc));
1906         for (i = 0; i < rdev->usec_timeout; i++) {
1907                 if (RREG32(PIPE0_DMIF_BUFFER_CONTROL + pipe_offset) &
1908                     DMIF_BUFFERS_ALLOCATED_COMPLETED)
1909                         break;
1910                 udelay(1);
1911         }
1912
1913         if (radeon_crtc->base.enabled && mode) {
1914                 switch (tmp) {
1915                 case 0:
1916                 default:
1917                         return 4096 * 2;
1918                 case 2:
1919                         return 8192 * 2;
1920                 }
1921         }
1922
1923         /* controller not enabled, so no lb used */
1924         return 0;
1925 }
1926
1927 static u32 si_get_number_of_dram_channels(struct radeon_device *rdev)
1928 {
1929         u32 tmp = RREG32(MC_SHARED_CHMAP);
1930
1931         switch ((tmp & NOOFCHAN_MASK) >> NOOFCHAN_SHIFT) {
1932         case 0:
1933         default:
1934                 return 1;
1935         case 1:
1936                 return 2;
1937         case 2:
1938                 return 4;
1939         case 3:
1940                 return 8;
1941         case 4:
1942                 return 3;
1943         case 5:
1944                 return 6;
1945         case 6:
1946                 return 10;
1947         case 7:
1948                 return 12;
1949         case 8:
1950                 return 16;
1951         }
1952 }
1953
1954 struct dce6_wm_params {
1955         u32 dram_channels; /* number of dram channels */
1956         u32 yclk;          /* bandwidth per dram data pin in kHz */
1957         u32 sclk;          /* engine clock in kHz */
1958         u32 disp_clk;      /* display clock in kHz */
1959         u32 src_width;     /* viewport width */
1960         u32 active_time;   /* active display time in ns */
1961         u32 blank_time;    /* blank time in ns */
1962         bool interlaced;    /* mode is interlaced */
1963         fixed20_12 vsc;    /* vertical scale ratio */
1964         u32 num_heads;     /* number of active crtcs */
1965         u32 bytes_per_pixel; /* bytes per pixel display + overlay */
1966         u32 lb_size;       /* line buffer allocated to pipe */
1967         u32 vtaps;         /* vertical scaler taps */
1968 };
1969
1970 static u32 dce6_dram_bandwidth(struct dce6_wm_params *wm)
1971 {
1972         /* Calculate raw DRAM Bandwidth */
1973         fixed20_12 dram_efficiency; /* 0.7 */
1974         fixed20_12 yclk, dram_channels, bandwidth;
1975         fixed20_12 a;
1976
1977         a.full = dfixed_const(1000);
1978         yclk.full = dfixed_const(wm->yclk);
1979         yclk.full = dfixed_div(yclk, a);
1980         dram_channels.full = dfixed_const(wm->dram_channels * 4);
1981         a.full = dfixed_const(10);
1982         dram_efficiency.full = dfixed_const(7);
1983         dram_efficiency.full = dfixed_div(dram_efficiency, a);
1984         bandwidth.full = dfixed_mul(dram_channels, yclk);
1985         bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
1986
1987         return dfixed_trunc(bandwidth);
1988 }
1989
1990 static u32 dce6_dram_bandwidth_for_display(struct dce6_wm_params *wm)
1991 {
1992         /* Calculate DRAM Bandwidth and the part allocated to display. */
1993         fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
1994         fixed20_12 yclk, dram_channels, bandwidth;
1995         fixed20_12 a;
1996
1997         a.full = dfixed_const(1000);
1998         yclk.full = dfixed_const(wm->yclk);
1999         yclk.full = dfixed_div(yclk, a);
2000         dram_channels.full = dfixed_const(wm->dram_channels * 4);
2001         a.full = dfixed_const(10);
2002         disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
2003         disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
2004         bandwidth.full = dfixed_mul(dram_channels, yclk);
2005         bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
2006
2007         return dfixed_trunc(bandwidth);
2008 }
2009
2010 static u32 dce6_data_return_bandwidth(struct dce6_wm_params *wm)
2011 {
2012         /* Calculate the display Data return Bandwidth */
2013         fixed20_12 return_efficiency; /* 0.8 */
2014         fixed20_12 sclk, bandwidth;
2015         fixed20_12 a;
2016
2017         a.full = dfixed_const(1000);
2018         sclk.full = dfixed_const(wm->sclk);
2019         sclk.full = dfixed_div(sclk, a);
2020         a.full = dfixed_const(10);
2021         return_efficiency.full = dfixed_const(8);
2022         return_efficiency.full = dfixed_div(return_efficiency, a);
2023         a.full = dfixed_const(32);
2024         bandwidth.full = dfixed_mul(a, sclk);
2025         bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
2026
2027         return dfixed_trunc(bandwidth);
2028 }
2029
2030 static u32 dce6_get_dmif_bytes_per_request(struct dce6_wm_params *wm)
2031 {
2032         return 32;
2033 }
2034
2035 static u32 dce6_dmif_request_bandwidth(struct dce6_wm_params *wm)
2036 {
2037         /* Calculate the DMIF Request Bandwidth */
2038         fixed20_12 disp_clk_request_efficiency; /* 0.8 */
2039         fixed20_12 disp_clk, sclk, bandwidth;
2040         fixed20_12 a, b1, b2;
2041         u32 min_bandwidth;
2042
2043         a.full = dfixed_const(1000);
2044         disp_clk.full = dfixed_const(wm->disp_clk);
2045         disp_clk.full = dfixed_div(disp_clk, a);
2046         a.full = dfixed_const(dce6_get_dmif_bytes_per_request(wm) / 2);
2047         b1.full = dfixed_mul(a, disp_clk);
2048
2049         a.full = dfixed_const(1000);
2050         sclk.full = dfixed_const(wm->sclk);
2051         sclk.full = dfixed_div(sclk, a);
2052         a.full = dfixed_const(dce6_get_dmif_bytes_per_request(wm));
2053         b2.full = dfixed_mul(a, sclk);
2054
2055         a.full = dfixed_const(10);
2056         disp_clk_request_efficiency.full = dfixed_const(8);
2057         disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
2058
2059         min_bandwidth = min(dfixed_trunc(b1), dfixed_trunc(b2));
2060
2061         a.full = dfixed_const(min_bandwidth);
2062         bandwidth.full = dfixed_mul(a, disp_clk_request_efficiency);
2063
2064         return dfixed_trunc(bandwidth);
2065 }
2066
2067 static u32 dce6_available_bandwidth(struct dce6_wm_params *wm)
2068 {
2069         /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
2070         u32 dram_bandwidth = dce6_dram_bandwidth(wm);
2071         u32 data_return_bandwidth = dce6_data_return_bandwidth(wm);
2072         u32 dmif_req_bandwidth = dce6_dmif_request_bandwidth(wm);
2073
2074         return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
2075 }
2076
2077 static u32 dce6_average_bandwidth(struct dce6_wm_params *wm)
2078 {
2079         /* Calculate the display mode Average Bandwidth
2080          * DisplayMode should contain the source and destination dimensions,
2081          * timing, etc.
2082          */
2083         fixed20_12 bpp;
2084         fixed20_12 line_time;
2085         fixed20_12 src_width;
2086         fixed20_12 bandwidth;
2087         fixed20_12 a;
2088
2089         a.full = dfixed_const(1000);
2090         line_time.full = dfixed_const(wm->active_time + wm->blank_time);
2091         line_time.full = dfixed_div(line_time, a);
2092         bpp.full = dfixed_const(wm->bytes_per_pixel);
2093         src_width.full = dfixed_const(wm->src_width);
2094         bandwidth.full = dfixed_mul(src_width, bpp);
2095         bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
2096         bandwidth.full = dfixed_div(bandwidth, line_time);
2097
2098         return dfixed_trunc(bandwidth);
2099 }
2100
2101 static u32 dce6_latency_watermark(struct dce6_wm_params *wm)
2102 {
2103         /* First calcualte the latency in ns */
2104         u32 mc_latency = 2000; /* 2000 ns. */
2105         u32 available_bandwidth = dce6_available_bandwidth(wm);
2106         u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
2107         u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
2108         u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
2109         u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
2110                 (wm->num_heads * cursor_line_pair_return_time);
2111         u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
2112         u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
2113         u32 tmp, dmif_size = 12288;
2114         fixed20_12 a, b, c;
2115
2116         if (wm->num_heads == 0)
2117                 return 0;
2118
2119         a.full = dfixed_const(2);
2120         b.full = dfixed_const(1);
2121         if ((wm->vsc.full > a.full) ||
2122             ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
2123             (wm->vtaps >= 5) ||
2124             ((wm->vsc.full >= a.full) && wm->interlaced))
2125                 max_src_lines_per_dst_line = 4;
2126         else
2127                 max_src_lines_per_dst_line = 2;
2128
2129         a.full = dfixed_const(available_bandwidth);
2130         b.full = dfixed_const(wm->num_heads);
2131         a.full = dfixed_div(a, b);
2132
2133         b.full = dfixed_const(mc_latency + 512);
2134         c.full = dfixed_const(wm->disp_clk);
2135         b.full = dfixed_div(b, c);
2136
2137         c.full = dfixed_const(dmif_size);
2138         b.full = dfixed_div(c, b);
2139
2140         tmp = min(dfixed_trunc(a), dfixed_trunc(b));
2141
2142         b.full = dfixed_const(1000);
2143         c.full = dfixed_const(wm->disp_clk);
2144         b.full = dfixed_div(c, b);
2145         c.full = dfixed_const(wm->bytes_per_pixel);
2146         b.full = dfixed_mul(b, c);
2147
2148         lb_fill_bw = min(tmp, dfixed_trunc(b));
2149
2150         a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
2151         b.full = dfixed_const(1000);
2152         c.full = dfixed_const(lb_fill_bw);
2153         b.full = dfixed_div(c, b);
2154         a.full = dfixed_div(a, b);
2155         line_fill_time = dfixed_trunc(a);
2156
2157         if (line_fill_time < wm->active_time)
2158                 return latency;
2159         else
2160                 return latency + (line_fill_time - wm->active_time);
2161
2162 }
2163
2164 static bool dce6_average_bandwidth_vs_dram_bandwidth_for_display(struct dce6_wm_params *wm)
2165 {
2166         if (dce6_average_bandwidth(wm) <=
2167             (dce6_dram_bandwidth_for_display(wm) / wm->num_heads))
2168                 return true;
2169         else
2170                 return false;
2171 };
2172
2173 static bool dce6_average_bandwidth_vs_available_bandwidth(struct dce6_wm_params *wm)
2174 {
2175         if (dce6_average_bandwidth(wm) <=
2176             (dce6_available_bandwidth(wm) / wm->num_heads))
2177                 return true;
2178         else
2179                 return false;
2180 };
2181
2182 static bool dce6_check_latency_hiding(struct dce6_wm_params *wm)
2183 {
2184         u32 lb_partitions = wm->lb_size / wm->src_width;
2185         u32 line_time = wm->active_time + wm->blank_time;
2186         u32 latency_tolerant_lines;
2187         u32 latency_hiding;
2188         fixed20_12 a;
2189
2190         a.full = dfixed_const(1);
2191         if (wm->vsc.full > a.full)
2192                 latency_tolerant_lines = 1;
2193         else {
2194                 if (lb_partitions <= (wm->vtaps + 1))
2195                         latency_tolerant_lines = 1;
2196                 else
2197                         latency_tolerant_lines = 2;
2198         }
2199
2200         latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
2201
2202         if (dce6_latency_watermark(wm) <= latency_hiding)
2203                 return true;
2204         else
2205                 return false;
2206 }
2207
2208 static void dce6_program_watermarks(struct radeon_device *rdev,
2209                                          struct radeon_crtc *radeon_crtc,
2210                                          u32 lb_size, u32 num_heads)
2211 {
2212         struct drm_display_mode *mode = &radeon_crtc->base.mode;
2213         struct dce6_wm_params wm_low, wm_high;
2214         u32 dram_channels;
2215         u32 pixel_period;
2216         u32 line_time = 0;
2217         u32 latency_watermark_a = 0, latency_watermark_b = 0;
2218         u32 priority_a_mark = 0, priority_b_mark = 0;
2219         u32 priority_a_cnt = PRIORITY_OFF;
2220         u32 priority_b_cnt = PRIORITY_OFF;
2221         u32 tmp, arb_control3;
2222         fixed20_12 a, b, c;
2223
2224         if (radeon_crtc->base.enabled && num_heads && mode) {
2225                 pixel_period = 1000000 / (u32)mode->clock;
2226                 line_time = min((u32)mode->crtc_htotal * pixel_period, (u32)65535);
2227                 priority_a_cnt = 0;
2228                 priority_b_cnt = 0;
2229
2230                 if (rdev->family == CHIP_ARUBA)
2231                         dram_channels = evergreen_get_number_of_dram_channels(rdev);
2232                 else
2233                         dram_channels = si_get_number_of_dram_channels(rdev);
2234
2235                 /* watermark for high clocks */
2236                 if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
2237                         wm_high.yclk =
2238                                 radeon_dpm_get_mclk(rdev, false) * 10;
2239                         wm_high.sclk =
2240                                 radeon_dpm_get_sclk(rdev, false) * 10;
2241                 } else {
2242                         wm_high.yclk = rdev->pm.current_mclk * 10;
2243                         wm_high.sclk = rdev->pm.current_sclk * 10;
2244                 }
2245
2246                 wm_high.disp_clk = mode->clock;
2247                 wm_high.src_width = mode->crtc_hdisplay;
2248                 wm_high.active_time = mode->crtc_hdisplay * pixel_period;
2249                 wm_high.blank_time = line_time - wm_high.active_time;
2250                 wm_high.interlaced = false;
2251                 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2252                         wm_high.interlaced = true;
2253                 wm_high.vsc = radeon_crtc->vsc;
2254                 wm_high.vtaps = 1;
2255                 if (radeon_crtc->rmx_type != RMX_OFF)
2256                         wm_high.vtaps = 2;
2257                 wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
2258                 wm_high.lb_size = lb_size;
2259                 wm_high.dram_channels = dram_channels;
2260                 wm_high.num_heads = num_heads;
2261
2262                 /* watermark for low clocks */
2263                 if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
2264                         wm_low.yclk =
2265                                 radeon_dpm_get_mclk(rdev, true) * 10;
2266                         wm_low.sclk =
2267                                 radeon_dpm_get_sclk(rdev, true) * 10;
2268                 } else {
2269                         wm_low.yclk = rdev->pm.current_mclk * 10;
2270                         wm_low.sclk = rdev->pm.current_sclk * 10;
2271                 }
2272
2273                 wm_low.disp_clk = mode->clock;
2274                 wm_low.src_width = mode->crtc_hdisplay;
2275                 wm_low.active_time = mode->crtc_hdisplay * pixel_period;
2276                 wm_low.blank_time = line_time - wm_low.active_time;
2277                 wm_low.interlaced = false;
2278                 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2279                         wm_low.interlaced = true;
2280                 wm_low.vsc = radeon_crtc->vsc;
2281                 wm_low.vtaps = 1;
2282                 if (radeon_crtc->rmx_type != RMX_OFF)
2283                         wm_low.vtaps = 2;
2284                 wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
2285                 wm_low.lb_size = lb_size;
2286                 wm_low.dram_channels = dram_channels;
2287                 wm_low.num_heads = num_heads;
2288
2289                 /* set for high clocks */
2290                 latency_watermark_a = min(dce6_latency_watermark(&wm_high), (u32)65535);
2291                 /* set for low clocks */
2292                 latency_watermark_b = min(dce6_latency_watermark(&wm_low), (u32)65535);
2293
2294                 /* possibly force display priority to high */
2295                 /* should really do this at mode validation time... */
2296                 if (!dce6_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
2297                     !dce6_average_bandwidth_vs_available_bandwidth(&wm_high) ||
2298                     !dce6_check_latency_hiding(&wm_high) ||
2299                     (rdev->disp_priority == 2)) {
2300                         DRM_DEBUG_KMS("force priority to high\n");
2301                         priority_a_cnt |= PRIORITY_ALWAYS_ON;
2302                         priority_b_cnt |= PRIORITY_ALWAYS_ON;
2303                 }
2304                 if (!dce6_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
2305                     !dce6_average_bandwidth_vs_available_bandwidth(&wm_low) ||
2306                     !dce6_check_latency_hiding(&wm_low) ||
2307                     (rdev->disp_priority == 2)) {
2308                         DRM_DEBUG_KMS("force priority to high\n");
2309                         priority_a_cnt |= PRIORITY_ALWAYS_ON;
2310                         priority_b_cnt |= PRIORITY_ALWAYS_ON;
2311                 }
2312
2313                 a.full = dfixed_const(1000);
2314                 b.full = dfixed_const(mode->clock);
2315                 b.full = dfixed_div(b, a);
2316                 c.full = dfixed_const(latency_watermark_a);
2317                 c.full = dfixed_mul(c, b);
2318                 c.full = dfixed_mul(c, radeon_crtc->hsc);
2319                 c.full = dfixed_div(c, a);
2320                 a.full = dfixed_const(16);
2321                 c.full = dfixed_div(c, a);
2322                 priority_a_mark = dfixed_trunc(c);
2323                 priority_a_cnt |= priority_a_mark & PRIORITY_MARK_MASK;
2324
2325                 a.full = dfixed_const(1000);
2326                 b.full = dfixed_const(mode->clock);
2327                 b.full = dfixed_div(b, a);
2328                 c.full = dfixed_const(latency_watermark_b);
2329                 c.full = dfixed_mul(c, b);
2330                 c.full = dfixed_mul(c, radeon_crtc->hsc);
2331                 c.full = dfixed_div(c, a);
2332                 a.full = dfixed_const(16);
2333                 c.full = dfixed_div(c, a);
2334                 priority_b_mark = dfixed_trunc(c);
2335                 priority_b_cnt |= priority_b_mark & PRIORITY_MARK_MASK;
2336
2337                 /* Save number of lines the linebuffer leads before the scanout */
2338                 radeon_crtc->lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
2339         }
2340
2341         /* select wm A */
2342         arb_control3 = RREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset);
2343         tmp = arb_control3;
2344         tmp &= ~LATENCY_WATERMARK_MASK(3);
2345         tmp |= LATENCY_WATERMARK_MASK(1);
2346         WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset, tmp);
2347         WREG32(DPG_PIPE_LATENCY_CONTROL + radeon_crtc->crtc_offset,
2348                (LATENCY_LOW_WATERMARK(latency_watermark_a) |
2349                 LATENCY_HIGH_WATERMARK(line_time)));
2350         /* select wm B */
2351         tmp = RREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset);
2352         tmp &= ~LATENCY_WATERMARK_MASK(3);
2353         tmp |= LATENCY_WATERMARK_MASK(2);
2354         WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset, tmp);
2355         WREG32(DPG_PIPE_LATENCY_CONTROL + radeon_crtc->crtc_offset,
2356                (LATENCY_LOW_WATERMARK(latency_watermark_b) |
2357                 LATENCY_HIGH_WATERMARK(line_time)));
2358         /* restore original selection */
2359         WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset, arb_control3);
2360
2361         /* write the priority marks */
2362         WREG32(PRIORITY_A_CNT + radeon_crtc->crtc_offset, priority_a_cnt);
2363         WREG32(PRIORITY_B_CNT + radeon_crtc->crtc_offset, priority_b_cnt);
2364
2365         /* save values for DPM */
2366         radeon_crtc->line_time = line_time;
2367         radeon_crtc->wm_high = latency_watermark_a;
2368         radeon_crtc->wm_low = latency_watermark_b;
2369 }
2370
2371 void dce6_bandwidth_update(struct radeon_device *rdev)
2372 {
2373         struct drm_display_mode *mode0 = NULL;
2374         struct drm_display_mode *mode1 = NULL;
2375         u32 num_heads = 0, lb_size;
2376         int i;
2377
2378         if (!rdev->mode_info.mode_config_initialized)
2379                 return;
2380
2381         radeon_update_display_priority(rdev);
2382
2383         for (i = 0; i < rdev->num_crtc; i++) {
2384                 if (rdev->mode_info.crtcs[i]->base.enabled)
2385                         num_heads++;
2386         }
2387         for (i = 0; i < rdev->num_crtc; i += 2) {
2388                 mode0 = &rdev->mode_info.crtcs[i]->base.mode;
2389                 mode1 = &rdev->mode_info.crtcs[i+1]->base.mode;
2390                 lb_size = dce6_line_buffer_adjust(rdev, rdev->mode_info.crtcs[i], mode0, mode1);
2391                 dce6_program_watermarks(rdev, rdev->mode_info.crtcs[i], lb_size, num_heads);
2392                 lb_size = dce6_line_buffer_adjust(rdev, rdev->mode_info.crtcs[i+1], mode1, mode0);
2393                 dce6_program_watermarks(rdev, rdev->mode_info.crtcs[i+1], lb_size, num_heads);
2394         }
2395 }
2396
2397 /*
2398  * Core functions
2399  */
2400 static void si_tiling_mode_table_init(struct radeon_device *rdev)
2401 {
2402         u32 *tile = rdev->config.si.tile_mode_array;
2403         const u32 num_tile_mode_states =
2404                         ARRAY_SIZE(rdev->config.si.tile_mode_array);
2405         u32 reg_offset, split_equal_to_row_size;
2406
2407         switch (rdev->config.si.mem_row_size_in_kb) {
2408         case 1:
2409                 split_equal_to_row_size = ADDR_SURF_TILE_SPLIT_1KB;
2410                 break;
2411         case 2:
2412         default:
2413                 split_equal_to_row_size = ADDR_SURF_TILE_SPLIT_2KB;
2414                 break;
2415         case 4:
2416                 split_equal_to_row_size = ADDR_SURF_TILE_SPLIT_4KB;
2417                 break;
2418         }
2419
2420         for (reg_offset = 0; reg_offset < num_tile_mode_states; reg_offset++)
2421                 tile[reg_offset] = 0;
2422
2423         switch(rdev->family) {
2424         case CHIP_TAHITI:
2425         case CHIP_PITCAIRN:
2426                 /* non-AA compressed depth or any compressed stencil */
2427                 tile[0] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2428                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2429                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2430                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2431                            NUM_BANKS(ADDR_SURF_16_BANK) |
2432                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2433                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2434                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2435                 /* 2xAA/4xAA compressed depth only */
2436                 tile[1] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2437                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2438                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2439                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
2440                            NUM_BANKS(ADDR_SURF_16_BANK) |
2441                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2442                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2443                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2444                 /* 8xAA compressed depth only */
2445                 tile[2] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2446                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2447                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2448                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2449                            NUM_BANKS(ADDR_SURF_16_BANK) |
2450                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2451                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2452                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2453                 /* 2xAA/4xAA compressed depth with stencil (for depth buffer) */
2454                 tile[3] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2455                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2456                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2457                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
2458                            NUM_BANKS(ADDR_SURF_16_BANK) |
2459                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2460                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2461                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2462                 /* Maps w/ a dimension less than the 2D macro-tile dimensions (for mipmapped depth textures) */
2463                 tile[4] = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
2464                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2465                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2466                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2467                            NUM_BANKS(ADDR_SURF_16_BANK) |
2468                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2469                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2470                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2471                 /* Uncompressed 16bpp depth - and stencil buffer allocated with it */
2472                 tile[5] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2473                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2474                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2475                            TILE_SPLIT(split_equal_to_row_size) |
2476                            NUM_BANKS(ADDR_SURF_16_BANK) |
2477                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2478                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2479                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2480                 /* Uncompressed 32bpp depth - and stencil buffer allocated with it */
2481                 tile[6] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2482                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2483                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2484                            TILE_SPLIT(split_equal_to_row_size) |
2485                            NUM_BANKS(ADDR_SURF_16_BANK) |
2486                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2487                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2488                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2489                 /* Uncompressed 8bpp stencil without depth (drivers typically do not use) */
2490                 tile[7] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2491                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2492                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2493                            TILE_SPLIT(split_equal_to_row_size) |
2494                            NUM_BANKS(ADDR_SURF_16_BANK) |
2495                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2496                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2497                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2498                 /* 1D and 1D Array Surfaces */
2499                 tile[8] = (ARRAY_MODE(ARRAY_LINEAR_ALIGNED) |
2500                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2501                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2502                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2503                            NUM_BANKS(ADDR_SURF_16_BANK) |
2504                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2505                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2506                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2507                 /* Displayable maps. */
2508                 tile[9] = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
2509                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2510                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2511                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2512                            NUM_BANKS(ADDR_SURF_16_BANK) |
2513                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2514                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2515                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2516                 /* Display 8bpp. */
2517                 tile[10] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2518                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2519                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2520                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2521                            NUM_BANKS(ADDR_SURF_16_BANK) |
2522                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2523                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2524                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2525                 /* Display 16bpp. */
2526                 tile[11] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2527                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2528                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2529                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2530                            NUM_BANKS(ADDR_SURF_16_BANK) |
2531                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2532                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2533                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2534                 /* Display 32bpp. */
2535                 tile[12] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2536                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2537                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2538                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
2539                            NUM_BANKS(ADDR_SURF_16_BANK) |
2540                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2541                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2542                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2543                 /* Thin. */
2544                 tile[13] = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
2545                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2546                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2547                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2548                            NUM_BANKS(ADDR_SURF_16_BANK) |
2549                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2550                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2551                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2552                 /* Thin 8 bpp. */
2553                 tile[14] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2554                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2555                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2556                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2557                            NUM_BANKS(ADDR_SURF_16_BANK) |
2558                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2559                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2560                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2561                 /* Thin 16 bpp. */
2562                 tile[15] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2563                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2564                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2565                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2566                            NUM_BANKS(ADDR_SURF_16_BANK) |
2567                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2568                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2569                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2570                 /* Thin 32 bpp. */
2571                 tile[16] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2572                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2573                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2574                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
2575                            NUM_BANKS(ADDR_SURF_16_BANK) |
2576                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2577                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2578                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2579                 /* Thin 64 bpp. */
2580                 tile[17] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2581                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2582                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2583                            TILE_SPLIT(split_equal_to_row_size) |
2584                            NUM_BANKS(ADDR_SURF_16_BANK) |
2585                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2586                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2587                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2588                 /* 8 bpp PRT. */
2589                 tile[21] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2590                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2591                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2592                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2593                            NUM_BANKS(ADDR_SURF_16_BANK) |
2594                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_2) |
2595                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2596                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2597                 /* 16 bpp PRT */
2598                 tile[22] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2599                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2600                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2601                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2602                            NUM_BANKS(ADDR_SURF_16_BANK) |
2603                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2604                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2605                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2606                 /* 32 bpp PRT */
2607                 tile[23] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2608                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2609                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2610                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2611                            NUM_BANKS(ADDR_SURF_16_BANK) |
2612                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2613                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2614                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2615                 /* 64 bpp PRT */
2616                 tile[24] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2617                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2618                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2619                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
2620                            NUM_BANKS(ADDR_SURF_16_BANK) |
2621                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2622                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2623                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2624                 /* 128 bpp PRT */
2625                 tile[25] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2626                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2627                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2628                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_1KB) |
2629                            NUM_BANKS(ADDR_SURF_8_BANK) |
2630                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2631                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2632                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2633
2634                 for (reg_offset = 0; reg_offset < num_tile_mode_states; reg_offset++)
2635                         WREG32(GB_TILE_MODE0 + (reg_offset * 4), tile[reg_offset]);
2636                 break;
2637
2638         case CHIP_VERDE:
2639         case CHIP_OLAND:
2640         case CHIP_HAINAN:
2641                 /* non-AA compressed depth or any compressed stencil */
2642                 tile[0] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2643                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2644                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2645                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2646                            NUM_BANKS(ADDR_SURF_16_BANK) |
2647                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2648                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2649                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2650                 /* 2xAA/4xAA compressed depth only */
2651                 tile[1] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2652                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2653                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2654                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
2655                            NUM_BANKS(ADDR_SURF_16_BANK) |
2656                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2657                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2658                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2659                 /* 8xAA compressed depth only */
2660                 tile[2] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2661                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2662                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2663                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2664                            NUM_BANKS(ADDR_SURF_16_BANK) |
2665                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2666                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2667                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2668                 /* 2xAA/4xAA compressed depth with stencil (for depth buffer) */
2669                 tile[3] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2670                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2671                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2672                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
2673                            NUM_BANKS(ADDR_SURF_16_BANK) |
2674                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2675                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2676                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2677                 /* Maps w/ a dimension less than the 2D macro-tile dimensions (for mipmapped depth textures) */
2678                 tile[4] = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
2679                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2680                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2681                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2682                            NUM_BANKS(ADDR_SURF_16_BANK) |
2683                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2684                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2685                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2686                 /* Uncompressed 16bpp depth - and stencil buffer allocated with it */
2687                 tile[5] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2688                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2689                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2690                            TILE_SPLIT(split_equal_to_row_size) |
2691                            NUM_BANKS(ADDR_SURF_16_BANK) |
2692                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2693                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2694                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2695                 /* Uncompressed 32bpp depth - and stencil buffer allocated with it */
2696                 tile[6] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2697                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2698                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2699                            TILE_SPLIT(split_equal_to_row_size) |
2700                            NUM_BANKS(ADDR_SURF_16_BANK) |
2701                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2702                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2703                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2704                 /* Uncompressed 8bpp stencil without depth (drivers typically do not use) */
2705                 tile[7] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2706                            MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
2707                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2708                            TILE_SPLIT(split_equal_to_row_size) |
2709                            NUM_BANKS(ADDR_SURF_16_BANK) |
2710                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2711                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2712                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2713                 /* 1D and 1D Array Surfaces */
2714                 tile[8] = (ARRAY_MODE(ARRAY_LINEAR_ALIGNED) |
2715                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2716                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2717                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2718                            NUM_BANKS(ADDR_SURF_16_BANK) |
2719                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2720                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2721                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2722                 /* Displayable maps. */
2723                 tile[9] = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
2724                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2725                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2726                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2727                            NUM_BANKS(ADDR_SURF_16_BANK) |
2728                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2729                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2730                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2731                 /* Display 8bpp. */
2732                 tile[10] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2733                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2734                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2735                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2736                            NUM_BANKS(ADDR_SURF_16_BANK) |
2737                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2738                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2739                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2740                 /* Display 16bpp. */
2741                 tile[11] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2742                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2743                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2744                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2745                            NUM_BANKS(ADDR_SURF_16_BANK) |
2746                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2747                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2748                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2749                 /* Display 32bpp. */
2750                 tile[12] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2751                            MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
2752                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2753                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
2754                            NUM_BANKS(ADDR_SURF_16_BANK) |
2755                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2756                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2757                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2758                 /* Thin. */
2759                 tile[13] = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
2760                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2761                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2762                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
2763                            NUM_BANKS(ADDR_SURF_16_BANK) |
2764                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2765                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2766                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2767                 /* Thin 8 bpp. */
2768                 tile[14] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2769                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2770                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2771                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2772                            NUM_BANKS(ADDR_SURF_16_BANK) |
2773                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2774                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2775                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2776                 /* Thin 16 bpp. */
2777                 tile[15] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2778                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2779                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2780                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2781                            NUM_BANKS(ADDR_SURF_16_BANK) |
2782                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2783                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2784                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2785                 /* Thin 32 bpp. */
2786                 tile[16] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2787                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2788                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2789                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
2790                            NUM_BANKS(ADDR_SURF_16_BANK) |
2791                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2792                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2793                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2794                 /* Thin 64 bpp. */
2795                 tile[17] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2796                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2797                            PIPE_CONFIG(ADDR_SURF_P4_8x16) |
2798                            TILE_SPLIT(split_equal_to_row_size) |
2799                            NUM_BANKS(ADDR_SURF_16_BANK) |
2800                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2801                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2802                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2803                 /* 8 bpp PRT. */
2804                 tile[21] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2805                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2806                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2807                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2808                            NUM_BANKS(ADDR_SURF_16_BANK) |
2809                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_2) |
2810                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2811                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2812                 /* 16 bpp PRT */
2813                 tile[22] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2814                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2815                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2816                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2817                            NUM_BANKS(ADDR_SURF_16_BANK) |
2818                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2819                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
2820                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
2821                 /* 32 bpp PRT */
2822                 tile[23] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2823                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2824                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2825                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
2826                            NUM_BANKS(ADDR_SURF_16_BANK) |
2827                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2828                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
2829                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2830                 /* 64 bpp PRT */
2831                 tile[24] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2832                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2833                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2834                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
2835                            NUM_BANKS(ADDR_SURF_16_BANK) |
2836                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2837                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2838                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
2839                 /* 128 bpp PRT */
2840                 tile[25] = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
2841                            MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
2842                            PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
2843                            TILE_SPLIT(ADDR_SURF_TILE_SPLIT_1KB) |
2844                            NUM_BANKS(ADDR_SURF_8_BANK) |
2845                            BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
2846                            BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
2847                            MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
2848
2849                 for (reg_offset = 0; reg_offset < num_tile_mode_states; reg_offset++)
2850                         WREG32(GB_TILE_MODE0 + (reg_offset * 4), tile[reg_offset]);
2851                 break;
2852
2853         default:
2854                 DRM_ERROR("unknown asic: 0x%x\n", rdev->family);
2855         }
2856 }
2857
2858 static void si_select_se_sh(struct radeon_device *rdev,
2859                             u32 se_num, u32 sh_num)
2860 {
2861         u32 data = INSTANCE_BROADCAST_WRITES;
2862
2863         if ((se_num == 0xffffffff) && (sh_num == 0xffffffff))
2864                 data |= SH_BROADCAST_WRITES | SE_BROADCAST_WRITES;
2865         else if (se_num == 0xffffffff)
2866                 data |= SE_BROADCAST_WRITES | SH_INDEX(sh_num);
2867         else if (sh_num == 0xffffffff)
2868                 data |= SH_BROADCAST_WRITES | SE_INDEX(se_num);
2869         else
2870                 data |= SH_INDEX(sh_num) | SE_INDEX(se_num);
2871         WREG32(GRBM_GFX_INDEX, data);
2872 }
2873
2874 static u32 si_create_bitmask(u32 bit_width)
2875 {
2876         u32 i, mask = 0;
2877
2878         for (i = 0; i < bit_width; i++) {
2879                 mask <<= 1;
2880                 mask |= 1;
2881         }
2882         return mask;
2883 }
2884
2885 static u32 si_get_cu_enabled(struct radeon_device *rdev, u32 cu_per_sh)
2886 {
2887         u32 data, mask;
2888
2889         data = RREG32(CC_GC_SHADER_ARRAY_CONFIG);
2890         if (data & 1)
2891                 data &= INACTIVE_CUS_MASK;
2892         else
2893                 data = 0;
2894         data |= RREG32(GC_USER_SHADER_ARRAY_CONFIG);
2895
2896         data >>= INACTIVE_CUS_SHIFT;
2897
2898         mask = si_create_bitmask(cu_per_sh);
2899
2900         return ~data & mask;
2901 }
2902
2903 static void si_setup_spi(struct radeon_device *rdev,
2904                          u32 se_num, u32 sh_per_se,
2905                          u32 cu_per_sh)
2906 {
2907         int i, j, k;
2908         u32 data, mask, active_cu;
2909
2910         for (i = 0; i < se_num; i++) {
2911                 for (j = 0; j < sh_per_se; j++) {
2912                         si_select_se_sh(rdev, i, j);
2913                         data = RREG32(SPI_STATIC_THREAD_MGMT_3);
2914                         active_cu = si_get_cu_enabled(rdev, cu_per_sh);
2915
2916                         mask = 1;
2917                         for (k = 0; k < 16; k++) {
2918                                 mask <<= k;
2919                                 if (active_cu & mask) {
2920                                         data &= ~mask;
2921                                         WREG32(SPI_STATIC_THREAD_MGMT_3, data);
2922                                         break;
2923                                 }
2924                         }
2925                 }
2926         }
2927         si_select_se_sh(rdev, 0xffffffff, 0xffffffff);
2928 }
2929
2930 static u32 si_get_rb_disabled(struct radeon_device *rdev,
2931                               u32 max_rb_num_per_se,
2932                               u32 sh_per_se)
2933 {
2934         u32 data, mask;
2935
2936         data = RREG32(CC_RB_BACKEND_DISABLE);
2937         if (data & 1)
2938                 data &= BACKEND_DISABLE_MASK;
2939         else
2940                 data = 0;
2941         data |= RREG32(GC_USER_RB_BACKEND_DISABLE);
2942
2943         data >>= BACKEND_DISABLE_SHIFT;
2944
2945         mask = si_create_bitmask(max_rb_num_per_se / sh_per_se);
2946
2947         return data & mask;
2948 }
2949
2950 static void si_setup_rb(struct radeon_device *rdev,
2951                         u32 se_num, u32 sh_per_se,
2952                         u32 max_rb_num_per_se)
2953 {
2954         int i, j;
2955         u32 data, mask;
2956         u32 disabled_rbs = 0;
2957         u32 enabled_rbs = 0;
2958
2959         for (i = 0; i < se_num; i++) {
2960                 for (j = 0; j < sh_per_se; j++) {
2961                         si_select_se_sh(rdev, i, j);
2962                         data = si_get_rb_disabled(rdev, max_rb_num_per_se, sh_per_se);
2963                         disabled_rbs |= data << ((i * sh_per_se + j) * TAHITI_RB_BITMAP_WIDTH_PER_SH);
2964                 }
2965         }
2966         si_select_se_sh(rdev, 0xffffffff, 0xffffffff);
2967
2968         mask = 1;
2969         for (i = 0; i < max_rb_num_per_se * se_num; i++) {
2970                 if (!(disabled_rbs & mask))
2971                         enabled_rbs |= mask;
2972                 mask <<= 1;
2973         }
2974
2975         rdev->config.si.backend_enable_mask = enabled_rbs;
2976
2977         for (i = 0; i < se_num; i++) {
2978                 si_select_se_sh(rdev, i, 0xffffffff);
2979                 data = 0;
2980                 for (j = 0; j < sh_per_se; j++) {
2981                         switch (enabled_rbs & 3) {
2982                         case 1:
2983                                 data |= (RASTER_CONFIG_RB_MAP_0 << (i * sh_per_se + j) * 2);
2984                                 break;
2985                         case 2:
2986                                 data |= (RASTER_CONFIG_RB_MAP_3 << (i * sh_per_se + j) * 2);
2987                                 break;
2988                         case 3:
2989                         default:
2990                                 data |= (RASTER_CONFIG_RB_MAP_2 << (i * sh_per_se + j) * 2);
2991                                 break;
2992                         }
2993                         enabled_rbs >>= 2;
2994                 }
2995                 WREG32(PA_SC_RASTER_CONFIG, data);
2996         }
2997         si_select_se_sh(rdev, 0xffffffff, 0xffffffff);
2998 }
2999
3000 static void si_gpu_init(struct radeon_device *rdev)
3001 {
3002         u32 gb_addr_config = 0;
3003         u32 mc_shared_chmap, mc_arb_ramcfg;
3004         u32 sx_debug_1;
3005         u32 hdp_host_path_cntl;
3006         u32 tmp;
3007         int i, j;
3008
3009         switch (rdev->family) {
3010         case CHIP_TAHITI:
3011                 rdev->config.si.max_shader_engines = 2;
3012                 rdev->config.si.max_tile_pipes = 12;
3013                 rdev->config.si.max_cu_per_sh = 8;
3014                 rdev->config.si.max_sh_per_se = 2;
3015                 rdev->config.si.max_backends_per_se = 4;
3016                 rdev->config.si.max_texture_channel_caches = 12;
3017                 rdev->config.si.max_gprs = 256;
3018                 rdev->config.si.max_gs_threads = 32;
3019                 rdev->config.si.max_hw_contexts = 8;
3020
3021                 rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
3022                 rdev->config.si.sc_prim_fifo_size_backend = 0x100;
3023                 rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
3024                 rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
3025                 gb_addr_config = TAHITI_GB_ADDR_CONFIG_GOLDEN;
3026                 break;
3027         case CHIP_PITCAIRN:
3028                 rdev->config.si.max_shader_engines = 2;
3029                 rdev->config.si.max_tile_pipes = 8;
3030                 rdev->config.si.max_cu_per_sh = 5;
3031                 rdev->config.si.max_sh_per_se = 2;
3032                 rdev->config.si.max_backends_per_se = 4;
3033                 rdev->config.si.max_texture_channel_caches = 8;
3034                 rdev->config.si.max_gprs = 256;
3035                 rdev->config.si.max_gs_threads = 32;
3036                 rdev->config.si.max_hw_contexts = 8;
3037
3038                 rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
3039                 rdev->config.si.sc_prim_fifo_size_backend = 0x100;
3040                 rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
3041                 rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
3042                 gb_addr_config = TAHITI_GB_ADDR_CONFIG_GOLDEN;
3043                 break;
3044         case CHIP_VERDE:
3045         default:
3046                 rdev->config.si.max_shader_engines = 1;
3047                 rdev->config.si.max_tile_pipes = 4;
3048                 rdev->config.si.max_cu_per_sh = 5;
3049                 rdev->config.si.max_sh_per_se = 2;
3050                 rdev->config.si.max_backends_per_se = 4;
3051                 rdev->config.si.max_texture_channel_caches = 4;
3052                 rdev->config.si.max_gprs = 256;
3053                 rdev->config.si.max_gs_threads = 32;
3054                 rdev->config.si.max_hw_contexts = 8;
3055
3056                 rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
3057                 rdev->config.si.sc_prim_fifo_size_backend = 0x40;
3058                 rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
3059                 rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
3060                 gb_addr_config = VERDE_GB_ADDR_CONFIG_GOLDEN;
3061                 break;
3062         case CHIP_OLAND:
3063                 rdev->config.si.max_shader_engines = 1;
3064                 rdev->config.si.max_tile_pipes = 4;
3065                 rdev->config.si.max_cu_per_sh = 6;
3066                 rdev->config.si.max_sh_per_se = 1;
3067                 rdev->config.si.max_backends_per_se = 2;
3068                 rdev->config.si.max_texture_channel_caches = 4;
3069                 rdev->config.si.max_gprs = 256;
3070                 rdev->config.si.max_gs_threads = 16;
3071                 rdev->config.si.max_hw_contexts = 8;
3072
3073                 rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
3074                 rdev->config.si.sc_prim_fifo_size_backend = 0x40;
3075                 rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
3076                 rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
3077                 gb_addr_config = VERDE_GB_ADDR_CONFIG_GOLDEN;
3078                 break;
3079         case CHIP_HAINAN:
3080                 rdev->config.si.max_shader_engines = 1;
3081                 rdev->config.si.max_tile_pipes = 4;
3082                 rdev->config.si.max_cu_per_sh = 5;
3083                 rdev->config.si.max_sh_per_se = 1;
3084                 rdev->config.si.max_backends_per_se = 1;
3085                 rdev->config.si.max_texture_channel_caches = 2;
3086                 rdev->config.si.max_gprs = 256;
3087                 rdev->config.si.max_gs_threads = 16;
3088                 rdev->config.si.max_hw_contexts = 8;
3089
3090                 rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
3091                 rdev->config.si.sc_prim_fifo_size_backend = 0x40;
3092                 rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
3093                 rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
3094                 gb_addr_config = HAINAN_GB_ADDR_CONFIG_GOLDEN;
3095                 break;
3096         }
3097
3098         /* Initialize HDP */
3099         for (i = 0, j = 0; i < 32; i++, j += 0x18) {
3100                 WREG32((0x2c14 + j), 0x00000000);
3101                 WREG32((0x2c18 + j), 0x00000000);
3102                 WREG32((0x2c1c + j), 0x00000000);
3103                 WREG32((0x2c20 + j), 0x00000000);
3104                 WREG32((0x2c24 + j), 0x00000000);
3105         }
3106
3107         WREG32(GRBM_CNTL, GRBM_READ_TIMEOUT(0xff));
3108         WREG32(SRBM_INT_CNTL, 1);
3109         WREG32(SRBM_INT_ACK, 1);
3110
3111         evergreen_fix_pci_max_read_req_size(rdev);
3112
3113         WREG32(BIF_FB_EN, FB_READ_EN | FB_WRITE_EN);
3114
3115         mc_shared_chmap = RREG32(MC_SHARED_CHMAP);
3116         mc_arb_ramcfg = RREG32(MC_ARB_RAMCFG);
3117
3118         rdev->config.si.num_tile_pipes = rdev->config.si.max_tile_pipes;
3119         rdev->config.si.mem_max_burst_length_bytes = 256;
3120         tmp = (mc_arb_ramcfg & NOOFCOLS_MASK) >> NOOFCOLS_SHIFT;
3121         rdev->config.si.mem_row_size_in_kb = (4 * (1 << (8 + tmp))) / 1024;
3122         if (rdev->config.si.mem_row_size_in_kb > 4)
3123                 rdev->config.si.mem_row_size_in_kb = 4;
3124         /* XXX use MC settings? */
3125         rdev->config.si.shader_engine_tile_size = 32;
3126         rdev->config.si.num_gpus = 1;
3127         rdev->config.si.multi_gpu_tile_size = 64;
3128
3129         /* fix up row size */
3130         gb_addr_config &= ~ROW_SIZE_MASK;
3131         switch (rdev->config.si.mem_row_size_in_kb) {
3132         case 1:
3133         default:
3134                 gb_addr_config |= ROW_SIZE(0);
3135                 break;
3136         case 2:
3137                 gb_addr_config |= ROW_SIZE(1);
3138                 break;
3139         case 4:
3140                 gb_addr_config |= ROW_SIZE(2);
3141                 break;
3142         }
3143
3144         /* setup tiling info dword.  gb_addr_config is not adequate since it does
3145          * not have bank info, so create a custom tiling dword.
3146          * bits 3:0   num_pipes
3147          * bits 7:4   num_banks
3148          * bits 11:8  group_size
3149          * bits 15:12 row_size
3150          */
3151         rdev->config.si.tile_config = 0;
3152         switch (rdev->config.si.num_tile_pipes) {
3153         case 1:
3154                 rdev->config.si.tile_config |= (0 << 0);
3155                 break;
3156         case 2:
3157                 rdev->config.si.tile_config |= (1 << 0);
3158                 break;
3159         case 4:
3160                 rdev->config.si.tile_config |= (2 << 0);
3161                 break;
3162         case 8:
3163         default:
3164                 /* XXX what about 12? */
3165                 rdev->config.si.tile_config |= (3 << 0);
3166                 break;
3167         }       
3168         switch ((mc_arb_ramcfg & NOOFBANK_MASK) >> NOOFBANK_SHIFT) {
3169         case 0: /* four banks */
3170                 rdev->config.si.tile_config |= 0 << 4;
3171                 break;
3172         case 1: /* eight banks */
3173                 rdev->config.si.tile_config |= 1 << 4;
3174                 break;
3175         case 2: /* sixteen banks */
3176         default:
3177                 rdev->config.si.tile_config |= 2 << 4;
3178                 break;
3179         }
3180         rdev->config.si.tile_config |=
3181                 ((gb_addr_config & PIPE_INTERLEAVE_SIZE_MASK) >> PIPE_INTERLEAVE_SIZE_SHIFT) << 8;
3182         rdev->config.si.tile_config |=
3183                 ((gb_addr_config & ROW_SIZE_MASK) >> ROW_SIZE_SHIFT) << 12;
3184
3185         WREG32(GB_ADDR_CONFIG, gb_addr_config);
3186         WREG32(DMIF_ADDR_CONFIG, gb_addr_config);
3187         WREG32(DMIF_ADDR_CALC, gb_addr_config);
3188         WREG32(HDP_ADDR_CONFIG, gb_addr_config);
3189         WREG32(DMA_TILING_CONFIG + DMA0_REGISTER_OFFSET, gb_addr_config);
3190         WREG32(DMA_TILING_CONFIG + DMA1_REGISTER_OFFSET, gb_addr_config);
3191         if (rdev->has_uvd) {
3192                 WREG32(UVD_UDEC_ADDR_CONFIG, gb_addr_config);
3193                 WREG32(UVD_UDEC_DB_ADDR_CONFIG, gb_addr_config);
3194                 WREG32(UVD_UDEC_DBW_ADDR_CONFIG, gb_addr_config);
3195         }
3196
3197         si_tiling_mode_table_init(rdev);
3198
3199         si_setup_rb(rdev, rdev->config.si.max_shader_engines,
3200                     rdev->config.si.max_sh_per_se,
3201                     rdev->config.si.max_backends_per_se);
3202
3203         si_setup_spi(rdev, rdev->config.si.max_shader_engines,
3204                      rdev->config.si.max_sh_per_se,
3205                      rdev->config.si.max_cu_per_sh);
3206
3207         rdev->config.si.active_cus = 0;
3208         for (i = 0; i < rdev->config.si.max_shader_engines; i++) {
3209                 for (j = 0; j < rdev->config.si.max_sh_per_se; j++) {
3210                         rdev->config.si.active_cus +=
3211                                 hweight32(si_get_cu_active_bitmap(rdev, i, j));
3212                 }
3213         }
3214
3215         /* set HW defaults for 3D engine */
3216         WREG32(CP_QUEUE_THRESHOLDS, (ROQ_IB1_START(0x16) |
3217                                      ROQ_IB2_START(0x2b)));
3218         WREG32(CP_MEQ_THRESHOLDS, MEQ1_START(0x30) | MEQ2_START(0x60));
3219
3220         sx_debug_1 = RREG32(SX_DEBUG_1);
3221         WREG32(SX_DEBUG_1, sx_debug_1);
3222
3223         WREG32(SPI_CONFIG_CNTL_1, VTX_DONE_DELAY(4));
3224
3225         WREG32(PA_SC_FIFO_SIZE, (SC_FRONTEND_PRIM_FIFO_SIZE(rdev->config.si.sc_prim_fifo_size_frontend) |
3226                                  SC_BACKEND_PRIM_FIFO_SIZE(rdev->config.si.sc_prim_fifo_size_backend) |
3227                                  SC_HIZ_TILE_FIFO_SIZE(rdev->config.si.sc_hiz_tile_fifo_size) |
3228                                  SC_EARLYZ_TILE_FIFO_SIZE(rdev->config.si.sc_earlyz_tile_fifo_size)));
3229
3230         WREG32(VGT_NUM_INSTANCES, 1);
3231
3232         WREG32(CP_PERFMON_CNTL, 0);
3233
3234         WREG32(SQ_CONFIG, 0);
3235
3236         WREG32(PA_SC_FORCE_EOV_MAX_CNTS, (FORCE_EOV_MAX_CLK_CNT(4095) |
3237                                           FORCE_EOV_MAX_REZ_CNT(255)));
3238
3239         WREG32(VGT_CACHE_INVALIDATION, CACHE_INVALIDATION(VC_AND_TC) |
3240                AUTO_INVLD_EN(ES_AND_GS_AUTO));
3241
3242         WREG32(VGT_GS_VERTEX_REUSE, 16);
3243         WREG32(PA_SC_LINE_STIPPLE_STATE, 0);
3244
3245         WREG32(CB_PERFCOUNTER0_SELECT0, 0);
3246         WREG32(CB_PERFCOUNTER0_SELECT1, 0);
3247         WREG32(CB_PERFCOUNTER1_SELECT0, 0);
3248         WREG32(CB_PERFCOUNTER1_SELECT1, 0);
3249         WREG32(CB_PERFCOUNTER2_SELECT0, 0);
3250         WREG32(CB_PERFCOUNTER2_SELECT1, 0);
3251         WREG32(CB_PERFCOUNTER3_SELECT0, 0);
3252         WREG32(CB_PERFCOUNTER3_SELECT1, 0);
3253
3254         tmp = RREG32(HDP_MISC_CNTL);
3255         tmp |= HDP_FLUSH_INVALIDATE_CACHE;
3256         WREG32(HDP_MISC_CNTL, tmp);
3257
3258         hdp_host_path_cntl = RREG32(HDP_HOST_PATH_CNTL);
3259         WREG32(HDP_HOST_PATH_CNTL, hdp_host_path_cntl);
3260
3261         WREG32(PA_CL_ENHANCE, CLIP_VTX_REORDER_ENA | NUM_CLIP_SEQ(3));
3262
3263         udelay(50);
3264 }
3265
3266 /*
3267  * GPU scratch registers helpers function.
3268  */
3269 static void si_scratch_init(struct radeon_device *rdev)
3270 {
3271         int i;
3272
3273         rdev->scratch.num_reg = 7;
3274         rdev->scratch.reg_base = SCRATCH_REG0;
3275         for (i = 0; i < rdev->scratch.num_reg; i++) {
3276                 rdev->scratch.free[i] = true;
3277                 rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
3278         }
3279 }
3280
3281 void si_fence_ring_emit(struct radeon_device *rdev,
3282                         struct radeon_fence *fence)
3283 {
3284         struct radeon_ring *ring = &rdev->ring[fence->ring];
3285         u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
3286
3287         /* flush read cache over gart */
3288         radeon_ring_write(ring, PACKET3(PACKET3_SET_CONFIG_REG, 1));
3289         radeon_ring_write(ring, (CP_COHER_CNTL2 - PACKET3_SET_CONFIG_REG_START) >> 2);
3290         radeon_ring_write(ring, 0);
3291         radeon_ring_write(ring, PACKET3(PACKET3_SURFACE_SYNC, 3));
3292         radeon_ring_write(ring, PACKET3_TCL1_ACTION_ENA |
3293                           PACKET3_TC_ACTION_ENA |
3294                           PACKET3_SH_KCACHE_ACTION_ENA |
3295                           PACKET3_SH_ICACHE_ACTION_ENA);
3296         radeon_ring_write(ring, 0xFFFFFFFF);
3297         radeon_ring_write(ring, 0);
3298         radeon_ring_write(ring, 10); /* poll interval */
3299         /* EVENT_WRITE_EOP - flush caches, send int */
3300         radeon_ring_write(ring, PACKET3(PACKET3_EVENT_WRITE_EOP, 4));
3301         radeon_ring_write(ring, EVENT_TYPE(CACHE_FLUSH_AND_INV_TS_EVENT) | EVENT_INDEX(5));
3302         radeon_ring_write(ring, lower_32_bits(addr));
3303         radeon_ring_write(ring, (upper_32_bits(addr) & 0xff) | DATA_SEL(1) | INT_SEL(2));
3304         radeon_ring_write(ring, fence->seq);
3305         radeon_ring_write(ring, 0);
3306 }
3307
3308 /*
3309  * IB stuff
3310  */
3311 void si_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
3312 {
3313         struct radeon_ring *ring = &rdev->ring[ib->ring];
3314         unsigned vm_id = ib->vm ? ib->vm->ids[ib->ring].id : 0;
3315         u32 header;
3316
3317         if (ib->is_const_ib) {
3318                 /* set switch buffer packet before const IB */
3319                 radeon_ring_write(ring, PACKET3(PACKET3_SWITCH_BUFFER, 0));
3320                 radeon_ring_write(ring, 0);
3321
3322                 header = PACKET3(PACKET3_INDIRECT_BUFFER_CONST, 2);
3323         } else {
3324                 u32 next_rptr;
3325                 if (ring->rptr_save_reg) {
3326                         next_rptr = ring->wptr + 3 + 4 + 8;
3327                         radeon_ring_write(ring, PACKET3(PACKET3_SET_CONFIG_REG, 1));
3328                         radeon_ring_write(ring, ((ring->rptr_save_reg -
3329                                                   PACKET3_SET_CONFIG_REG_START) >> 2));
3330                         radeon_ring_write(ring, next_rptr);
3331                 } else if (rdev->wb.enabled) {
3332                         next_rptr = ring->wptr + 5 + 4 + 8;
3333                         radeon_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
3334                         radeon_ring_write(ring, (1 << 8));
3335                         radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
3336                         radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr));
3337                         radeon_ring_write(ring, next_rptr);
3338                 }
3339
3340                 header = PACKET3(PACKET3_INDIRECT_BUFFER, 2);
3341         }
3342
3343         radeon_ring_write(ring, header);
3344         radeon_ring_write(ring,
3345 #ifdef __BIG_ENDIAN
3346                           (2 << 0) |
3347 #endif
3348                           (ib->gpu_addr & 0xFFFFFFFC));
3349         radeon_ring_write(ring, upper_32_bits(ib->gpu_addr) & 0xFFFF);
3350         radeon_ring_write(ring, ib->length_dw | (vm_id << 24));
3351
3352         if (!ib->is_const_ib) {
3353                 /* flush read cache over gart for this vmid */
3354                 radeon_ring_write(ring, PACKET3(PACKET3_SET_CONFIG_REG, 1));
3355                 radeon_ring_write(ring, (CP_COHER_CNTL2 - PACKET3_SET_CONFIG_REG_START) >> 2);
3356                 radeon_ring_write(ring, vm_id);
3357                 radeon_ring_write(ring, PACKET3(PACKET3_SURFACE_SYNC, 3));
3358                 radeon_ring_write(ring, PACKET3_TCL1_ACTION_ENA |
3359                                   PACKET3_TC_ACTION_ENA |
3360                                   PACKET3_SH_KCACHE_ACTION_ENA |
3361                                   PACKET3_SH_ICACHE_ACTION_ENA);
3362                 radeon_ring_write(ring, 0xFFFFFFFF);
3363                 radeon_ring_write(ring, 0);
3364                 radeon_ring_write(ring, 10); /* poll interval */
3365         }
3366 }
3367
3368 /*
3369  * CP.
3370  */
3371 static void si_cp_enable(struct radeon_device *rdev, bool enable)
3372 {
3373         if (enable)
3374                 WREG32(CP_ME_CNTL, 0);
3375         else {
3376                 if (rdev->asic->copy.copy_ring_index == RADEON_RING_TYPE_GFX_INDEX)
3377                         radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
3378                 WREG32(CP_ME_CNTL, (CP_ME_HALT | CP_PFP_HALT | CP_CE_HALT));
3379                 WREG32(SCRATCH_UMSK, 0);
3380                 rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
3381                 rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = false;
3382                 rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = false;
3383         }
3384         udelay(50);
3385 }
3386
3387 static int si_cp_load_microcode(struct radeon_device *rdev)
3388 {
3389         int i;
3390
3391         if (!rdev->me_fw || !rdev->pfp_fw || !rdev->ce_fw)
3392                 return -EINVAL;
3393
3394         si_cp_enable(rdev, false);
3395
3396         if (rdev->new_fw) {
3397                 const struct gfx_firmware_header_v1_0 *pfp_hdr =
3398                         (const struct gfx_firmware_header_v1_0 *)rdev->pfp_fw->data;
3399                 const struct gfx_firmware_header_v1_0 *ce_hdr =
3400                         (const struct gfx_firmware_header_v1_0 *)rdev->ce_fw->data;
3401                 const struct gfx_firmware_header_v1_0 *me_hdr =
3402                         (const struct gfx_firmware_header_v1_0 *)rdev->me_fw->data;
3403                 const __le32 *fw_data;
3404                 u32 fw_size;
3405
3406                 radeon_ucode_print_gfx_hdr(&pfp_hdr->header);
3407                 radeon_ucode_print_gfx_hdr(&ce_hdr->header);
3408                 radeon_ucode_print_gfx_hdr(&me_hdr->header);
3409
3410                 /* PFP */
3411                 fw_data = (const __le32 *)
3412                         (rdev->pfp_fw->data + le32_to_cpu(pfp_hdr->header.ucode_array_offset_bytes));
3413                 fw_size = le32_to_cpu(pfp_hdr->header.ucode_size_bytes) / 4;
3414                 WREG32(CP_PFP_UCODE_ADDR, 0);
3415                 for (i = 0; i < fw_size; i++)
3416                         WREG32(CP_PFP_UCODE_DATA, le32_to_cpup(fw_data++));
3417                 WREG32(CP_PFP_UCODE_ADDR, 0);
3418
3419                 /* CE */
3420                 fw_data = (const __le32 *)
3421                         (rdev->ce_fw->data + le32_to_cpu(ce_hdr->header.ucode_array_offset_bytes));
3422                 fw_size = le32_to_cpu(ce_hdr->header.ucode_size_bytes) / 4;
3423                 WREG32(CP_CE_UCODE_ADDR, 0);
3424                 for (i = 0; i < fw_size; i++)
3425                         WREG32(CP_CE_UCODE_DATA, le32_to_cpup(fw_data++));
3426                 WREG32(CP_CE_UCODE_ADDR, 0);
3427
3428                 /* ME */
3429                 fw_data = (const __be32 *)
3430                         (rdev->me_fw->data + le32_to_cpu(me_hdr->header.ucode_array_offset_bytes));
3431                 fw_size = le32_to_cpu(me_hdr->header.ucode_size_bytes) / 4;
3432                 WREG32(CP_ME_RAM_WADDR, 0);
3433                 for (i = 0; i < fw_size; i++)
3434                         WREG32(CP_ME_RAM_DATA, le32_to_cpup(fw_data++));
3435                 WREG32(CP_ME_RAM_WADDR, 0);
3436         } else {
3437                 const __be32 *fw_data;
3438
3439                 /* PFP */
3440                 fw_data = (const __be32 *)rdev->pfp_fw->data;
3441                 WREG32(CP_PFP_UCODE_ADDR, 0);
3442                 for (i = 0; i < SI_PFP_UCODE_SIZE; i++)
3443                         WREG32(CP_PFP_UCODE_DATA, be32_to_cpup(fw_data++));
3444                 WREG32(CP_PFP_UCODE_ADDR, 0);
3445
3446                 /* CE */
3447                 fw_data = (const __be32 *)rdev->ce_fw->data;
3448                 WREG32(CP_CE_UCODE_ADDR, 0);
3449                 for (i = 0; i < SI_CE_UCODE_SIZE; i++)
3450                         WREG32(CP_CE_UCODE_DATA, be32_to_cpup(fw_data++));
3451                 WREG32(CP_CE_UCODE_ADDR, 0);
3452
3453                 /* ME */
3454                 fw_data = (const __be32 *)rdev->me_fw->data;
3455                 WREG32(CP_ME_RAM_WADDR, 0);
3456                 for (i = 0; i < SI_PM4_UCODE_SIZE; i++)
3457                         WREG32(CP_ME_RAM_DATA, be32_to_cpup(fw_data++));
3458                 WREG32(CP_ME_RAM_WADDR, 0);
3459         }
3460
3461         WREG32(CP_PFP_UCODE_ADDR, 0);
3462         WREG32(CP_CE_UCODE_ADDR, 0);
3463         WREG32(CP_ME_RAM_WADDR, 0);
3464         WREG32(CP_ME_RAM_RADDR, 0);
3465         return 0;
3466 }
3467
3468 static int si_cp_start(struct radeon_device *rdev)
3469 {
3470         struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
3471         int r, i;
3472
3473         r = radeon_ring_lock(rdev, ring, 7 + 4);
3474         if (r) {
3475                 DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
3476                 return r;
3477         }
3478         /* init the CP */
3479         radeon_ring_write(ring, PACKET3(PACKET3_ME_INITIALIZE, 5));
3480         radeon_ring_write(ring, 0x1);
3481         radeon_ring_write(ring, 0x0);
3482         radeon_ring_write(ring, rdev->config.si.max_hw_contexts - 1);
3483         radeon_ring_write(ring, PACKET3_ME_INITIALIZE_DEVICE_ID(1));
3484         radeon_ring_write(ring, 0);
3485         radeon_ring_write(ring, 0);
3486
3487         /* init the CE partitions */
3488         radeon_ring_write(ring, PACKET3(PACKET3_SET_BASE, 2));
3489         radeon_ring_write(ring, PACKET3_BASE_INDEX(CE_PARTITION_BASE));
3490         radeon_ring_write(ring, 0xc000);
3491         radeon_ring_write(ring, 0xe000);
3492         radeon_ring_unlock_commit(rdev, ring, false);
3493
3494         si_cp_enable(rdev, true);
3495
3496         r = radeon_ring_lock(rdev, ring, si_default_size + 10);
3497         if (r) {
3498                 DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
3499                 return r;
3500         }
3501
3502         /* setup clear context state */
3503         radeon_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
3504         radeon_ring_write(ring, PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
3505
3506         for (i = 0; i < si_default_size; i++)
3507                 radeon_ring_write(ring, si_default_state[i]);
3508
3509         radeon_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
3510         radeon_ring_write(ring, PACKET3_PREAMBLE_END_CLEAR_STATE);
3511
3512         /* set clear context state */
3513         radeon_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
3514         radeon_ring_write(ring, 0);
3515
3516         radeon_ring_write(ring, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
3517         radeon_ring_write(ring, 0x00000316);
3518         radeon_ring_write(ring, 0x0000000e); /* VGT_VERTEX_REUSE_BLOCK_CNTL */
3519         radeon_ring_write(ring, 0x00000010); /* VGT_OUT_DEALLOC_CNTL */
3520
3521         radeon_ring_unlock_commit(rdev, ring, false);
3522
3523         for (i = RADEON_RING_TYPE_GFX_INDEX; i <= CAYMAN_RING_TYPE_CP2_INDEX; ++i) {
3524                 ring = &rdev->ring[i];
3525                 r = radeon_ring_lock(rdev, ring, 2);
3526
3527                 /* clear the compute context state */
3528                 radeon_ring_write(ring, PACKET3_COMPUTE(PACKET3_CLEAR_STATE, 0));
3529                 radeon_ring_write(ring, 0);
3530
3531                 radeon_ring_unlock_commit(rdev, ring, false);
3532         }
3533
3534         return 0;
3535 }
3536
3537 static void si_cp_fini(struct radeon_device *rdev)
3538 {
3539         struct radeon_ring *ring;
3540         si_cp_enable(rdev, false);
3541
3542         ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
3543         radeon_ring_fini(rdev, ring);
3544         radeon_scratch_free(rdev, ring->rptr_save_reg);
3545
3546         ring = &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX];
3547         radeon_ring_fini(rdev, ring);
3548         radeon_scratch_free(rdev, ring->rptr_save_reg);
3549
3550         ring = &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX];
3551         radeon_ring_fini(rdev, ring);
3552         radeon_scratch_free(rdev, ring->rptr_save_reg);
3553 }
3554
3555 static int si_cp_resume(struct radeon_device *rdev)
3556 {
3557         struct radeon_ring *ring;
3558         u32 tmp;
3559         u32 rb_bufsz;
3560         int r;
3561
3562         si_enable_gui_idle_interrupt(rdev, false);
3563
3564         WREG32(CP_SEM_WAIT_TIMER, 0x0);
3565         WREG32(CP_SEM_INCOMPLETE_TIMER_CNTL, 0x0);
3566
3567         /* Set the write pointer delay */
3568         WREG32(CP_RB_WPTR_DELAY, 0);
3569
3570         WREG32(CP_DEBUG, 0);
3571         WREG32(SCRATCH_ADDR, ((rdev->wb.gpu_addr + RADEON_WB_SCRATCH_OFFSET) >> 8) & 0xFFFFFFFF);
3572
3573         /* ring 0 - compute and gfx */
3574         /* Set ring buffer size */
3575         ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
3576         rb_bufsz = order_base_2(ring->ring_size / 8);
3577         tmp = (order_base_2(RADEON_GPU_PAGE_SIZE/8) << 8) | rb_bufsz;
3578 #ifdef __BIG_ENDIAN
3579         tmp |= BUF_SWAP_32BIT;
3580 #endif
3581         WREG32(CP_RB0_CNTL, tmp);
3582
3583         /* Initialize the ring buffer's read and write pointers */
3584         WREG32(CP_RB0_CNTL, tmp | RB_RPTR_WR_ENA);
3585         ring->wptr = 0;
3586         WREG32(CP_RB0_WPTR, ring->wptr);
3587
3588         /* set the wb address whether it's enabled or not */
3589         WREG32(CP_RB0_RPTR_ADDR, (rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) & 0xFFFFFFFC);
3590         WREG32(CP_RB0_RPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) & 0xFF);
3591
3592         if (rdev->wb.enabled)
3593                 WREG32(SCRATCH_UMSK, 0xff);
3594         else {
3595                 tmp |= RB_NO_UPDATE;
3596                 WREG32(SCRATCH_UMSK, 0);
3597         }
3598
3599         mdelay(1);
3600         WREG32(CP_RB0_CNTL, tmp);
3601
3602         WREG32(CP_RB0_BASE, ring->gpu_addr >> 8);
3603
3604         /* ring1  - compute only */
3605         /* Set ring buffer size */
3606         ring = &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX];
3607         rb_bufsz = order_base_2(ring->ring_size / 8);
3608         tmp = (order_base_2(RADEON_GPU_PAGE_SIZE/8) << 8) | rb_bufsz;
3609 #ifdef __BIG_ENDIAN
3610         tmp |= BUF_SWAP_32BIT;
3611 #endif
3612         WREG32(CP_RB1_CNTL, tmp);
3613
3614         /* Initialize the ring buffer's read and write pointers */
3615         WREG32(CP_RB1_CNTL, tmp | RB_RPTR_WR_ENA);
3616         ring->wptr = 0;
3617         WREG32(CP_RB1_WPTR, ring->wptr);
3618
3619         /* set the wb address whether it's enabled or not */
3620         WREG32(CP_RB1_RPTR_ADDR, (rdev->wb.gpu_addr + RADEON_WB_CP1_RPTR_OFFSET) & 0xFFFFFFFC);
3621         WREG32(CP_RB1_RPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + RADEON_WB_CP1_RPTR_OFFSET) & 0xFF);
3622
3623         mdelay(1);
3624         WREG32(CP_RB1_CNTL, tmp);
3625
3626         WREG32(CP_RB1_BASE, ring->gpu_addr >> 8);
3627
3628         /* ring2 - compute only */
3629         /* Set ring buffer size */
3630         ring = &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX];
3631         rb_bufsz = order_base_2(ring->ring_size / 8);
3632         tmp = (order_base_2(RADEON_GPU_PAGE_SIZE/8) << 8) | rb_bufsz;
3633 #ifdef __BIG_ENDIAN
3634         tmp |= BUF_SWAP_32BIT;
3635 #endif
3636         WREG32(CP_RB2_CNTL, tmp);
3637
3638         /* Initialize the ring buffer's read and write pointers */
3639         WREG32(CP_RB2_CNTL, tmp | RB_RPTR_WR_ENA);
3640         ring->wptr = 0;
3641         WREG32(CP_RB2_WPTR, ring->wptr);
3642
3643         /* set the wb address whether it's enabled or not */
3644         WREG32(CP_RB2_RPTR_ADDR, (rdev->wb.gpu_addr + RADEON_WB_CP2_RPTR_OFFSET) & 0xFFFFFFFC);
3645         WREG32(CP_RB2_RPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + RADEON_WB_CP2_RPTR_OFFSET) & 0xFF);
3646
3647         mdelay(1);
3648         WREG32(CP_RB2_CNTL, tmp);
3649
3650         WREG32(CP_RB2_BASE, ring->gpu_addr >> 8);
3651
3652         /* start the rings */
3653         si_cp_start(rdev);
3654         rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = true;
3655         rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = true;
3656         rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = true;
3657         r = radeon_ring_test(rdev, RADEON_RING_TYPE_GFX_INDEX, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
3658         if (r) {
3659                 rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
3660                 rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = false;
3661                 rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = false;
3662                 return r;
3663         }
3664         r = radeon_ring_test(rdev, CAYMAN_RING_TYPE_CP1_INDEX, &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX]);
3665         if (r) {
3666                 rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = false;
3667         }
3668         r = radeon_ring_test(rdev, CAYMAN_RING_TYPE_CP2_INDEX, &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX]);
3669         if (r) {
3670                 rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = false;
3671         }
3672
3673         si_enable_gui_idle_interrupt(rdev, true);
3674
3675         if (rdev->asic->copy.copy_ring_index == RADEON_RING_TYPE_GFX_INDEX)
3676                 radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
3677
3678         return 0;
3679 }
3680
3681 u32 si_gpu_check_soft_reset(struct radeon_device *rdev)
3682 {
3683         u32 reset_mask = 0;
3684         u32 tmp;
3685
3686         /* GRBM_STATUS */
3687         tmp = RREG32(GRBM_STATUS);
3688         if (tmp & (PA_BUSY | SC_BUSY |
3689                    BCI_BUSY | SX_BUSY |
3690                    TA_BUSY | VGT_BUSY |
3691                    DB_BUSY | CB_BUSY |
3692                    GDS_BUSY | SPI_BUSY |
3693                    IA_BUSY | IA_BUSY_NO_DMA))
3694                 reset_mask |= RADEON_RESET_GFX;
3695
3696         if (tmp & (CF_RQ_PENDING | PF_RQ_PENDING |
3697                    CP_BUSY | CP_COHERENCY_BUSY))
3698                 reset_mask |= RADEON_RESET_CP;
3699
3700         if (tmp & GRBM_EE_BUSY)
3701                 reset_mask |= RADEON_RESET_GRBM | RADEON_RESET_GFX | RADEON_RESET_CP;
3702
3703         /* GRBM_STATUS2 */
3704         tmp = RREG32(GRBM_STATUS2);
3705         if (tmp & (RLC_RQ_PENDING | RLC_BUSY))
3706                 reset_mask |= RADEON_RESET_RLC;
3707
3708         /* DMA_STATUS_REG 0 */
3709         tmp = RREG32(DMA_STATUS_REG + DMA0_REGISTER_OFFSET);
3710         if (!(tmp & DMA_IDLE))
3711                 reset_mask |= RADEON_RESET_DMA;
3712
3713         /* DMA_STATUS_REG 1 */
3714         tmp = RREG32(DMA_STATUS_REG + DMA1_REGISTER_OFFSET);
3715         if (!(tmp & DMA_IDLE))
3716                 reset_mask |= RADEON_RESET_DMA1;
3717
3718         /* SRBM_STATUS2 */
3719         tmp = RREG32(SRBM_STATUS2);
3720         if (tmp & DMA_BUSY)
3721                 reset_mask |= RADEON_RESET_DMA;
3722
3723         if (tmp & DMA1_BUSY)
3724                 reset_mask |= RADEON_RESET_DMA1;
3725
3726         /* SRBM_STATUS */
3727         tmp = RREG32(SRBM_STATUS);
3728
3729         if (tmp & IH_BUSY)
3730                 reset_mask |= RADEON_RESET_IH;
3731
3732         if (tmp & SEM_BUSY)
3733                 reset_mask |= RADEON_RESET_SEM;
3734
3735         if (tmp & GRBM_RQ_PENDING)
3736                 reset_mask |= RADEON_RESET_GRBM;
3737
3738         if (tmp & VMC_BUSY)
3739                 reset_mask |= RADEON_RESET_VMC;
3740
3741         if (tmp & (MCB_BUSY | MCB_NON_DISPLAY_BUSY |
3742                    MCC_BUSY | MCD_BUSY))
3743                 reset_mask |= RADEON_RESET_MC;
3744
3745         if (evergreen_is_display_hung(rdev))
3746                 reset_mask |= RADEON_RESET_DISPLAY;
3747
3748         /* VM_L2_STATUS */
3749         tmp = RREG32(VM_L2_STATUS);
3750         if (tmp & L2_BUSY)
3751                 reset_mask |= RADEON_RESET_VMC;
3752
3753         /* Skip MC reset as it's mostly likely not hung, just busy */
3754         if (reset_mask & RADEON_RESET_MC) {
3755                 DRM_DEBUG("MC busy: 0x%08X, clearing.\n", reset_mask);
3756                 reset_mask &= ~RADEON_RESET_MC;
3757         }
3758
3759         return reset_mask;
3760 }
3761
3762 static void si_gpu_soft_reset(struct radeon_device *rdev, u32 reset_mask)
3763 {
3764         struct evergreen_mc_save save;
3765         u32 grbm_soft_reset = 0, srbm_soft_reset = 0;
3766         u32 tmp;
3767
3768         if (reset_mask == 0)
3769                 return;
3770
3771         dev_info(rdev->dev, "GPU softreset: 0x%08X\n", reset_mask);
3772
3773         evergreen_print_gpu_status_regs(rdev);
3774         dev_info(rdev->dev, "  VM_CONTEXT1_PROTECTION_FAULT_ADDR   0x%08X\n",
3775                  RREG32(VM_CONTEXT1_PROTECTION_FAULT_ADDR));
3776         dev_info(rdev->dev, "  VM_CONTEXT1_PROTECTION_FAULT_STATUS 0x%08X\n",
3777                  RREG32(VM_CONTEXT1_PROTECTION_FAULT_STATUS));
3778
3779         /* disable PG/CG */
3780         si_fini_pg(rdev);
3781         si_fini_cg(rdev);
3782
3783         /* stop the rlc */
3784         si_rlc_stop(rdev);
3785
3786         /* Disable CP parsing/prefetching */
3787         WREG32(CP_ME_CNTL, CP_ME_HALT | CP_PFP_HALT | CP_CE_HALT);
3788
3789         if (reset_mask & RADEON_RESET_DMA) {
3790                 /* dma0 */
3791                 tmp = RREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET);
3792                 tmp &= ~DMA_RB_ENABLE;
3793                 WREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET, tmp);
3794         }
3795         if (reset_mask & RADEON_RESET_DMA1) {
3796                 /* dma1 */
3797                 tmp = RREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET);
3798                 tmp &= ~DMA_RB_ENABLE;
3799                 WREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET, tmp);
3800         }
3801
3802         udelay(50);
3803
3804         evergreen_mc_stop(rdev, &save);
3805         if (evergreen_mc_wait_for_idle(rdev)) {
3806                 dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
3807         }
3808
3809         if (reset_mask & (RADEON_RESET_GFX | RADEON_RESET_COMPUTE | RADEON_RESET_CP)) {
3810                 grbm_soft_reset = SOFT_RESET_CB |
3811                         SOFT_RESET_DB |
3812                         SOFT_RESET_GDS |
3813                         SOFT_RESET_PA |
3814                         SOFT_RESET_SC |
3815                         SOFT_RESET_BCI |
3816                         SOFT_RESET_SPI |
3817                         SOFT_RESET_SX |
3818                         SOFT_RESET_TC |
3819                         SOFT_RESET_TA |
3820                         SOFT_RESET_VGT |
3821                         SOFT_RESET_IA;
3822         }
3823
3824         if (reset_mask & RADEON_RESET_CP) {
3825                 grbm_soft_reset |= SOFT_RESET_CP | SOFT_RESET_VGT;
3826
3827                 srbm_soft_reset |= SOFT_RESET_GRBM;
3828         }
3829
3830         if (reset_mask & RADEON_RESET_DMA)
3831                 srbm_soft_reset |= SOFT_RESET_DMA;
3832
3833         if (reset_mask & RADEON_RESET_DMA1)
3834                 srbm_soft_reset |= SOFT_RESET_DMA1;
3835
3836         if (reset_mask & RADEON_RESET_DISPLAY)
3837                 srbm_soft_reset |= SOFT_RESET_DC;
3838
3839         if (reset_mask & RADEON_RESET_RLC)
3840                 grbm_soft_reset |= SOFT_RESET_RLC;
3841
3842         if (reset_mask & RADEON_RESET_SEM)
3843                 srbm_soft_reset |= SOFT_RESET_SEM;
3844
3845         if (reset_mask & RADEON_RESET_IH)
3846                 srbm_soft_reset |= SOFT_RESET_IH;
3847
3848         if (reset_mask & RADEON_RESET_GRBM)
3849                 srbm_soft_reset |= SOFT_RESET_GRBM;
3850
3851         if (reset_mask & RADEON_RESET_VMC)
3852                 srbm_soft_reset |= SOFT_RESET_VMC;
3853
3854         if (reset_mask & RADEON_RESET_MC)
3855                 srbm_soft_reset |= SOFT_RESET_MC;
3856
3857         if (grbm_soft_reset) {
3858                 tmp = RREG32(GRBM_SOFT_RESET);
3859                 tmp |= grbm_soft_reset;
3860                 dev_info(rdev->dev, "GRBM_SOFT_RESET=0x%08X\n", tmp);
3861                 WREG32(GRBM_SOFT_RESET, tmp);
3862                 tmp = RREG32(GRBM_SOFT_RESET);
3863
3864                 udelay(50);
3865
3866                 tmp &= ~grbm_soft_reset;
3867                 WREG32(GRBM_SOFT_RESET, tmp);
3868                 tmp = RREG32(GRBM_SOFT_RESET);
3869         }
3870
3871         if (srbm_soft_reset) {
3872                 tmp = RREG32(SRBM_SOFT_RESET);
3873                 tmp |= srbm_soft_reset;
3874                 dev_info(rdev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
3875                 WREG32(SRBM_SOFT_RESET, tmp);
3876                 tmp = RREG32(SRBM_SOFT_RESET);
3877
3878                 udelay(50);
3879
3880                 tmp &= ~srbm_soft_reset;
3881                 WREG32(SRBM_SOFT_RESET, tmp);
3882                 tmp = RREG32(SRBM_SOFT_RESET);
3883         }
3884
3885         /* Wait a little for things to settle down */
3886         udelay(50);
3887
3888         evergreen_mc_resume(rdev, &save);
3889         udelay(50);
3890
3891         evergreen_print_gpu_status_regs(rdev);
3892 }
3893
3894 static void si_set_clk_bypass_mode(struct radeon_device *rdev)
3895 {
3896         u32 tmp, i;
3897
3898         tmp = RREG32(CG_SPLL_FUNC_CNTL);
3899         tmp |= SPLL_BYPASS_EN;
3900         WREG32(CG_SPLL_FUNC_CNTL, tmp);
3901
3902         tmp = RREG32(CG_SPLL_FUNC_CNTL_2);
3903         tmp |= SPLL_CTLREQ_CHG;
3904         WREG32(CG_SPLL_FUNC_CNTL_2, tmp);
3905
3906         for (i = 0; i < rdev->usec_timeout; i++) {
3907                 if (RREG32(SPLL_STATUS) & SPLL_CHG_STATUS)
3908                         break;
3909                 udelay(1);
3910         }
3911
3912         tmp = RREG32(CG_SPLL_FUNC_CNTL_2);
3913         tmp &= ~(SPLL_CTLREQ_CHG | SCLK_MUX_UPDATE);
3914         WREG32(CG_SPLL_FUNC_CNTL_2, tmp);
3915
3916         tmp = RREG32(MPLL_CNTL_MODE);
3917         tmp &= ~MPLL_MCLK_SEL;
3918         WREG32(MPLL_CNTL_MODE, tmp);
3919 }
3920
3921 static void si_spll_powerdown(struct radeon_device *rdev)
3922 {
3923         u32 tmp;
3924
3925         tmp = RREG32(SPLL_CNTL_MODE);
3926         tmp |= SPLL_SW_DIR_CONTROL;
3927         WREG32(SPLL_CNTL_MODE, tmp);
3928
3929         tmp = RREG32(CG_SPLL_FUNC_CNTL);
3930         tmp |= SPLL_RESET;
3931         WREG32(CG_SPLL_FUNC_CNTL, tmp);
3932
3933         tmp = RREG32(CG_SPLL_FUNC_CNTL);
3934         tmp |= SPLL_SLEEP;
3935         WREG32(CG_SPLL_FUNC_CNTL, tmp);
3936
3937         tmp = RREG32(SPLL_CNTL_MODE);
3938         tmp &= ~SPLL_SW_DIR_CONTROL;
3939         WREG32(SPLL_CNTL_MODE, tmp);
3940 }
3941
3942 static void si_gpu_pci_config_reset(struct radeon_device *rdev)
3943 {
3944         struct evergreen_mc_save save;
3945         u32 tmp, i;
3946
3947         dev_info(rdev->dev, "GPU pci config reset\n");
3948
3949         /* disable dpm? */
3950
3951         /* disable cg/pg */
3952         si_fini_pg(rdev);
3953         si_fini_cg(rdev);
3954
3955         /* Disable CP parsing/prefetching */
3956         WREG32(CP_ME_CNTL, CP_ME_HALT | CP_PFP_HALT | CP_CE_HALT);
3957         /* dma0 */
3958         tmp = RREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET);
3959         tmp &= ~DMA_RB_ENABLE;
3960         WREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET, tmp);
3961         /* dma1 */
3962         tmp = RREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET);
3963         tmp &= ~DMA_RB_ENABLE;
3964         WREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET, tmp);
3965         /* XXX other engines? */
3966
3967         /* halt the rlc, disable cp internal ints */
3968         si_rlc_stop(rdev);
3969
3970         udelay(50);
3971
3972         /* disable mem access */
3973         evergreen_mc_stop(rdev, &save);
3974         if (evergreen_mc_wait_for_idle(rdev)) {
3975                 dev_warn(rdev->dev, "Wait for MC idle timed out !\n");
3976         }
3977
3978         /* set mclk/sclk to bypass */
3979         si_set_clk_bypass_mode(rdev);
3980         /* powerdown spll */
3981         si_spll_powerdown(rdev);
3982         /* disable BM */
3983         pci_clear_master(rdev->pdev);
3984         /* reset */
3985         radeon_pci_config_reset(rdev);
3986         /* wait for asic to come out of reset */
3987         for (i = 0; i < rdev->usec_timeout; i++) {
3988                 if (RREG32(CONFIG_MEMSIZE) != 0xffffffff)
3989                         break;
3990                 udelay(1);
3991         }
3992 }
3993
3994 int si_asic_reset(struct radeon_device *rdev, bool hard)
3995 {
3996         u32 reset_mask;
3997
3998         if (hard) {
3999                 si_gpu_pci_config_reset(rdev);
4000                 return 0;
4001         }
4002
4003         reset_mask = si_gpu_check_soft_reset(rdev);
4004
4005         if (reset_mask)
4006                 r600_set_bios_scratch_engine_hung(rdev, true);
4007
4008         /* try soft reset */
4009         si_gpu_soft_reset(rdev, reset_mask);
4010
4011         reset_mask = si_gpu_check_soft_reset(rdev);
4012
4013         /* try pci config reset */
4014         if (reset_mask && radeon_hard_reset)
4015                 si_gpu_pci_config_reset(rdev);
4016
4017         reset_mask = si_gpu_check_soft_reset(rdev);
4018
4019         if (!reset_mask)
4020                 r600_set_bios_scratch_engine_hung(rdev, false);
4021
4022         return 0;
4023 }
4024
4025 /**
4026  * si_gfx_is_lockup - Check if the GFX engine is locked up
4027  *
4028  * @rdev: radeon_device pointer
4029  * @ring: radeon_ring structure holding ring information
4030  *
4031  * Check if the GFX engine is locked up.
4032  * Returns true if the engine appears to be locked up, false if not.
4033  */
4034 bool si_gfx_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
4035 {
4036         u32 reset_mask = si_gpu_check_soft_reset(rdev);
4037
4038         if (!(reset_mask & (RADEON_RESET_GFX |
4039                             RADEON_RESET_COMPUTE |
4040                             RADEON_RESET_CP))) {
4041                 radeon_ring_lockup_update(rdev, ring);
4042                 return false;
4043         }
4044         return radeon_ring_test_lockup(rdev, ring);
4045 }
4046
4047 /* MC */
4048 static void si_mc_program(struct radeon_device *rdev)
4049 {
4050         struct evergreen_mc_save save;
4051         u32 tmp;
4052         int i, j;
4053
4054         /* Initialize HDP */
4055         for (i = 0, j = 0; i < 32; i++, j += 0x18) {
4056                 WREG32((0x2c14 + j), 0x00000000);
4057                 WREG32((0x2c18 + j), 0x00000000);
4058                 WREG32((0x2c1c + j), 0x00000000);
4059                 WREG32((0x2c20 + j), 0x00000000);
4060                 WREG32((0x2c24 + j), 0x00000000);
4061         }
4062         WREG32(HDP_REG_COHERENCY_FLUSH_CNTL, 0);
4063
4064         evergreen_mc_stop(rdev, &save);
4065         if (radeon_mc_wait_for_idle(rdev)) {
4066                 dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
4067         }
4068         if (!ASIC_IS_NODCE(rdev))
4069                 /* Lockout access through VGA aperture*/
4070                 WREG32(VGA_HDP_CONTROL, VGA_MEMORY_DISABLE);
4071         /* Update configuration */
4072         WREG32(MC_VM_SYSTEM_APERTURE_LOW_ADDR,
4073                rdev->mc.vram_start >> 12);
4074         WREG32(MC_VM_SYSTEM_APERTURE_HIGH_ADDR,
4075                rdev->mc.vram_end >> 12);
4076         WREG32(MC_VM_SYSTEM_APERTURE_DEFAULT_ADDR,
4077                rdev->vram_scratch.gpu_addr >> 12);
4078         tmp = ((rdev->mc.vram_end >> 24) & 0xFFFF) << 16;
4079         tmp |= ((rdev->mc.vram_start >> 24) & 0xFFFF);
4080         WREG32(MC_VM_FB_LOCATION, tmp);
4081         /* XXX double check these! */
4082         WREG32(HDP_NONSURFACE_BASE, (rdev->mc.vram_start >> 8));
4083         WREG32(HDP_NONSURFACE_INFO, (2 << 7) | (1 << 30));
4084         WREG32(HDP_NONSURFACE_SIZE, 0x3FFFFFFF);
4085         WREG32(MC_VM_AGP_BASE, 0);
4086         WREG32(MC_VM_AGP_TOP, 0x0FFFFFFF);
4087         WREG32(MC_VM_AGP_BOT, 0x0FFFFFFF);
4088         if (radeon_mc_wait_for_idle(rdev)) {
4089                 dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
4090         }
4091         evergreen_mc_resume(rdev, &save);
4092         if (!ASIC_IS_NODCE(rdev)) {
4093                 /* we need to own VRAM, so turn off the VGA renderer here
4094                  * to stop it overwriting our objects */
4095                 rv515_vga_render_disable(rdev);
4096         }
4097 }
4098
4099 void si_vram_gtt_location(struct radeon_device *rdev,
4100                           struct radeon_mc *mc)
4101 {
4102         if (mc->mc_vram_size > 0xFFC0000000ULL) {
4103                 /* leave room for at least 1024M GTT */
4104                 dev_warn(rdev->dev, "limiting VRAM\n");
4105                 mc->real_vram_size = 0xFFC0000000ULL;
4106                 mc->mc_vram_size = 0xFFC0000000ULL;
4107         }
4108         radeon_vram_location(rdev, &rdev->mc, 0);
4109         rdev->mc.gtt_base_align = 0;
4110         radeon_gtt_location(rdev, mc);
4111 }
4112
4113 static int si_mc_init(struct radeon_device *rdev)
4114 {
4115         u32 tmp;
4116         int chansize, numchan;
4117
4118         /* Get VRAM informations */
4119         rdev->mc.vram_is_ddr = true;
4120         tmp = RREG32(MC_ARB_RAMCFG);
4121         if (tmp & CHANSIZE_OVERRIDE) {
4122                 chansize = 16;
4123         } else if (tmp & CHANSIZE_MASK) {
4124                 chansize = 64;
4125         } else {
4126                 chansize = 32;
4127         }
4128         tmp = RREG32(MC_SHARED_CHMAP);
4129         switch ((tmp & NOOFCHAN_MASK) >> NOOFCHAN_SHIFT) {
4130         case 0:
4131         default:
4132                 numchan = 1;
4133                 break;
4134         case 1:
4135                 numchan = 2;
4136                 break;
4137         case 2:
4138                 numchan = 4;
4139                 break;
4140         case 3:
4141                 numchan = 8;
4142                 break;
4143         case 4:
4144                 numchan = 3;
4145                 break;
4146         case 5:
4147                 numchan = 6;
4148                 break;
4149         case 6:
4150                 numchan = 10;
4151                 break;
4152         case 7:
4153                 numchan = 12;
4154                 break;
4155         case 8:
4156                 numchan = 16;
4157                 break;
4158         }
4159         rdev->mc.vram_width = numchan * chansize;
4160         /* Could aper size report 0 ? */
4161         rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
4162         rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
4163         /* size in MB on si */
4164         tmp = RREG32(CONFIG_MEMSIZE);
4165         /* some boards may have garbage in the upper 16 bits */
4166         if (tmp & 0xffff0000) {
4167                 DRM_INFO("Probable bad vram size: 0x%08x\n", tmp);
4168                 if (tmp & 0xffff)
4169                         tmp &= 0xffff;
4170         }
4171         rdev->mc.mc_vram_size = tmp * 1024ULL * 1024ULL;
4172         rdev->mc.real_vram_size = rdev->mc.mc_vram_size;
4173         rdev->mc.visible_vram_size = rdev->mc.aper_size;
4174         si_vram_gtt_location(rdev, &rdev->mc);
4175         radeon_update_bandwidth_info(rdev);
4176
4177         return 0;
4178 }
4179
4180 /*
4181  * GART
4182  */
4183 void si_pcie_gart_tlb_flush(struct radeon_device *rdev)
4184 {
4185         /* flush hdp cache */
4186         WREG32(HDP_MEM_COHERENCY_FLUSH_CNTL, 0x1);
4187
4188         /* bits 0-15 are the VM contexts0-15 */
4189         WREG32(VM_INVALIDATE_REQUEST, 1);
4190 }
4191
4192 static int si_pcie_gart_enable(struct radeon_device *rdev)
4193 {
4194         int r, i;
4195
4196         if (rdev->gart.robj == NULL) {
4197                 dev_err(rdev->dev, "No VRAM object for PCIE GART.\n");
4198                 return -EINVAL;
4199         }
4200         r = radeon_gart_table_vram_pin(rdev);
4201         if (r)
4202                 return r;
4203         /* Setup TLB control */
4204         WREG32(MC_VM_MX_L1_TLB_CNTL,
4205                (0xA << 7) |
4206                ENABLE_L1_TLB |
4207                ENABLE_L1_FRAGMENT_PROCESSING |
4208                SYSTEM_ACCESS_MODE_NOT_IN_SYS |
4209                ENABLE_ADVANCED_DRIVER_MODEL |
4210                SYSTEM_APERTURE_UNMAPPED_ACCESS_PASS_THRU);
4211         /* Setup L2 cache */
4212         WREG32(VM_L2_CNTL, ENABLE_L2_CACHE |
4213                ENABLE_L2_FRAGMENT_PROCESSING |
4214                ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE |
4215                ENABLE_L2_PDE0_CACHE_LRU_UPDATE_BY_WRITE |
4216                EFFECTIVE_L2_QUEUE_SIZE(7) |
4217                CONTEXT1_IDENTITY_ACCESS_MODE(1));
4218         WREG32(VM_L2_CNTL2, INVALIDATE_ALL_L1_TLBS | INVALIDATE_L2_CACHE);
4219         WREG32(VM_L2_CNTL3, L2_CACHE_BIGK_ASSOCIATIVITY |
4220                BANK_SELECT(4) |
4221                L2_CACHE_BIGK_FRAGMENT_SIZE(4));
4222         /* setup context0 */
4223         WREG32(VM_CONTEXT0_PAGE_TABLE_START_ADDR, rdev->mc.gtt_start >> 12);
4224         WREG32(VM_CONTEXT0_PAGE_TABLE_END_ADDR, rdev->mc.gtt_end >> 12);
4225         WREG32(VM_CONTEXT0_PAGE_TABLE_BASE_ADDR, rdev->gart.table_addr >> 12);
4226         WREG32(VM_CONTEXT0_PROTECTION_FAULT_DEFAULT_ADDR,
4227                         (u32)(rdev->dummy_page.addr >> 12));
4228         WREG32(VM_CONTEXT0_CNTL2, 0);
4229         WREG32(VM_CONTEXT0_CNTL, (ENABLE_CONTEXT | PAGE_TABLE_DEPTH(0) |
4230                                   RANGE_PROTECTION_FAULT_ENABLE_DEFAULT));
4231
4232         WREG32(0x15D4, 0);
4233         WREG32(0x15D8, 0);
4234         WREG32(0x15DC, 0);
4235
4236         /* empty context1-15 */
4237         /* set vm size, must be a multiple of 4 */
4238         WREG32(VM_CONTEXT1_PAGE_TABLE_START_ADDR, 0);
4239         WREG32(VM_CONTEXT1_PAGE_TABLE_END_ADDR, rdev->vm_manager.max_pfn - 1);
4240         /* Assign the pt base to something valid for now; the pts used for
4241          * the VMs are determined by the application and setup and assigned
4242          * on the fly in the vm part of radeon_gart.c
4243          */
4244         for (i = 1; i < 16; i++) {
4245                 if (i < 8)
4246                         WREG32(VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (i << 2),
4247                                rdev->vm_manager.saved_table_addr[i]);
4248                 else
4249                         WREG32(VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((i - 8) << 2),
4250                                rdev->vm_manager.saved_table_addr[i]);
4251         }
4252
4253         /* enable context1-15 */
4254         WREG32(VM_CONTEXT1_PROTECTION_FAULT_DEFAULT_ADDR,
4255                (u32)(rdev->dummy_page.addr >> 12));
4256         WREG32(VM_CONTEXT1_CNTL2, 4);
4257         WREG32(VM_CONTEXT1_CNTL, ENABLE_CONTEXT | PAGE_TABLE_DEPTH(1) |
4258                                 PAGE_TABLE_BLOCK_SIZE(radeon_vm_block_size - 9) |
4259                                 RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT |
4260                                 RANGE_PROTECTION_FAULT_ENABLE_DEFAULT |
4261                                 DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT |
4262                                 DUMMY_PAGE_PROTECTION_FAULT_ENABLE_DEFAULT |
4263                                 PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT |
4264                                 PDE0_PROTECTION_FAULT_ENABLE_DEFAULT |
4265                                 VALID_PROTECTION_FAULT_ENABLE_INTERRUPT |
4266                                 VALID_PROTECTION_FAULT_ENABLE_DEFAULT |
4267                                 READ_PROTECTION_FAULT_ENABLE_INTERRUPT |
4268                                 READ_PROTECTION_FAULT_ENABLE_DEFAULT |
4269                                 WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT |
4270                                 WRITE_PROTECTION_FAULT_ENABLE_DEFAULT);
4271
4272         si_pcie_gart_tlb_flush(rdev);
4273         DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
4274                  (unsigned)(rdev->mc.gtt_size >> 20),
4275                  (unsigned long long)rdev->gart.table_addr);
4276         rdev->gart.ready = true;
4277         return 0;
4278 }
4279
4280 static void si_pcie_gart_disable(struct radeon_device *rdev)
4281 {
4282         unsigned i;
4283
4284         for (i = 1; i < 16; ++i) {
4285                 uint32_t reg;
4286                 if (i < 8)
4287                         reg = VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (i << 2);
4288                 else
4289                         reg = VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((i - 8) << 2);
4290                 rdev->vm_manager.saved_table_addr[i] = RREG32(reg);
4291         }
4292
4293         /* Disable all tables */
4294         WREG32(VM_CONTEXT0_CNTL, 0);
4295         WREG32(VM_CONTEXT1_CNTL, 0);
4296         /* Setup TLB control */
4297         WREG32(MC_VM_MX_L1_TLB_CNTL, SYSTEM_ACCESS_MODE_NOT_IN_SYS |
4298                SYSTEM_APERTURE_UNMAPPED_ACCESS_PASS_THRU);
4299         /* Setup L2 cache */
4300         WREG32(VM_L2_CNTL, ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE |
4301                ENABLE_L2_PDE0_CACHE_LRU_UPDATE_BY_WRITE |
4302                EFFECTIVE_L2_QUEUE_SIZE(7) |
4303                CONTEXT1_IDENTITY_ACCESS_MODE(1));
4304         WREG32(VM_L2_CNTL2, 0);
4305         WREG32(VM_L2_CNTL3, L2_CACHE_BIGK_ASSOCIATIVITY |
4306                L2_CACHE_BIGK_FRAGMENT_SIZE(0));
4307         radeon_gart_table_vram_unpin(rdev);
4308 }
4309
4310 static void si_pcie_gart_fini(struct radeon_device *rdev)
4311 {
4312         si_pcie_gart_disable(rdev);
4313         radeon_gart_table_vram_free(rdev);
4314         radeon_gart_fini(rdev);
4315 }
4316
4317 /* vm parser */
4318 static bool si_vm_reg_valid(u32 reg)
4319 {
4320         /* context regs are fine */
4321         if (reg >= 0x28000)
4322                 return true;
4323
4324         /* shader regs are also fine */
4325         if (reg >= 0xB000 && reg < 0xC000)
4326                 return true;
4327
4328         /* check config regs */
4329         switch (reg) {
4330         case GRBM_GFX_INDEX:
4331         case CP_STRMOUT_CNTL:
4332         case VGT_VTX_VECT_EJECT_REG:
4333         case VGT_CACHE_INVALIDATION:
4334         case VGT_ESGS_RING_SIZE:
4335         case VGT_GSVS_RING_SIZE:
4336         case VGT_GS_VERTEX_REUSE:
4337         case VGT_PRIMITIVE_TYPE:
4338         case VGT_INDEX_TYPE:
4339         case VGT_NUM_INDICES:
4340         case VGT_NUM_INSTANCES:
4341         case VGT_TF_RING_SIZE:
4342         case VGT_HS_OFFCHIP_PARAM:
4343         case VGT_TF_MEMORY_BASE:
4344         case PA_CL_ENHANCE:
4345         case PA_SU_LINE_STIPPLE_VALUE:
4346         case PA_SC_LINE_STIPPLE_STATE:
4347         case PA_SC_ENHANCE:
4348         case SQC_CACHES:
4349         case SPI_STATIC_THREAD_MGMT_1:
4350         case SPI_STATIC_THREAD_MGMT_2:
4351         case SPI_STATIC_THREAD_MGMT_3:
4352         case SPI_PS_MAX_WAVE_ID:
4353         case SPI_CONFIG_CNTL:
4354         case SPI_CONFIG_CNTL_1:
4355         case TA_CNTL_AUX:
4356         case TA_CS_BC_BASE_ADDR:
4357                 return true;
4358         default:
4359                 DRM_ERROR("Invalid register 0x%x in CS\n", reg);
4360                 return false;
4361         }
4362 }
4363
4364 static int si_vm_packet3_ce_check(struct radeon_device *rdev,
4365                                   u32 *ib, struct radeon_cs_packet *pkt)
4366 {
4367         switch (pkt->opcode) {
4368         case PACKET3_NOP:
4369         case PACKET3_SET_BASE:
4370         case PACKET3_SET_CE_DE_COUNTERS:
4371         case PACKET3_LOAD_CONST_RAM:
4372         case PACKET3_WRITE_CONST_RAM:
4373         case PACKET3_WRITE_CONST_RAM_OFFSET:
4374         case PACKET3_DUMP_CONST_RAM:
4375         case PACKET3_INCREMENT_CE_COUNTER:
4376         case PACKET3_WAIT_ON_DE_COUNTER:
4377         case PACKET3_CE_WRITE:
4378                 break;
4379         default:
4380                 DRM_ERROR("Invalid CE packet3: 0x%x\n", pkt->opcode);
4381                 return -EINVAL;
4382         }
4383         return 0;
4384 }
4385
4386 static int si_vm_packet3_cp_dma_check(u32 *ib, u32 idx)
4387 {
4388         u32 start_reg, reg, i;
4389         u32 command = ib[idx + 4];
4390         u32 info = ib[idx + 1];
4391         u32 idx_value = ib[idx];
4392         if (command & PACKET3_CP_DMA_CMD_SAS) {
4393                 /* src address space is register */
4394                 if (((info & 0x60000000) >> 29) == 0) {
4395                         start_reg = idx_value << 2;
4396                         if (command & PACKET3_CP_DMA_CMD_SAIC) {
4397                                 reg = start_reg;
4398                                 if (!si_vm_reg_valid(reg)) {
4399                                         DRM_ERROR("CP DMA Bad SRC register\n");
4400                                         return -EINVAL;
4401                                 }
4402                         } else {
4403                                 for (i = 0; i < (command & 0x1fffff); i++) {
4404                                         reg = start_reg + (4 * i);
4405                                         if (!si_vm_reg_valid(reg)) {
4406                                                 DRM_ERROR("CP DMA Bad SRC register\n");
4407                                                 return -EINVAL;
4408                                         }
4409                                 }
4410                         }
4411                 }
4412         }
4413         if (command & PACKET3_CP_DMA_CMD_DAS) {
4414                 /* dst address space is register */
4415                 if (((info & 0x00300000) >> 20) == 0) {
4416                         start_reg = ib[idx + 2];
4417                         if (command & PACKET3_CP_DMA_CMD_DAIC) {
4418                                 reg = start_reg;
4419                                 if (!si_vm_reg_valid(reg)) {
4420                                         DRM_ERROR("CP DMA Bad DST register\n");
4421                                         return -EINVAL;
4422                                 }
4423                         } else {
4424                                 for (i = 0; i < (command & 0x1fffff); i++) {
4425                                         reg = start_reg + (4 * i);
4426                                 if (!si_vm_reg_valid(reg)) {
4427                                                 DRM_ERROR("CP DMA Bad DST register\n");
4428                                                 return -EINVAL;
4429                                         }
4430                                 }
4431                         }
4432                 }
4433         }
4434         return 0;
4435 }
4436
4437 static int si_vm_packet3_gfx_check(struct radeon_device *rdev,
4438                                    u32 *ib, struct radeon_cs_packet *pkt)
4439 {
4440         int r;
4441         u32 idx = pkt->idx + 1;
4442         u32 idx_value = ib[idx];
4443         u32 start_reg, end_reg, reg, i;
4444
4445         switch (pkt->opcode) {
4446         case PACKET3_NOP:
4447         case PACKET3_SET_BASE:
4448         case PACKET3_CLEAR_STATE:
4449         case PACKET3_INDEX_BUFFER_SIZE:
4450         case PACKET3_DISPATCH_DIRECT:
4451         case PACKET3_DISPATCH_INDIRECT:
4452         case PACKET3_ALLOC_GDS:
4453         case PACKET3_WRITE_GDS_RAM:
4454         case PACKET3_ATOMIC_GDS:
4455         case PACKET3_ATOMIC:
4456         case PACKET3_OCCLUSION_QUERY:
4457         case PACKET3_SET_PREDICATION:
4458         case PACKET3_COND_EXEC:
4459         case PACKET3_PRED_EXEC:
4460         case PACKET3_DRAW_INDIRECT:
4461         case PACKET3_DRAW_INDEX_INDIRECT:
4462         case PACKET3_INDEX_BASE:
4463         case PACKET3_DRAW_INDEX_2:
4464         case PACKET3_CONTEXT_CONTROL:
4465         case PACKET3_INDEX_TYPE:
4466         case PACKET3_DRAW_INDIRECT_MULTI:
4467         case PACKET3_DRAW_INDEX_AUTO:
4468         case PACKET3_DRAW_INDEX_IMMD:
4469         case PACKET3_NUM_INSTANCES:
4470         case PACKET3_DRAW_INDEX_MULTI_AUTO:
4471         case PACKET3_STRMOUT_BUFFER_UPDATE:
4472         case PACKET3_DRAW_INDEX_OFFSET_2:
4473         case PACKET3_DRAW_INDEX_MULTI_ELEMENT:
4474         case PACKET3_DRAW_INDEX_INDIRECT_MULTI:
4475         case PACKET3_MPEG_INDEX:
4476         case PACKET3_WAIT_REG_MEM:
4477         case PACKET3_MEM_WRITE:
4478         case PACKET3_PFP_SYNC_ME:
4479         case PACKET3_SURFACE_SYNC:
4480         case PACKET3_EVENT_WRITE:
4481         case PACKET3_EVENT_WRITE_EOP:
4482         case PACKET3_EVENT_WRITE_EOS:
4483         case PACKET3_SET_CONTEXT_REG:
4484         case PACKET3_SET_CONTEXT_REG_INDIRECT:
4485         case PACKET3_SET_SH_REG:
4486         case PACKET3_SET_SH_REG_OFFSET:
4487         case PACKET3_INCREMENT_DE_COUNTER:
4488         case PACKET3_WAIT_ON_CE_COUNTER:
4489         case PACKET3_WAIT_ON_AVAIL_BUFFER:
4490         case PACKET3_ME_WRITE:
4491                 break;
4492         case PACKET3_COPY_DATA:
4493                 if ((idx_value & 0xf00) == 0) {
4494                         reg = ib[idx + 3] * 4;
4495                         if (!si_vm_reg_valid(reg))
4496                                 return -EINVAL;
4497                 }
4498                 break;
4499         case PACKET3_WRITE_DATA:
4500                 if ((idx_value & 0xf00) == 0) {
4501                         start_reg = ib[idx + 1] * 4;
4502                         if (idx_value & 0x10000) {
4503                                 if (!si_vm_reg_valid(start_reg))
4504                                         return -EINVAL;
4505                         } else {
4506                                 for (i = 0; i < (pkt->count - 2); i++) {
4507                                         reg = start_reg + (4 * i);
4508                                         if (!si_vm_reg_valid(reg))
4509                                                 return -EINVAL;
4510                                 }
4511                         }
4512                 }
4513                 break;
4514         case PACKET3_COND_WRITE:
4515                 if (idx_value & 0x100) {
4516                         reg = ib[idx + 5] * 4;
4517                         if (!si_vm_reg_valid(reg))
4518                                 return -EINVAL;
4519                 }
4520                 break;
4521         case PACKET3_COPY_DW:
4522                 if (idx_value & 0x2) {
4523                         reg = ib[idx + 3] * 4;
4524                         if (!si_vm_reg_valid(reg))
4525                                 return -EINVAL;
4526                 }
4527                 break;
4528         case PACKET3_SET_CONFIG_REG:
4529                 start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_START;
4530                 end_reg = 4 * pkt->count + start_reg - 4;
4531                 if ((start_reg < PACKET3_SET_CONFIG_REG_START) ||
4532                     (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
4533                     (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
4534                         DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
4535                         return -EINVAL;
4536                 }
4537                 for (i = 0; i < pkt->count; i++) {
4538                         reg = start_reg + (4 * i);
4539                         if (!si_vm_reg_valid(reg))
4540                                 return -EINVAL;
4541                 }
4542                 break;
4543         case PACKET3_CP_DMA:
4544                 r = si_vm_packet3_cp_dma_check(ib, idx);
4545                 if (r)
4546                         return r;
4547                 break;
4548         default:
4549                 DRM_ERROR("Invalid GFX packet3: 0x%x\n", pkt->opcode);
4550                 return -EINVAL;
4551         }
4552         return 0;
4553 }
4554
4555 static int si_vm_packet3_compute_check(struct radeon_device *rdev,
4556                                        u32 *ib, struct radeon_cs_packet *pkt)
4557 {
4558         int r;
4559         u32 idx = pkt->idx + 1;
4560         u32 idx_value = ib[idx];
4561         u32 start_reg, reg, i;
4562
4563         switch (pkt->opcode) {
4564         case PACKET3_NOP:
4565         case PACKET3_SET_BASE:
4566         case PACKET3_CLEAR_STATE:
4567         case PACKET3_DISPATCH_DIRECT:
4568         case PACKET3_DISPATCH_INDIRECT:
4569         case PACKET3_ALLOC_GDS:
4570         case PACKET3_WRITE_GDS_RAM:
4571         case PACKET3_ATOMIC_GDS:
4572         case PACKET3_ATOMIC:
4573         case PACKET3_OCCLUSION_QUERY:
4574         case PACKET3_SET_PREDICATION:
4575         case PACKET3_COND_EXEC:
4576         case PACKET3_PRED_EXEC:
4577         case PACKET3_CONTEXT_CONTROL:
4578         case PACKET3_STRMOUT_BUFFER_UPDATE:
4579         case PACKET3_WAIT_REG_MEM:
4580         case PACKET3_MEM_WRITE:
4581         case PACKET3_PFP_SYNC_ME:
4582         case PACKET3_SURFACE_SYNC:
4583         case PACKET3_EVENT_WRITE:
4584         case PACKET3_EVENT_WRITE_EOP:
4585         case PACKET3_EVENT_WRITE_EOS:
4586         case PACKET3_SET_CONTEXT_REG:
4587         case PACKET3_SET_CONTEXT_REG_INDIRECT:
4588         case PACKET3_SET_SH_REG:
4589         case PACKET3_SET_SH_REG_OFFSET:
4590         case PACKET3_INCREMENT_DE_COUNTER:
4591         case PACKET3_WAIT_ON_CE_COUNTER:
4592         case PACKET3_WAIT_ON_AVAIL_BUFFER:
4593         case PACKET3_ME_WRITE:
4594                 break;
4595         case PACKET3_COPY_DATA:
4596                 if ((idx_value & 0xf00) == 0) {
4597                         reg = ib[idx + 3] * 4;
4598                         if (!si_vm_reg_valid(reg))
4599                                 return -EINVAL;
4600                 }
4601                 break;
4602         case PACKET3_WRITE_DATA:
4603                 if ((idx_value & 0xf00) == 0) {
4604                         start_reg = ib[idx + 1] * 4;
4605                         if (idx_value & 0x10000) {
4606                                 if (!si_vm_reg_valid(start_reg))
4607                                         return -EINVAL;
4608                         } else {
4609                                 for (i = 0; i < (pkt->count - 2); i++) {
4610                                         reg = start_reg + (4 * i);
4611                                         if (!si_vm_reg_valid(reg))
4612                                                 return -EINVAL;
4613                                 }
4614                         }
4615                 }
4616                 break;
4617         case PACKET3_COND_WRITE:
4618                 if (idx_value & 0x100) {
4619                         reg = ib[idx + 5] * 4;
4620                         if (!si_vm_reg_valid(reg))
4621                                 return -EINVAL;
4622                 }
4623                 break;
4624         case PACKET3_COPY_DW:
4625                 if (idx_value & 0x2) {
4626                         reg = ib[idx + 3] * 4;
4627                         if (!si_vm_reg_valid(reg))
4628                                 return -EINVAL;
4629                 }
4630                 break;
4631         case PACKET3_CP_DMA:
4632                 r = si_vm_packet3_cp_dma_check(ib, idx);
4633                 if (r)
4634                         return r;
4635                 break;
4636         default:
4637                 DRM_ERROR("Invalid Compute packet3: 0x%x\n", pkt->opcode);
4638                 return -EINVAL;
4639         }
4640         return 0;
4641 }
4642
4643 int si_ib_parse(struct radeon_device *rdev, struct radeon_ib *ib)
4644 {
4645         int ret = 0;
4646         u32 idx = 0, i;
4647         struct radeon_cs_packet pkt;
4648
4649         do {
4650                 pkt.idx = idx;
4651                 pkt.type = RADEON_CP_PACKET_GET_TYPE(ib->ptr[idx]);
4652                 pkt.count = RADEON_CP_PACKET_GET_COUNT(ib->ptr[idx]);
4653                 pkt.one_reg_wr = 0;
4654                 switch (pkt.type) {
4655                 case RADEON_PACKET_TYPE0:
4656                         dev_err(rdev->dev, "Packet0 not allowed!\n");
4657                         ret = -EINVAL;
4658                         break;
4659                 case RADEON_PACKET_TYPE2:
4660                         idx += 1;
4661                         break;
4662                 case RADEON_PACKET_TYPE3:
4663                         pkt.opcode = RADEON_CP_PACKET3_GET_OPCODE(ib->ptr[idx]);
4664                         if (ib->is_const_ib)
4665                                 ret = si_vm_packet3_ce_check(rdev, ib->ptr, &pkt);
4666                         else {
4667                                 switch (ib->ring) {
4668                                 case RADEON_RING_TYPE_GFX_INDEX:
4669                                         ret = si_vm_packet3_gfx_check(rdev, ib->ptr, &pkt);
4670                                         break;
4671                                 case CAYMAN_RING_TYPE_CP1_INDEX:
4672                                 case CAYMAN_RING_TYPE_CP2_INDEX:
4673                                         ret = si_vm_packet3_compute_check(rdev, ib->ptr, &pkt);
4674                                         break;
4675                                 default:
4676                                         dev_err(rdev->dev, "Non-PM4 ring %d !\n", ib->ring);
4677                                         ret = -EINVAL;
4678                                         break;
4679                                 }
4680                         }
4681                         idx += pkt.count + 2;
4682                         break;
4683                 default:
4684                         dev_err(rdev->dev, "Unknown packet type %d !\n", pkt.type);
4685                         ret = -EINVAL;
4686                         break;
4687                 }
4688                 if (ret) {
4689                         for (i = 0; i < ib->length_dw; i++) {
4690                                 if (i == idx)
4691                                         printk("\t0x%08x <---\n", ib->ptr[i]);
4692                                 else
4693                                         printk("\t0x%08x\n", ib->ptr[i]);
4694                         }
4695                         break;
4696                 }
4697         } while (idx < ib->length_dw);
4698
4699         return ret;
4700 }
4701
4702 /*
4703  * vm
4704  */
4705 int si_vm_init(struct radeon_device *rdev)
4706 {
4707         /* number of VMs */
4708         rdev->vm_manager.nvm = 16;
4709         /* base offset of vram pages */
4710         rdev->vm_manager.vram_base_offset = 0;
4711
4712         return 0;
4713 }
4714
4715 void si_vm_fini(struct radeon_device *rdev)
4716 {
4717 }
4718
4719 /**
4720  * si_vm_decode_fault - print human readable fault info
4721  *
4722  * @rdev: radeon_device pointer
4723  * @status: VM_CONTEXT1_PROTECTION_FAULT_STATUS register value
4724  * @addr: VM_CONTEXT1_PROTECTION_FAULT_ADDR register value
4725  *
4726  * Print human readable fault information (SI).
4727  */
4728 static void si_vm_decode_fault(struct radeon_device *rdev,
4729                                u32 status, u32 addr)
4730 {
4731         u32 mc_id = (status & MEMORY_CLIENT_ID_MASK) >> MEMORY_CLIENT_ID_SHIFT;
4732         u32 vmid = (status & FAULT_VMID_MASK) >> FAULT_VMID_SHIFT;
4733         u32 protections = (status & PROTECTIONS_MASK) >> PROTECTIONS_SHIFT;
4734         char *block;
4735
4736         if (rdev->family == CHIP_TAHITI) {
4737                 switch (mc_id) {
4738                 case 160:
4739                 case 144:
4740                 case 96:
4741                 case 80:
4742                 case 224:
4743                 case 208:
4744                 case 32:
4745                 case 16:
4746                         block = "CB";
4747                         break;
4748                 case 161:
4749                 case 145:
4750                 case 97:
4751                 case 81:
4752                 case 225:
4753                 case 209:
4754                 case 33:
4755                 case 17:
4756                         block = "CB_FMASK";
4757                         break;
4758                 case 162:
4759                 case 146:
4760                 case 98:
4761                 case 82:
4762                 case 226:
4763                 case 210:
4764                 case 34:
4765                 case 18:
4766                         block = "CB_CMASK";
4767                         break;
4768                 case 163:
4769                 case 147:
4770                 case 99:
4771                 case 83:
4772                 case 227:
4773                 case 211:
4774                 case 35:
4775                 case 19:
4776                         block = "CB_IMMED";
4777                         break;
4778                 case 164:
4779                 case 148:
4780                 case 100:
4781                 case 84:
4782                 case 228:
4783                 case 212:
4784                 case 36:
4785                 case 20:
4786                         block = "DB";
4787                         break;
4788                 case 165:
4789                 case 149:
4790                 case 101:
4791                 case 85:
4792                 case 229:
4793                 case 213:
4794                 case 37:
4795                 case 21:
4796                         block = "DB_HTILE";
4797                         break;
4798                 case 167:
4799                 case 151:
4800                 case 103:
4801                 case 87:
4802                 case 231:
4803                 case 215:
4804                 case 39:
4805                 case 23:
4806                         block = "DB_STEN";
4807                         break;
4808                 case 72:
4809                 case 68:
4810                 case 64:
4811                 case 8:
4812                 case 4:
4813                 case 0:
4814                 case 136:
4815                 case 132:
4816                 case 128:
4817                 case 200:
4818                 case 196:
4819                 case 192:
4820                         block = "TC";
4821                         break;
4822                 case 112:
4823                 case 48:
4824                         block = "CP";
4825                         break;
4826                 case 49:
4827                 case 177:
4828                 case 50:
4829                 case 178:
4830                         block = "SH";
4831                         break;
4832                 case 53:
4833                 case 190:
4834                         block = "VGT";
4835                         break;
4836                 case 117:
4837                         block = "IH";
4838                         break;
4839                 case 51:
4840                 case 115:
4841                         block = "RLC";
4842                         break;
4843                 case 119:
4844                 case 183:
4845                         block = "DMA0";
4846                         break;
4847                 case 61:
4848                         block = "DMA1";
4849                         break;
4850                 case 248:
4851                 case 120:
4852                         block = "HDP";
4853                         break;
4854                 default:
4855                         block = "unknown";
4856                         break;
4857                 }
4858         } else {
4859                 switch (mc_id) {
4860                 case 32:
4861                 case 16:
4862                 case 96:
4863                 case 80:
4864                 case 160:
4865                 case 144:
4866                 case 224:
4867                 case 208:
4868                         block = "CB";
4869                         break;
4870                 case 33:
4871                 case 17:
4872                 case 97:
4873                 case 81:
4874                 case 161:
4875                 case 145:
4876                 case 225:
4877                 case 209:
4878                         block = "CB_FMASK";
4879                         break;
4880                 case 34:
4881                 case 18:
4882                 case 98:
4883                 case 82:
4884                 case 162:
4885                 case 146:
4886                 case 226:
4887                 case 210:
4888                         block = "CB_CMASK";
4889                         break;
4890                 case 35:
4891                 case 19:
4892                 case 99:
4893                 case 83:
4894                 case 163:
4895                 case 147:
4896                 case 227:
4897                 case 211:
4898                         block = "CB_IMMED";
4899                         break;
4900                 case 36:
4901                 case 20:
4902                 case 100:
4903                 case 84:
4904                 case 164:
4905                 case 148:
4906                 case 228:
4907                 case 212:
4908                         block = "DB";
4909                         break;
4910                 case 37:
4911                 case 21:
4912                 case 101:
4913                 case 85:
4914                 case 165:
4915                 case 149:
4916                 case 229:
4917                 case 213:
4918                         block = "DB_HTILE";
4919                         break;
4920                 case 39:
4921                 case 23:
4922                 case 103:
4923                 case 87:
4924                 case 167:
4925                 case 151:
4926                 case 231:
4927                 case 215:
4928                         block = "DB_STEN";
4929                         break;
4930                 case 72:
4931                 case 68:
4932                 case 8:
4933                 case 4:
4934                 case 136:
4935                 case 132:
4936                 case 200:
4937                 case 196:
4938                         block = "TC";
4939                         break;
4940                 case 112:
4941                 case 48:
4942                         block = "CP";
4943                         break;
4944                 case 49:
4945                 case 177:
4946                 case 50:
4947                 case 178:
4948                         block = "SH";
4949                         break;
4950                 case 53:
4951                         block = "VGT";
4952                         break;
4953                 case 117:
4954                         block = "IH";
4955                         break;
4956                 case 51:
4957                 case 115:
4958                         block = "RLC";
4959                         break;
4960                 case 119:
4961                 case 183:
4962                         block = "DMA0";
4963                         break;
4964                 case 61:
4965                         block = "DMA1";
4966                         break;
4967                 case 248:
4968                 case 120:
4969                         block = "HDP";
4970                         break;
4971                 default:
4972                         block = "unknown";
4973                         break;
4974                 }
4975         }
4976
4977         printk("VM fault (0x%02x, vmid %d) at page %u, %s from %s (%d)\n",
4978                protections, vmid, addr,
4979                (status & MEMORY_CLIENT_RW_MASK) ? "write" : "read",
4980                block, mc_id);
4981 }
4982
4983 void si_vm_flush(struct radeon_device *rdev, struct radeon_ring *ring,
4984                  unsigned vm_id, uint64_t pd_addr)
4985 {
4986         /* write new base address */
4987         radeon_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
4988         radeon_ring_write(ring, (WRITE_DATA_ENGINE_SEL(1) |
4989                                  WRITE_DATA_DST_SEL(0)));
4990
4991         if (vm_id < 8) {
4992                 radeon_ring_write(ring,
4993                                   (VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm_id << 2)) >> 2);
4994         } else {
4995                 radeon_ring_write(ring,
4996                                   (VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((vm_id - 8) << 2)) >> 2);
4997         }
4998         radeon_ring_write(ring, 0);
4999         radeon_ring_write(ring, pd_addr >> 12);
5000
5001         /* flush hdp cache */
5002         radeon_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
5003         radeon_ring_write(ring, (WRITE_DATA_ENGINE_SEL(1) |
5004                                  WRITE_DATA_DST_SEL(0)));
5005         radeon_ring_write(ring, HDP_MEM_COHERENCY_FLUSH_CNTL >> 2);
5006         radeon_ring_write(ring, 0);
5007         radeon_ring_write(ring, 0x1);
5008
5009         /* bits 0-15 are the VM contexts0-15 */
5010         radeon_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
5011         radeon_ring_write(ring, (WRITE_DATA_ENGINE_SEL(1) |
5012                                  WRITE_DATA_DST_SEL(0)));
5013         radeon_ring_write(ring, VM_INVALIDATE_REQUEST >> 2);
5014         radeon_ring_write(ring, 0);
5015         radeon_ring_write(ring, 1 << vm_id);
5016
5017         /* wait for the invalidate to complete */
5018         radeon_ring_write(ring, PACKET3(PACKET3_WAIT_REG_MEM, 5));
5019         radeon_ring_write(ring, (WAIT_REG_MEM_FUNCTION(0) |  /* always */
5020                                  WAIT_REG_MEM_ENGINE(0))); /* me */
5021         radeon_ring_write(ring, VM_INVALIDATE_REQUEST >> 2);
5022         radeon_ring_write(ring, 0);
5023         radeon_ring_write(ring, 0); /* ref */
5024         radeon_ring_write(ring, 0); /* mask */
5025         radeon_ring_write(ring, 0x20); /* poll interval */
5026
5027         /* sync PFP to ME, otherwise we might get invalid PFP reads */
5028         radeon_ring_write(ring, PACKET3(PACKET3_PFP_SYNC_ME, 0));
5029         radeon_ring_write(ring, 0x0);
5030 }
5031
5032 /*
5033  *  Power and clock gating
5034  */
5035 static void si_wait_for_rlc_serdes(struct radeon_device *rdev)
5036 {
5037         int i;
5038
5039         for (i = 0; i < rdev->usec_timeout; i++) {
5040                 if (RREG32(RLC_SERDES_MASTER_BUSY_0) == 0)
5041                         break;
5042                 udelay(1);
5043         }
5044
5045         for (i = 0; i < rdev->usec_timeout; i++) {
5046                 if (RREG32(RLC_SERDES_MASTER_BUSY_1) == 0)
5047                         break;
5048                 udelay(1);
5049         }
5050 }
5051
5052 static void si_enable_gui_idle_interrupt(struct radeon_device *rdev,
5053                                          bool enable)
5054 {
5055         u32 tmp = RREG32(CP_INT_CNTL_RING0);
5056         u32 mask;
5057         int i;
5058
5059         if (enable)
5060                 tmp |= (CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE);
5061         else
5062                 tmp &= ~(CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE);
5063         WREG32(CP_INT_CNTL_RING0, tmp);
5064
5065         if (!enable) {
5066                 /* read a gfx register */
5067                 tmp = RREG32(DB_DEPTH_INFO);
5068
5069                 mask = RLC_BUSY_STATUS | GFX_POWER_STATUS | GFX_CLOCK_STATUS | GFX_LS_STATUS;
5070                 for (i = 0; i < rdev->usec_timeout; i++) {
5071                         if ((RREG32(RLC_STAT) & mask) == (GFX_CLOCK_STATUS | GFX_POWER_STATUS))
5072                                 break;
5073                         udelay(1);
5074                 }
5075         }
5076 }
5077
5078 static void si_set_uvd_dcm(struct radeon_device *rdev,
5079                            bool sw_mode)
5080 {
5081         u32 tmp, tmp2;
5082
5083         tmp = RREG32(UVD_CGC_CTRL);
5084         tmp &= ~(CLK_OD_MASK | CG_DT_MASK);
5085         tmp |= DCM | CG_DT(1) | CLK_OD(4);
5086
5087         if (sw_mode) {
5088                 tmp &= ~0x7ffff800;
5089                 tmp2 = DYN_OR_EN | DYN_RR_EN | G_DIV_ID(7);
5090         } else {
5091                 tmp |= 0x7ffff800;
5092                 tmp2 = 0;
5093         }
5094
5095         WREG32(UVD_CGC_CTRL, tmp);
5096         WREG32_UVD_CTX(UVD_CGC_CTRL2, tmp2);
5097 }
5098
5099 void si_init_uvd_internal_cg(struct radeon_device *rdev)
5100 {
5101         bool hw_mode = true;
5102
5103         if (hw_mode) {
5104                 si_set_uvd_dcm(rdev, false);
5105         } else {
5106                 u32 tmp = RREG32(UVD_CGC_CTRL);
5107                 tmp &= ~DCM;
5108                 WREG32(UVD_CGC_CTRL, tmp);
5109         }
5110 }
5111
5112 static u32 si_halt_rlc(struct radeon_device *rdev)
5113 {
5114         u32 data, orig;
5115
5116         orig = data = RREG32(RLC_CNTL);
5117
5118         if (data & RLC_ENABLE) {
5119                 data &= ~RLC_ENABLE;
5120                 WREG32(RLC_CNTL, data);
5121
5122                 si_wait_for_rlc_serdes(rdev);
5123         }
5124
5125         return orig;
5126 }
5127
5128 static void si_update_rlc(struct radeon_device *rdev, u32 rlc)
5129 {
5130         u32 tmp;
5131
5132         tmp = RREG32(RLC_CNTL);
5133         if (tmp != rlc)
5134                 WREG32(RLC_CNTL, rlc);
5135 }
5136
5137 static void si_enable_dma_pg(struct radeon_device *rdev, bool enable)
5138 {
5139         u32 data, orig;
5140
5141         orig = data = RREG32(DMA_PG);
5142         if (enable && (rdev->pg_flags & RADEON_PG_SUPPORT_SDMA))
5143                 data |= PG_CNTL_ENABLE;
5144         else
5145                 data &= ~PG_CNTL_ENABLE;
5146         if (orig != data)
5147                 WREG32(DMA_PG, data);
5148 }
5149
5150 static void si_init_dma_pg(struct radeon_device *rdev)
5151 {
5152         u32 tmp;
5153
5154         WREG32(DMA_PGFSM_WRITE,  0x00002000);
5155         WREG32(DMA_PGFSM_CONFIG, 0x100010ff);
5156
5157         for (tmp = 0; tmp < 5; tmp++)
5158                 WREG32(DMA_PGFSM_WRITE, 0);
5159 }
5160
5161 static void si_enable_gfx_cgpg(struct radeon_device *rdev,
5162                                bool enable)
5163 {
5164         u32 tmp;
5165
5166         if (enable && (rdev->pg_flags & RADEON_PG_SUPPORT_GFX_PG)) {
5167                 tmp = RLC_PUD(0x10) | RLC_PDD(0x10) | RLC_TTPD(0x10) | RLC_MSD(0x10);
5168                 WREG32(RLC_TTOP_D, tmp);
5169
5170                 tmp = RREG32(RLC_PG_CNTL);
5171                 tmp |= GFX_PG_ENABLE;
5172                 WREG32(RLC_PG_CNTL, tmp);
5173
5174                 tmp = RREG32(RLC_AUTO_PG_CTRL);
5175                 tmp |= AUTO_PG_EN;
5176                 WREG32(RLC_AUTO_PG_CTRL, tmp);
5177         } else {
5178                 tmp = RREG32(RLC_AUTO_PG_CTRL);
5179                 tmp &= ~AUTO_PG_EN;
5180                 WREG32(RLC_AUTO_PG_CTRL, tmp);
5181
5182                 tmp = RREG32(DB_RENDER_CONTROL);
5183         }
5184 }
5185
5186 static void si_init_gfx_cgpg(struct radeon_device *rdev)
5187 {
5188         u32 tmp;
5189
5190         WREG32(RLC_SAVE_AND_RESTORE_BASE, rdev->rlc.save_restore_gpu_addr >> 8);
5191
5192         tmp = RREG32(RLC_PG_CNTL);
5193         tmp |= GFX_PG_SRC;
5194         WREG32(RLC_PG_CNTL, tmp);
5195
5196         WREG32(RLC_CLEAR_STATE_RESTORE_BASE, rdev->rlc.clear_state_gpu_addr >> 8);
5197
5198         tmp = RREG32(RLC_AUTO_PG_CTRL);
5199
5200         tmp &= ~GRBM_REG_SGIT_MASK;
5201         tmp |= GRBM_REG_SGIT(0x700);
5202         tmp &= ~PG_AFTER_GRBM_REG_ST_MASK;
5203         WREG32(RLC_AUTO_PG_CTRL, tmp);
5204 }
5205
5206 static u32 si_get_cu_active_bitmap(struct radeon_device *rdev, u32 se, u32 sh)
5207 {
5208         u32 mask = 0, tmp, tmp1;
5209         int i;
5210
5211         si_select_se_sh(rdev, se, sh);
5212         tmp = RREG32(CC_GC_SHADER_ARRAY_CONFIG);
5213         tmp1 = RREG32(GC_USER_SHADER_ARRAY_CONFIG);
5214         si_select_se_sh(rdev, 0xffffffff, 0xffffffff);
5215
5216         tmp &= 0xffff0000;
5217
5218         tmp |= tmp1;
5219         tmp >>= 16;
5220
5221         for (i = 0; i < rdev->config.si.max_cu_per_sh; i ++) {
5222                 mask <<= 1;
5223                 mask |= 1;
5224         }
5225
5226         return (~tmp) & mask;
5227 }
5228
5229 static void si_init_ao_cu_mask(struct radeon_device *rdev)
5230 {
5231         u32 i, j, k, active_cu_number = 0;
5232         u32 mask, counter, cu_bitmap;
5233         u32 tmp = 0;
5234
5235         for (i = 0; i < rdev->config.si.max_shader_engines; i++) {
5236                 for (j = 0; j < rdev->config.si.max_sh_per_se; j++) {
5237                         mask = 1;
5238                         cu_bitmap = 0;
5239                         counter  = 0;
5240                         for (k = 0; k < rdev->config.si.max_cu_per_sh; k++) {
5241                                 if (si_get_cu_active_bitmap(rdev, i, j) & mask) {
5242                                         if (counter < 2)
5243                                                 cu_bitmap |= mask;
5244                                         counter++;
5245                                 }
5246                                 mask <<= 1;
5247                         }
5248
5249                         active_cu_number += counter;
5250                         tmp |= (cu_bitmap << (i * 16 + j * 8));
5251                 }
5252         }
5253
5254         WREG32(RLC_PG_AO_CU_MASK, tmp);
5255
5256         tmp = RREG32(RLC_MAX_PG_CU);
5257         tmp &= ~MAX_PU_CU_MASK;
5258         tmp |= MAX_PU_CU(active_cu_number);
5259         WREG32(RLC_MAX_PG_CU, tmp);
5260 }
5261
5262 static void si_enable_cgcg(struct radeon_device *rdev,
5263                            bool enable)
5264 {
5265         u32 data, orig, tmp;
5266
5267         orig = data = RREG32(RLC_CGCG_CGLS_CTRL);
5268
5269         if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_GFX_CGCG)) {
5270                 si_enable_gui_idle_interrupt(rdev, true);
5271
5272                 WREG32(RLC_GCPM_GENERAL_3, 0x00000080);
5273
5274                 tmp = si_halt_rlc(rdev);
5275
5276                 WREG32(RLC_SERDES_WR_MASTER_MASK_0, 0xffffffff);
5277                 WREG32(RLC_SERDES_WR_MASTER_MASK_1, 0xffffffff);
5278                 WREG32(RLC_SERDES_WR_CTRL, 0x00b000ff);
5279
5280                 si_wait_for_rlc_serdes(rdev);
5281
5282                 si_update_rlc(rdev, tmp);
5283
5284                 WREG32(RLC_SERDES_WR_CTRL, 0x007000ff);
5285
5286                 data |= CGCG_EN | CGLS_EN;
5287         } else {
5288                 si_enable_gui_idle_interrupt(rdev, false);
5289
5290                 RREG32(CB_CGTT_SCLK_CTRL);
5291                 RREG32(CB_CGTT_SCLK_CTRL);
5292                 RREG32(CB_CGTT_SCLK_CTRL);
5293                 RREG32(CB_CGTT_SCLK_CTRL);
5294
5295                 data &= ~(CGCG_EN | CGLS_EN);
5296         }
5297
5298         if (orig != data)
5299                 WREG32(RLC_CGCG_CGLS_CTRL, data);
5300 }
5301
5302 static void si_enable_mgcg(struct radeon_device *rdev,
5303                            bool enable)
5304 {
5305         u32 data, orig, tmp = 0;
5306
5307         if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_GFX_MGCG)) {
5308                 orig = data = RREG32(CGTS_SM_CTRL_REG);
5309                 data = 0x96940200;
5310                 if (orig != data)
5311                         WREG32(CGTS_SM_CTRL_REG, data);
5312
5313                 if (rdev->cg_flags & RADEON_CG_SUPPORT_GFX_CP_LS) {
5314                         orig = data = RREG32(CP_MEM_SLP_CNTL);
5315                         data |= CP_MEM_LS_EN;
5316                         if (orig != data)
5317                                 WREG32(CP_MEM_SLP_CNTL, data);
5318                 }
5319
5320                 orig = data = RREG32(RLC_CGTT_MGCG_OVERRIDE);
5321                 data &= 0xffffffc0;
5322                 if (orig != data)
5323                         WREG32(RLC_CGTT_MGCG_OVERRIDE, data);
5324
5325                 tmp = si_halt_rlc(rdev);
5326
5327                 WREG32(RLC_SERDES_WR_MASTER_MASK_0, 0xffffffff);
5328                 WREG32(RLC_SERDES_WR_MASTER_MASK_1, 0xffffffff);
5329                 WREG32(RLC_SERDES_WR_CTRL, 0x00d000ff);
5330
5331                 si_update_rlc(rdev, tmp);
5332         } else {
5333                 orig = data = RREG32(RLC_CGTT_MGCG_OVERRIDE);
5334                 data |= 0x00000003;
5335                 if (orig != data)
5336                         WREG32(RLC_CGTT_MGCG_OVERRIDE, data);
5337
5338                 data = RREG32(CP_MEM_SLP_CNTL);
5339                 if (data & CP_MEM_LS_EN) {
5340                         data &= ~CP_MEM_LS_EN;
5341                         WREG32(CP_MEM_SLP_CNTL, data);
5342                 }
5343                 orig = data = RREG32(CGTS_SM_CTRL_REG);
5344                 data |= LS_OVERRIDE | OVERRIDE;
5345                 if (orig != data)
5346                         WREG32(CGTS_SM_CTRL_REG, data);
5347
5348                 tmp = si_halt_rlc(rdev);
5349
5350                 WREG32(RLC_SERDES_WR_MASTER_MASK_0, 0xffffffff);
5351                 WREG32(RLC_SERDES_WR_MASTER_MASK_1, 0xffffffff);
5352                 WREG32(RLC_SERDES_WR_CTRL, 0x00e000ff);
5353
5354                 si_update_rlc(rdev, tmp);
5355         }
5356 }
5357
5358 static void si_enable_uvd_mgcg(struct radeon_device *rdev,
5359                                bool enable)
5360 {
5361         u32 orig, data, tmp;
5362
5363         if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_UVD_MGCG)) {
5364                 tmp = RREG32_UVD_CTX(UVD_CGC_MEM_CTRL);
5365                 tmp |= 0x3fff;
5366                 WREG32_UVD_CTX(UVD_CGC_MEM_CTRL, tmp);
5367
5368                 orig = data = RREG32(UVD_CGC_CTRL);
5369                 data |= DCM;
5370                 if (orig != data)
5371                         WREG32(UVD_CGC_CTRL, data);
5372
5373                 WREG32_SMC(SMC_CG_IND_START + CG_CGTT_LOCAL_0, 0);
5374                 WREG32_SMC(SMC_CG_IND_START + CG_CGTT_LOCAL_1, 0);
5375         } else {
5376                 tmp = RREG32_UVD_CTX(UVD_CGC_MEM_CTRL);
5377                 tmp &= ~0x3fff;
5378                 WREG32_UVD_CTX(UVD_CGC_MEM_CTRL, tmp);
5379
5380                 orig = data = RREG32(UVD_CGC_CTRL);
5381                 data &= ~DCM;
5382                 if (orig != data)
5383                         WREG32(UVD_CGC_CTRL, data);
5384
5385                 WREG32_SMC(SMC_CG_IND_START + CG_CGTT_LOCAL_0, 0xffffffff);
5386                 WREG32_SMC(SMC_CG_IND_START + CG_CGTT_LOCAL_1, 0xffffffff);
5387         }
5388 }
5389
5390 static const u32 mc_cg_registers[] =
5391 {
5392         MC_HUB_MISC_HUB_CG,
5393         MC_HUB_MISC_SIP_CG,
5394         MC_HUB_MISC_VM_CG,
5395         MC_XPB_CLK_GAT,
5396         ATC_MISC_CG,
5397         MC_CITF_MISC_WR_CG,
5398         MC_CITF_MISC_RD_CG,
5399         MC_CITF_MISC_VM_CG,
5400         VM_L2_CG,
5401 };
5402
5403 static void si_enable_mc_ls(struct radeon_device *rdev,
5404                             bool enable)
5405 {
5406         int i;
5407         u32 orig, data;
5408
5409         for (i = 0; i < ARRAY_SIZE(mc_cg_registers); i++) {
5410                 orig = data = RREG32(mc_cg_registers[i]);
5411                 if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_MC_LS))
5412                         data |= MC_LS_ENABLE;
5413                 else
5414                         data &= ~MC_LS_ENABLE;
5415                 if (data != orig)
5416                         WREG32(mc_cg_registers[i], data);
5417         }
5418 }
5419
5420 static void si_enable_mc_mgcg(struct radeon_device *rdev,
5421                                bool enable)
5422 {
5423         int i;
5424         u32 orig, data;
5425
5426         for (i = 0; i < ARRAY_SIZE(mc_cg_registers); i++) {
5427                 orig = data = RREG32(mc_cg_registers[i]);
5428                 if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_MC_MGCG))
5429                         data |= MC_CG_ENABLE;
5430                 else
5431                         data &= ~MC_CG_ENABLE;
5432                 if (data != orig)
5433                         WREG32(mc_cg_registers[i], data);
5434         }
5435 }
5436
5437 static void si_enable_dma_mgcg(struct radeon_device *rdev,
5438                                bool enable)
5439 {
5440         u32 orig, data, offset;
5441         int i;
5442
5443         if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_SDMA_MGCG)) {
5444                 for (i = 0; i < 2; i++) {
5445                         if (i == 0)
5446                                 offset = DMA0_REGISTER_OFFSET;
5447                         else
5448                                 offset = DMA1_REGISTER_OFFSET;
5449                         orig = data = RREG32(DMA_POWER_CNTL + offset);
5450                         data &= ~MEM_POWER_OVERRIDE;
5451                         if (data != orig)
5452                                 WREG32(DMA_POWER_CNTL + offset, data);
5453                         WREG32(DMA_CLK_CTRL + offset, 0x00000100);
5454                 }
5455         } else {
5456                 for (i = 0; i < 2; i++) {
5457                         if (i == 0)
5458                                 offset = DMA0_REGISTER_OFFSET;
5459                         else
5460                                 offset = DMA1_REGISTER_OFFSET;
5461                         orig = data = RREG32(DMA_POWER_CNTL + offset);
5462                         data |= MEM_POWER_OVERRIDE;
5463                         if (data != orig)
5464                                 WREG32(DMA_POWER_CNTL + offset, data);
5465
5466                         orig = data = RREG32(DMA_CLK_CTRL + offset);
5467                         data = 0xff000000;
5468                         if (data != orig)
5469                                 WREG32(DMA_CLK_CTRL + offset, data);
5470                 }
5471         }
5472 }
5473
5474 static void si_enable_bif_mgls(struct radeon_device *rdev,
5475                                bool enable)
5476 {
5477         u32 orig, data;
5478
5479         orig = data = RREG32_PCIE(PCIE_CNTL2);
5480
5481         if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_BIF_LS))
5482                 data |= SLV_MEM_LS_EN | MST_MEM_LS_EN |
5483                         REPLAY_MEM_LS_EN | SLV_MEM_AGGRESSIVE_LS_EN;
5484         else
5485                 data &= ~(SLV_MEM_LS_EN | MST_MEM_LS_EN |
5486                           REPLAY_MEM_LS_EN | SLV_MEM_AGGRESSIVE_LS_EN);
5487
5488         if (orig != data)
5489                 WREG32_PCIE(PCIE_CNTL2, data);
5490 }
5491
5492 static void si_enable_hdp_mgcg(struct radeon_device *rdev,
5493                                bool enable)
5494 {
5495         u32 orig, data;
5496
5497         orig = data = RREG32(HDP_HOST_PATH_CNTL);
5498
5499         if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_HDP_MGCG))
5500                 data &= ~CLOCK_GATING_DIS;
5501         else
5502                 data |= CLOCK_GATING_DIS;
5503
5504         if (orig != data)
5505                 WREG32(HDP_HOST_PATH_CNTL, data);
5506 }
5507
5508 static void si_enable_hdp_ls(struct radeon_device *rdev,
5509                              bool enable)
5510 {
5511         u32 orig, data;
5512
5513         orig = data = RREG32(HDP_MEM_POWER_LS);
5514
5515         if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_HDP_LS))
5516                 data |= HDP_LS_ENABLE;
5517         else
5518                 data &= ~HDP_LS_ENABLE;
5519
5520         if (orig != data)
5521                 WREG32(HDP_MEM_POWER_LS, data);
5522 }
5523
5524 static void si_update_cg(struct radeon_device *rdev,
5525                          u32 block, bool enable)
5526 {
5527         if (block & RADEON_CG_BLOCK_GFX) {
5528                 si_enable_gui_idle_interrupt(rdev, false);
5529                 /* order matters! */
5530                 if (enable) {
5531                         si_enable_mgcg(rdev, true);
5532                         si_enable_cgcg(rdev, true);
5533                 } else {
5534                         si_enable_cgcg(rdev, false);
5535                         si_enable_mgcg(rdev, false);
5536                 }
5537                 si_enable_gui_idle_interrupt(rdev, true);
5538         }
5539
5540         if (block & RADEON_CG_BLOCK_MC) {
5541                 si_enable_mc_mgcg(rdev, enable);
5542                 si_enable_mc_ls(rdev, enable);
5543         }
5544
5545         if (block & RADEON_CG_BLOCK_SDMA) {
5546                 si_enable_dma_mgcg(rdev, enable);
5547         }
5548
5549         if (block & RADEON_CG_BLOCK_BIF) {
5550                 si_enable_bif_mgls(rdev, enable);
5551         }
5552
5553         if (block & RADEON_CG_BLOCK_UVD) {
5554                 if (rdev->has_uvd) {
5555                         si_enable_uvd_mgcg(rdev, enable);
5556                 }
5557         }
5558
5559         if (block & RADEON_CG_BLOCK_HDP) {
5560                 si_enable_hdp_mgcg(rdev, enable);
5561                 si_enable_hdp_ls(rdev, enable);
5562         }
5563 }
5564
5565 static void si_init_cg(struct radeon_device *rdev)
5566 {
5567         si_update_cg(rdev, (RADEON_CG_BLOCK_GFX |
5568                             RADEON_CG_BLOCK_MC |
5569                             RADEON_CG_BLOCK_SDMA |
5570                             RADEON_CG_BLOCK_BIF |
5571                             RADEON_CG_BLOCK_HDP), true);
5572         if (rdev->has_uvd) {
5573                 si_update_cg(rdev, RADEON_CG_BLOCK_UVD, true);
5574                 si_init_uvd_internal_cg(rdev);
5575         }
5576 }
5577
5578 static void si_fini_cg(struct radeon_device *rdev)
5579 {
5580         if (rdev->has_uvd) {
5581                 si_update_cg(rdev, RADEON_CG_BLOCK_UVD, false);
5582         }
5583         si_update_cg(rdev, (RADEON_CG_BLOCK_GFX |
5584                             RADEON_CG_BLOCK_MC |
5585                             RADEON_CG_BLOCK_SDMA |
5586                             RADEON_CG_BLOCK_BIF |
5587                             RADEON_CG_BLOCK_HDP), false);
5588 }
5589
5590 u32 si_get_csb_size(struct radeon_device *rdev)
5591 {
5592         u32 count = 0;
5593         const struct cs_section_def *sect = NULL;
5594         const struct cs_extent_def *ext = NULL;
5595
5596         if (rdev->rlc.cs_data == NULL)
5597                 return 0;
5598
5599         /* begin clear state */
5600         count += 2;
5601         /* context control state */
5602         count += 3;
5603
5604         for (sect = rdev->rlc.cs_data; sect->section != NULL; ++sect) {
5605                 for (ext = sect->section; ext->extent != NULL; ++ext) {
5606                         if (sect->id == SECT_CONTEXT)
5607                                 count += 2 + ext->reg_count;
5608                         else
5609                                 return 0;
5610                 }
5611         }
5612         /* pa_sc_raster_config */
5613         count += 3;
5614         /* end clear state */
5615         count += 2;
5616         /* clear state */
5617         count += 2;
5618
5619         return count;
5620 }
5621
5622 void si_get_csb_buffer(struct radeon_device *rdev, volatile u32 *buffer)
5623 {
5624         u32 count = 0, i;
5625         const struct cs_section_def *sect = NULL;
5626         const struct cs_extent_def *ext = NULL;
5627
5628         if (rdev->rlc.cs_data == NULL)
5629                 return;
5630         if (buffer == NULL)
5631                 return;
5632
5633         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
5634         buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
5635
5636         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CONTEXT_CONTROL, 1));
5637         buffer[count++] = cpu_to_le32(0x80000000);
5638         buffer[count++] = cpu_to_le32(0x80000000);
5639
5640         for (sect = rdev->rlc.cs_data; sect->section != NULL; ++sect) {
5641                 for (ext = sect->section; ext->extent != NULL; ++ext) {
5642                         if (sect->id == SECT_CONTEXT) {
5643                                 buffer[count++] =
5644                                         cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, ext->reg_count));
5645                                 buffer[count++] = cpu_to_le32(ext->reg_index - 0xa000);
5646                                 for (i = 0; i < ext->reg_count; i++)
5647                                         buffer[count++] = cpu_to_le32(ext->extent[i]);
5648                         } else {
5649                                 return;
5650                         }
5651                 }
5652         }
5653
5654         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, 1));
5655         buffer[count++] = cpu_to_le32(PA_SC_RASTER_CONFIG - PACKET3_SET_CONTEXT_REG_START);
5656         switch (rdev->family) {
5657         case CHIP_TAHITI:
5658         case CHIP_PITCAIRN:
5659                 buffer[count++] = cpu_to_le32(0x2a00126a);
5660                 break;
5661         case CHIP_VERDE:
5662                 buffer[count++] = cpu_to_le32(0x0000124a);
5663                 break;
5664         case CHIP_OLAND:
5665                 buffer[count++] = cpu_to_le32(0x00000082);
5666                 break;
5667         case CHIP_HAINAN:
5668                 buffer[count++] = cpu_to_le32(0x00000000);
5669                 break;
5670         default:
5671                 buffer[count++] = cpu_to_le32(0x00000000);
5672                 break;
5673         }
5674
5675         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
5676         buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_END_CLEAR_STATE);
5677
5678         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CLEAR_STATE, 0));
5679         buffer[count++] = cpu_to_le32(0);
5680 }
5681
5682 static void si_init_pg(struct radeon_device *rdev)
5683 {
5684         if (rdev->pg_flags) {
5685                 if (rdev->pg_flags & RADEON_PG_SUPPORT_SDMA) {
5686                         si_init_dma_pg(rdev);
5687                 }
5688                 si_init_ao_cu_mask(rdev);
5689                 if (rdev->pg_flags & RADEON_PG_SUPPORT_GFX_PG) {
5690                         si_init_gfx_cgpg(rdev);
5691                 } else {
5692                         WREG32(RLC_SAVE_AND_RESTORE_BASE, rdev->rlc.save_restore_gpu_addr >> 8);
5693                         WREG32(RLC_CLEAR_STATE_RESTORE_BASE, rdev->rlc.clear_state_gpu_addr >> 8);
5694                 }
5695                 si_enable_dma_pg(rdev, true);
5696                 si_enable_gfx_cgpg(rdev, true);
5697         } else {
5698                 WREG32(RLC_SAVE_AND_RESTORE_BASE, rdev->rlc.save_restore_gpu_addr >> 8);
5699                 WREG32(RLC_CLEAR_STATE_RESTORE_BASE, rdev->rlc.clear_state_gpu_addr >> 8);
5700         }
5701 }
5702
5703 static void si_fini_pg(struct radeon_device *rdev)
5704 {
5705         if (rdev->pg_flags) {
5706                 si_enable_dma_pg(rdev, false);
5707                 si_enable_gfx_cgpg(rdev, false);
5708         }
5709 }
5710
5711 /*
5712  * RLC
5713  */
5714 void si_rlc_reset(struct radeon_device *rdev)
5715 {
5716         u32 tmp = RREG32(GRBM_SOFT_RESET);
5717
5718         tmp |= SOFT_RESET_RLC;
5719         WREG32(GRBM_SOFT_RESET, tmp);
5720         udelay(50);
5721         tmp &= ~SOFT_RESET_RLC;
5722         WREG32(GRBM_SOFT_RESET, tmp);
5723         udelay(50);
5724 }
5725
5726 static void si_rlc_stop(struct radeon_device *rdev)
5727 {
5728         WREG32(RLC_CNTL, 0);
5729
5730         si_enable_gui_idle_interrupt(rdev, false);
5731
5732         si_wait_for_rlc_serdes(rdev);
5733 }
5734
5735 static void si_rlc_start(struct radeon_device *rdev)
5736 {
5737         WREG32(RLC_CNTL, RLC_ENABLE);
5738
5739         si_enable_gui_idle_interrupt(rdev, true);
5740
5741         udelay(50);
5742 }
5743
5744 static bool si_lbpw_supported(struct radeon_device *rdev)
5745 {
5746         u32 tmp;
5747
5748         /* Enable LBPW only for DDR3 */
5749         tmp = RREG32(MC_SEQ_MISC0);
5750         if ((tmp & 0xF0000000) == 0xB0000000)
5751                 return true;
5752         return false;
5753 }
5754
5755 static void si_enable_lbpw(struct radeon_device *rdev, bool enable)
5756 {
5757         u32 tmp;
5758
5759         tmp = RREG32(RLC_LB_CNTL);
5760         if (enable)
5761                 tmp |= LOAD_BALANCE_ENABLE;
5762         else
5763                 tmp &= ~LOAD_BALANCE_ENABLE;
5764         WREG32(RLC_LB_CNTL, tmp);
5765
5766         if (!enable) {
5767                 si_select_se_sh(rdev, 0xffffffff, 0xffffffff);
5768                 WREG32(SPI_LB_CU_MASK, 0x00ff);
5769         }
5770 }
5771
5772 static int si_rlc_resume(struct radeon_device *rdev)
5773 {
5774         u32 i;
5775
5776         if (!rdev->rlc_fw)
5777                 return -EINVAL;
5778
5779         si_rlc_stop(rdev);
5780
5781         si_rlc_reset(rdev);
5782
5783         si_init_pg(rdev);
5784
5785         si_init_cg(rdev);
5786
5787         WREG32(RLC_RL_BASE, 0);
5788         WREG32(RLC_RL_SIZE, 0);
5789         WREG32(RLC_LB_CNTL, 0);
5790         WREG32(RLC_LB_CNTR_MAX, 0xffffffff);
5791         WREG32(RLC_LB_CNTR_INIT, 0);
5792         WREG32(RLC_LB_INIT_CU_MASK, 0xffffffff);
5793
5794         WREG32(RLC_MC_CNTL, 0);
5795         WREG32(RLC_UCODE_CNTL, 0);
5796
5797         if (rdev->new_fw) {
5798                 const struct rlc_firmware_header_v1_0 *hdr =
5799                         (const struct rlc_firmware_header_v1_0 *)rdev->rlc_fw->data;
5800                 u32 fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
5801                 const __le32 *fw_data = (const __le32 *)
5802                         (rdev->rlc_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
5803
5804                 radeon_ucode_print_rlc_hdr(&hdr->header);
5805
5806                 for (i = 0; i < fw_size; i++) {
5807                         WREG32(RLC_UCODE_ADDR, i);
5808                         WREG32(RLC_UCODE_DATA, le32_to_cpup(fw_data++));
5809                 }
5810         } else {
5811                 const __be32 *fw_data =
5812                         (const __be32 *)rdev->rlc_fw->data;
5813                 for (i = 0; i < SI_RLC_UCODE_SIZE; i++) {
5814                         WREG32(RLC_UCODE_ADDR, i);
5815                         WREG32(RLC_UCODE_DATA, be32_to_cpup(fw_data++));
5816                 }
5817         }
5818         WREG32(RLC_UCODE_ADDR, 0);
5819
5820         si_enable_lbpw(rdev, si_lbpw_supported(rdev));
5821
5822         si_rlc_start(rdev);
5823
5824         return 0;
5825 }
5826
5827 static void si_enable_interrupts(struct radeon_device *rdev)
5828 {
5829         u32 ih_cntl = RREG32(IH_CNTL);
5830         u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
5831
5832         ih_cntl |= ENABLE_INTR;
5833         ih_rb_cntl |= IH_RB_ENABLE;
5834         WREG32(IH_CNTL, ih_cntl);
5835         WREG32(IH_RB_CNTL, ih_rb_cntl);
5836         rdev->ih.enabled = true;
5837 }
5838
5839 static void si_disable_interrupts(struct radeon_device *rdev)
5840 {
5841         u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
5842         u32 ih_cntl = RREG32(IH_CNTL);
5843
5844         ih_rb_cntl &= ~IH_RB_ENABLE;
5845         ih_cntl &= ~ENABLE_INTR;
5846         WREG32(IH_RB_CNTL, ih_rb_cntl);
5847         WREG32(IH_CNTL, ih_cntl);
5848         /* set rptr, wptr to 0 */
5849         WREG32(IH_RB_RPTR, 0);
5850         WREG32(IH_RB_WPTR, 0);
5851         rdev->ih.enabled = false;
5852         rdev->ih.rptr = 0;
5853 }
5854
5855 static void si_disable_interrupt_state(struct radeon_device *rdev)
5856 {
5857         u32 tmp;
5858
5859         tmp = RREG32(CP_INT_CNTL_RING0) &
5860                 (CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE);
5861         WREG32(CP_INT_CNTL_RING0, tmp);
5862         WREG32(CP_INT_CNTL_RING1, 0);
5863         WREG32(CP_INT_CNTL_RING2, 0);
5864         tmp = RREG32(DMA_CNTL + DMA0_REGISTER_OFFSET) & ~TRAP_ENABLE;
5865         WREG32(DMA_CNTL + DMA0_REGISTER_OFFSET, tmp);
5866         tmp = RREG32(DMA_CNTL + DMA1_REGISTER_OFFSET) & ~TRAP_ENABLE;
5867         WREG32(DMA_CNTL + DMA1_REGISTER_OFFSET, tmp);
5868         WREG32(GRBM_INT_CNTL, 0);
5869         WREG32(SRBM_INT_CNTL, 0);
5870         if (rdev->num_crtc >= 2) {
5871                 WREG32(INT_MASK + EVERGREEN_CRTC0_REGISTER_OFFSET, 0);
5872                 WREG32(INT_MASK + EVERGREEN_CRTC1_REGISTER_OFFSET, 0);
5873         }
5874         if (rdev->num_crtc >= 4) {
5875                 WREG32(INT_MASK + EVERGREEN_CRTC2_REGISTER_OFFSET, 0);
5876                 WREG32(INT_MASK + EVERGREEN_CRTC3_REGISTER_OFFSET, 0);
5877         }
5878         if (rdev->num_crtc >= 6) {
5879                 WREG32(INT_MASK + EVERGREEN_CRTC4_REGISTER_OFFSET, 0);
5880                 WREG32(INT_MASK + EVERGREEN_CRTC5_REGISTER_OFFSET, 0);
5881         }
5882
5883         if (rdev->num_crtc >= 2) {
5884                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET, 0);
5885                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET, 0);
5886         }
5887         if (rdev->num_crtc >= 4) {
5888                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET, 0);
5889                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET, 0);
5890         }
5891         if (rdev->num_crtc >= 6) {
5892                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET, 0);
5893                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET, 0);
5894         }
5895
5896         if (!ASIC_IS_NODCE(rdev)) {
5897                 WREG32(DAC_AUTODETECT_INT_CONTROL, 0);
5898
5899                 tmp = RREG32(DC_HPD1_INT_CONTROL) & DC_HPDx_INT_POLARITY;
5900                 WREG32(DC_HPD1_INT_CONTROL, tmp);
5901                 tmp = RREG32(DC_HPD2_INT_CONTROL) & DC_HPDx_INT_POLARITY;
5902                 WREG32(DC_HPD2_INT_CONTROL, tmp);
5903                 tmp = RREG32(DC_HPD3_INT_CONTROL) & DC_HPDx_INT_POLARITY;
5904                 WREG32(DC_HPD3_INT_CONTROL, tmp);
5905                 tmp = RREG32(DC_HPD4_INT_CONTROL) & DC_HPDx_INT_POLARITY;
5906                 WREG32(DC_HPD4_INT_CONTROL, tmp);
5907                 tmp = RREG32(DC_HPD5_INT_CONTROL) & DC_HPDx_INT_POLARITY;
5908                 WREG32(DC_HPD5_INT_CONTROL, tmp);
5909                 tmp = RREG32(DC_HPD6_INT_CONTROL) & DC_HPDx_INT_POLARITY;
5910                 WREG32(DC_HPD6_INT_CONTROL, tmp);
5911         }
5912 }
5913
5914 static int si_irq_init(struct radeon_device *rdev)
5915 {
5916         int ret = 0;
5917         int rb_bufsz;
5918         u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
5919
5920         /* allocate ring */
5921         ret = r600_ih_ring_alloc(rdev);
5922         if (ret)
5923                 return ret;
5924
5925         /* disable irqs */
5926         si_disable_interrupts(rdev);
5927
5928         /* init rlc */
5929         ret = si_rlc_resume(rdev);
5930         if (ret) {
5931                 r600_ih_ring_fini(rdev);
5932                 return ret;
5933         }
5934
5935         /* setup interrupt control */
5936         /* set dummy read address to ring address */
5937         WREG32(INTERRUPT_CNTL2, rdev->ih.gpu_addr >> 8);
5938         interrupt_cntl = RREG32(INTERRUPT_CNTL);
5939         /* IH_DUMMY_RD_OVERRIDE=0 - dummy read disabled with msi, enabled without msi
5940          * IH_DUMMY_RD_OVERRIDE=1 - dummy read controlled by IH_DUMMY_RD_EN
5941          */
5942         interrupt_cntl &= ~IH_DUMMY_RD_OVERRIDE;
5943         /* IH_REQ_NONSNOOP_EN=1 if ring is in non-cacheable memory, e.g., vram */
5944         interrupt_cntl &= ~IH_REQ_NONSNOOP_EN;
5945         WREG32(INTERRUPT_CNTL, interrupt_cntl);
5946
5947         WREG32(IH_RB_BASE, rdev->ih.gpu_addr >> 8);
5948         rb_bufsz = order_base_2(rdev->ih.ring_size / 4);
5949
5950         ih_rb_cntl = (IH_WPTR_OVERFLOW_ENABLE |
5951                       IH_WPTR_OVERFLOW_CLEAR |
5952                       (rb_bufsz << 1));
5953
5954         if (rdev->wb.enabled)
5955                 ih_rb_cntl |= IH_WPTR_WRITEBACK_ENABLE;
5956
5957         /* set the writeback address whether it's enabled or not */
5958         WREG32(IH_RB_WPTR_ADDR_LO, (rdev->wb.gpu_addr + R600_WB_IH_WPTR_OFFSET) & 0xFFFFFFFC);
5959         WREG32(IH_RB_WPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + R600_WB_IH_WPTR_OFFSET) & 0xFF);
5960
5961         WREG32(IH_RB_CNTL, ih_rb_cntl);
5962
5963         /* set rptr, wptr to 0 */
5964         WREG32(IH_RB_RPTR, 0);
5965         WREG32(IH_RB_WPTR, 0);
5966
5967         /* Default settings for IH_CNTL (disabled at first) */
5968         ih_cntl = MC_WRREQ_CREDIT(0x10) | MC_WR_CLEAN_CNT(0x10) | MC_VMID(0);
5969         /* RPTR_REARM only works if msi's are enabled */
5970         if (rdev->msi_enabled)
5971                 ih_cntl |= RPTR_REARM;
5972         WREG32(IH_CNTL, ih_cntl);
5973
5974         /* force the active interrupt state to all disabled */
5975         si_disable_interrupt_state(rdev);
5976
5977         pci_set_master(rdev->pdev);
5978
5979         /* enable irqs */
5980         si_enable_interrupts(rdev);
5981
5982         return ret;
5983 }
5984
5985 int si_irq_set(struct radeon_device *rdev)
5986 {
5987         u32 cp_int_cntl;
5988         u32 cp_int_cntl1 = 0, cp_int_cntl2 = 0;
5989         u32 crtc1 = 0, crtc2 = 0, crtc3 = 0, crtc4 = 0, crtc5 = 0, crtc6 = 0;
5990         u32 hpd1 = 0, hpd2 = 0, hpd3 = 0, hpd4 = 0, hpd5 = 0, hpd6 = 0;
5991         u32 grbm_int_cntl = 0;
5992         u32 dma_cntl, dma_cntl1;
5993         u32 thermal_int = 0;
5994
5995         if (!rdev->irq.installed) {
5996                 WARN(1, "Can't enable IRQ/MSI because no handler is installed\n");
5997                 return -EINVAL;
5998         }
5999         /* don't enable anything if the ih is disabled */
6000         if (!rdev->ih.enabled) {
6001                 si_disable_interrupts(rdev);
6002                 /* force the active interrupt state to all disabled */
6003                 si_disable_interrupt_state(rdev);
6004                 return 0;
6005         }
6006
6007         cp_int_cntl = RREG32(CP_INT_CNTL_RING0) &
6008                 (CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE);
6009
6010         if (!ASIC_IS_NODCE(rdev)) {
6011                 hpd1 = RREG32(DC_HPD1_INT_CONTROL) & ~(DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
6012                 hpd2 = RREG32(DC_HPD2_INT_CONTROL) & ~(DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
6013                 hpd3 = RREG32(DC_HPD3_INT_CONTROL) & ~(DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
6014                 hpd4 = RREG32(DC_HPD4_INT_CONTROL) & ~(DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
6015                 hpd5 = RREG32(DC_HPD5_INT_CONTROL) & ~(DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
6016                 hpd6 = RREG32(DC_HPD6_INT_CONTROL) & ~(DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
6017         }
6018
6019         dma_cntl = RREG32(DMA_CNTL + DMA0_REGISTER_OFFSET) & ~TRAP_ENABLE;
6020         dma_cntl1 = RREG32(DMA_CNTL + DMA1_REGISTER_OFFSET) & ~TRAP_ENABLE;
6021
6022         thermal_int = RREG32(CG_THERMAL_INT) &
6023                 ~(THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW);
6024
6025         /* enable CP interrupts on all rings */
6026         if (atomic_read(&rdev->irq.ring_int[RADEON_RING_TYPE_GFX_INDEX])) {
6027                 DRM_DEBUG("si_irq_set: sw int gfx\n");
6028                 cp_int_cntl |= TIME_STAMP_INT_ENABLE;
6029         }
6030         if (atomic_read(&rdev->irq.ring_int[CAYMAN_RING_TYPE_CP1_INDEX])) {
6031                 DRM_DEBUG("si_irq_set: sw int cp1\n");
6032                 cp_int_cntl1 |= TIME_STAMP_INT_ENABLE;
6033         }
6034         if (atomic_read(&rdev->irq.ring_int[CAYMAN_RING_TYPE_CP2_INDEX])) {
6035                 DRM_DEBUG("si_irq_set: sw int cp2\n");
6036                 cp_int_cntl2 |= TIME_STAMP_INT_ENABLE;
6037         }
6038         if (atomic_read(&rdev->irq.ring_int[R600_RING_TYPE_DMA_INDEX])) {
6039                 DRM_DEBUG("si_irq_set: sw int dma\n");
6040                 dma_cntl |= TRAP_ENABLE;
6041         }
6042
6043         if (atomic_read(&rdev->irq.ring_int[CAYMAN_RING_TYPE_DMA1_INDEX])) {
6044                 DRM_DEBUG("si_irq_set: sw int dma1\n");
6045                 dma_cntl1 |= TRAP_ENABLE;
6046         }
6047         if (rdev->irq.crtc_vblank_int[0] ||
6048             atomic_read(&rdev->irq.pflip[0])) {
6049                 DRM_DEBUG("si_irq_set: vblank 0\n");
6050                 crtc1 |= VBLANK_INT_MASK;
6051         }
6052         if (rdev->irq.crtc_vblank_int[1] ||
6053             atomic_read(&rdev->irq.pflip[1])) {
6054                 DRM_DEBUG("si_irq_set: vblank 1\n");
6055                 crtc2 |= VBLANK_INT_MASK;
6056         }
6057         if (rdev->irq.crtc_vblank_int[2] ||
6058             atomic_read(&rdev->irq.pflip[2])) {
6059                 DRM_DEBUG("si_irq_set: vblank 2\n");
6060                 crtc3 |= VBLANK_INT_MASK;
6061         }
6062         if (rdev->irq.crtc_vblank_int[3] ||
6063             atomic_read(&rdev->irq.pflip[3])) {
6064                 DRM_DEBUG("si_irq_set: vblank 3\n");
6065                 crtc4 |= VBLANK_INT_MASK;
6066         }
6067         if (rdev->irq.crtc_vblank_int[4] ||
6068             atomic_read(&rdev->irq.pflip[4])) {
6069                 DRM_DEBUG("si_irq_set: vblank 4\n");
6070                 crtc5 |= VBLANK_INT_MASK;
6071         }
6072         if (rdev->irq.crtc_vblank_int[5] ||
6073             atomic_read(&rdev->irq.pflip[5])) {
6074                 DRM_DEBUG("si_irq_set: vblank 5\n");
6075                 crtc6 |= VBLANK_INT_MASK;
6076         }
6077         if (rdev->irq.hpd[0]) {
6078                 DRM_DEBUG("si_irq_set: hpd 1\n");
6079                 hpd1 |= DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN;
6080         }
6081         if (rdev->irq.hpd[1]) {
6082                 DRM_DEBUG("si_irq_set: hpd 2\n");
6083                 hpd2 |= DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN;
6084         }
6085         if (rdev->irq.hpd[2]) {
6086                 DRM_DEBUG("si_irq_set: hpd 3\n");
6087                 hpd3 |= DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN;
6088         }
6089         if (rdev->irq.hpd[3]) {
6090                 DRM_DEBUG("si_irq_set: hpd 4\n");
6091                 hpd4 |= DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN;
6092         }
6093         if (rdev->irq.hpd[4]) {
6094                 DRM_DEBUG("si_irq_set: hpd 5\n");
6095                 hpd5 |= DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN;
6096         }
6097         if (rdev->irq.hpd[5]) {
6098                 DRM_DEBUG("si_irq_set: hpd 6\n");
6099                 hpd6 |= DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN;
6100         }
6101
6102         WREG32(CP_INT_CNTL_RING0, cp_int_cntl);
6103         WREG32(CP_INT_CNTL_RING1, cp_int_cntl1);
6104         WREG32(CP_INT_CNTL_RING2, cp_int_cntl2);
6105
6106         WREG32(DMA_CNTL + DMA0_REGISTER_OFFSET, dma_cntl);
6107         WREG32(DMA_CNTL + DMA1_REGISTER_OFFSET, dma_cntl1);
6108
6109         WREG32(GRBM_INT_CNTL, grbm_int_cntl);
6110
6111         if (rdev->irq.dpm_thermal) {
6112                 DRM_DEBUG("dpm thermal\n");
6113                 thermal_int |= THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW;
6114         }
6115
6116         if (rdev->num_crtc >= 2) {
6117                 WREG32(INT_MASK + EVERGREEN_CRTC0_REGISTER_OFFSET, crtc1);
6118                 WREG32(INT_MASK + EVERGREEN_CRTC1_REGISTER_OFFSET, crtc2);
6119         }
6120         if (rdev->num_crtc >= 4) {
6121                 WREG32(INT_MASK + EVERGREEN_CRTC2_REGISTER_OFFSET, crtc3);
6122                 WREG32(INT_MASK + EVERGREEN_CRTC3_REGISTER_OFFSET, crtc4);
6123         }
6124         if (rdev->num_crtc >= 6) {
6125                 WREG32(INT_MASK + EVERGREEN_CRTC4_REGISTER_OFFSET, crtc5);
6126                 WREG32(INT_MASK + EVERGREEN_CRTC5_REGISTER_OFFSET, crtc6);
6127         }
6128
6129         if (rdev->num_crtc >= 2) {
6130                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET,
6131                        GRPH_PFLIP_INT_MASK);
6132                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET,
6133                        GRPH_PFLIP_INT_MASK);
6134         }
6135         if (rdev->num_crtc >= 4) {
6136                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET,
6137                        GRPH_PFLIP_INT_MASK);
6138                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET,
6139                        GRPH_PFLIP_INT_MASK);
6140         }
6141         if (rdev->num_crtc >= 6) {
6142                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET,
6143                        GRPH_PFLIP_INT_MASK);
6144                 WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET,
6145                        GRPH_PFLIP_INT_MASK);
6146         }
6147
6148         if (!ASIC_IS_NODCE(rdev)) {
6149                 WREG32(DC_HPD1_INT_CONTROL, hpd1);
6150                 WREG32(DC_HPD2_INT_CONTROL, hpd2);
6151                 WREG32(DC_HPD3_INT_CONTROL, hpd3);
6152                 WREG32(DC_HPD4_INT_CONTROL, hpd4);
6153                 WREG32(DC_HPD5_INT_CONTROL, hpd5);
6154                 WREG32(DC_HPD6_INT_CONTROL, hpd6);
6155         }
6156
6157         WREG32(CG_THERMAL_INT, thermal_int);
6158
6159         /* posting read */
6160         RREG32(SRBM_STATUS);
6161
6162         return 0;
6163 }
6164
6165 static inline void si_irq_ack(struct radeon_device *rdev)
6166 {
6167         u32 tmp;
6168
6169         if (ASIC_IS_NODCE(rdev))
6170                 return;
6171
6172         rdev->irq.stat_regs.evergreen.disp_int = RREG32(DISP_INTERRUPT_STATUS);
6173         rdev->irq.stat_regs.evergreen.disp_int_cont = RREG32(DISP_INTERRUPT_STATUS_CONTINUE);
6174         rdev->irq.stat_regs.evergreen.disp_int_cont2 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE2);
6175         rdev->irq.stat_regs.evergreen.disp_int_cont3 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE3);
6176         rdev->irq.stat_regs.evergreen.disp_int_cont4 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE4);
6177         rdev->irq.stat_regs.evergreen.disp_int_cont5 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE5);
6178         rdev->irq.stat_regs.evergreen.d1grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET);
6179         rdev->irq.stat_regs.evergreen.d2grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET);
6180         if (rdev->num_crtc >= 4) {
6181                 rdev->irq.stat_regs.evergreen.d3grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET);
6182                 rdev->irq.stat_regs.evergreen.d4grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET);
6183         }
6184         if (rdev->num_crtc >= 6) {
6185                 rdev->irq.stat_regs.evergreen.d5grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET);
6186                 rdev->irq.stat_regs.evergreen.d6grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET);
6187         }
6188
6189         if (rdev->irq.stat_regs.evergreen.d1grph_int & GRPH_PFLIP_INT_OCCURRED)
6190                 WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
6191         if (rdev->irq.stat_regs.evergreen.d2grph_int & GRPH_PFLIP_INT_OCCURRED)
6192                 WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
6193         if (rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VBLANK_INTERRUPT)
6194                 WREG32(VBLANK_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET, VBLANK_ACK);
6195         if (rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VLINE_INTERRUPT)
6196                 WREG32(VLINE_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET, VLINE_ACK);
6197         if (rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VBLANK_INTERRUPT)
6198                 WREG32(VBLANK_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET, VBLANK_ACK);
6199         if (rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VLINE_INTERRUPT)
6200                 WREG32(VLINE_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET, VLINE_ACK);
6201
6202         if (rdev->num_crtc >= 4) {
6203                 if (rdev->irq.stat_regs.evergreen.d3grph_int & GRPH_PFLIP_INT_OCCURRED)
6204                         WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
6205                 if (rdev->irq.stat_regs.evergreen.d4grph_int & GRPH_PFLIP_INT_OCCURRED)
6206                         WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
6207                 if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VBLANK_INTERRUPT)
6208                         WREG32(VBLANK_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET, VBLANK_ACK);
6209                 if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VLINE_INTERRUPT)
6210                         WREG32(VLINE_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET, VLINE_ACK);
6211                 if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VBLANK_INTERRUPT)
6212                         WREG32(VBLANK_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET, VBLANK_ACK);
6213                 if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VLINE_INTERRUPT)
6214                         WREG32(VLINE_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET, VLINE_ACK);
6215         }
6216
6217         if (rdev->num_crtc >= 6) {
6218                 if (rdev->irq.stat_regs.evergreen.d5grph_int & GRPH_PFLIP_INT_OCCURRED)
6219                         WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
6220                 if (rdev->irq.stat_regs.evergreen.d6grph_int & GRPH_PFLIP_INT_OCCURRED)
6221                         WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
6222                 if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VBLANK_INTERRUPT)
6223                         WREG32(VBLANK_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET, VBLANK_ACK);
6224                 if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VLINE_INTERRUPT)
6225                         WREG32(VLINE_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET, VLINE_ACK);
6226                 if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VBLANK_INTERRUPT)
6227                         WREG32(VBLANK_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET, VBLANK_ACK);
6228                 if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VLINE_INTERRUPT)
6229                         WREG32(VLINE_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET, VLINE_ACK);
6230         }
6231
6232         if (rdev->irq.stat_regs.evergreen.disp_int & DC_HPD1_INTERRUPT) {
6233                 tmp = RREG32(DC_HPD1_INT_CONTROL);
6234                 tmp |= DC_HPDx_INT_ACK;
6235                 WREG32(DC_HPD1_INT_CONTROL, tmp);
6236         }
6237         if (rdev->irq.stat_regs.evergreen.disp_int_cont & DC_HPD2_INTERRUPT) {
6238                 tmp = RREG32(DC_HPD2_INT_CONTROL);
6239                 tmp |= DC_HPDx_INT_ACK;
6240                 WREG32(DC_HPD2_INT_CONTROL, tmp);
6241         }
6242         if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & DC_HPD3_INTERRUPT) {
6243                 tmp = RREG32(DC_HPD3_INT_CONTROL);
6244                 tmp |= DC_HPDx_INT_ACK;
6245                 WREG32(DC_HPD3_INT_CONTROL, tmp);
6246         }
6247         if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & DC_HPD4_INTERRUPT) {
6248                 tmp = RREG32(DC_HPD4_INT_CONTROL);
6249                 tmp |= DC_HPDx_INT_ACK;
6250                 WREG32(DC_HPD4_INT_CONTROL, tmp);
6251         }
6252         if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & DC_HPD5_INTERRUPT) {
6253                 tmp = RREG32(DC_HPD5_INT_CONTROL);
6254                 tmp |= DC_HPDx_INT_ACK;
6255                 WREG32(DC_HPD5_INT_CONTROL, tmp);
6256         }
6257         if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & DC_HPD6_INTERRUPT) {
6258                 tmp = RREG32(DC_HPD5_INT_CONTROL);
6259                 tmp |= DC_HPDx_INT_ACK;
6260                 WREG32(DC_HPD6_INT_CONTROL, tmp);
6261         }
6262
6263         if (rdev->irq.stat_regs.evergreen.disp_int & DC_HPD1_RX_INTERRUPT) {
6264                 tmp = RREG32(DC_HPD1_INT_CONTROL);
6265                 tmp |= DC_HPDx_RX_INT_ACK;
6266                 WREG32(DC_HPD1_INT_CONTROL, tmp);
6267         }
6268         if (rdev->irq.stat_regs.evergreen.disp_int_cont & DC_HPD2_RX_INTERRUPT) {
6269                 tmp = RREG32(DC_HPD2_INT_CONTROL);
6270                 tmp |= DC_HPDx_RX_INT_ACK;
6271                 WREG32(DC_HPD2_INT_CONTROL, tmp);
6272         }
6273         if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & DC_HPD3_RX_INTERRUPT) {
6274                 tmp = RREG32(DC_HPD3_INT_CONTROL);
6275                 tmp |= DC_HPDx_RX_INT_ACK;
6276                 WREG32(DC_HPD3_INT_CONTROL, tmp);
6277         }
6278         if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & DC_HPD4_RX_INTERRUPT) {
6279                 tmp = RREG32(DC_HPD4_INT_CONTROL);
6280                 tmp |= DC_HPDx_RX_INT_ACK;
6281                 WREG32(DC_HPD4_INT_CONTROL, tmp);
6282         }
6283         if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & DC_HPD5_RX_INTERRUPT) {
6284                 tmp = RREG32(DC_HPD5_INT_CONTROL);
6285                 tmp |= DC_HPDx_RX_INT_ACK;
6286                 WREG32(DC_HPD5_INT_CONTROL, tmp);
6287         }
6288         if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & DC_HPD6_RX_INTERRUPT) {
6289                 tmp = RREG32(DC_HPD5_INT_CONTROL);
6290                 tmp |= DC_HPDx_RX_INT_ACK;
6291                 WREG32(DC_HPD6_INT_CONTROL, tmp);
6292         }
6293 }
6294
6295 static void si_irq_disable(struct radeon_device *rdev)
6296 {
6297         si_disable_interrupts(rdev);
6298         /* Wait and acknowledge irq */
6299         mdelay(1);
6300         si_irq_ack(rdev);
6301         si_disable_interrupt_state(rdev);
6302 }
6303
6304 static void si_irq_suspend(struct radeon_device *rdev)
6305 {
6306         si_irq_disable(rdev);
6307         si_rlc_stop(rdev);
6308 }
6309
6310 static void si_irq_fini(struct radeon_device *rdev)
6311 {
6312         si_irq_suspend(rdev);
6313         r600_ih_ring_fini(rdev);
6314 }
6315
6316 static inline u32 si_get_ih_wptr(struct radeon_device *rdev)
6317 {
6318         u32 wptr, tmp;
6319
6320         if (rdev->wb.enabled)
6321                 wptr = le32_to_cpu(rdev->wb.wb[R600_WB_IH_WPTR_OFFSET/4]);
6322         else
6323                 wptr = RREG32(IH_RB_WPTR);
6324
6325         if (wptr & RB_OVERFLOW) {
6326                 wptr &= ~RB_OVERFLOW;
6327                 /* When a ring buffer overflow happen start parsing interrupt
6328                  * from the last not overwritten vector (wptr + 16). Hopefully
6329                  * this should allow us to catchup.
6330                  */
6331                 dev_warn(rdev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
6332                          wptr, rdev->ih.rptr, (wptr + 16) & rdev->ih.ptr_mask);
6333                 rdev->ih.rptr = (wptr + 16) & rdev->ih.ptr_mask;
6334                 tmp = RREG32(IH_RB_CNTL);
6335                 tmp |= IH_WPTR_OVERFLOW_CLEAR;
6336                 WREG32(IH_RB_CNTL, tmp);
6337         }
6338         return (wptr & rdev->ih.ptr_mask);
6339 }
6340
6341 /*        SI IV Ring
6342  * Each IV ring entry is 128 bits:
6343  * [7:0]    - interrupt source id
6344  * [31:8]   - reserved
6345  * [59:32]  - interrupt source data
6346  * [63:60]  - reserved
6347  * [71:64]  - RINGID
6348  * [79:72]  - VMID
6349  * [127:80] - reserved
6350  */
6351 int si_irq_process(struct radeon_device *rdev)
6352 {
6353         u32 wptr;
6354         u32 rptr;
6355         u32 src_id, src_data, ring_id;
6356         u32 ring_index;
6357         bool queue_hotplug = false;
6358         bool queue_dp = false;
6359         bool queue_thermal = false;
6360         u32 status, addr;
6361
6362         if (!rdev->ih.enabled || rdev->shutdown)
6363                 return IRQ_NONE;
6364
6365         wptr = si_get_ih_wptr(rdev);
6366
6367 restart_ih:
6368         /* is somebody else already processing irqs? */
6369         if (atomic_xchg(&rdev->ih.lock, 1))
6370                 return IRQ_NONE;
6371
6372         rptr = rdev->ih.rptr;
6373         DRM_DEBUG("si_irq_process start: rptr %d, wptr %d\n", rptr, wptr);
6374
6375         /* Order reading of wptr vs. reading of IH ring data */
6376         rmb();
6377
6378         /* display interrupts */
6379         si_irq_ack(rdev);
6380
6381         while (rptr != wptr) {
6382                 /* wptr/rptr are in bytes! */
6383                 ring_index = rptr / 4;
6384                 src_id =  le32_to_cpu(rdev->ih.ring[ring_index]) & 0xff;
6385                 src_data = le32_to_cpu(rdev->ih.ring[ring_index + 1]) & 0xfffffff;
6386                 ring_id = le32_to_cpu(rdev->ih.ring[ring_index + 2]) & 0xff;
6387
6388                 switch (src_id) {
6389                 case 1: /* D1 vblank/vline */
6390                         switch (src_data) {
6391                         case 0: /* D1 vblank */
6392                                 if (!(rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VBLANK_INTERRUPT))
6393                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6394
6395                                 if (rdev->irq.crtc_vblank_int[0]) {
6396                                         drm_handle_vblank(rdev->ddev, 0);
6397                                         rdev->pm.vblank_sync = true;
6398                                         wake_up(&rdev->irq.vblank_queue);
6399                                 }
6400                                 if (atomic_read(&rdev->irq.pflip[0]))
6401                                         radeon_crtc_handle_vblank(rdev, 0);
6402                                 rdev->irq.stat_regs.evergreen.disp_int &= ~LB_D1_VBLANK_INTERRUPT;
6403                                 DRM_DEBUG("IH: D1 vblank\n");
6404
6405                                 break;
6406                         case 1: /* D1 vline */
6407                                 if (!(rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VLINE_INTERRUPT))
6408                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6409
6410                                 rdev->irq.stat_regs.evergreen.disp_int &= ~LB_D1_VLINE_INTERRUPT;
6411                                 DRM_DEBUG("IH: D1 vline\n");
6412
6413                                 break;
6414                         default:
6415                                 DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6416                                 break;
6417                         }
6418                         break;
6419                 case 2: /* D2 vblank/vline */
6420                         switch (src_data) {
6421                         case 0: /* D2 vblank */
6422                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VBLANK_INTERRUPT))
6423                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6424
6425                                 if (rdev->irq.crtc_vblank_int[1]) {
6426                                         drm_handle_vblank(rdev->ddev, 1);
6427                                         rdev->pm.vblank_sync = true;
6428                                         wake_up(&rdev->irq.vblank_queue);
6429                                 }
6430                                 if (atomic_read(&rdev->irq.pflip[1]))
6431                                         radeon_crtc_handle_vblank(rdev, 1);
6432                                 rdev->irq.stat_regs.evergreen.disp_int_cont &= ~LB_D2_VBLANK_INTERRUPT;
6433                                 DRM_DEBUG("IH: D2 vblank\n");
6434
6435                                 break;
6436                         case 1: /* D2 vline */
6437                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VLINE_INTERRUPT))
6438                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6439
6440                                 rdev->irq.stat_regs.evergreen.disp_int_cont &= ~LB_D2_VLINE_INTERRUPT;
6441                                 DRM_DEBUG("IH: D2 vline\n");
6442
6443                                 break;
6444                         default:
6445                                 DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6446                                 break;
6447                         }
6448                         break;
6449                 case 3: /* D3 vblank/vline */
6450                         switch (src_data) {
6451                         case 0: /* D3 vblank */
6452                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VBLANK_INTERRUPT))
6453                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6454
6455                                 if (rdev->irq.crtc_vblank_int[2]) {
6456                                         drm_handle_vblank(rdev->ddev, 2);
6457                                         rdev->pm.vblank_sync = true;
6458                                         wake_up(&rdev->irq.vblank_queue);
6459                                 }
6460                                 if (atomic_read(&rdev->irq.pflip[2]))
6461                                         radeon_crtc_handle_vblank(rdev, 2);
6462                                 rdev->irq.stat_regs.evergreen.disp_int_cont2 &= ~LB_D3_VBLANK_INTERRUPT;
6463                                 DRM_DEBUG("IH: D3 vblank\n");
6464
6465                                 break;
6466                         case 1: /* D3 vline */
6467                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VLINE_INTERRUPT))
6468                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6469
6470                                 rdev->irq.stat_regs.evergreen.disp_int_cont2 &= ~LB_D3_VLINE_INTERRUPT;
6471                                 DRM_DEBUG("IH: D3 vline\n");
6472
6473                                 break;
6474                         default:
6475                                 DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6476                                 break;
6477                         }
6478                         break;
6479                 case 4: /* D4 vblank/vline */
6480                         switch (src_data) {
6481                         case 0: /* D4 vblank */
6482                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VBLANK_INTERRUPT))
6483                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6484
6485                                 if (rdev->irq.crtc_vblank_int[3]) {
6486                                         drm_handle_vblank(rdev->ddev, 3);
6487                                         rdev->pm.vblank_sync = true;
6488                                         wake_up(&rdev->irq.vblank_queue);
6489                                 }
6490                                 if (atomic_read(&rdev->irq.pflip[3]))
6491                                         radeon_crtc_handle_vblank(rdev, 3);
6492                                 rdev->irq.stat_regs.evergreen.disp_int_cont3 &= ~LB_D4_VBLANK_INTERRUPT;
6493                                 DRM_DEBUG("IH: D4 vblank\n");
6494
6495                                 break;
6496                         case 1: /* D4 vline */
6497                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VLINE_INTERRUPT))
6498                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6499
6500                                 rdev->irq.stat_regs.evergreen.disp_int_cont3 &= ~LB_D4_VLINE_INTERRUPT;
6501                                 DRM_DEBUG("IH: D4 vline\n");
6502
6503                                 break;
6504                         default:
6505                                 DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6506                                 break;
6507                         }
6508                         break;
6509                 case 5: /* D5 vblank/vline */
6510                         switch (src_data) {
6511                         case 0: /* D5 vblank */
6512                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VBLANK_INTERRUPT))
6513                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6514
6515                                 if (rdev->irq.crtc_vblank_int[4]) {
6516                                         drm_handle_vblank(rdev->ddev, 4);
6517                                         rdev->pm.vblank_sync = true;
6518                                         wake_up(&rdev->irq.vblank_queue);
6519                                 }
6520                                 if (atomic_read(&rdev->irq.pflip[4]))
6521                                         radeon_crtc_handle_vblank(rdev, 4);
6522                                 rdev->irq.stat_regs.evergreen.disp_int_cont4 &= ~LB_D5_VBLANK_INTERRUPT;
6523                                 DRM_DEBUG("IH: D5 vblank\n");
6524
6525                                 break;
6526                         case 1: /* D5 vline */
6527                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VLINE_INTERRUPT))
6528                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6529
6530                                 rdev->irq.stat_regs.evergreen.disp_int_cont4 &= ~LB_D5_VLINE_INTERRUPT;
6531                                 DRM_DEBUG("IH: D5 vline\n");
6532
6533                                 break;
6534                         default:
6535                                 DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6536                                 break;
6537                         }
6538                         break;
6539                 case 6: /* D6 vblank/vline */
6540                         switch (src_data) {
6541                         case 0: /* D6 vblank */
6542                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VBLANK_INTERRUPT))
6543                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6544
6545                                 if (rdev->irq.crtc_vblank_int[5]) {
6546                                         drm_handle_vblank(rdev->ddev, 5);
6547                                         rdev->pm.vblank_sync = true;
6548                                         wake_up(&rdev->irq.vblank_queue);
6549                                 }
6550                                 if (atomic_read(&rdev->irq.pflip[5]))
6551                                         radeon_crtc_handle_vblank(rdev, 5);
6552                                 rdev->irq.stat_regs.evergreen.disp_int_cont5 &= ~LB_D6_VBLANK_INTERRUPT;
6553                                 DRM_DEBUG("IH: D6 vblank\n");
6554
6555                                 break;
6556                         case 1: /* D6 vline */
6557                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VLINE_INTERRUPT))
6558                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6559
6560                                 rdev->irq.stat_regs.evergreen.disp_int_cont5 &= ~LB_D6_VLINE_INTERRUPT;
6561                                 DRM_DEBUG("IH: D6 vline\n");
6562
6563                                 break;
6564                         default:
6565                                 DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6566                                 break;
6567                         }
6568                         break;
6569                 case 8: /* D1 page flip */
6570                 case 10: /* D2 page flip */
6571                 case 12: /* D3 page flip */
6572                 case 14: /* D4 page flip */
6573                 case 16: /* D5 page flip */
6574                 case 18: /* D6 page flip */
6575                         DRM_DEBUG("IH: D%d flip\n", ((src_id - 8) >> 1) + 1);
6576                         if (radeon_use_pflipirq > 0)
6577                                 radeon_crtc_handle_flip(rdev, (src_id - 8) >> 1);
6578                         break;
6579                 case 42: /* HPD hotplug */
6580                         switch (src_data) {
6581                         case 0:
6582                                 if (!(rdev->irq.stat_regs.evergreen.disp_int & DC_HPD1_INTERRUPT))
6583                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6584
6585                                 rdev->irq.stat_regs.evergreen.disp_int &= ~DC_HPD1_INTERRUPT;
6586                                 queue_hotplug = true;
6587                                 DRM_DEBUG("IH: HPD1\n");
6588
6589                                 break;
6590                         case 1:
6591                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont & DC_HPD2_INTERRUPT))
6592                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6593
6594                                 rdev->irq.stat_regs.evergreen.disp_int_cont &= ~DC_HPD2_INTERRUPT;
6595                                 queue_hotplug = true;
6596                                 DRM_DEBUG("IH: HPD2\n");
6597
6598                                 break;
6599                         case 2:
6600                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont2 & DC_HPD3_INTERRUPT))
6601                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6602
6603                                 rdev->irq.stat_regs.evergreen.disp_int_cont2 &= ~DC_HPD3_INTERRUPT;
6604                                 queue_hotplug = true;
6605                                 DRM_DEBUG("IH: HPD3\n");
6606
6607                                 break;
6608                         case 3:
6609                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont3 & DC_HPD4_INTERRUPT))
6610                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6611
6612                                 rdev->irq.stat_regs.evergreen.disp_int_cont3 &= ~DC_HPD4_INTERRUPT;
6613                                 queue_hotplug = true;
6614                                 DRM_DEBUG("IH: HPD4\n");
6615
6616                                 break;
6617                         case 4:
6618                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont4 & DC_HPD5_INTERRUPT))
6619                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6620
6621                                 rdev->irq.stat_regs.evergreen.disp_int_cont4 &= ~DC_HPD5_INTERRUPT;
6622                                 queue_hotplug = true;
6623                                 DRM_DEBUG("IH: HPD5\n");
6624
6625                                 break;
6626                         case 5:
6627                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont5 & DC_HPD6_INTERRUPT))
6628                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6629
6630                                 rdev->irq.stat_regs.evergreen.disp_int_cont5 &= ~DC_HPD6_INTERRUPT;
6631                                 queue_hotplug = true;
6632                                 DRM_DEBUG("IH: HPD6\n");
6633
6634                                 break;
6635                         case 6:
6636                                 if (!(rdev->irq.stat_regs.evergreen.disp_int & DC_HPD1_RX_INTERRUPT))
6637                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6638
6639                                 rdev->irq.stat_regs.evergreen.disp_int &= ~DC_HPD1_RX_INTERRUPT;
6640                                 queue_dp = true;
6641                                 DRM_DEBUG("IH: HPD_RX 1\n");
6642
6643                                 break;
6644                         case 7:
6645                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont & DC_HPD2_RX_INTERRUPT))
6646                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6647
6648                                 rdev->irq.stat_regs.evergreen.disp_int_cont &= ~DC_HPD2_RX_INTERRUPT;
6649                                 queue_dp = true;
6650                                 DRM_DEBUG("IH: HPD_RX 2\n");
6651
6652                                 break;
6653                         case 8:
6654                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont2 & DC_HPD3_RX_INTERRUPT))
6655                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6656
6657                                 rdev->irq.stat_regs.evergreen.disp_int_cont2 &= ~DC_HPD3_RX_INTERRUPT;
6658                                 queue_dp = true;
6659                                 DRM_DEBUG("IH: HPD_RX 3\n");
6660
6661                                 break;
6662                         case 9:
6663                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont3 & DC_HPD4_RX_INTERRUPT))
6664                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6665
6666                                 rdev->irq.stat_regs.evergreen.disp_int_cont3 &= ~DC_HPD4_RX_INTERRUPT;
6667                                 queue_dp = true;
6668                                 DRM_DEBUG("IH: HPD_RX 4\n");
6669
6670                                 break;
6671                         case 10:
6672                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont4 & DC_HPD5_RX_INTERRUPT))
6673                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6674
6675                                 rdev->irq.stat_regs.evergreen.disp_int_cont4 &= ~DC_HPD5_RX_INTERRUPT;
6676                                 queue_dp = true;
6677                                 DRM_DEBUG("IH: HPD_RX 5\n");
6678
6679                                 break;
6680                         case 11:
6681                                 if (!(rdev->irq.stat_regs.evergreen.disp_int_cont5 & DC_HPD6_RX_INTERRUPT))
6682                                         DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
6683
6684                                 rdev->irq.stat_regs.evergreen.disp_int_cont5 &= ~DC_HPD6_RX_INTERRUPT;
6685                                 queue_dp = true;
6686                                 DRM_DEBUG("IH: HPD_RX 6\n");
6687
6688                                 break;
6689                         default:
6690                                 DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6691                                 break;
6692                         }
6693                         break;
6694                 case 96:
6695                         DRM_ERROR("SRBM_READ_ERROR: 0x%x\n", RREG32(SRBM_READ_ERROR));
6696                         WREG32(SRBM_INT_ACK, 0x1);
6697                         break;
6698                 case 124: /* UVD */
6699                         DRM_DEBUG("IH: UVD int: 0x%08x\n", src_data);
6700                         radeon_fence_process(rdev, R600_RING_TYPE_UVD_INDEX);
6701                         break;
6702                 case 146:
6703                 case 147:
6704                         addr = RREG32(VM_CONTEXT1_PROTECTION_FAULT_ADDR);
6705                         status = RREG32(VM_CONTEXT1_PROTECTION_FAULT_STATUS);
6706                         /* reset addr and status */
6707                         WREG32_P(VM_CONTEXT1_CNTL2, 1, ~1);
6708                         if (addr == 0x0 && status == 0x0)
6709                                 break;
6710                         dev_err(rdev->dev, "GPU fault detected: %d 0x%08x\n", src_id, src_data);
6711                         dev_err(rdev->dev, "  VM_CONTEXT1_PROTECTION_FAULT_ADDR   0x%08X\n",
6712                                 addr);
6713                         dev_err(rdev->dev, "  VM_CONTEXT1_PROTECTION_FAULT_STATUS 0x%08X\n",
6714                                 status);
6715                         si_vm_decode_fault(rdev, status, addr);
6716                         break;
6717                 case 176: /* RINGID0 CP_INT */
6718                         radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
6719                         break;
6720                 case 177: /* RINGID1 CP_INT */
6721                         radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP1_INDEX);
6722                         break;
6723                 case 178: /* RINGID2 CP_INT */
6724                         radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP2_INDEX);
6725                         break;
6726                 case 181: /* CP EOP event */
6727                         DRM_DEBUG("IH: CP EOP\n");
6728                         switch (ring_id) {
6729                         case 0:
6730                                 radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
6731                                 break;
6732                         case 1:
6733                                 radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP1_INDEX);
6734                                 break;
6735                         case 2:
6736                                 radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP2_INDEX);
6737                                 break;
6738                         }
6739                         break;
6740                 case 224: /* DMA trap event */
6741                         DRM_DEBUG("IH: DMA trap\n");
6742                         radeon_fence_process(rdev, R600_RING_TYPE_DMA_INDEX);
6743                         break;
6744                 case 230: /* thermal low to high */
6745                         DRM_DEBUG("IH: thermal low to high\n");
6746                         rdev->pm.dpm.thermal.high_to_low = false;
6747                         queue_thermal = true;
6748                         break;
6749                 case 231: /* thermal high to low */
6750                         DRM_DEBUG("IH: thermal high to low\n");
6751                         rdev->pm.dpm.thermal.high_to_low = true;
6752                         queue_thermal = true;
6753                         break;
6754                 case 233: /* GUI IDLE */
6755                         DRM_DEBUG("IH: GUI idle\n");
6756                         break;
6757                 case 244: /* DMA trap event */
6758                         DRM_DEBUG("IH: DMA1 trap\n");
6759                         radeon_fence_process(rdev, CAYMAN_RING_TYPE_DMA1_INDEX);
6760                         break;
6761                 default:
6762                         DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
6763                         break;
6764                 }
6765
6766                 /* wptr/rptr are in bytes! */
6767                 rptr += 16;
6768                 rptr &= rdev->ih.ptr_mask;
6769                 WREG32(IH_RB_RPTR, rptr);
6770         }
6771         if (queue_dp)
6772                 schedule_work(&rdev->dp_work);
6773         if (queue_hotplug)
6774                 schedule_delayed_work(&rdev->hotplug_work, 0);
6775         if (queue_thermal && rdev->pm.dpm_enabled)
6776                 schedule_work(&rdev->pm.dpm.thermal.work);
6777         rdev->ih.rptr = rptr;
6778         atomic_set(&rdev->ih.lock, 0);
6779
6780         /* make sure wptr hasn't changed while processing */
6781         wptr = si_get_ih_wptr(rdev);
6782         if (wptr != rptr)
6783                 goto restart_ih;
6784
6785         return IRQ_HANDLED;
6786 }
6787
6788 /*
6789  * startup/shutdown callbacks
6790  */
6791 static void si_uvd_init(struct radeon_device *rdev)
6792 {
6793         int r;
6794
6795         if (!rdev->has_uvd)
6796                 return;
6797
6798         r = radeon_uvd_init(rdev);
6799         if (r) {
6800                 dev_err(rdev->dev, "failed UVD (%d) init.\n", r);
6801                 /*
6802                  * At this point rdev->uvd.vcpu_bo is NULL which trickles down
6803                  * to early fails uvd_v2_2_resume() and thus nothing happens
6804                  * there. So it is pointless to try to go through that code
6805                  * hence why we disable uvd here.
6806                  */
6807                 rdev->has_uvd = 0;
6808                 return;
6809         }
6810         rdev->ring[R600_RING_TYPE_UVD_INDEX].ring_obj = NULL;
6811         r600_ring_init(rdev, &rdev->ring[R600_RING_TYPE_UVD_INDEX], 4096);
6812 }
6813
6814 static void si_uvd_start(struct radeon_device *rdev)
6815 {
6816         int r;
6817
6818         if (!rdev->has_uvd)
6819                 return;
6820
6821         r = uvd_v2_2_resume(rdev);
6822         if (r) {
6823                 dev_err(rdev->dev, "failed UVD resume (%d).\n", r);
6824                 goto error;
6825         }
6826         r = radeon_fence_driver_start_ring(rdev, R600_RING_TYPE_UVD_INDEX);
6827         if (r) {
6828                 dev_err(rdev->dev, "failed initializing UVD fences (%d).\n", r);
6829                 goto error;
6830         }
6831         return;
6832
6833 error:
6834         rdev->ring[R600_RING_TYPE_UVD_INDEX].ring_size = 0;
6835 }
6836
6837 static void si_uvd_resume(struct radeon_device *rdev)
6838 {
6839         struct radeon_ring *ring;
6840         int r;
6841
6842         if (!rdev->has_uvd || !rdev->ring[R600_RING_TYPE_UVD_INDEX].ring_size)
6843                 return;
6844
6845         ring = &rdev->ring[R600_RING_TYPE_UVD_INDEX];
6846         r = radeon_ring_init(rdev, ring, ring->ring_size, 0, PACKET0(UVD_NO_OP, 0));
6847         if (r) {
6848                 dev_err(rdev->dev, "failed initializing UVD ring (%d).\n", r);
6849                 return;
6850         }
6851         r = uvd_v1_0_init(rdev);
6852         if (r) {
6853                 dev_err(rdev->dev, "failed initializing UVD (%d).\n", r);
6854                 return;
6855         }
6856 }
6857
6858 static void si_vce_init(struct radeon_device *rdev)
6859 {
6860         int r;
6861
6862         if (!rdev->has_vce)
6863                 return;
6864
6865         r = radeon_vce_init(rdev);
6866         if (r) {
6867                 dev_err(rdev->dev, "failed VCE (%d) init.\n", r);
6868                 /*
6869                  * At this point rdev->vce.vcpu_bo is NULL which trickles down
6870                  * to early fails si_vce_start() and thus nothing happens
6871                  * there. So it is pointless to try to go through that code
6872                  * hence why we disable vce here.
6873                  */
6874                 rdev->has_vce = 0;
6875                 return;
6876         }
6877         rdev->ring[TN_RING_TYPE_VCE1_INDEX].ring_obj = NULL;
6878         r600_ring_init(rdev, &rdev->ring[TN_RING_TYPE_VCE1_INDEX], 4096);
6879         rdev->ring[TN_RING_TYPE_VCE2_INDEX].ring_obj = NULL;
6880         r600_ring_init(rdev, &rdev->ring[TN_RING_TYPE_VCE2_INDEX], 4096);
6881 }
6882
6883 static void si_vce_start(struct radeon_device *rdev)
6884 {
6885         int r;
6886
6887         if (!rdev->has_vce)
6888                 return;
6889
6890         r = radeon_vce_resume(rdev);
6891         if (r) {
6892                 dev_err(rdev->dev, "failed VCE resume (%d).\n", r);
6893                 goto error;
6894         }
6895         r = vce_v1_0_resume(rdev);
6896         if (r) {
6897                 dev_err(rdev->dev, "failed VCE resume (%d).\n", r);
6898                 goto error;
6899         }
6900         r = radeon_fence_driver_start_ring(rdev, TN_RING_TYPE_VCE1_INDEX);
6901         if (r) {
6902                 dev_err(rdev->dev, "failed initializing VCE1 fences (%d).\n", r);
6903                 goto error;
6904         }
6905         r = radeon_fence_driver_start_ring(rdev, TN_RING_TYPE_VCE2_INDEX);
6906         if (r) {
6907                 dev_err(rdev->dev, "failed initializing VCE2 fences (%d).\n", r);
6908                 goto error;
6909         }
6910         return;
6911
6912 error:
6913         rdev->ring[TN_RING_TYPE_VCE1_INDEX].ring_size = 0;
6914         rdev->ring[TN_RING_TYPE_VCE2_INDEX].ring_size = 0;
6915 }
6916
6917 static void si_vce_resume(struct radeon_device *rdev)
6918 {
6919         struct radeon_ring *ring;
6920         int r;
6921
6922         if (!rdev->has_vce || !rdev->ring[TN_RING_TYPE_VCE1_INDEX].ring_size)
6923                 return;
6924
6925         ring = &rdev->ring[TN_RING_TYPE_VCE1_INDEX];
6926         r = radeon_ring_init(rdev, ring, ring->ring_size, 0, VCE_CMD_NO_OP);
6927         if (r) {
6928                 dev_err(rdev->dev, "failed initializing VCE1 ring (%d).\n", r);
6929                 return;
6930         }
6931         ring = &rdev->ring[TN_RING_TYPE_VCE2_INDEX];
6932         r = radeon_ring_init(rdev, ring, ring->ring_size, 0, VCE_CMD_NO_OP);
6933         if (r) {
6934                 dev_err(rdev->dev, "failed initializing VCE1 ring (%d).\n", r);
6935                 return;
6936         }
6937         r = vce_v1_0_init(rdev);
6938         if (r) {
6939                 dev_err(rdev->dev, "failed initializing VCE (%d).\n", r);
6940                 return;
6941         }
6942 }
6943
6944 static int si_startup(struct radeon_device *rdev)
6945 {
6946         struct radeon_ring *ring;
6947         int r;
6948
6949         /* enable pcie gen2/3 link */
6950         si_pcie_gen3_enable(rdev);
6951         /* enable aspm */
6952         si_program_aspm(rdev);
6953
6954         /* scratch needs to be initialized before MC */
6955         r = r600_vram_scratch_init(rdev);
6956         if (r)
6957                 return r;
6958
6959         si_mc_program(rdev);
6960
6961         if (!rdev->pm.dpm_enabled) {
6962                 r = si_mc_load_microcode(rdev);
6963                 if (r) {
6964                         DRM_ERROR("Failed to load MC firmware!\n");
6965                         return r;
6966                 }
6967         }
6968
6969         r = si_pcie_gart_enable(rdev);
6970         if (r)
6971                 return r;
6972         si_gpu_init(rdev);
6973
6974         /* allocate rlc buffers */
6975         if (rdev->family == CHIP_VERDE) {
6976                 rdev->rlc.reg_list = verde_rlc_save_restore_register_list;
6977                 rdev->rlc.reg_list_size =
6978                         (u32)ARRAY_SIZE(verde_rlc_save_restore_register_list);
6979         }
6980         rdev->rlc.cs_data = si_cs_data;
6981         r = sumo_rlc_init(rdev);
6982         if (r) {
6983                 DRM_ERROR("Failed to init rlc BOs!\n");
6984                 return r;
6985         }
6986
6987         /* allocate wb buffer */
6988         r = radeon_wb_init(rdev);
6989         if (r)
6990                 return r;
6991
6992         r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
6993         if (r) {
6994                 dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
6995                 return r;
6996         }
6997
6998         r = radeon_fence_driver_start_ring(rdev, CAYMAN_RING_TYPE_CP1_INDEX);
6999         if (r) {
7000                 dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
7001                 return r;
7002         }
7003
7004         r = radeon_fence_driver_start_ring(rdev, CAYMAN_RING_TYPE_CP2_INDEX);
7005         if (r) {
7006                 dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
7007                 return r;
7008         }
7009
7010         r = radeon_fence_driver_start_ring(rdev, R600_RING_TYPE_DMA_INDEX);
7011         if (r) {
7012                 dev_err(rdev->dev, "failed initializing DMA fences (%d).\n", r);
7013                 return r;
7014         }
7015
7016         r = radeon_fence_driver_start_ring(rdev, CAYMAN_RING_TYPE_DMA1_INDEX);
7017         if (r) {
7018                 dev_err(rdev->dev, "failed initializing DMA fences (%d).\n", r);
7019                 return r;
7020         }
7021
7022         si_uvd_start(rdev);
7023         si_vce_start(rdev);
7024
7025         /* Enable IRQ */
7026         if (!rdev->irq.installed) {
7027                 r = radeon_irq_kms_init(rdev);
7028                 if (r)
7029                         return r;
7030         }
7031
7032         r = si_irq_init(rdev);
7033         if (r) {
7034                 DRM_ERROR("radeon: IH init failed (%d).\n", r);
7035                 radeon_irq_kms_fini(rdev);
7036                 return r;
7037         }
7038         si_irq_set(rdev);
7039
7040         ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
7041         r = radeon_ring_init(rdev, ring, ring->ring_size, RADEON_WB_CP_RPTR_OFFSET,
7042                              RADEON_CP_PACKET2);
7043         if (r)
7044                 return r;
7045
7046         ring = &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX];
7047         r = radeon_ring_init(rdev, ring, ring->ring_size, RADEON_WB_CP1_RPTR_OFFSET,
7048                              RADEON_CP_PACKET2);
7049         if (r)
7050                 return r;
7051
7052         ring = &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX];
7053         r = radeon_ring_init(rdev, ring, ring->ring_size, RADEON_WB_CP2_RPTR_OFFSET,
7054                              RADEON_CP_PACKET2);
7055         if (r)
7056                 return r;
7057
7058         ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
7059         r = radeon_ring_init(rdev, ring, ring->ring_size, R600_WB_DMA_RPTR_OFFSET,
7060                              DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0, 0));
7061         if (r)
7062                 return r;
7063
7064         ring = &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX];
7065         r = radeon_ring_init(rdev, ring, ring->ring_size, CAYMAN_WB_DMA1_RPTR_OFFSET,
7066                              DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0, 0));
7067         if (r)
7068                 return r;
7069
7070         r = si_cp_load_microcode(rdev);
7071         if (r)
7072                 return r;
7073         r = si_cp_resume(rdev);
7074         if (r)
7075                 return r;
7076
7077         r = cayman_dma_resume(rdev);
7078         if (r)
7079                 return r;
7080
7081         si_uvd_resume(rdev);
7082         si_vce_resume(rdev);
7083
7084         r = radeon_ib_pool_init(rdev);
7085         if (r) {
7086                 dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
7087                 return r;
7088         }
7089
7090         r = radeon_vm_manager_init(rdev);
7091         if (r) {
7092                 dev_err(rdev->dev, "vm manager initialization failed (%d).\n", r);
7093                 return r;
7094         }
7095
7096         r = radeon_audio_init(rdev);
7097         if (r)
7098                 return r;
7099
7100         return 0;
7101 }
7102
7103 int si_resume(struct radeon_device *rdev)
7104 {
7105         int r;
7106
7107         /* Do not reset GPU before posting, on rv770 hw unlike on r500 hw,
7108          * posting will perform necessary task to bring back GPU into good
7109          * shape.
7110          */
7111         /* post card */
7112         atom_asic_init(rdev->mode_info.atom_context);
7113
7114         /* init golden registers */
7115         si_init_golden_registers(rdev);
7116
7117         if (rdev->pm.pm_method == PM_METHOD_DPM)
7118                 radeon_pm_resume(rdev);
7119
7120         rdev->accel_working = true;
7121         r = si_startup(rdev);
7122         if (r) {
7123                 DRM_ERROR("si startup failed on resume\n");
7124                 rdev->accel_working = false;
7125                 return r;
7126         }
7127
7128         return r;
7129
7130 }
7131
7132 int si_suspend(struct radeon_device *rdev)
7133 {
7134         radeon_pm_suspend(rdev);
7135         radeon_audio_fini(rdev);
7136         radeon_vm_manager_fini(rdev);
7137         si_cp_enable(rdev, false);
7138         cayman_dma_stop(rdev);
7139         if (rdev->has_uvd) {
7140                 uvd_v1_0_fini(rdev);
7141                 radeon_uvd_suspend(rdev);
7142         }
7143         if (rdev->has_vce)
7144                 radeon_vce_suspend(rdev);
7145         si_fini_pg(rdev);
7146         si_fini_cg(rdev);
7147         si_irq_suspend(rdev);
7148         radeon_wb_disable(rdev);
7149         si_pcie_gart_disable(rdev);
7150         return 0;
7151 }
7152
7153 /* Plan is to move initialization in that function and use
7154  * helper function so that radeon_device_init pretty much
7155  * do nothing more than calling asic specific function. This
7156  * should also allow to remove a bunch of callback function
7157  * like vram_info.
7158  */
7159 int si_init(struct radeon_device *rdev)
7160 {
7161         struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
7162         int r;
7163
7164         /* Read BIOS */
7165         if (!radeon_get_bios(rdev)) {
7166                 if (ASIC_IS_AVIVO(rdev))
7167                         return -EINVAL;
7168         }
7169         /* Must be an ATOMBIOS */
7170         if (!rdev->is_atom_bios) {
7171                 dev_err(rdev->dev, "Expecting atombios for cayman GPU\n");
7172                 return -EINVAL;
7173         }
7174         r = radeon_atombios_init(rdev);
7175         if (r)
7176                 return r;
7177
7178         /* Post card if necessary */
7179         if (!radeon_card_posted(rdev)) {
7180                 if (!rdev->bios) {
7181                         dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
7182                         return -EINVAL;
7183                 }
7184                 DRM_INFO("GPU not posted. posting now...\n");
7185                 atom_asic_init(rdev->mode_info.atom_context);
7186         }
7187         /* init golden registers */
7188         si_init_golden_registers(rdev);
7189         /* Initialize scratch registers */
7190         si_scratch_init(rdev);
7191         /* Initialize surface registers */
7192         radeon_surface_init(rdev);
7193         /* Initialize clocks */
7194         radeon_get_clock_info(rdev->ddev);
7195
7196         /* Fence driver */
7197         r = radeon_fence_driver_init(rdev);
7198         if (r)
7199                 return r;
7200
7201         /* initialize memory controller */
7202         r = si_mc_init(rdev);
7203         if (r)
7204                 return r;
7205         /* Memory manager */
7206         r = radeon_bo_init(rdev);
7207         if (r)
7208                 return r;
7209
7210         if (!rdev->me_fw || !rdev->pfp_fw || !rdev->ce_fw ||
7211             !rdev->rlc_fw || !rdev->mc_fw) {
7212                 r = si_init_microcode(rdev);
7213                 if (r) {
7214                         DRM_ERROR("Failed to load firmware!\n");
7215                         return r;
7216                 }
7217         }
7218
7219         /* Initialize power management */
7220         radeon_pm_init(rdev);
7221
7222         ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
7223         ring->ring_obj = NULL;
7224         r600_ring_init(rdev, ring, 1024 * 1024);
7225
7226         ring = &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX];
7227         ring->ring_obj = NULL;
7228         r600_ring_init(rdev, ring, 1024 * 1024);
7229
7230         ring = &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX];
7231         ring->ring_obj = NULL;
7232         r600_ring_init(rdev, ring, 1024 * 1024);
7233
7234         ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
7235         ring->ring_obj = NULL;
7236         r600_ring_init(rdev, ring, 64 * 1024);
7237
7238         ring = &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX];
7239         ring->ring_obj = NULL;
7240         r600_ring_init(rdev, ring, 64 * 1024);
7241
7242         si_uvd_init(rdev);
7243         si_vce_init(rdev);
7244
7245         rdev->ih.ring_obj = NULL;
7246         r600_ih_ring_init(rdev, 64 * 1024);
7247
7248         r = r600_pcie_gart_init(rdev);
7249         if (r)
7250                 return r;
7251
7252         rdev->accel_working = true;
7253         r = si_startup(rdev);
7254         if (r) {
7255                 dev_err(rdev->dev, "disabling GPU acceleration\n");
7256                 si_cp_fini(rdev);
7257                 cayman_dma_fini(rdev);
7258                 si_irq_fini(rdev);
7259                 sumo_rlc_fini(rdev);
7260                 radeon_wb_fini(rdev);
7261                 radeon_ib_pool_fini(rdev);
7262                 radeon_vm_manager_fini(rdev);
7263                 radeon_irq_kms_fini(rdev);
7264                 si_pcie_gart_fini(rdev);
7265                 rdev->accel_working = false;
7266         }
7267
7268         /* Don't start up if the MC ucode is missing.
7269          * The default clocks and voltages before the MC ucode
7270          * is loaded are not suffient for advanced operations.
7271          */
7272         if (!rdev->mc_fw) {
7273                 DRM_ERROR("radeon: MC ucode required for NI+.\n");
7274                 return -EINVAL;
7275         }
7276
7277         return 0;
7278 }
7279
7280 void si_fini(struct radeon_device *rdev)
7281 {
7282         radeon_pm_fini(rdev);
7283         si_cp_fini(rdev);
7284         cayman_dma_fini(rdev);
7285         si_fini_pg(rdev);
7286         si_fini_cg(rdev);
7287         si_irq_fini(rdev);
7288         sumo_rlc_fini(rdev);
7289         radeon_wb_fini(rdev);
7290         radeon_vm_manager_fini(rdev);
7291         radeon_ib_pool_fini(rdev);
7292         radeon_irq_kms_fini(rdev);
7293         if (rdev->has_uvd) {
7294                 uvd_v1_0_fini(rdev);
7295                 radeon_uvd_fini(rdev);
7296         }
7297         if (rdev->has_vce)
7298                 radeon_vce_fini(rdev);
7299         si_pcie_gart_fini(rdev);
7300         r600_vram_scratch_fini(rdev);
7301         radeon_gem_fini(rdev);
7302         radeon_fence_driver_fini(rdev);
7303         radeon_bo_fini(rdev);
7304         radeon_atombios_fini(rdev);
7305         kfree(rdev->bios);
7306         rdev->bios = NULL;
7307 }
7308
7309 /**
7310  * si_get_gpu_clock_counter - return GPU clock counter snapshot
7311  *
7312  * @rdev: radeon_device pointer
7313  *
7314  * Fetches a GPU clock counter snapshot (SI).
7315  * Returns the 64 bit clock counter snapshot.
7316  */
7317 uint64_t si_get_gpu_clock_counter(struct radeon_device *rdev)
7318 {
7319         uint64_t clock;
7320
7321         mutex_lock(&rdev->gpu_clock_mutex);
7322         WREG32(RLC_CAPTURE_GPU_CLOCK_COUNT, 1);
7323         clock = (uint64_t)RREG32(RLC_GPU_CLOCK_COUNT_LSB) |
7324                 ((uint64_t)RREG32(RLC_GPU_CLOCK_COUNT_MSB) << 32ULL);
7325         mutex_unlock(&rdev->gpu_clock_mutex);
7326         return clock;
7327 }
7328
7329 int si_set_uvd_clocks(struct radeon_device *rdev, u32 vclk, u32 dclk)
7330 {
7331         unsigned fb_div = 0, vclk_div = 0, dclk_div = 0;
7332         int r;
7333
7334         /* bypass vclk and dclk with bclk */
7335         WREG32_P(CG_UPLL_FUNC_CNTL_2,
7336                 VCLK_SRC_SEL(1) | DCLK_SRC_SEL(1),
7337                 ~(VCLK_SRC_SEL_MASK | DCLK_SRC_SEL_MASK));
7338
7339         /* put PLL in bypass mode */
7340         WREG32_P(CG_UPLL_FUNC_CNTL, UPLL_BYPASS_EN_MASK, ~UPLL_BYPASS_EN_MASK);
7341
7342         if (!vclk || !dclk) {
7343                 /* keep the Bypass mode */
7344                 return 0;
7345         }
7346
7347         r = radeon_uvd_calc_upll_dividers(rdev, vclk, dclk, 125000, 250000,
7348                                           16384, 0x03FFFFFF, 0, 128, 5,
7349                                           &fb_div, &vclk_div, &dclk_div);
7350         if (r)
7351                 return r;
7352
7353         /* set RESET_ANTI_MUX to 0 */
7354         WREG32_P(CG_UPLL_FUNC_CNTL_5, 0, ~RESET_ANTI_MUX_MASK);
7355
7356         /* set VCO_MODE to 1 */
7357         WREG32_P(CG_UPLL_FUNC_CNTL, UPLL_VCO_MODE_MASK, ~UPLL_VCO_MODE_MASK);
7358
7359         /* disable sleep mode */
7360         WREG32_P(CG_UPLL_FUNC_CNTL, 0, ~UPLL_SLEEP_MASK);
7361
7362         /* deassert UPLL_RESET */
7363         WREG32_P(CG_UPLL_FUNC_CNTL, 0, ~UPLL_RESET_MASK);
7364
7365         mdelay(1);
7366
7367         r = radeon_uvd_send_upll_ctlreq(rdev, CG_UPLL_FUNC_CNTL);
7368         if (r)
7369                 return r;
7370
7371         /* assert UPLL_RESET again */
7372         WREG32_P(CG_UPLL_FUNC_CNTL, UPLL_RESET_MASK, ~UPLL_RESET_MASK);
7373
7374         /* disable spread spectrum. */
7375         WREG32_P(CG_UPLL_SPREAD_SPECTRUM, 0, ~SSEN_MASK);
7376
7377         /* set feedback divider */
7378         WREG32_P(CG_UPLL_FUNC_CNTL_3, UPLL_FB_DIV(fb_div), ~UPLL_FB_DIV_MASK);
7379
7380         /* set ref divider to 0 */
7381         WREG32_P(CG_UPLL_FUNC_CNTL, 0, ~UPLL_REF_DIV_MASK);
7382
7383         if (fb_div < 307200)
7384                 WREG32_P(CG_UPLL_FUNC_CNTL_4, 0, ~UPLL_SPARE_ISPARE9);
7385         else
7386                 WREG32_P(CG_UPLL_FUNC_CNTL_4, UPLL_SPARE_ISPARE9, ~UPLL_SPARE_ISPARE9);
7387
7388         /* set PDIV_A and PDIV_B */
7389         WREG32_P(CG_UPLL_FUNC_CNTL_2,
7390                 UPLL_PDIV_A(vclk_div) | UPLL_PDIV_B(dclk_div),
7391                 ~(UPLL_PDIV_A_MASK | UPLL_PDIV_B_MASK));
7392
7393         /* give the PLL some time to settle */
7394         mdelay(15);
7395
7396         /* deassert PLL_RESET */
7397         WREG32_P(CG_UPLL_FUNC_CNTL, 0, ~UPLL_RESET_MASK);
7398
7399         mdelay(15);
7400
7401         /* switch from bypass mode to normal mode */
7402         WREG32_P(CG_UPLL_FUNC_CNTL, 0, ~UPLL_BYPASS_EN_MASK);
7403
7404         r = radeon_uvd_send_upll_ctlreq(rdev, CG_UPLL_FUNC_CNTL);
7405         if (r)
7406                 return r;
7407
7408         /* switch VCLK and DCLK selection */
7409         WREG32_P(CG_UPLL_FUNC_CNTL_2,
7410                 VCLK_SRC_SEL(2) | DCLK_SRC_SEL(2),
7411                 ~(VCLK_SRC_SEL_MASK | DCLK_SRC_SEL_MASK));
7412
7413         mdelay(100);
7414
7415         return 0;
7416 }
7417
7418 static void si_pcie_gen3_enable(struct radeon_device *rdev)
7419 {
7420         struct pci_dev *root = rdev->pdev->bus->self;
7421         int bridge_pos, gpu_pos;
7422         u32 speed_cntl, mask, current_data_rate;
7423         int ret, i;
7424         u16 tmp16;
7425
7426         if (pci_is_root_bus(rdev->pdev->bus))
7427                 return;
7428
7429         if (radeon_pcie_gen2 == 0)
7430                 return;
7431
7432         if (rdev->flags & RADEON_IS_IGP)
7433                 return;
7434
7435         if (!(rdev->flags & RADEON_IS_PCIE))
7436                 return;
7437
7438         ret = drm_pcie_get_speed_cap_mask(rdev->ddev, &mask);
7439         if (ret != 0)
7440                 return;
7441
7442         if (!(mask & (DRM_PCIE_SPEED_50 | DRM_PCIE_SPEED_80)))
7443                 return;
7444
7445         speed_cntl = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
7446         current_data_rate = (speed_cntl & LC_CURRENT_DATA_RATE_MASK) >>
7447                 LC_CURRENT_DATA_RATE_SHIFT;
7448         if (mask & DRM_PCIE_SPEED_80) {
7449                 if (current_data_rate == 2) {
7450                         DRM_INFO("PCIE gen 3 link speeds already enabled\n");
7451                         return;
7452                 }
7453                 DRM_INFO("enabling PCIE gen 3 link speeds, disable with radeon.pcie_gen2=0\n");
7454         } else if (mask & DRM_PCIE_SPEED_50) {
7455                 if (current_data_rate == 1) {
7456                         DRM_INFO("PCIE gen 2 link speeds already enabled\n");
7457                         return;
7458                 }
7459                 DRM_INFO("enabling PCIE gen 2 link speeds, disable with radeon.pcie_gen2=0\n");
7460         }
7461
7462         bridge_pos = pci_pcie_cap(root);
7463         if (!bridge_pos)
7464                 return;
7465
7466         gpu_pos = pci_pcie_cap(rdev->pdev);
7467         if (!gpu_pos)
7468                 return;
7469
7470         if (mask & DRM_PCIE_SPEED_80) {
7471                 /* re-try equalization if gen3 is not already enabled */
7472                 if (current_data_rate != 2) {
7473                         u16 bridge_cfg, gpu_cfg;
7474                         u16 bridge_cfg2, gpu_cfg2;
7475                         u32 max_lw, current_lw, tmp;
7476
7477                         pci_read_config_word(root, bridge_pos + PCI_EXP_LNKCTL, &bridge_cfg);
7478                         pci_read_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL, &gpu_cfg);
7479
7480                         tmp16 = bridge_cfg | PCI_EXP_LNKCTL_HAWD;
7481                         pci_write_config_word(root, bridge_pos + PCI_EXP_LNKCTL, tmp16);
7482
7483                         tmp16 = gpu_cfg | PCI_EXP_LNKCTL_HAWD;
7484                         pci_write_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL, tmp16);
7485
7486                         tmp = RREG32_PCIE(PCIE_LC_STATUS1);
7487                         max_lw = (tmp & LC_DETECTED_LINK_WIDTH_MASK) >> LC_DETECTED_LINK_WIDTH_SHIFT;
7488                         current_lw = (tmp & LC_OPERATING_LINK_WIDTH_MASK) >> LC_OPERATING_LINK_WIDTH_SHIFT;
7489
7490                         if (current_lw < max_lw) {
7491                                 tmp = RREG32_PCIE_PORT(PCIE_LC_LINK_WIDTH_CNTL);
7492                                 if (tmp & LC_RENEGOTIATION_SUPPORT) {
7493                                         tmp &= ~(LC_LINK_WIDTH_MASK | LC_UPCONFIGURE_DIS);
7494                                         tmp |= (max_lw << LC_LINK_WIDTH_SHIFT);
7495                                         tmp |= LC_UPCONFIGURE_SUPPORT | LC_RENEGOTIATE_EN | LC_RECONFIG_NOW;
7496                                         WREG32_PCIE_PORT(PCIE_LC_LINK_WIDTH_CNTL, tmp);
7497                                 }
7498                         }
7499
7500                         for (i = 0; i < 10; i++) {
7501                                 /* check status */
7502                                 pci_read_config_word(rdev->pdev, gpu_pos + PCI_EXP_DEVSTA, &tmp16);
7503                                 if (tmp16 & PCI_EXP_DEVSTA_TRPND)
7504                                         break;
7505
7506                                 pci_read_config_word(root, bridge_pos + PCI_EXP_LNKCTL, &bridge_cfg);
7507                                 pci_read_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL, &gpu_cfg);
7508
7509                                 pci_read_config_word(root, bridge_pos + PCI_EXP_LNKCTL2, &bridge_cfg2);
7510                                 pci_read_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL2, &gpu_cfg2);
7511
7512                                 tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL4);
7513                                 tmp |= LC_SET_QUIESCE;
7514                                 WREG32_PCIE_PORT(PCIE_LC_CNTL4, tmp);
7515
7516                                 tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL4);
7517                                 tmp |= LC_REDO_EQ;
7518                                 WREG32_PCIE_PORT(PCIE_LC_CNTL4, tmp);
7519
7520                                 mdelay(100);
7521
7522                                 /* linkctl */
7523                                 pci_read_config_word(root, bridge_pos + PCI_EXP_LNKCTL, &tmp16);
7524                                 tmp16 &= ~PCI_EXP_LNKCTL_HAWD;
7525                                 tmp16 |= (bridge_cfg & PCI_EXP_LNKCTL_HAWD);
7526                                 pci_write_config_word(root, bridge_pos + PCI_EXP_LNKCTL, tmp16);
7527
7528                                 pci_read_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL, &tmp16);
7529                                 tmp16 &= ~PCI_EXP_LNKCTL_HAWD;
7530                                 tmp16 |= (gpu_cfg & PCI_EXP_LNKCTL_HAWD);
7531                                 pci_write_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL, tmp16);
7532
7533                                 /* linkctl2 */
7534                                 pci_read_config_word(root, bridge_pos + PCI_EXP_LNKCTL2, &tmp16);
7535                                 tmp16 &= ~((1 << 4) | (7 << 9));
7536                                 tmp16 |= (bridge_cfg2 & ((1 << 4) | (7 << 9)));
7537                                 pci_write_config_word(root, bridge_pos + PCI_EXP_LNKCTL2, tmp16);
7538
7539                                 pci_read_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL2, &tmp16);
7540                                 tmp16 &= ~((1 << 4) | (7 << 9));
7541                                 tmp16 |= (gpu_cfg2 & ((1 << 4) | (7 << 9)));
7542                                 pci_write_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL2, tmp16);
7543
7544                                 tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL4);
7545                                 tmp &= ~LC_SET_QUIESCE;
7546                                 WREG32_PCIE_PORT(PCIE_LC_CNTL4, tmp);
7547                         }
7548                 }
7549         }
7550
7551         /* set the link speed */
7552         speed_cntl |= LC_FORCE_EN_SW_SPEED_CHANGE | LC_FORCE_DIS_HW_SPEED_CHANGE;
7553         speed_cntl &= ~LC_FORCE_DIS_SW_SPEED_CHANGE;
7554         WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, speed_cntl);
7555
7556         pci_read_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL2, &tmp16);
7557         tmp16 &= ~0xf;
7558         if (mask & DRM_PCIE_SPEED_80)
7559                 tmp16 |= 3; /* gen3 */
7560         else if (mask & DRM_PCIE_SPEED_50)
7561                 tmp16 |= 2; /* gen2 */
7562         else
7563                 tmp16 |= 1; /* gen1 */
7564         pci_write_config_word(rdev->pdev, gpu_pos + PCI_EXP_LNKCTL2, tmp16);
7565
7566         speed_cntl = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
7567         speed_cntl |= LC_INITIATE_LINK_SPEED_CHANGE;
7568         WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, speed_cntl);
7569
7570         for (i = 0; i < rdev->usec_timeout; i++) {
7571                 speed_cntl = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
7572                 if ((speed_cntl & LC_INITIATE_LINK_SPEED_CHANGE) == 0)
7573                         break;
7574                 udelay(1);
7575         }
7576 }
7577
7578 static void si_program_aspm(struct radeon_device *rdev)
7579 {
7580         u32 data, orig;
7581         bool disable_l0s = false, disable_l1 = false, disable_plloff_in_l1 = false;
7582         bool disable_clkreq = false;
7583
7584         if (radeon_aspm == 0)
7585                 return;
7586
7587         if (!(rdev->flags & RADEON_IS_PCIE))
7588                 return;
7589
7590         orig = data = RREG32_PCIE_PORT(PCIE_LC_N_FTS_CNTL);
7591         data &= ~LC_XMIT_N_FTS_MASK;
7592         data |= LC_XMIT_N_FTS(0x24) | LC_XMIT_N_FTS_OVERRIDE_EN;
7593         if (orig != data)
7594                 WREG32_PCIE_PORT(PCIE_LC_N_FTS_CNTL, data);
7595
7596         orig = data = RREG32_PCIE_PORT(PCIE_LC_CNTL3);
7597         data |= LC_GO_TO_RECOVERY;
7598         if (orig != data)
7599                 WREG32_PCIE_PORT(PCIE_LC_CNTL3, data);
7600
7601         orig = data = RREG32_PCIE(PCIE_P_CNTL);
7602         data |= P_IGNORE_EDB_ERR;
7603         if (orig != data)
7604                 WREG32_PCIE(PCIE_P_CNTL, data);
7605
7606         orig = data = RREG32_PCIE_PORT(PCIE_LC_CNTL);
7607         data &= ~(LC_L0S_INACTIVITY_MASK | LC_L1_INACTIVITY_MASK);
7608         data |= LC_PMI_TO_L1_DIS;
7609         if (!disable_l0s)
7610                 data |= LC_L0S_INACTIVITY(7);
7611
7612         if (!disable_l1) {
7613                 data |= LC_L1_INACTIVITY(7);
7614                 data &= ~LC_PMI_TO_L1_DIS;
7615                 if (orig != data)
7616                         WREG32_PCIE_PORT(PCIE_LC_CNTL, data);
7617
7618                 if (!disable_plloff_in_l1) {
7619                         bool clk_req_support;
7620
7621                         orig = data = RREG32_PIF_PHY0(PB0_PIF_PWRDOWN_0);
7622                         data &= ~(PLL_POWER_STATE_IN_OFF_0_MASK | PLL_POWER_STATE_IN_TXS2_0_MASK);
7623                         data |= PLL_POWER_STATE_IN_OFF_0(7) | PLL_POWER_STATE_IN_TXS2_0(7);
7624                         if (orig != data)
7625                                 WREG32_PIF_PHY0(PB0_PIF_PWRDOWN_0, data);
7626
7627                         orig = data = RREG32_PIF_PHY0(PB0_PIF_PWRDOWN_1);
7628                         data &= ~(PLL_POWER_STATE_IN_OFF_1_MASK | PLL_POWER_STATE_IN_TXS2_1_MASK);
7629                         data |= PLL_POWER_STATE_IN_OFF_1(7) | PLL_POWER_STATE_IN_TXS2_1(7);
7630                         if (orig != data)
7631                                 WREG32_PIF_PHY0(PB0_PIF_PWRDOWN_1, data);
7632
7633                         orig = data = RREG32_PIF_PHY1(PB1_PIF_PWRDOWN_0);
7634                         data &= ~(PLL_POWER_STATE_IN_OFF_0_MASK | PLL_POWER_STATE_IN_TXS2_0_MASK);
7635                         data |= PLL_POWER_STATE_IN_OFF_0(7) | PLL_POWER_STATE_IN_TXS2_0(7);
7636                         if (orig != data)
7637                                 WREG32_PIF_PHY1(PB1_PIF_PWRDOWN_0, data);
7638
7639                         orig = data = RREG32_PIF_PHY1(PB1_PIF_PWRDOWN_1);
7640                         data &= ~(PLL_POWER_STATE_IN_OFF_1_MASK | PLL_POWER_STATE_IN_TXS2_1_MASK);
7641                         data |= PLL_POWER_STATE_IN_OFF_1(7) | PLL_POWER_STATE_IN_TXS2_1(7);
7642                         if (orig != data)
7643                                 WREG32_PIF_PHY1(PB1_PIF_PWRDOWN_1, data);
7644
7645                         if ((rdev->family != CHIP_OLAND) && (rdev->family != CHIP_HAINAN)) {
7646                                 orig = data = RREG32_PIF_PHY0(PB0_PIF_PWRDOWN_0);
7647                                 data &= ~PLL_RAMP_UP_TIME_0_MASK;
7648                                 if (orig != data)
7649                                         WREG32_PIF_PHY0(PB0_PIF_PWRDOWN_0, data);
7650
7651                                 orig = data = RREG32_PIF_PHY0(PB0_PIF_PWRDOWN_1);
7652                                 data &= ~PLL_RAMP_UP_TIME_1_MASK;
7653                                 if (orig != data)
7654                                         WREG32_PIF_PHY0(PB0_PIF_PWRDOWN_1, data);
7655
7656                                 orig = data = RREG32_PIF_PHY0(PB0_PIF_PWRDOWN_2);
7657                                 data &= ~PLL_RAMP_UP_TIME_2_MASK;
7658                                 if (orig != data)
7659                                         WREG32_PIF_PHY0(PB0_PIF_PWRDOWN_2, data);
7660
7661                                 orig = data = RREG32_PIF_PHY0(PB0_PIF_PWRDOWN_3);
7662                                 data &= ~PLL_RAMP_UP_TIME_3_MASK;
7663                                 if (orig != data)
7664                                         WREG32_PIF_PHY0(PB0_PIF_PWRDOWN_3, data);
7665
7666                                 orig = data = RREG32_PIF_PHY1(PB1_PIF_PWRDOWN_0);
7667                                 data &= ~PLL_RAMP_UP_TIME_0_MASK;
7668                                 if (orig != data)
7669                                         WREG32_PIF_PHY1(PB1_PIF_PWRDOWN_0, data);
7670
7671                                 orig = data = RREG32_PIF_PHY1(PB1_PIF_PWRDOWN_1);
7672                                 data &= ~PLL_RAMP_UP_TIME_1_MASK;
7673                                 if (orig != data)
7674                                         WREG32_PIF_PHY1(PB1_PIF_PWRDOWN_1, data);
7675
7676                                 orig = data = RREG32_PIF_PHY1(PB1_PIF_PWRDOWN_2);
7677                                 data &= ~PLL_RAMP_UP_TIME_2_MASK;
7678                                 if (orig != data)
7679                                         WREG32_PIF_PHY1(PB1_PIF_PWRDOWN_2, data);
7680
7681                                 orig = data = RREG32_PIF_PHY1(PB1_PIF_PWRDOWN_3);
7682                                 data &= ~PLL_RAMP_UP_TIME_3_MASK;
7683                                 if (orig != data)
7684                                         WREG32_PIF_PHY1(PB1_PIF_PWRDOWN_3, data);
7685                         }
7686                         orig = data = RREG32_PCIE_PORT(PCIE_LC_LINK_WIDTH_CNTL);
7687                         data &= ~LC_DYN_LANES_PWR_STATE_MASK;
7688                         data |= LC_DYN_LANES_PWR_STATE(3);
7689                         if (orig != data)
7690                                 WREG32_PCIE_PORT(PCIE_LC_LINK_WIDTH_CNTL, data);
7691
7692                         orig = data = RREG32_PIF_PHY0(PB0_PIF_CNTL);
7693                         data &= ~LS2_EXIT_TIME_MASK;
7694                         if ((rdev->family == CHIP_OLAND) || (rdev->family == CHIP_HAINAN))
7695                                 data |= LS2_EXIT_TIME(5);
7696                         if (orig != data)
7697                                 WREG32_PIF_PHY0(PB0_PIF_CNTL, data);
7698
7699                         orig = data = RREG32_PIF_PHY1(PB1_PIF_CNTL);
7700                         data &= ~LS2_EXIT_TIME_MASK;
7701                         if ((rdev->family == CHIP_OLAND) || (rdev->family == CHIP_HAINAN))
7702                                 data |= LS2_EXIT_TIME(5);
7703                         if (orig != data)
7704                                 WREG32_PIF_PHY1(PB1_PIF_CNTL, data);
7705
7706                         if (!disable_clkreq &&
7707                             !pci_is_root_bus(rdev->pdev->bus)) {
7708                                 struct pci_dev *root = rdev->pdev->bus->self;
7709                                 u32 lnkcap;
7710
7711                                 clk_req_support = false;
7712                                 pcie_capability_read_dword(root, PCI_EXP_LNKCAP, &lnkcap);
7713                                 if (lnkcap & PCI_EXP_LNKCAP_CLKPM)
7714                                         clk_req_support = true;
7715                         } else {
7716                                 clk_req_support = false;
7717                         }
7718
7719                         if (clk_req_support) {
7720                                 orig = data = RREG32_PCIE_PORT(PCIE_LC_CNTL2);
7721                                 data |= LC_ALLOW_PDWN_IN_L1 | LC_ALLOW_PDWN_IN_L23;
7722                                 if (orig != data)
7723                                         WREG32_PCIE_PORT(PCIE_LC_CNTL2, data);
7724
7725                                 orig = data = RREG32(THM_CLK_CNTL);
7726                                 data &= ~(CMON_CLK_SEL_MASK | TMON_CLK_SEL_MASK);
7727                                 data |= CMON_CLK_SEL(1) | TMON_CLK_SEL(1);
7728                                 if (orig != data)
7729                                         WREG32(THM_CLK_CNTL, data);
7730
7731                                 orig = data = RREG32(MISC_CLK_CNTL);
7732                                 data &= ~(DEEP_SLEEP_CLK_SEL_MASK | ZCLK_SEL_MASK);
7733                                 data |= DEEP_SLEEP_CLK_SEL(1) | ZCLK_SEL(1);
7734                                 if (orig != data)
7735                                         WREG32(MISC_CLK_CNTL, data);
7736
7737                                 orig = data = RREG32(CG_CLKPIN_CNTL);
7738                                 data &= ~BCLK_AS_XCLK;
7739                                 if (orig != data)
7740                                         WREG32(CG_CLKPIN_CNTL, data);
7741
7742                                 orig = data = RREG32(CG_CLKPIN_CNTL_2);
7743                                 data &= ~FORCE_BIF_REFCLK_EN;
7744                                 if (orig != data)
7745                                         WREG32(CG_CLKPIN_CNTL_2, data);
7746
7747                                 orig = data = RREG32(MPLL_BYPASSCLK_SEL);
7748                                 data &= ~MPLL_CLKOUT_SEL_MASK;
7749                                 data |= MPLL_CLKOUT_SEL(4);
7750                                 if (orig != data)
7751                                         WREG32(MPLL_BYPASSCLK_SEL, data);
7752
7753                                 orig = data = RREG32(SPLL_CNTL_MODE);
7754                                 data &= ~SPLL_REFCLK_SEL_MASK;
7755                                 if (orig != data)
7756                                         WREG32(SPLL_CNTL_MODE, data);
7757                         }
7758                 }
7759         } else {
7760                 if (orig != data)
7761                         WREG32_PCIE_PORT(PCIE_LC_CNTL, data);
7762         }
7763
7764         orig = data = RREG32_PCIE(PCIE_CNTL2);
7765         data |= SLV_MEM_LS_EN | MST_MEM_LS_EN | REPLAY_MEM_LS_EN;
7766         if (orig != data)
7767                 WREG32_PCIE(PCIE_CNTL2, data);
7768
7769         if (!disable_l0s) {
7770                 data = RREG32_PCIE_PORT(PCIE_LC_N_FTS_CNTL);
7771                 if((data & LC_N_FTS_MASK) == LC_N_FTS_MASK) {
7772                         data = RREG32_PCIE(PCIE_LC_STATUS1);
7773                         if ((data & LC_REVERSE_XMIT) && (data & LC_REVERSE_RCVR)) {
7774                                 orig = data = RREG32_PCIE_PORT(PCIE_LC_CNTL);
7775                                 data &= ~LC_L0S_INACTIVITY_MASK;
7776                                 if (orig != data)
7777                                         WREG32_PCIE_PORT(PCIE_LC_CNTL, data);
7778                         }
7779                 }
7780         }
7781 }
7782
7783 int si_vce_send_vcepll_ctlreq(struct radeon_device *rdev)
7784 {
7785         unsigned i;
7786
7787         /* make sure VCEPLL_CTLREQ is deasserted */
7788         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, 0, ~UPLL_CTLREQ_MASK);
7789
7790         mdelay(10);
7791
7792         /* assert UPLL_CTLREQ */
7793         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
7794
7795         /* wait for CTLACK and CTLACK2 to get asserted */
7796         for (i = 0; i < 100; ++i) {
7797                 uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
7798                 if ((RREG32_SMC(CG_VCEPLL_FUNC_CNTL) & mask) == mask)
7799                         break;
7800                 mdelay(10);
7801         }
7802
7803         /* deassert UPLL_CTLREQ */
7804         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, 0, ~UPLL_CTLREQ_MASK);
7805
7806         if (i == 100) {
7807                 DRM_ERROR("Timeout setting UVD clocks!\n");
7808                 return -ETIMEDOUT;
7809         }
7810
7811         return 0;
7812 }
7813
7814 int si_set_vce_clocks(struct radeon_device *rdev, u32 evclk, u32 ecclk)
7815 {
7816         unsigned fb_div = 0, evclk_div = 0, ecclk_div = 0;
7817         int r;
7818
7819         /* bypass evclk and ecclk with bclk */
7820         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL_2,
7821                      EVCLK_SRC_SEL(1) | ECCLK_SRC_SEL(1),
7822                      ~(EVCLK_SRC_SEL_MASK | ECCLK_SRC_SEL_MASK));
7823
7824         /* put PLL in bypass mode */
7825         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, VCEPLL_BYPASS_EN_MASK,
7826                      ~VCEPLL_BYPASS_EN_MASK);
7827
7828         if (!evclk || !ecclk) {
7829                 /* keep the Bypass mode, put PLL to sleep */
7830                 WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, VCEPLL_SLEEP_MASK,
7831                              ~VCEPLL_SLEEP_MASK);
7832                 return 0;
7833         }
7834
7835         r = radeon_uvd_calc_upll_dividers(rdev, evclk, ecclk, 125000, 250000,
7836                                           16384, 0x03FFFFFF, 0, 128, 5,
7837                                           &fb_div, &evclk_div, &ecclk_div);
7838         if (r)
7839                 return r;
7840
7841         /* set RESET_ANTI_MUX to 0 */
7842         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL_5, 0, ~RESET_ANTI_MUX_MASK);
7843
7844         /* set VCO_MODE to 1 */
7845         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, VCEPLL_VCO_MODE_MASK,
7846                      ~VCEPLL_VCO_MODE_MASK);
7847
7848         /* toggle VCEPLL_SLEEP to 1 then back to 0 */
7849         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, VCEPLL_SLEEP_MASK,
7850                      ~VCEPLL_SLEEP_MASK);
7851         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, 0, ~VCEPLL_SLEEP_MASK);
7852
7853         /* deassert VCEPLL_RESET */
7854         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, 0, ~VCEPLL_RESET_MASK);
7855
7856         mdelay(1);
7857
7858         r = si_vce_send_vcepll_ctlreq(rdev);
7859         if (r)
7860                 return r;
7861
7862         /* assert VCEPLL_RESET again */
7863         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, VCEPLL_RESET_MASK, ~VCEPLL_RESET_MASK);
7864
7865         /* disable spread spectrum. */
7866         WREG32_SMC_P(CG_VCEPLL_SPREAD_SPECTRUM, 0, ~SSEN_MASK);
7867
7868         /* set feedback divider */
7869         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL_3, VCEPLL_FB_DIV(fb_div), ~VCEPLL_FB_DIV_MASK);
7870
7871         /* set ref divider to 0 */
7872         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, 0, ~VCEPLL_REF_DIV_MASK);
7873
7874         /* set PDIV_A and PDIV_B */
7875         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL_2,
7876                      VCEPLL_PDIV_A(evclk_div) | VCEPLL_PDIV_B(ecclk_div),
7877                      ~(VCEPLL_PDIV_A_MASK | VCEPLL_PDIV_B_MASK));
7878
7879         /* give the PLL some time to settle */
7880         mdelay(15);
7881
7882         /* deassert PLL_RESET */
7883         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, 0, ~VCEPLL_RESET_MASK);
7884
7885         mdelay(15);
7886
7887         /* switch from bypass mode to normal mode */
7888         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL, 0, ~VCEPLL_BYPASS_EN_MASK);
7889
7890         r = si_vce_send_vcepll_ctlreq(rdev);
7891         if (r)
7892                 return r;
7893
7894         /* switch VCLK and DCLK selection */
7895         WREG32_SMC_P(CG_VCEPLL_FUNC_CNTL_2,
7896                      EVCLK_SRC_SEL(16) | ECCLK_SRC_SEL(16),
7897                      ~(EVCLK_SRC_SEL_MASK | ECCLK_SRC_SEL_MASK));
7898
7899         mdelay(100);
7900
7901         return 0;
7902 }