DRM part of Radeon DRI suspend/resume support (Charl Botha).
[platform/upstream/libdrm.git] / shared-core / radeon_cp.c
1 /* radeon_cp.c -- CP support for Radeon -*- linux-c -*-
2  *
3  * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
4  * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  * Authors:
27  *    Kevin E. Martin <martin@valinux.com>
28  *    Gareth Hughes <gareth@valinux.com>
29  */
30
31 #include "radeon.h"
32 #include "drmP.h"
33 #include "drm.h"
34 #include "radeon_drm.h"
35 #include "radeon_drv.h"
36
37 #define RADEON_FIFO_DEBUG       0
38
39
40 /* CP microcode (from ATI) */
41 static u32 R200_cp_microcode[][2] = {
42         { 0x21007000, 0000000000 },        
43         { 0x20007000, 0000000000 }, 
44         { 0x000000ab, 0x00000004 },
45         { 0x000000af, 0x00000004 },
46         { 0x66544a49, 0000000000 },
47         { 0x49494174, 0000000000 },
48         { 0x54517d83, 0000000000 },
49         { 0x498d8b64, 0000000000 },
50         { 0x49494949, 0000000000 },
51         { 0x49da493c, 0000000000 },
52         { 0x49989898, 0000000000 },
53         { 0xd34949d5, 0000000000 },
54         { 0x9dc90e11, 0000000000 },
55         { 0xce9b9b9b, 0000000000 },
56         { 0x000f0000, 0x00000016 },
57         { 0x352e232c, 0000000000 },
58         { 0x00000013, 0x00000004 },
59         { 0x000f0000, 0x00000016 },
60         { 0x352e272c, 0000000000 },
61         { 0x000f0001, 0x00000016 },
62         { 0x3239362f, 0000000000 },
63         { 0x000077ef, 0x00000002 },
64         { 0x00061000, 0x00000002 },
65         { 0x00000020, 0x0000001a },
66         { 0x00004000, 0x0000001e },
67         { 0x00061000, 0x00000002 },
68         { 0x00000020, 0x0000001a },
69         { 0x00004000, 0x0000001e },
70         { 0x00061000, 0x00000002 },
71         { 0x00000020, 0x0000001a },
72         { 0x00004000, 0x0000001e },
73         { 0x00000016, 0x00000004 },
74         { 0x0003802a, 0x00000002 },
75         { 0x040067e0, 0x00000002 },
76         { 0x00000016, 0x00000004 },
77         { 0x000077e0, 0x00000002 },
78         { 0x00065000, 0x00000002 },
79         { 0x000037e1, 0x00000002 },
80         { 0x040067e1, 0x00000006 },
81         { 0x000077e0, 0x00000002 },
82         { 0x000077e1, 0x00000002 },
83         { 0x000077e1, 0x00000006 },
84         { 0xffffffff, 0000000000 },
85         { 0x10000000, 0000000000 },
86         { 0x0003802a, 0x00000002 },
87         { 0x040067e0, 0x00000006 },
88         { 0x00007675, 0x00000002 },
89         { 0x00007676, 0x00000002 },
90         { 0x00007677, 0x00000002 },
91         { 0x00007678, 0x00000006 },
92         { 0x0003802b, 0x00000002 },
93         { 0x04002676, 0x00000002 },
94         { 0x00007677, 0x00000002 },
95         { 0x00007678, 0x00000006 },
96         { 0x0000002e, 0x00000018 },
97         { 0x0000002e, 0x00000018 },
98         { 0000000000, 0x00000006 },
99         { 0x0000002f, 0x00000018 },
100         { 0x0000002f, 0x00000018 },
101         { 0000000000, 0x00000006 },
102         { 0x01605000, 0x00000002 },
103         { 0x00065000, 0x00000002 },
104         { 0x00098000, 0x00000002 },
105         { 0x00061000, 0x00000002 },
106         { 0x64c0603d, 0x00000004 },
107         { 0x00080000, 0x00000016 },
108         { 0000000000, 0000000000 },
109         { 0x0400251d, 0x00000002 },
110         { 0x00007580, 0x00000002 },
111         { 0x00067581, 0x00000002 },
112         { 0x04002580, 0x00000002 },
113         { 0x00067581, 0x00000002 },
114         { 0x00000046, 0x00000004 },
115         { 0x00005000, 0000000000 },
116         { 0x00061000, 0x00000002 },
117         { 0x0000750e, 0x00000002 },
118         { 0x00019000, 0x00000002 },
119         { 0x00011055, 0x00000014 },
120         { 0x00000055, 0x00000012 },
121         { 0x0400250f, 0x00000002 },
122         { 0x0000504a, 0x00000004 },
123         { 0x00007565, 0x00000002 },
124         { 0x00007566, 0x00000002 },
125         { 0x00000051, 0x00000004 },
126         { 0x01e655b4, 0x00000002 },
127         { 0x4401b0dc, 0x00000002 },
128         { 0x01c110dc, 0x00000002 },
129         { 0x2666705d, 0x00000018 },
130         { 0x040c2565, 0x00000002 },
131         { 0x0000005d, 0x00000018 },
132         { 0x04002564, 0x00000002 },
133         { 0x00007566, 0x00000002 },
134         { 0x00000054, 0x00000004 },
135         { 0x00401060, 0x00000008 },
136         { 0x00101000, 0x00000002 },
137         { 0x000d80ff, 0x00000002 },
138         { 0x00800063, 0x00000008 },
139         { 0x000f9000, 0x00000002 },
140         { 0x000e00ff, 0x00000002 },
141         { 0000000000, 0x00000006 },
142         { 0x00000080, 0x00000018 },
143         { 0x00000054, 0x00000004 },
144         { 0x00007576, 0x00000002 },
145         { 0x00065000, 0x00000002 },
146         { 0x00009000, 0x00000002 },
147         { 0x00041000, 0x00000002 },
148         { 0x0c00350e, 0x00000002 },
149         { 0x00049000, 0x00000002 },
150         { 0x00051000, 0x00000002 },
151         { 0x01e785f8, 0x00000002 },
152         { 0x00200000, 0x00000002 },
153         { 0x00600073, 0x0000000c },
154         { 0x00007563, 0x00000002 },
155         { 0x006075f0, 0x00000021 },
156         { 0x20007068, 0x00000004 },
157         { 0x00005068, 0x00000004 },
158         { 0x00007576, 0x00000002 },
159         { 0x00007577, 0x00000002 },
160         { 0x0000750e, 0x00000002 },
161         { 0x0000750f, 0x00000002 },
162         { 0x00a05000, 0x00000002 },
163         { 0x00600076, 0x0000000c },
164         { 0x006075f0, 0x00000021 },
165         { 0x000075f8, 0x00000002 },
166         { 0x00000076, 0x00000004 },
167         { 0x000a750e, 0x00000002 },
168         { 0x0020750f, 0x00000002 },
169         { 0x00600079, 0x00000004 },
170         { 0x00007570, 0x00000002 },
171         { 0x00007571, 0x00000002 },
172         { 0x00007572, 0x00000006 },
173         { 0x00005000, 0x00000002 },
174         { 0x00a05000, 0x00000002 },
175         { 0x00007568, 0x00000002 },
176         { 0x00061000, 0x00000002 },
177         { 0x00000084, 0x0000000c },
178         { 0x00058000, 0x00000002 },
179         { 0x0c607562, 0x00000002 },
180         { 0x00000086, 0x00000004 },
181         { 0x00600085, 0x00000004 },
182         { 0x400070dd, 0000000000 },
183         { 0x000380dd, 0x00000002 },
184         { 0x00000093, 0x0000001c },
185         { 0x00065095, 0x00000018 },
186         { 0x040025bb, 0x00000002 },
187         { 0x00061096, 0x00000018 },
188         { 0x040075bc, 0000000000 },
189         { 0x000075bb, 0x00000002 },
190         { 0x000075bc, 0000000000 },
191         { 0x00090000, 0x00000006 },
192         { 0x00090000, 0x00000002 },
193         { 0x000d8002, 0x00000006 },
194         { 0x00005000, 0x00000002 },
195         { 0x00007821, 0x00000002 },
196         { 0x00007800, 0000000000 },
197         { 0x00007821, 0x00000002 },
198         { 0x00007800, 0000000000 },
199         { 0x01665000, 0x00000002 },
200         { 0x000a0000, 0x00000002 },
201         { 0x000671cc, 0x00000002 },
202         { 0x0286f1cd, 0x00000002 },
203         { 0x000000a3, 0x00000010 },
204         { 0x21007000, 0000000000 },
205         { 0x000000aa, 0x0000001c },
206         { 0x00065000, 0x00000002 },
207         { 0x000a0000, 0x00000002 },
208         { 0x00061000, 0x00000002 },
209         { 0x000b0000, 0x00000002 },
210         { 0x38067000, 0x00000002 },
211         { 0x000a00a6, 0x00000004 },
212         { 0x20007000, 0000000000 },
213         { 0x01200000, 0x00000002 },
214         { 0x20077000, 0x00000002 },
215         { 0x01200000, 0x00000002 },
216         { 0x20007000, 0000000000 },
217         { 0x00061000, 0x00000002 },
218         { 0x0120751b, 0x00000002 },
219         { 0x8040750a, 0x00000002 },
220         { 0x8040750b, 0x00000002 },
221         { 0x00110000, 0x00000002 },
222         { 0x000380dd, 0x00000002 },
223         { 0x000000bd, 0x0000001c },
224         { 0x00061096, 0x00000018 },
225         { 0x844075bd, 0x00000002 },
226         { 0x00061095, 0x00000018 },
227         { 0x840075bb, 0x00000002 },
228         { 0x00061096, 0x00000018 },
229         { 0x844075bc, 0x00000002 },
230         { 0x000000c0, 0x00000004 },
231         { 0x804075bd, 0x00000002 },
232         { 0x800075bb, 0x00000002 },
233         { 0x804075bc, 0x00000002 },
234         { 0x00108000, 0x00000002 },
235         { 0x01400000, 0x00000002 },
236         { 0x006000c4, 0x0000000c },
237         { 0x20c07000, 0x00000020 },
238         { 0x000000c6, 0x00000012 },
239         { 0x00800000, 0x00000006 },
240         { 0x0080751d, 0x00000006 },
241         { 0x000025bb, 0x00000002 },
242         { 0x000040c0, 0x00000004 },
243         { 0x0000775c, 0x00000002 },
244         { 0x00a05000, 0x00000002 },
245         { 0x00661000, 0x00000002 },
246         { 0x0460275d, 0x00000020 },
247         { 0x00004000, 0000000000 },
248         { 0x00007999, 0x00000002 },
249         { 0x00a05000, 0x00000002 },
250         { 0x00661000, 0x00000002 },
251         { 0x0460299b, 0x00000020 },
252         { 0x00004000, 0000000000 },
253         { 0x01e00830, 0x00000002 },
254         { 0x21007000, 0000000000 },
255         { 0x00005000, 0x00000002 },
256         { 0x00038042, 0x00000002 },
257         { 0x040025e0, 0x00000002 },
258         { 0x000075e1, 0000000000 },
259         { 0x00000001, 0000000000 },
260         { 0x000380d9, 0x00000002 },
261         { 0x04007394, 0000000000 },
262         { 0000000000, 0000000000 },
263         { 0000000000, 0000000000 },
264         { 0000000000, 0000000000 },
265         { 0000000000, 0000000000 },
266         { 0000000000, 0000000000 },
267         { 0000000000, 0000000000 },
268         { 0000000000, 0000000000 },
269         { 0000000000, 0000000000 },
270         { 0000000000, 0000000000 },
271         { 0000000000, 0000000000 },
272         { 0000000000, 0000000000 },
273         { 0000000000, 0000000000 },
274         { 0000000000, 0000000000 },
275         { 0000000000, 0000000000 },
276         { 0000000000, 0000000000 },
277         { 0000000000, 0000000000 },
278         { 0000000000, 0000000000 },
279         { 0000000000, 0000000000 },
280         { 0000000000, 0000000000 },
281         { 0000000000, 0000000000 },
282         { 0000000000, 0000000000 },
283         { 0000000000, 0000000000 },
284         { 0000000000, 0000000000 },
285         { 0000000000, 0000000000 },
286         { 0000000000, 0000000000 },
287         { 0000000000, 0000000000 },
288         { 0000000000, 0000000000 },
289         { 0000000000, 0000000000 },
290         { 0000000000, 0000000000 },
291         { 0000000000, 0000000000 },
292         { 0000000000, 0000000000 },
293         { 0000000000, 0000000000 },
294         { 0000000000, 0000000000 },
295         { 0000000000, 0000000000 },
296         { 0000000000, 0000000000 },
297         { 0000000000, 0000000000 },
298 };
299
300
301 static u32 radeon_cp_microcode[][2] = {
302         { 0x21007000, 0000000000 },
303         { 0x20007000, 0000000000 },
304         { 0x000000b4, 0x00000004 },
305         { 0x000000b8, 0x00000004 },
306         { 0x6f5b4d4c, 0000000000 },
307         { 0x4c4c427f, 0000000000 },
308         { 0x5b568a92, 0000000000 },
309         { 0x4ca09c6d, 0000000000 },
310         { 0xad4c4c4c, 0000000000 },
311         { 0x4ce1af3d, 0000000000 },
312         { 0xd8afafaf, 0000000000 },
313         { 0xd64c4cdc, 0000000000 },
314         { 0x4cd10d10, 0000000000 },
315         { 0x000f0000, 0x00000016 },
316         { 0x362f242d, 0000000000 },
317         { 0x00000012, 0x00000004 },
318         { 0x000f0000, 0x00000016 },
319         { 0x362f282d, 0000000000 },
320         { 0x000380e7, 0x00000002 },
321         { 0x04002c97, 0x00000002 },
322         { 0x000f0001, 0x00000016 },
323         { 0x333a3730, 0000000000 },
324         { 0x000077ef, 0x00000002 },
325         { 0x00061000, 0x00000002 },
326         { 0x00000021, 0x0000001a },
327         { 0x00004000, 0x0000001e },
328         { 0x00061000, 0x00000002 },
329         { 0x00000021, 0x0000001a },
330         { 0x00004000, 0x0000001e },
331         { 0x00061000, 0x00000002 },
332         { 0x00000021, 0x0000001a },
333         { 0x00004000, 0x0000001e },
334         { 0x00000017, 0x00000004 },
335         { 0x0003802b, 0x00000002 },
336         { 0x040067e0, 0x00000002 },
337         { 0x00000017, 0x00000004 },
338         { 0x000077e0, 0x00000002 },
339         { 0x00065000, 0x00000002 },
340         { 0x000037e1, 0x00000002 },
341         { 0x040067e1, 0x00000006 },
342         { 0x000077e0, 0x00000002 },
343         { 0x000077e1, 0x00000002 },
344         { 0x000077e1, 0x00000006 },
345         { 0xffffffff, 0000000000 },
346         { 0x10000000, 0000000000 },
347         { 0x0003802b, 0x00000002 },
348         { 0x040067e0, 0x00000006 },
349         { 0x00007675, 0x00000002 },
350         { 0x00007676, 0x00000002 },
351         { 0x00007677, 0x00000002 },
352         { 0x00007678, 0x00000006 },
353         { 0x0003802c, 0x00000002 },
354         { 0x04002676, 0x00000002 },
355         { 0x00007677, 0x00000002 },
356         { 0x00007678, 0x00000006 },
357         { 0x0000002f, 0x00000018 },
358         { 0x0000002f, 0x00000018 },
359         { 0000000000, 0x00000006 },
360         { 0x00000030, 0x00000018 },
361         { 0x00000030, 0x00000018 },
362         { 0000000000, 0x00000006 },
363         { 0x01605000, 0x00000002 },
364         { 0x00065000, 0x00000002 },
365         { 0x00098000, 0x00000002 },
366         { 0x00061000, 0x00000002 },
367         { 0x64c0603e, 0x00000004 },
368         { 0x000380e6, 0x00000002 },
369         { 0x040025c5, 0x00000002 },
370         { 0x00080000, 0x00000016 },
371         { 0000000000, 0000000000 },
372         { 0x0400251d, 0x00000002 },
373         { 0x00007580, 0x00000002 },
374         { 0x00067581, 0x00000002 },
375         { 0x04002580, 0x00000002 },
376         { 0x00067581, 0x00000002 },
377         { 0x00000049, 0x00000004 },
378         { 0x00005000, 0000000000 },
379         { 0x000380e6, 0x00000002 },
380         { 0x040025c5, 0x00000002 },
381         { 0x00061000, 0x00000002 },
382         { 0x0000750e, 0x00000002 },
383         { 0x00019000, 0x00000002 },
384         { 0x00011055, 0x00000014 },
385         { 0x00000055, 0x00000012 },
386         { 0x0400250f, 0x00000002 },
387         { 0x0000504f, 0x00000004 },
388         { 0x000380e6, 0x00000002 },
389         { 0x040025c5, 0x00000002 },
390         { 0x00007565, 0x00000002 },
391         { 0x00007566, 0x00000002 },
392         { 0x00000058, 0x00000004 },
393         { 0x000380e6, 0x00000002 },
394         { 0x040025c5, 0x00000002 },
395         { 0x01e655b4, 0x00000002 },
396         { 0x4401b0e4, 0x00000002 },
397         { 0x01c110e4, 0x00000002 },
398         { 0x26667066, 0x00000018 },
399         { 0x040c2565, 0x00000002 },
400         { 0x00000066, 0x00000018 },
401         { 0x04002564, 0x00000002 },
402         { 0x00007566, 0x00000002 },
403         { 0x0000005d, 0x00000004 },
404         { 0x00401069, 0x00000008 },
405         { 0x00101000, 0x00000002 },
406         { 0x000d80ff, 0x00000002 },
407         { 0x0080006c, 0x00000008 },
408         { 0x000f9000, 0x00000002 },
409         { 0x000e00ff, 0x00000002 },
410         { 0000000000, 0x00000006 },
411         { 0x0000008f, 0x00000018 },
412         { 0x0000005b, 0x00000004 },
413         { 0x000380e6, 0x00000002 },
414         { 0x040025c5, 0x00000002 },
415         { 0x00007576, 0x00000002 },
416         { 0x00065000, 0x00000002 },
417         { 0x00009000, 0x00000002 },
418         { 0x00041000, 0x00000002 },
419         { 0x0c00350e, 0x00000002 },
420         { 0x00049000, 0x00000002 },
421         { 0x00051000, 0x00000002 },
422         { 0x01e785f8, 0x00000002 },
423         { 0x00200000, 0x00000002 },
424         { 0x0060007e, 0x0000000c },
425         { 0x00007563, 0x00000002 },
426         { 0x006075f0, 0x00000021 },
427         { 0x20007073, 0x00000004 },
428         { 0x00005073, 0x00000004 },
429         { 0x000380e6, 0x00000002 },
430         { 0x040025c5, 0x00000002 },
431         { 0x00007576, 0x00000002 },
432         { 0x00007577, 0x00000002 },
433         { 0x0000750e, 0x00000002 },
434         { 0x0000750f, 0x00000002 },
435         { 0x00a05000, 0x00000002 },
436         { 0x00600083, 0x0000000c },
437         { 0x006075f0, 0x00000021 },
438         { 0x000075f8, 0x00000002 },
439         { 0x00000083, 0x00000004 },
440         { 0x000a750e, 0x00000002 },
441         { 0x000380e6, 0x00000002 },
442         { 0x040025c5, 0x00000002 },
443         { 0x0020750f, 0x00000002 },
444         { 0x00600086, 0x00000004 },
445         { 0x00007570, 0x00000002 },
446         { 0x00007571, 0x00000002 },
447         { 0x00007572, 0x00000006 },
448         { 0x000380e6, 0x00000002 },
449         { 0x040025c5, 0x00000002 },
450         { 0x00005000, 0x00000002 },
451         { 0x00a05000, 0x00000002 },
452         { 0x00007568, 0x00000002 },
453         { 0x00061000, 0x00000002 },
454         { 0x00000095, 0x0000000c },
455         { 0x00058000, 0x00000002 },
456         { 0x0c607562, 0x00000002 },
457         { 0x00000097, 0x00000004 },
458         { 0x000380e6, 0x00000002 },
459         { 0x040025c5, 0x00000002 },
460         { 0x00600096, 0x00000004 },
461         { 0x400070e5, 0000000000 },
462         { 0x000380e6, 0x00000002 },
463         { 0x040025c5, 0x00000002 },
464         { 0x000380e5, 0x00000002 },
465         { 0x000000a8, 0x0000001c },
466         { 0x000650aa, 0x00000018 },
467         { 0x040025bb, 0x00000002 },
468         { 0x000610ab, 0x00000018 },
469         { 0x040075bc, 0000000000 },
470         { 0x000075bb, 0x00000002 },
471         { 0x000075bc, 0000000000 },
472         { 0x00090000, 0x00000006 },
473         { 0x00090000, 0x00000002 },
474         { 0x000d8002, 0x00000006 },
475         { 0x00007832, 0x00000002 },
476         { 0x00005000, 0x00000002 },
477         { 0x000380e7, 0x00000002 },
478         { 0x04002c97, 0x00000002 },
479         { 0x00007820, 0x00000002 },
480         { 0x00007821, 0x00000002 },
481         { 0x00007800, 0000000000 },
482         { 0x01200000, 0x00000002 },
483         { 0x20077000, 0x00000002 },
484         { 0x01200000, 0x00000002 },
485         { 0x20007000, 0x00000002 },
486         { 0x00061000, 0x00000002 },
487         { 0x0120751b, 0x00000002 },
488         { 0x8040750a, 0x00000002 },
489         { 0x8040750b, 0x00000002 },
490         { 0x00110000, 0x00000002 },
491         { 0x000380e5, 0x00000002 },
492         { 0x000000c6, 0x0000001c },
493         { 0x000610ab, 0x00000018 },
494         { 0x844075bd, 0x00000002 },
495         { 0x000610aa, 0x00000018 },
496         { 0x840075bb, 0x00000002 },
497         { 0x000610ab, 0x00000018 },
498         { 0x844075bc, 0x00000002 },
499         { 0x000000c9, 0x00000004 },
500         { 0x804075bd, 0x00000002 },
501         { 0x800075bb, 0x00000002 },
502         { 0x804075bc, 0x00000002 },
503         { 0x00108000, 0x00000002 },
504         { 0x01400000, 0x00000002 },
505         { 0x006000cd, 0x0000000c },
506         { 0x20c07000, 0x00000020 },
507         { 0x000000cf, 0x00000012 },
508         { 0x00800000, 0x00000006 },
509         { 0x0080751d, 0x00000006 },
510         { 0000000000, 0000000000 },
511         { 0x0000775c, 0x00000002 },
512         { 0x00a05000, 0x00000002 },
513         { 0x00661000, 0x00000002 },
514         { 0x0460275d, 0x00000020 },
515         { 0x00004000, 0000000000 },
516         { 0x01e00830, 0x00000002 },
517         { 0x21007000, 0000000000 },
518         { 0x6464614d, 0000000000 },
519         { 0x69687420, 0000000000 },
520         { 0x00000073, 0000000000 },
521         { 0000000000, 0000000000 },
522         { 0x00005000, 0x00000002 },
523         { 0x000380d0, 0x00000002 },
524         { 0x040025e0, 0x00000002 },
525         { 0x000075e1, 0000000000 },
526         { 0x00000001, 0000000000 },
527         { 0x000380e0, 0x00000002 },
528         { 0x04002394, 0x00000002 },
529         { 0x00005000, 0000000000 },
530         { 0000000000, 0000000000 },
531         { 0000000000, 0000000000 },
532         { 0x00000008, 0000000000 },
533         { 0x00000004, 0000000000 },
534         { 0000000000, 0000000000 },
535         { 0000000000, 0000000000 },
536         { 0000000000, 0000000000 },
537         { 0000000000, 0000000000 },
538         { 0000000000, 0000000000 },
539         { 0000000000, 0000000000 },
540         { 0000000000, 0000000000 },
541         { 0000000000, 0000000000 },
542         { 0000000000, 0000000000 },
543         { 0000000000, 0000000000 },
544         { 0000000000, 0000000000 },
545         { 0000000000, 0000000000 },
546         { 0000000000, 0000000000 },
547         { 0000000000, 0000000000 },
548         { 0000000000, 0000000000 },
549         { 0000000000, 0000000000 },
550         { 0000000000, 0000000000 },
551         { 0000000000, 0000000000 },
552         { 0000000000, 0000000000 },
553         { 0000000000, 0000000000 },
554         { 0000000000, 0000000000 },
555         { 0000000000, 0000000000 },
556         { 0000000000, 0000000000 },
557         { 0000000000, 0000000000 },
558 };
559
560
561 int RADEON_READ_PLL(drm_device_t *dev, int addr)
562 {
563         drm_radeon_private_t *dev_priv = dev->dev_private;
564
565         RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
566         return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
567 }
568
569 #if RADEON_FIFO_DEBUG
570 static void radeon_status( drm_radeon_private_t *dev_priv )
571 {
572         printk( "%s:\n", __FUNCTION__ );
573         printk( "RBBM_STATUS = 0x%08x\n",
574                 (unsigned int)RADEON_READ( RADEON_RBBM_STATUS ) );
575         printk( "CP_RB_RTPR = 0x%08x\n",
576                 (unsigned int)RADEON_READ( RADEON_CP_RB_RPTR ) );
577         printk( "CP_RB_WTPR = 0x%08x\n",
578                 (unsigned int)RADEON_READ( RADEON_CP_RB_WPTR ) );
579         printk( "AIC_CNTL = 0x%08x\n",
580                 (unsigned int)RADEON_READ( RADEON_AIC_CNTL ) );
581         printk( "AIC_STAT = 0x%08x\n",
582                 (unsigned int)RADEON_READ( RADEON_AIC_STAT ) );
583         printk( "AIC_PT_BASE = 0x%08x\n",
584                 (unsigned int)RADEON_READ( RADEON_AIC_PT_BASE ) );
585         printk( "TLB_ADDR = 0x%08x\n",
586                 (unsigned int)RADEON_READ( RADEON_AIC_TLB_ADDR ) );
587         printk( "TLB_DATA = 0x%08x\n",
588                 (unsigned int)RADEON_READ( RADEON_AIC_TLB_DATA ) );
589 }
590 #endif
591
592
593 /* ================================================================
594  * Engine, FIFO control
595  */
596
597 static int radeon_do_pixcache_flush( drm_radeon_private_t *dev_priv )
598 {
599         u32 tmp;
600         int i;
601
602         dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
603
604         tmp  = RADEON_READ( RADEON_RB2D_DSTCACHE_CTLSTAT );
605         tmp |= RADEON_RB2D_DC_FLUSH_ALL;
606         RADEON_WRITE( RADEON_RB2D_DSTCACHE_CTLSTAT, tmp );
607
608         for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
609                 if ( !(RADEON_READ( RADEON_RB2D_DSTCACHE_CTLSTAT )
610                        & RADEON_RB2D_DC_BUSY) ) {
611                         return 0;
612                 }
613                 DRM_UDELAY( 1 );
614         }
615
616 #if RADEON_FIFO_DEBUG
617         DRM_ERROR( "failed!\n" );
618         radeon_status( dev_priv );
619 #endif
620         return DRM_ERR(EBUSY);
621 }
622
623 static int radeon_do_wait_for_fifo( drm_radeon_private_t *dev_priv,
624                                     int entries )
625 {
626         int i;
627
628         dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
629
630         for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
631                 int slots = ( RADEON_READ( RADEON_RBBM_STATUS )
632                               & RADEON_RBBM_FIFOCNT_MASK );
633                 if ( slots >= entries ) return 0;
634                 DRM_UDELAY( 1 );
635         }
636
637 #if RADEON_FIFO_DEBUG
638         DRM_ERROR( "failed!\n" );
639         radeon_status( dev_priv );
640 #endif
641         return DRM_ERR(EBUSY);
642 }
643
644 static int radeon_do_wait_for_idle( drm_radeon_private_t *dev_priv )
645 {
646         int i, ret;
647
648         dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
649
650         ret = radeon_do_wait_for_fifo( dev_priv, 64 );
651         if ( ret ) return ret;
652
653         for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
654                 if ( !(RADEON_READ( RADEON_RBBM_STATUS )
655                        & RADEON_RBBM_ACTIVE) ) {
656                         radeon_do_pixcache_flush( dev_priv );
657                         return 0;
658                 }
659                 DRM_UDELAY( 1 );
660         }
661
662 #if RADEON_FIFO_DEBUG
663         DRM_ERROR( "failed!\n" );
664         radeon_status( dev_priv );
665 #endif
666         return DRM_ERR(EBUSY);
667 }
668
669
670 /* ================================================================
671  * CP control, initialization
672  */
673
674 /* Load the microcode for the CP */
675 static void radeon_cp_load_microcode( drm_radeon_private_t *dev_priv )
676 {
677         int i;
678         DRM_DEBUG( "\n" );
679
680         radeon_do_wait_for_idle( dev_priv );
681
682         RADEON_WRITE( RADEON_CP_ME_RAM_ADDR, 0 );
683
684         if (dev_priv->is_r200)
685         {
686                 DRM_INFO("Loading R200 Microcode\n");
687                 for ( i = 0 ; i < 256 ; i++ ) 
688                 {
689                         RADEON_WRITE( RADEON_CP_ME_RAM_DATAH,
690                                       R200_cp_microcode[i][1] );
691                         RADEON_WRITE( RADEON_CP_ME_RAM_DATAL,
692                                       R200_cp_microcode[i][0] );
693                 }
694         }
695         else
696         {
697                 for ( i = 0 ; i < 256 ; i++ ) {
698                         RADEON_WRITE( RADEON_CP_ME_RAM_DATAH,
699                                       radeon_cp_microcode[i][1] );
700                         RADEON_WRITE( RADEON_CP_ME_RAM_DATAL,
701                                       radeon_cp_microcode[i][0] );
702                 }
703         }
704 }
705
706 /* Flush any pending commands to the CP.  This should only be used just
707  * prior to a wait for idle, as it informs the engine that the command
708  * stream is ending.
709  */
710 static void radeon_do_cp_flush( drm_radeon_private_t *dev_priv )
711 {
712         DRM_DEBUG( "\n" );
713 #if 0
714         u32 tmp;
715
716         tmp = RADEON_READ( RADEON_CP_RB_WPTR ) | (1 << 31);
717         RADEON_WRITE( RADEON_CP_RB_WPTR, tmp );
718 #endif
719 }
720
721 /* Wait for the CP to go idle.
722  */
723 int radeon_do_cp_idle( drm_radeon_private_t *dev_priv )
724 {
725         RING_LOCALS;
726         DRM_DEBUG( "\n" );
727
728         BEGIN_RING( 6 );
729
730         RADEON_PURGE_CACHE();
731         RADEON_PURGE_ZCACHE();
732         RADEON_WAIT_UNTIL_IDLE();
733
734         ADVANCE_RING();
735         COMMIT_RING();
736
737         return radeon_do_wait_for_idle( dev_priv );
738 }
739
740 /* Start the Command Processor.
741  */
742 static void radeon_do_cp_start( drm_radeon_private_t *dev_priv )
743 {
744         RING_LOCALS;
745         DRM_DEBUG( "\n" );
746
747         radeon_do_wait_for_idle( dev_priv );
748
749         RADEON_WRITE( RADEON_CP_CSQ_CNTL, dev_priv->cp_mode );
750
751         dev_priv->cp_running = 1;
752
753         BEGIN_RING( 6 );
754
755         RADEON_PURGE_CACHE();
756         RADEON_PURGE_ZCACHE();
757         RADEON_WAIT_UNTIL_IDLE();
758
759         ADVANCE_RING();
760         COMMIT_RING();
761 }
762
763 /* Reset the Command Processor.  This will not flush any pending
764  * commands, so you must wait for the CP command stream to complete
765  * before calling this routine.
766  */
767 static void radeon_do_cp_reset( drm_radeon_private_t *dev_priv )
768 {
769         u32 cur_read_ptr;
770         DRM_DEBUG( "\n" );
771
772         cur_read_ptr = RADEON_READ( RADEON_CP_RB_RPTR );
773         RADEON_WRITE( RADEON_CP_RB_WPTR, cur_read_ptr );
774         SET_RING_HEAD( dev_priv, cur_read_ptr );
775         dev_priv->ring.tail = cur_read_ptr;
776 }
777
778 /* Stop the Command Processor.  This will not flush any pending
779  * commands, so you must flush the command stream and wait for the CP
780  * to go idle before calling this routine.
781  */
782 static void radeon_do_cp_stop( drm_radeon_private_t *dev_priv )
783 {
784         DRM_DEBUG( "\n" );
785
786         RADEON_WRITE( RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS );
787
788         dev_priv->cp_running = 0;
789 }
790
791 /* Reset the engine.  This will stop the CP if it is running.
792  */
793 static int radeon_do_engine_reset( drm_device_t *dev )
794 {
795         drm_radeon_private_t *dev_priv = dev->dev_private;
796         u32 clock_cntl_index, mclk_cntl, rbbm_soft_reset;
797         DRM_DEBUG( "\n" );
798
799         radeon_do_pixcache_flush( dev_priv );
800
801         clock_cntl_index = RADEON_READ( RADEON_CLOCK_CNTL_INDEX );
802         mclk_cntl = RADEON_READ_PLL( dev, RADEON_MCLK_CNTL );
803
804         RADEON_WRITE_PLL( RADEON_MCLK_CNTL, ( mclk_cntl |
805                                               RADEON_FORCEON_MCLKA |
806                                               RADEON_FORCEON_MCLKB |
807                                               RADEON_FORCEON_YCLKA |
808                                               RADEON_FORCEON_YCLKB |
809                                               RADEON_FORCEON_MC |
810                                               RADEON_FORCEON_AIC ) );
811
812         rbbm_soft_reset = RADEON_READ( RADEON_RBBM_SOFT_RESET );
813
814         RADEON_WRITE( RADEON_RBBM_SOFT_RESET, ( rbbm_soft_reset |
815                                                 RADEON_SOFT_RESET_CP |
816                                                 RADEON_SOFT_RESET_HI |
817                                                 RADEON_SOFT_RESET_SE |
818                                                 RADEON_SOFT_RESET_RE |
819                                                 RADEON_SOFT_RESET_PP |
820                                                 RADEON_SOFT_RESET_E2 |
821                                                 RADEON_SOFT_RESET_RB ) );
822         RADEON_READ( RADEON_RBBM_SOFT_RESET );
823         RADEON_WRITE( RADEON_RBBM_SOFT_RESET, ( rbbm_soft_reset &
824                                                 ~( RADEON_SOFT_RESET_CP |
825                                                    RADEON_SOFT_RESET_HI |
826                                                    RADEON_SOFT_RESET_SE |
827                                                    RADEON_SOFT_RESET_RE |
828                                                    RADEON_SOFT_RESET_PP |
829                                                    RADEON_SOFT_RESET_E2 |
830                                                    RADEON_SOFT_RESET_RB ) ) );
831         RADEON_READ( RADEON_RBBM_SOFT_RESET );
832
833
834         RADEON_WRITE_PLL( RADEON_MCLK_CNTL, mclk_cntl );
835         RADEON_WRITE( RADEON_CLOCK_CNTL_INDEX, clock_cntl_index );
836         RADEON_WRITE( RADEON_RBBM_SOFT_RESET,  rbbm_soft_reset );
837
838         /* Reset the CP ring */
839         radeon_do_cp_reset( dev_priv );
840
841         /* The CP is no longer running after an engine reset */
842         dev_priv->cp_running = 0;
843
844         /* Reset any pending vertex, indirect buffers */
845         radeon_freelist_reset( dev );
846
847         return 0;
848 }
849
850 static void radeon_cp_init_ring_buffer( drm_device_t *dev,
851                                         drm_radeon_private_t *dev_priv )
852 {
853         u32 ring_start, cur_read_ptr;
854         u32 tmp;
855
856         /* Initialize the memory controller */
857         RADEON_WRITE( RADEON_MC_FB_LOCATION,
858                       (dev_priv->agp_vm_start - 1) & 0xffff0000 );
859
860         if ( !dev_priv->is_pci ) {
861                 RADEON_WRITE( RADEON_MC_AGP_LOCATION,
862                               (((dev_priv->agp_vm_start - 1 +
863                                  dev_priv->agp_size) & 0xffff0000) |
864                                (dev_priv->agp_vm_start >> 16)) );
865         }
866
867 #if __REALLY_HAVE_AGP
868         if ( !dev_priv->is_pci )
869                 ring_start = (dev_priv->cp_ring->offset
870                               - dev->agp->base
871                               + dev_priv->agp_vm_start);
872        else
873 #endif
874                 ring_start = (dev_priv->cp_ring->offset
875                               - dev->sg->handle
876                               + dev_priv->agp_vm_start);
877
878         RADEON_WRITE( RADEON_CP_RB_BASE, ring_start );
879
880         /* Set the write pointer delay */
881         RADEON_WRITE( RADEON_CP_RB_WPTR_DELAY, 0 );
882
883         /* Initialize the ring buffer's read and write pointers */
884         cur_read_ptr = RADEON_READ( RADEON_CP_RB_RPTR );
885         RADEON_WRITE( RADEON_CP_RB_WPTR, cur_read_ptr );
886         SET_RING_HEAD( dev_priv, cur_read_ptr );
887         dev_priv->ring.tail = cur_read_ptr;
888
889 #if __REALLY_HAVE_AGP
890         if ( !dev_priv->is_pci ) {
891                 RADEON_WRITE( RADEON_CP_RB_RPTR_ADDR,
892                               dev_priv->ring_rptr->offset
893                               - dev->agp->base
894                               + dev_priv->agp_vm_start);
895         } else
896 #endif
897         {
898                 drm_sg_mem_t *entry = dev->sg;
899                 unsigned long tmp_ofs, page_ofs;
900
901                 tmp_ofs = dev_priv->ring_rptr->offset - dev->sg->handle;
902                 page_ofs = tmp_ofs >> PAGE_SHIFT;
903
904                 RADEON_WRITE( RADEON_CP_RB_RPTR_ADDR,
905                              entry->busaddr[page_ofs]);
906                 DRM_DEBUG( "ring rptr: offset=0x%08lx handle=0x%08lx\n",
907                            (unsigned long) entry->busaddr[page_ofs],
908                            entry->handle + tmp_ofs );
909         }
910
911         /* Initialize the scratch register pointer.  This will cause
912          * the scratch register values to be written out to memory
913          * whenever they are updated.
914          *
915          * We simply put this behind the ring read pointer, this works
916          * with PCI GART as well as (whatever kind of) AGP GART
917          */
918         RADEON_WRITE( RADEON_SCRATCH_ADDR, RADEON_READ( RADEON_CP_RB_RPTR_ADDR )
919                                          + RADEON_SCRATCH_REG_OFFSET );
920
921         dev_priv->scratch = ((__volatile__ u32 *)
922                              dev_priv->ring_rptr->handle +
923                              (RADEON_SCRATCH_REG_OFFSET / sizeof(u32)));
924
925         RADEON_WRITE( RADEON_SCRATCH_UMSK, 0x7 );
926
927         /* Writeback doesn't seem to work everywhere, test it first */
928         DRM_WRITE32( dev_priv->ring_rptr, RADEON_SCRATCHOFF(1), 0 );
929         RADEON_WRITE( RADEON_SCRATCH_REG1, 0xdeadbeef );
930
931         for ( tmp = 0 ; tmp < dev_priv->usec_timeout ; tmp++ ) {
932                 if ( DRM_READ32( dev_priv->ring_rptr, RADEON_SCRATCHOFF(1) ) == 0xdeadbeef )
933                         break;
934                 DRM_UDELAY( 1 );
935         }
936
937         if ( tmp < dev_priv->usec_timeout ) {
938                 dev_priv->writeback_works = 1;
939                 DRM_DEBUG( "writeback test succeeded, tmp=%d\n", tmp );
940         } else {
941                 dev_priv->writeback_works = 0;
942                 DRM_DEBUG( "writeback test failed\n" );
943         }
944
945         dev_priv->sarea_priv->last_frame = dev_priv->scratch[0] = 0;
946         RADEON_WRITE( RADEON_LAST_FRAME_REG,
947                       dev_priv->sarea_priv->last_frame );
948
949         dev_priv->sarea_priv->last_dispatch = dev_priv->scratch[1] = 0;
950         RADEON_WRITE( RADEON_LAST_DISPATCH_REG,
951                       dev_priv->sarea_priv->last_dispatch );
952
953         dev_priv->sarea_priv->last_clear = dev_priv->scratch[2] = 0;
954         RADEON_WRITE( RADEON_LAST_CLEAR_REG,
955                       dev_priv->sarea_priv->last_clear );
956
957         /* Set ring buffer size */
958 #ifdef __BIG_ENDIAN
959         RADEON_WRITE( RADEON_CP_RB_CNTL, dev_priv->ring.size_l2qw | RADEON_BUF_SWAP_32BIT );
960 #else
961         RADEON_WRITE( RADEON_CP_RB_CNTL, dev_priv->ring.size_l2qw );
962 #endif
963
964         radeon_do_wait_for_idle( dev_priv );
965
966         /* Turn on bus mastering */
967         tmp = RADEON_READ( RADEON_BUS_CNTL ) & ~RADEON_BUS_MASTER_DIS;
968         RADEON_WRITE( RADEON_BUS_CNTL, tmp );
969
970         /* Sync everything up */
971         RADEON_WRITE( RADEON_ISYNC_CNTL,
972                       (RADEON_ISYNC_ANY2D_IDLE3D |
973                        RADEON_ISYNC_ANY3D_IDLE2D |
974                        RADEON_ISYNC_WAIT_IDLEGUI |
975                        RADEON_ISYNC_CPSCRATCH_IDLEGUI) );
976 }
977
978 static int radeon_do_init_cp( drm_device_t *dev, drm_radeon_init_t *init )
979 {
980         drm_radeon_private_t *dev_priv;
981         u32 tmp;
982         DRM_DEBUG( "\n" );
983
984         dev_priv = DRM(alloc)( sizeof(drm_radeon_private_t), DRM_MEM_DRIVER );
985         if ( dev_priv == NULL )
986                 return DRM_ERR(ENOMEM);
987
988         memset( dev_priv, 0, sizeof(drm_radeon_private_t) );
989
990         dev_priv->is_pci = init->is_pci;
991
992         if ( dev_priv->is_pci && !dev->sg ) {
993                 DRM_ERROR( "PCI GART memory not allocated!\n" );
994                 dev->dev_private = (void *)dev_priv;
995                 radeon_do_cleanup_cp(dev);
996                 return DRM_ERR(EINVAL);
997         }
998
999         dev_priv->usec_timeout = init->usec_timeout;
1000         if ( dev_priv->usec_timeout < 1 ||
1001              dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT ) {
1002                 DRM_DEBUG( "TIMEOUT problem!\n" );
1003                 dev->dev_private = (void *)dev_priv;
1004                 radeon_do_cleanup_cp(dev);
1005                 return DRM_ERR(EINVAL);
1006         }
1007
1008         dev_priv->is_r200 = (init->func == RADEON_INIT_R200_CP);
1009         dev_priv->do_boxes = 0;
1010         dev_priv->cp_mode = init->cp_mode;
1011
1012         /* We don't support anything other than bus-mastering ring mode,
1013          * but the ring can be in either AGP or PCI space for the ring
1014          * read pointer.
1015          */
1016         if ( ( init->cp_mode != RADEON_CSQ_PRIBM_INDDIS ) &&
1017              ( init->cp_mode != RADEON_CSQ_PRIBM_INDBM ) ) {
1018                 DRM_DEBUG( "BAD cp_mode (%x)!\n", init->cp_mode );
1019                 dev->dev_private = (void *)dev_priv;
1020                 radeon_do_cleanup_cp(dev);
1021                 return DRM_ERR(EINVAL);
1022         }
1023
1024         switch ( init->fb_bpp ) {
1025         case 16:
1026                 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
1027                 break;
1028         case 32:
1029         default:
1030                 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
1031                 break;
1032         }
1033         dev_priv->front_offset  = init->front_offset;
1034         dev_priv->front_pitch   = init->front_pitch;
1035         dev_priv->back_offset   = init->back_offset;
1036         dev_priv->back_pitch    = init->back_pitch;
1037
1038         switch ( init->depth_bpp ) {
1039         case 16:
1040                 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
1041                 break;
1042         case 32:
1043         default:
1044                 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
1045                 break;
1046         }
1047         dev_priv->depth_offset  = init->depth_offset;
1048         dev_priv->depth_pitch   = init->depth_pitch;
1049
1050         dev_priv->front_pitch_offset = (((dev_priv->front_pitch/64) << 22) |
1051                                         (dev_priv->front_offset >> 10));
1052         dev_priv->back_pitch_offset = (((dev_priv->back_pitch/64) << 22) |
1053                                        (dev_priv->back_offset >> 10));
1054         dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch/64) << 22) |
1055                                         (dev_priv->depth_offset >> 10));
1056
1057         /* Hardware state for depth clears.  Remove this if/when we no
1058          * longer clear the depth buffer with a 3D rectangle.  Hard-code
1059          * all values to prevent unwanted 3D state from slipping through
1060          * and screwing with the clear operation.
1061          */
1062         dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
1063                                            (dev_priv->color_fmt << 10) |
1064                                            (1<<15));
1065
1066         dev_priv->depth_clear.rb3d_zstencilcntl = 
1067                 (dev_priv->depth_fmt |
1068                  RADEON_Z_TEST_ALWAYS |
1069                  RADEON_STENCIL_TEST_ALWAYS |
1070                  RADEON_STENCIL_S_FAIL_REPLACE |
1071                  RADEON_STENCIL_ZPASS_REPLACE |
1072                  RADEON_STENCIL_ZFAIL_REPLACE |
1073                  RADEON_Z_WRITE_ENABLE);
1074
1075         dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
1076                                          RADEON_BFACE_SOLID |
1077                                          RADEON_FFACE_SOLID |
1078                                          RADEON_FLAT_SHADE_VTX_LAST |
1079                                          RADEON_DIFFUSE_SHADE_FLAT |
1080                                          RADEON_ALPHA_SHADE_FLAT |
1081                                          RADEON_SPECULAR_SHADE_FLAT |
1082                                          RADEON_FOG_SHADE_FLAT |
1083                                          RADEON_VTX_PIX_CENTER_OGL |
1084                                          RADEON_ROUND_MODE_TRUNC |
1085                                          RADEON_ROUND_PREC_8TH_PIX);
1086
1087         DRM_GETSAREA();
1088
1089         dev_priv->fb_offset = init->fb_offset;
1090         dev_priv->mmio_offset = init->mmio_offset;
1091         dev_priv->ring_offset = init->ring_offset;
1092         dev_priv->ring_rptr_offset = init->ring_rptr_offset;
1093         dev_priv->buffers_offset = init->buffers_offset;
1094         dev_priv->agp_textures_offset = init->agp_textures_offset;
1095         
1096         if(!dev_priv->sarea) {
1097                 DRM_ERROR("could not find sarea!\n");
1098                 dev->dev_private = (void *)dev_priv;
1099                 radeon_do_cleanup_cp(dev);
1100                 return DRM_ERR(EINVAL);
1101         }
1102
1103         DRM_FIND_MAP( dev_priv->fb, init->fb_offset );
1104         if(!dev_priv->fb) {
1105                 DRM_ERROR("could not find framebuffer!\n");
1106                 dev->dev_private = (void *)dev_priv;
1107                 radeon_do_cleanup_cp(dev);
1108                 return DRM_ERR(EINVAL);
1109         }
1110         DRM_FIND_MAP( dev_priv->mmio, init->mmio_offset );
1111         if(!dev_priv->mmio) {
1112                 DRM_ERROR("could not find mmio region!\n");
1113                 dev->dev_private = (void *)dev_priv;
1114                 radeon_do_cleanup_cp(dev);
1115                 return DRM_ERR(EINVAL);
1116         }
1117         DRM_FIND_MAP( dev_priv->cp_ring, init->ring_offset );
1118         if(!dev_priv->cp_ring) {
1119                 DRM_ERROR("could not find cp ring region!\n");
1120                 dev->dev_private = (void *)dev_priv;
1121                 radeon_do_cleanup_cp(dev);
1122                 return DRM_ERR(EINVAL);
1123         }
1124         DRM_FIND_MAP( dev_priv->ring_rptr, init->ring_rptr_offset );
1125         if(!dev_priv->ring_rptr) {
1126                 DRM_ERROR("could not find ring read pointer!\n");
1127                 dev->dev_private = (void *)dev_priv;
1128                 radeon_do_cleanup_cp(dev);
1129                 return DRM_ERR(EINVAL);
1130         }
1131         DRM_FIND_MAP( dev_priv->buffers, init->buffers_offset );
1132         if(!dev_priv->buffers) {
1133                 DRM_ERROR("could not find dma buffer region!\n");
1134                 dev->dev_private = (void *)dev_priv;
1135                 radeon_do_cleanup_cp(dev);
1136                 return DRM_ERR(EINVAL);
1137         }
1138
1139         if ( !dev_priv->is_pci ) {
1140                 DRM_FIND_MAP( dev_priv->agp_textures,
1141                               init->agp_textures_offset );
1142                 if(!dev_priv->agp_textures) {
1143                         DRM_ERROR("could not find agp texture region!\n");
1144                         dev->dev_private = (void *)dev_priv;
1145                         radeon_do_cleanup_cp(dev);
1146                         return DRM_ERR(EINVAL);
1147                 }
1148         }
1149
1150         dev_priv->sarea_priv =
1151                 (drm_radeon_sarea_t *)((u8 *)dev_priv->sarea->handle +
1152                                        init->sarea_priv_offset);
1153
1154 #if __REALLY_HAVE_AGP
1155         if ( !dev_priv->is_pci ) {
1156                 DRM_IOREMAP( dev_priv->cp_ring, dev );
1157                 DRM_IOREMAP( dev_priv->ring_rptr, dev );
1158                 DRM_IOREMAP( dev_priv->buffers, dev );
1159                 if(!dev_priv->cp_ring->handle ||
1160                    !dev_priv->ring_rptr->handle ||
1161                    !dev_priv->buffers->handle) {
1162                         DRM_ERROR("could not find ioremap agp regions!\n");
1163                         dev->dev_private = (void *)dev_priv;
1164                         radeon_do_cleanup_cp(dev);
1165                         return DRM_ERR(EINVAL);
1166                 }
1167         } else
1168 #endif
1169         {
1170                 dev_priv->cp_ring->handle =
1171                         (void *)dev_priv->cp_ring->offset;
1172                 dev_priv->ring_rptr->handle =
1173                         (void *)dev_priv->ring_rptr->offset;
1174                 dev_priv->buffers->handle = (void *)dev_priv->buffers->offset;
1175
1176                 DRM_DEBUG( "dev_priv->cp_ring->handle %p\n",
1177                            dev_priv->cp_ring->handle );
1178                 DRM_DEBUG( "dev_priv->ring_rptr->handle %p\n",
1179                            dev_priv->ring_rptr->handle );
1180                 DRM_DEBUG( "dev_priv->buffers->handle %p\n",
1181                            dev_priv->buffers->handle );
1182         }
1183
1184
1185         dev_priv->agp_size = init->agp_size;
1186         dev_priv->agp_vm_start = RADEON_READ( RADEON_CONFIG_APER_SIZE );
1187 #if __REALLY_HAVE_AGP
1188         if ( !dev_priv->is_pci )
1189                 dev_priv->agp_buffers_offset = (dev_priv->buffers->offset
1190                                                 - dev->agp->base
1191                                                 + dev_priv->agp_vm_start);
1192         else
1193 #endif
1194                 dev_priv->agp_buffers_offset = (dev_priv->buffers->offset
1195                                                 - dev->sg->handle
1196                                                 + dev_priv->agp_vm_start);
1197
1198         DRM_DEBUG( "dev_priv->agp_size %d\n",
1199                    dev_priv->agp_size );
1200         DRM_DEBUG( "dev_priv->agp_vm_start 0x%x\n",
1201                    dev_priv->agp_vm_start );
1202         DRM_DEBUG( "dev_priv->agp_buffers_offset 0x%lx\n",
1203                    dev_priv->agp_buffers_offset );
1204
1205         dev_priv->ring.start = (u32 *)dev_priv->cp_ring->handle;
1206         dev_priv->ring.end = ((u32 *)dev_priv->cp_ring->handle
1207                               + init->ring_size / sizeof(u32));
1208         dev_priv->ring.size = init->ring_size;
1209         dev_priv->ring.size_l2qw = DRM(order)( init->ring_size / 8 );
1210
1211         dev_priv->ring.tail_mask =
1212                 (dev_priv->ring.size / sizeof(u32)) - 1;
1213
1214         dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1215
1216         if ( dev_priv->is_pci ) {
1217                 if (!DRM(ati_pcigart_init)( dev, &dev_priv->phys_pci_gart,
1218                                             &dev_priv->bus_pci_gart)) {
1219                         DRM_ERROR( "failed to init PCI GART!\n" );
1220                         dev->dev_private = (void *)dev_priv;
1221                         radeon_do_cleanup_cp(dev);
1222                         return DRM_ERR(ENOMEM);
1223                 }
1224                 /* Turn on PCI GART
1225                  */
1226                 tmp = RADEON_READ( RADEON_AIC_CNTL )
1227                       | RADEON_PCIGART_TRANSLATE_EN;
1228                 RADEON_WRITE( RADEON_AIC_CNTL, tmp );
1229
1230                 /* set PCI GART page-table base address
1231                  */
1232                 RADEON_WRITE( RADEON_AIC_PT_BASE, dev_priv->bus_pci_gart );
1233
1234                 /* set address range for PCI address translate
1235                  */
1236                 RADEON_WRITE( RADEON_AIC_LO_ADDR, dev_priv->agp_vm_start );
1237                 RADEON_WRITE( RADEON_AIC_HI_ADDR, dev_priv->agp_vm_start
1238                                                   + dev_priv->agp_size - 1);
1239
1240                 /* Turn off AGP aperture -- is this required for PCIGART?
1241                  */
1242                 RADEON_WRITE( RADEON_MC_AGP_LOCATION, 0xffffffc0 ); /* ?? */
1243                 RADEON_WRITE( RADEON_AGP_COMMAND, 0 ); /* clear AGP_COMMAND */
1244         } else {
1245                 /* Turn off PCI GART
1246                  */
1247                 tmp = RADEON_READ( RADEON_AIC_CNTL )
1248                       & ~RADEON_PCIGART_TRANSLATE_EN;
1249                 RADEON_WRITE( RADEON_AIC_CNTL, tmp );
1250         }
1251
1252         radeon_cp_load_microcode( dev_priv );
1253         radeon_cp_init_ring_buffer( dev, dev_priv );
1254
1255         dev_priv->last_buf = 0;
1256
1257         dev->dev_private = (void *)dev_priv;
1258
1259         radeon_do_engine_reset( dev );
1260
1261         return 0;
1262 }
1263
1264 int radeon_do_cleanup_cp( drm_device_t *dev )
1265 {
1266         DRM_DEBUG( "\n" );
1267
1268 #if _HAVE_DMA_IRQ
1269         /* Make sure interrupts are disabled here because the uninstall ioctl
1270          * may not have been called from userspace and after dev_private
1271          * is freed, it's too late.
1272          */
1273         if ( dev->irq ) DRM(irq_uninstall)(dev);
1274 #endif
1275
1276         if ( dev->dev_private ) {
1277                 drm_radeon_private_t *dev_priv = dev->dev_private;
1278
1279 #if __REALLY_HAVE_AGP
1280                 if ( !dev_priv->is_pci ) {
1281                         if ( dev_priv->cp_ring != NULL )
1282                                 DRM_IOREMAPFREE( dev_priv->cp_ring, dev );
1283                         if ( dev_priv->ring_rptr != NULL )
1284                                 DRM_IOREMAPFREE( dev_priv->ring_rptr, dev );
1285                         if ( dev_priv->buffers != NULL )
1286                                 DRM_IOREMAPFREE( dev_priv->buffers, dev );
1287                 } else
1288 #endif
1289                 {
1290                         if (!DRM(ati_pcigart_cleanup)( dev,
1291                                                 dev_priv->phys_pci_gart,
1292                                                 dev_priv->bus_pci_gart ))
1293                                 DRM_ERROR( "failed to cleanup PCI GART!\n" );
1294                 }
1295
1296                 DRM(free)( dev->dev_private, sizeof(drm_radeon_private_t),
1297                            DRM_MEM_DRIVER );
1298                 dev->dev_private = NULL;
1299         }
1300
1301         return 0;
1302 }
1303
1304 /* This code will reinit the Radeon CP hardware after a resume from disc.  
1305  * AFAIK, it would be very difficult to pickle the state at suspend time, so 
1306  * here we make sure that all Radeon hardware initialisation is re-done without
1307  * affecting running applications.  This function is called radeon_do_resume_cp()
1308  * as it was derived from radeon_init_cp, where most of the initialisation takes
1309  * place during DRI init.
1310  *
1311  * This patch is NOT to be confused with my and Michel Daenzer's earlier DRI
1312  * reinit work, which de- and re-initialised the complete DRI at every VT
1313  * switch.
1314  *
1315  * Charl P. Botha <http://cpbotha.net>
1316  */
1317 static int radeon_do_resume_cp( drm_device_t *dev)
1318 {
1319         drm_radeon_private_t *dev_priv;
1320         u32 tmp;
1321         DRM_DEBUG( "\n" );
1322         
1323         DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1324
1325         /* get the existing dev_private */
1326         dev_priv = dev->dev_private;
1327
1328 #if !defined(PCIGART_ENABLED)
1329         /* PCI support is not 100% working, so we disable it here.
1330          */
1331         if ( dev_priv->is_pci ) {
1332                 DRM_ERROR( "PCI GART not yet supported for Radeon!\n" );
1333                 radeon_do_cleanup_cp(dev);
1334                 return DRM_ERR(EINVAL);
1335         }
1336 #endif
1337
1338         if ( dev_priv->is_pci && !dev->sg ) {
1339                 DRM_ERROR( "PCI GART memory not allocated!\n" );
1340                 radeon_do_cleanup_cp(dev);
1341                 return DRM_ERR(EINVAL);
1342         }
1343
1344         if ( dev_priv->usec_timeout < 1 ||
1345              dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT ) {
1346                 DRM_DEBUG( "TIMEOUT problem!\n" );
1347                 radeon_do_cleanup_cp(dev);
1348                 return DRM_ERR(EINVAL);
1349         }
1350
1351         if ( ( dev_priv->cp_mode != RADEON_CSQ_PRIBM_INDDIS ) &&
1352              ( dev_priv->cp_mode != RADEON_CSQ_PRIBM_INDBM ) ) {
1353                 DRM_DEBUG( "BAD cp_mode (%x)!\n", dev_priv->cp_mode );
1354                 radeon_do_cleanup_cp(dev);
1355                 return DRM_ERR(EINVAL);
1356         }
1357
1358         if(!dev_priv->sarea) {
1359                 DRM_ERROR("could not find sarea!\n");
1360                 radeon_do_cleanup_cp(dev);
1361                 return DRM_ERR(EINVAL);
1362         }
1363
1364         if(!dev_priv->fb) {
1365                 DRM_ERROR("could not find framebuffer!\n");
1366                 radeon_do_cleanup_cp(dev);
1367                 return DRM_ERR(EINVAL);
1368         }
1369
1370         if(!dev_priv->mmio) {
1371                 DRM_ERROR("could not find mmio region!\n");
1372                 radeon_do_cleanup_cp(dev);
1373                 return DRM_ERR(EINVAL);
1374         }
1375
1376         if(!dev_priv->cp_ring) {
1377                 DRM_ERROR("could not find cp ring region!\n");
1378                 radeon_do_cleanup_cp(dev);
1379                 return DRM_ERR(EINVAL);
1380         }
1381
1382         if(!dev_priv->ring_rptr) {
1383                 DRM_ERROR("could not find ring read pointer!\n");
1384                 radeon_do_cleanup_cp(dev);
1385                 return DRM_ERR(EINVAL);
1386         }
1387
1388         if(!dev_priv->buffers) {
1389                 DRM_ERROR("could not find dma buffer region!\n");
1390                 radeon_do_cleanup_cp(dev);
1391                 return DRM_ERR(EINVAL);
1392         }
1393
1394         if ( !dev_priv->is_pci ) {
1395                 if(!dev_priv->agp_textures) {
1396                         DRM_ERROR("could not find agp texture region!\n");
1397                         radeon_do_cleanup_cp(dev);
1398                         return DRM_ERR(EINVAL);
1399                 }
1400         }
1401
1402         if ( !dev_priv->is_pci ) {
1403                 if(!dev_priv->cp_ring->handle ||
1404                    !dev_priv->ring_rptr->handle ||
1405                    !dev_priv->buffers->handle) {
1406                         DRM_ERROR("could not find ioremap agp regions!\n");
1407                         radeon_do_cleanup_cp(dev);
1408                         return DRM_ERR(EINVAL);
1409                 }
1410         } else {
1411                 DRM_DEBUG( "dev_priv->cp_ring->handle %p\n",
1412                            dev_priv->cp_ring->handle );
1413                 DRM_DEBUG( "dev_priv->ring_rptr->handle %p\n",
1414                            dev_priv->ring_rptr->handle );
1415                 DRM_DEBUG( "dev_priv->buffers->handle %p\n",
1416                            dev_priv->buffers->handle );
1417         }
1418
1419
1420         DRM_DEBUG( "dev_priv->agp_size %d\n",
1421                    dev_priv->agp_size );
1422         DRM_DEBUG( "dev_priv->agp_vm_start 0x%x\n",
1423                    dev_priv->agp_vm_start );
1424         DRM_DEBUG( "dev_priv->agp_buffers_offset 0x%lx\n",
1425                    dev_priv->agp_buffers_offset );
1426
1427 #if __REALLY_HAVE_AGP
1428         if ( !dev_priv->is_pci ) {
1429                 /* Turn off PCI GART
1430                  */
1431                 tmp = RADEON_READ( RADEON_AIC_CNTL )
1432                       & ~RADEON_PCIGART_TRANSLATE_EN;
1433                 RADEON_WRITE( RADEON_AIC_CNTL, tmp );
1434         } else
1435 #endif
1436         {
1437                 /* I'm not so sure about this ati_picgart_init after at resume-time... */
1438                 if (!DRM(ati_pcigart_init)( dev, &dev_priv->phys_pci_gart,
1439                                             &dev_priv->bus_pci_gart)) {   
1440                     DRM_ERROR( "failed to init PCI GART!\n" );        
1441                     radeon_do_cleanup_cp(dev);                        
1442                     return DRM_ERR(ENOMEM);                           
1443                 }
1444
1445                 tmp = RADEON_READ( RADEON_AIC_CNTL )
1446                       | RADEON_PCIGART_TRANSLATE_EN;
1447                 RADEON_WRITE( RADEON_AIC_CNTL, tmp );
1448
1449                 /* set PCI GART page-table base address
1450                  */
1451                 RADEON_WRITE( RADEON_AIC_PT_BASE, dev_priv->bus_pci_gart );
1452
1453                 /* set address range for PCI address translate
1454                  */
1455                 RADEON_WRITE( RADEON_AIC_LO_ADDR, dev_priv->agp_vm_start );
1456                 RADEON_WRITE( RADEON_AIC_HI_ADDR, dev_priv->agp_vm_start
1457                                                   + dev_priv->agp_size - 1);
1458
1459                 /* Turn off AGP aperture -- is this required for PCIGART?
1460                  */
1461                 RADEON_WRITE( RADEON_MC_AGP_LOCATION, 0xffffffc0 ); /* ?? */
1462                 RADEON_WRITE( RADEON_AGP_COMMAND, 0 ); /* clear AGP_COMMAND */
1463         }
1464
1465         radeon_cp_load_microcode( dev_priv );
1466         radeon_cp_init_ring_buffer( dev, dev_priv );
1467
1468         radeon_do_engine_reset( dev );
1469
1470         return 0;
1471 }
1472
1473
1474 int radeon_cp_init( DRM_IOCTL_ARGS )
1475 {
1476         DRM_DEVICE;
1477         drm_radeon_init_t init;
1478
1479         LOCK_TEST_WITH_RETURN( dev, filp );
1480
1481         DRM_COPY_FROM_USER_IOCTL( init, (drm_radeon_init_t *)data, sizeof(init) );
1482
1483         switch ( init.func ) {
1484         case RADEON_INIT_CP:
1485         case RADEON_INIT_R200_CP:
1486                 return radeon_do_init_cp( dev, &init );
1487         case RADEON_CLEANUP_CP:
1488                 return radeon_do_cleanup_cp( dev );
1489         }
1490
1491         return DRM_ERR(EINVAL);
1492 }
1493
1494 int radeon_cp_start( DRM_IOCTL_ARGS )
1495 {
1496         DRM_DEVICE;
1497         drm_radeon_private_t *dev_priv = dev->dev_private;
1498         DRM_DEBUG( "\n" );
1499
1500         LOCK_TEST_WITH_RETURN( dev, filp );
1501
1502         if ( dev_priv->cp_running ) {
1503                 DRM_DEBUG( "%s while CP running\n", __FUNCTION__ );
1504                 return 0;
1505         }
1506         if ( dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS ) {
1507                 DRM_DEBUG( "%s called with bogus CP mode (%d)\n",
1508                            __FUNCTION__, dev_priv->cp_mode );
1509                 return 0;
1510         }
1511
1512         radeon_do_cp_start( dev_priv );
1513
1514         return 0;
1515 }
1516
1517 /* Stop the CP.  The engine must have been idled before calling this
1518  * routine.
1519  */
1520 int radeon_cp_stop( DRM_IOCTL_ARGS )
1521 {
1522         DRM_DEVICE;
1523         drm_radeon_private_t *dev_priv = dev->dev_private;
1524         drm_radeon_cp_stop_t stop;
1525         int ret;
1526         DRM_DEBUG( "\n" );
1527
1528         LOCK_TEST_WITH_RETURN( dev, filp );
1529
1530         DRM_COPY_FROM_USER_IOCTL( stop, (drm_radeon_cp_stop_t *)data, sizeof(stop) );
1531
1532         if (!dev_priv->cp_running)
1533                 return 0;
1534
1535         /* Flush any pending CP commands.  This ensures any outstanding
1536          * commands are exectuted by the engine before we turn it off.
1537          */
1538         if ( stop.flush ) {
1539                 radeon_do_cp_flush( dev_priv );
1540         }
1541
1542         /* If we fail to make the engine go idle, we return an error
1543          * code so that the DRM ioctl wrapper can try again.
1544          */
1545         if ( stop.idle ) {
1546                 ret = radeon_do_cp_idle( dev_priv );
1547                 if ( ret ) return ret;
1548         }
1549
1550         /* Finally, we can turn off the CP.  If the engine isn't idle,
1551          * we will get some dropped triangles as they won't be fully
1552          * rendered before the CP is shut down.
1553          */
1554         radeon_do_cp_stop( dev_priv );
1555
1556         /* Reset the engine */
1557         radeon_do_engine_reset( dev );
1558
1559         return 0;
1560 }
1561
1562
1563 void radeon_do_release( drm_device_t *dev )
1564 {
1565         drm_radeon_private_t *dev_priv = dev->dev_private;
1566         int ret;
1567
1568         if (dev_priv) {
1569                 if (dev_priv->cp_running) {
1570                         /* Stop the cp */
1571                         while ((ret = radeon_do_cp_idle( dev_priv )) != 0) {
1572                                 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1573 #ifdef __linux__
1574                                 schedule();
1575 #else
1576                                 tsleep(&ret, PZERO, "rdnrel", 1);
1577 #endif
1578                         }
1579                         radeon_do_cp_stop( dev_priv );
1580                         radeon_do_engine_reset( dev );
1581                 }
1582
1583                 /* Disable *all* interrupts */
1584                 RADEON_WRITE( RADEON_GEN_INT_CNTL, 0 );
1585
1586                 /* Free memory heap structures */
1587                 radeon_mem_takedown( &(dev_priv->agp_heap) );
1588                 radeon_mem_takedown( &(dev_priv->fb_heap) );
1589
1590                 /* deallocate kernel resources */
1591                 radeon_do_cleanup_cp( dev );
1592         }
1593 }
1594
1595 /* Just reset the CP ring.  Called as part of an X Server engine reset.
1596  */
1597 int radeon_cp_reset( DRM_IOCTL_ARGS )
1598 {
1599         DRM_DEVICE;
1600         drm_radeon_private_t *dev_priv = dev->dev_private;
1601         DRM_DEBUG( "\n" );
1602
1603         LOCK_TEST_WITH_RETURN( dev, filp );
1604
1605         if ( !dev_priv ) {
1606                 DRM_DEBUG( "%s called before init done\n", __FUNCTION__ );
1607                 return DRM_ERR(EINVAL);
1608         }
1609
1610         radeon_do_cp_reset( dev_priv );
1611
1612         /* The CP is no longer running after an engine reset */
1613         dev_priv->cp_running = 0;
1614
1615         return 0;
1616 }
1617
1618 int radeon_cp_idle( DRM_IOCTL_ARGS )
1619 {
1620         DRM_DEVICE;
1621         drm_radeon_private_t *dev_priv = dev->dev_private;
1622         DRM_DEBUG( "\n" );
1623
1624         LOCK_TEST_WITH_RETURN( dev, filp );
1625
1626         return radeon_do_cp_idle( dev_priv );
1627 }
1628
1629 /* Added by Charl P. Botha to call radeon_do_resume_cp().
1630  */
1631 int radeon_cp_resume( DRM_IOCTL_ARGS )
1632 {
1633         DRM_DEVICE;
1634
1635         return radeon_do_resume_cp(dev);
1636 }
1637
1638
1639 int radeon_engine_reset( DRM_IOCTL_ARGS )
1640 {
1641         DRM_DEVICE;
1642         DRM_DEBUG( "\n" );
1643
1644         LOCK_TEST_WITH_RETURN( dev, filp );
1645
1646         return radeon_do_engine_reset( dev );
1647 }
1648
1649
1650 /* ================================================================
1651  * Fullscreen mode
1652  */
1653
1654 /* KW: Deprecated to say the least:
1655  */
1656 int radeon_fullscreen( DRM_IOCTL_ARGS )
1657 {
1658         return 0;
1659 }
1660
1661
1662 /* ================================================================
1663  * Freelist management
1664  */
1665
1666 /* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1667  *   bufs until freelist code is used.  Note this hides a problem with
1668  *   the scratch register * (used to keep track of last buffer
1669  *   completed) being written to before * the last buffer has actually
1670  *   completed rendering.  
1671  *
1672  * KW:  It's also a good way to find free buffers quickly.
1673  *
1674  * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1675  * sleep.  However, bugs in older versions of radeon_accel.c mean that
1676  * we essentially have to do this, else old clients will break.
1677  * 
1678  * However, it does leave open a potential deadlock where all the
1679  * buffers are held by other clients, which can't release them because
1680  * they can't get the lock.  
1681  */
1682
1683 drm_buf_t *radeon_freelist_get( drm_device_t *dev )
1684 {
1685         drm_device_dma_t *dma = dev->dma;
1686         drm_radeon_private_t *dev_priv = dev->dev_private;
1687         drm_radeon_buf_priv_t *buf_priv;
1688         drm_buf_t *buf;
1689         int i, t;
1690         int start;
1691
1692         if ( ++dev_priv->last_buf >= dma->buf_count )
1693                 dev_priv->last_buf = 0;
1694
1695         start = dev_priv->last_buf;
1696
1697         for ( t = 0 ; t < dev_priv->usec_timeout ; t++ ) {
1698                 u32 done_age = GET_SCRATCH( 1 );
1699                 DRM_DEBUG("done_age = %d\n",done_age);
1700                 for ( i = start ; i < dma->buf_count ; i++ ) {
1701                         buf = dma->buflist[i];
1702                         buf_priv = buf->dev_private;
1703                         if ( buf->filp == 0 || (buf->pending && 
1704                                                buf_priv->age <= done_age) ) {
1705                                 dev_priv->stats.requested_bufs++;
1706                                 buf->pending = 0;
1707                                 return buf;
1708                         }
1709                         start = 0;
1710                 }
1711
1712                 if (t) {
1713                         DRM_UDELAY( 1 );
1714                         dev_priv->stats.freelist_loops++;
1715                 }
1716         }
1717
1718         DRM_DEBUG( "returning NULL!\n" );
1719         return NULL;
1720 }
1721 #if 0
1722 drm_buf_t *radeon_freelist_get( drm_device_t *dev )
1723 {
1724         drm_device_dma_t *dma = dev->dma;
1725         drm_radeon_private_t *dev_priv = dev->dev_private;
1726         drm_radeon_buf_priv_t *buf_priv;
1727         drm_buf_t *buf;
1728         int i, t;
1729         int start;
1730         u32 done_age = DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1));
1731
1732         if ( ++dev_priv->last_buf >= dma->buf_count )
1733                 dev_priv->last_buf = 0;
1734
1735         start = dev_priv->last_buf;
1736         dev_priv->stats.freelist_loops++;
1737         
1738         for ( t = 0 ; t < 2 ; t++ ) {
1739                 for ( i = start ; i < dma->buf_count ; i++ ) {
1740                         buf = dma->buflist[i];
1741                         buf_priv = buf->dev_private;
1742                         if ( buf->filp == 0 || (buf->pending && 
1743                                                buf_priv->age <= done_age) ) {
1744                                 dev_priv->stats.requested_bufs++;
1745                                 buf->pending = 0;
1746                                 return buf;
1747                         }
1748                 }
1749                 start = 0;
1750         }
1751
1752         return NULL;
1753 }
1754 #endif
1755
1756 void radeon_freelist_reset( drm_device_t *dev )
1757 {
1758         drm_device_dma_t *dma = dev->dma;
1759         drm_radeon_private_t *dev_priv = dev->dev_private;
1760         int i;
1761
1762         dev_priv->last_buf = 0;
1763         for ( i = 0 ; i < dma->buf_count ; i++ ) {
1764                 drm_buf_t *buf = dma->buflist[i];
1765                 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1766                 buf_priv->age = 0;
1767         }
1768 }
1769
1770
1771 /* ================================================================
1772  * CP command submission
1773  */
1774
1775 int radeon_wait_ring( drm_radeon_private_t *dev_priv, int n )
1776 {
1777         drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1778         int i;
1779         u32 last_head = GET_RING_HEAD( dev_priv );
1780
1781         for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
1782                 u32 head = GET_RING_HEAD( dev_priv );
1783
1784                 ring->space = (head - ring->tail) * sizeof(u32);
1785                 if ( ring->space <= 0 )
1786                         ring->space += ring->size;
1787                 if ( ring->space > n )
1788                         return 0;
1789                 
1790                 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1791
1792                 if (head != last_head)
1793                         i = 0;
1794                 last_head = head;
1795
1796                 DRM_UDELAY( 1 );
1797         }
1798
1799         /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1800 #if RADEON_FIFO_DEBUG
1801         radeon_status( dev_priv );
1802         DRM_ERROR( "failed!\n" );
1803 #endif
1804         return DRM_ERR(EBUSY);
1805 }
1806
1807 static int radeon_cp_get_buffers( DRMFILE filp, drm_device_t *dev, drm_dma_t *d )
1808 {
1809         int i;
1810         drm_buf_t *buf;
1811
1812         for ( i = d->granted_count ; i < d->request_count ; i++ ) {
1813                 buf = radeon_freelist_get( dev );
1814                 if ( !buf ) return DRM_ERR(EBUSY); /* NOTE: broken client */
1815
1816                 buf->filp = filp;
1817
1818                 if ( DRM_COPY_TO_USER( &d->request_indices[i], &buf->idx,
1819                                    sizeof(buf->idx) ) )
1820                         return DRM_ERR(EFAULT);
1821                 if ( DRM_COPY_TO_USER( &d->request_sizes[i], &buf->total,
1822                                    sizeof(buf->total) ) )
1823                         return DRM_ERR(EFAULT);
1824
1825                 d->granted_count++;
1826         }
1827         return 0;
1828 }
1829
1830 int radeon_cp_buffers( DRM_IOCTL_ARGS )
1831 {
1832         DRM_DEVICE;
1833         drm_device_dma_t *dma = dev->dma;
1834         int ret = 0;
1835         drm_dma_t d;
1836
1837         LOCK_TEST_WITH_RETURN( dev, filp );
1838
1839         DRM_COPY_FROM_USER_IOCTL( d, (drm_dma_t *)data, sizeof(d) );
1840
1841         /* Please don't send us buffers.
1842          */
1843         if ( d.send_count != 0 ) {
1844                 DRM_ERROR( "Process %d trying to send %d buffers via drmDMA\n",
1845                            DRM_CURRENTPID, d.send_count );
1846                 return DRM_ERR(EINVAL);
1847         }
1848
1849         /* We'll send you buffers.
1850          */
1851         if ( d.request_count < 0 || d.request_count > dma->buf_count ) {
1852                 DRM_ERROR( "Process %d trying to get %d buffers (of %d max)\n",
1853                            DRM_CURRENTPID, d.request_count, dma->buf_count );
1854                 return DRM_ERR(EINVAL);
1855         }
1856
1857         d.granted_count = 0;
1858
1859         if ( d.request_count ) {
1860                 ret = radeon_cp_get_buffers( filp, dev, &d );
1861         }
1862
1863         DRM_COPY_TO_USER_IOCTL( (drm_dma_t *)data, d, sizeof(d) );
1864
1865         return ret;
1866 }