]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/gpu/drm/radeon/radeon.h
drm/radeon: make cp variable an array
[mv-sheeva.git] / drivers / gpu / drm / radeon / radeon.h
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alex Deucher
26  *          Jerome Glisse
27  */
28 #ifndef __RADEON_H__
29 #define __RADEON_H__
30
31 /* TODO: Here are things that needs to be done :
32  *      - surface allocator & initializer : (bit like scratch reg) should
33  *        initialize HDP_ stuff on RS600, R600, R700 hw, well anythings
34  *        related to surface
35  *      - WB : write back stuff (do it bit like scratch reg things)
36  *      - Vblank : look at Jesse's rework and what we should do
37  *      - r600/r700: gart & cp
38  *      - cs : clean cs ioctl use bitmap & things like that.
39  *      - power management stuff
40  *      - Barrier in gart code
41  *      - Unmappabled vram ?
42  *      - TESTING, TESTING, TESTING
43  */
44
45 /* Initialization path:
46  *  We expect that acceleration initialization might fail for various
47  *  reasons even thought we work hard to make it works on most
48  *  configurations. In order to still have a working userspace in such
49  *  situation the init path must succeed up to the memory controller
50  *  initialization point. Failure before this point are considered as
51  *  fatal error. Here is the init callchain :
52  *      radeon_device_init  perform common structure, mutex initialization
53  *      asic_init           setup the GPU memory layout and perform all
54  *                          one time initialization (failure in this
55  *                          function are considered fatal)
56  *      asic_startup        setup the GPU acceleration, in order to
57  *                          follow guideline the first thing this
58  *                          function should do is setting the GPU
59  *                          memory controller (only MC setup failure
60  *                          are considered as fatal)
61  */
62
63 #include <linux/atomic.h>
64 #include <linux/wait.h>
65 #include <linux/list.h>
66 #include <linux/kref.h>
67
68 #include <ttm/ttm_bo_api.h>
69 #include <ttm/ttm_bo_driver.h>
70 #include <ttm/ttm_placement.h>
71 #include <ttm/ttm_module.h>
72 #include <ttm/ttm_execbuf_util.h>
73
74 #include "radeon_family.h"
75 #include "radeon_mode.h"
76 #include "radeon_reg.h"
77
78 /*
79  * Modules parameters.
80  */
81 extern int radeon_no_wb;
82 extern int radeon_modeset;
83 extern int radeon_dynclks;
84 extern int radeon_r4xx_atom;
85 extern int radeon_agpmode;
86 extern int radeon_vram_limit;
87 extern int radeon_gart_size;
88 extern int radeon_benchmarking;
89 extern int radeon_testing;
90 extern int radeon_connector_table;
91 extern int radeon_tv;
92 extern int radeon_audio;
93 extern int radeon_disp_priority;
94 extern int radeon_hw_i2c;
95 extern int radeon_pcie_gen2;
96 extern int radeon_msi;
97
98 /*
99  * Copy from radeon_drv.h so we don't have to include both and have conflicting
100  * symbol;
101  */
102 #define RADEON_MAX_USEC_TIMEOUT         100000  /* 100 ms */
103 #define RADEON_FENCE_JIFFIES_TIMEOUT    (HZ / 2)
104 /* RADEON_IB_POOL_SIZE must be a power of 2 */
105 #define RADEON_IB_POOL_SIZE             16
106 #define RADEON_DEBUGFS_MAX_COMPONENTS   32
107 #define RADEONFB_CONN_LIMIT             4
108 #define RADEON_BIOS_NUM_SCRATCH         8
109
110 /*
111  * Errata workarounds.
112  */
113 enum radeon_pll_errata {
114         CHIP_ERRATA_R300_CG             = 0x00000001,
115         CHIP_ERRATA_PLL_DUMMYREADS      = 0x00000002,
116         CHIP_ERRATA_PLL_DELAY           = 0x00000004
117 };
118
119
120 struct radeon_device;
121
122
123 /*
124  * BIOS.
125  */
126 #define ATRM_BIOS_PAGE 4096
127
128 #if defined(CONFIG_VGA_SWITCHEROO)
129 bool radeon_atrm_supported(struct pci_dev *pdev);
130 int radeon_atrm_get_bios_chunk(uint8_t *bios, int offset, int len);
131 #else
132 static inline bool radeon_atrm_supported(struct pci_dev *pdev)
133 {
134         return false;
135 }
136
137 static inline int radeon_atrm_get_bios_chunk(uint8_t *bios, int offset, int len){
138         return -EINVAL;
139 }
140 #endif
141 bool radeon_get_bios(struct radeon_device *rdev);
142
143
144 /*
145  * Dummy page
146  */
147 struct radeon_dummy_page {
148         struct page     *page;
149         dma_addr_t      addr;
150 };
151 int radeon_dummy_page_init(struct radeon_device *rdev);
152 void radeon_dummy_page_fini(struct radeon_device *rdev);
153
154
155 /*
156  * Clocks
157  */
158 struct radeon_clock {
159         struct radeon_pll p1pll;
160         struct radeon_pll p2pll;
161         struct radeon_pll dcpll;
162         struct radeon_pll spll;
163         struct radeon_pll mpll;
164         /* 10 Khz units */
165         uint32_t default_mclk;
166         uint32_t default_sclk;
167         uint32_t default_dispclk;
168         uint32_t dp_extclk;
169         uint32_t max_pixel_clock;
170 };
171
172 /*
173  * Power management
174  */
175 int radeon_pm_init(struct radeon_device *rdev);
176 void radeon_pm_fini(struct radeon_device *rdev);
177 void radeon_pm_compute_clocks(struct radeon_device *rdev);
178 void radeon_pm_suspend(struct radeon_device *rdev);
179 void radeon_pm_resume(struct radeon_device *rdev);
180 void radeon_combios_get_power_modes(struct radeon_device *rdev);
181 void radeon_atombios_get_power_modes(struct radeon_device *rdev);
182 void radeon_atom_set_voltage(struct radeon_device *rdev, u16 voltage_level, u8 voltage_type);
183 int radeon_atom_get_max_vddc(struct radeon_device *rdev, u16 *voltage);
184 void rs690_pm_info(struct radeon_device *rdev);
185 extern int rv6xx_get_temp(struct radeon_device *rdev);
186 extern int rv770_get_temp(struct radeon_device *rdev);
187 extern int evergreen_get_temp(struct radeon_device *rdev);
188 extern int sumo_get_temp(struct radeon_device *rdev);
189
190 /*
191  * Fences.
192  */
193 struct radeon_fence_driver {
194         uint32_t                        scratch_reg;
195         atomic_t                        seq;
196         uint32_t                        last_seq;
197         unsigned long                   last_jiffies;
198         unsigned long                   last_timeout;
199         wait_queue_head_t               queue;
200         struct list_head                created;
201         struct list_head                emitted;
202         struct list_head                signaled;
203         bool                            initialized;
204 };
205
206 struct radeon_fence {
207         struct radeon_device            *rdev;
208         struct kref                     kref;
209         struct list_head                list;
210         /* protected by radeon_fence.lock */
211         uint32_t                        seq;
212         bool                            emitted;
213         bool                            signaled;
214         /* RB, DMA, etc. */
215         int                             ring;
216 };
217
218 int radeon_fence_driver_init(struct radeon_device *rdev, int num_rings);
219 void radeon_fence_driver_fini(struct radeon_device *rdev);
220 int radeon_fence_create(struct radeon_device *rdev, struct radeon_fence **fence, int ring);
221 int radeon_fence_emit(struct radeon_device *rdev, struct radeon_fence *fence);
222 void radeon_fence_process(struct radeon_device *rdev, int ring);
223 bool radeon_fence_signaled(struct radeon_fence *fence);
224 int radeon_fence_wait(struct radeon_fence *fence, bool interruptible);
225 int radeon_fence_wait_next(struct radeon_device *rdev, int ring);
226 int radeon_fence_wait_last(struct radeon_device *rdev, int ring);
227 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence);
228 void radeon_fence_unref(struct radeon_fence **fence);
229
230 /*
231  * Semaphores.
232  */
233 struct radeon_cp;
234
235 struct radeon_semaphore_driver {
236         rwlock_t                lock;
237         struct list_head        free;
238 };
239
240 struct radeon_semaphore {
241         struct radeon_bo        *robj;
242         struct list_head        list;
243         uint64_t                gpu_addr;
244 };
245
246 void radeon_semaphore_driver_fini(struct radeon_device *rdev);
247 int radeon_semaphore_create(struct radeon_device *rdev,
248                             struct radeon_semaphore **semaphore);
249 void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
250                                   struct radeon_semaphore *semaphore);
251 void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
252                                 struct radeon_semaphore *semaphore);
253 void radeon_semaphore_free(struct radeon_device *rdev,
254                            struct radeon_semaphore *semaphore);
255
256 /*
257  * Tiling registers
258  */
259 struct radeon_surface_reg {
260         struct radeon_bo *bo;
261 };
262
263 #define RADEON_GEM_MAX_SURFACES 8
264
265 /*
266  * TTM.
267  */
268 struct radeon_mman {
269         struct ttm_bo_global_ref        bo_global_ref;
270         struct drm_global_reference     mem_global_ref;
271         struct ttm_bo_device            bdev;
272         bool                            mem_global_referenced;
273         bool                            initialized;
274 };
275
276 struct radeon_bo {
277         /* Protected by gem.mutex */
278         struct list_head                list;
279         /* Protected by tbo.reserved */
280         u32                             placements[3];
281         struct ttm_placement            placement;
282         struct ttm_buffer_object        tbo;
283         struct ttm_bo_kmap_obj          kmap;
284         unsigned                        pin_count;
285         void                            *kptr;
286         u32                             tiling_flags;
287         u32                             pitch;
288         int                             surface_reg;
289         /* Constant after initialization */
290         struct radeon_device            *rdev;
291         struct drm_gem_object           gem_base;
292 };
293 #define gem_to_radeon_bo(gobj) container_of((gobj), struct radeon_bo, gem_base)
294
295 struct radeon_bo_list {
296         struct ttm_validate_buffer tv;
297         struct radeon_bo        *bo;
298         uint64_t                gpu_offset;
299         unsigned                rdomain;
300         unsigned                wdomain;
301         u32                     tiling_flags;
302 };
303
304 /*
305  * GEM objects.
306  */
307 struct radeon_gem {
308         struct mutex            mutex;
309         struct list_head        objects;
310 };
311
312 int radeon_gem_init(struct radeon_device *rdev);
313 void radeon_gem_fini(struct radeon_device *rdev);
314 int radeon_gem_object_create(struct radeon_device *rdev, int size,
315                                 int alignment, int initial_domain,
316                                 bool discardable, bool kernel,
317                                 struct drm_gem_object **obj);
318 int radeon_gem_object_pin(struct drm_gem_object *obj, uint32_t pin_domain,
319                           uint64_t *gpu_addr);
320 void radeon_gem_object_unpin(struct drm_gem_object *obj);
321
322 int radeon_mode_dumb_create(struct drm_file *file_priv,
323                             struct drm_device *dev,
324                             struct drm_mode_create_dumb *args);
325 int radeon_mode_dumb_mmap(struct drm_file *filp,
326                           struct drm_device *dev,
327                           uint32_t handle, uint64_t *offset_p);
328 int radeon_mode_dumb_destroy(struct drm_file *file_priv,
329                              struct drm_device *dev,
330                              uint32_t handle);
331
332 /*
333  * GART structures, functions & helpers
334  */
335 struct radeon_mc;
336
337 #define RADEON_GPU_PAGE_SIZE 4096
338 #define RADEON_GPU_PAGE_MASK (RADEON_GPU_PAGE_SIZE - 1)
339 #define RADEON_GPU_PAGE_SHIFT 12
340
341 struct radeon_gart {
342         dma_addr_t                      table_addr;
343         struct radeon_bo                *robj;
344         void                            *ptr;
345         unsigned                        num_gpu_pages;
346         unsigned                        num_cpu_pages;
347         unsigned                        table_size;
348         struct page                     **pages;
349         dma_addr_t                      *pages_addr;
350         bool                            ready;
351 };
352
353 int radeon_gart_table_ram_alloc(struct radeon_device *rdev);
354 void radeon_gart_table_ram_free(struct radeon_device *rdev);
355 int radeon_gart_table_vram_alloc(struct radeon_device *rdev);
356 void radeon_gart_table_vram_free(struct radeon_device *rdev);
357 int radeon_gart_table_vram_pin(struct radeon_device *rdev);
358 void radeon_gart_table_vram_unpin(struct radeon_device *rdev);
359 int radeon_gart_init(struct radeon_device *rdev);
360 void radeon_gart_fini(struct radeon_device *rdev);
361 void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
362                         int pages);
363 int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
364                      int pages, struct page **pagelist,
365                      dma_addr_t *dma_addr);
366 void radeon_gart_restore(struct radeon_device *rdev);
367
368
369 /*
370  * GPU MC structures, functions & helpers
371  */
372 struct radeon_mc {
373         resource_size_t         aper_size;
374         resource_size_t         aper_base;
375         resource_size_t         agp_base;
376         /* for some chips with <= 32MB we need to lie
377          * about vram size near mc fb location */
378         u64                     mc_vram_size;
379         u64                     visible_vram_size;
380         u64                     gtt_size;
381         u64                     gtt_start;
382         u64                     gtt_end;
383         u64                     vram_start;
384         u64                     vram_end;
385         unsigned                vram_width;
386         u64                     real_vram_size;
387         int                     vram_mtrr;
388         bool                    vram_is_ddr;
389         bool                    igp_sideport_enabled;
390         u64                     gtt_base_align;
391 };
392
393 bool radeon_combios_sideport_present(struct radeon_device *rdev);
394 bool radeon_atombios_sideport_present(struct radeon_device *rdev);
395
396 /*
397  * GPU scratch registers structures, functions & helpers
398  */
399 struct radeon_scratch {
400         unsigned                num_reg;
401         uint32_t                reg_base;
402         bool                    free[32];
403         uint32_t                reg[32];
404 };
405
406 int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg);
407 void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg);
408
409
410 /*
411  * IRQS.
412  */
413
414 struct radeon_unpin_work {
415         struct work_struct work;
416         struct radeon_device *rdev;
417         int crtc_id;
418         struct radeon_fence *fence;
419         struct drm_pending_vblank_event *event;
420         struct radeon_bo *old_rbo;
421         u64 new_crtc_base;
422 };
423
424 struct r500_irq_stat_regs {
425         u32 disp_int;
426 };
427
428 struct r600_irq_stat_regs {
429         u32 disp_int;
430         u32 disp_int_cont;
431         u32 disp_int_cont2;
432         u32 d1grph_int;
433         u32 d2grph_int;
434 };
435
436 struct evergreen_irq_stat_regs {
437         u32 disp_int;
438         u32 disp_int_cont;
439         u32 disp_int_cont2;
440         u32 disp_int_cont3;
441         u32 disp_int_cont4;
442         u32 disp_int_cont5;
443         u32 d1grph_int;
444         u32 d2grph_int;
445         u32 d3grph_int;
446         u32 d4grph_int;
447         u32 d5grph_int;
448         u32 d6grph_int;
449 };
450
451 union radeon_irq_stat_regs {
452         struct r500_irq_stat_regs r500;
453         struct r600_irq_stat_regs r600;
454         struct evergreen_irq_stat_regs evergreen;
455 };
456
457 #define RADEON_MAX_HPD_PINS 6
458 #define RADEON_MAX_CRTCS 6
459 #define RADEON_MAX_HDMI_BLOCKS 2
460
461 struct radeon_irq {
462         bool            installed;
463         bool            sw_int;
464         bool            crtc_vblank_int[RADEON_MAX_CRTCS];
465         bool            pflip[RADEON_MAX_CRTCS];
466         wait_queue_head_t       vblank_queue;
467         bool            hpd[RADEON_MAX_HPD_PINS];
468         bool            gui_idle;
469         bool            gui_idle_acked;
470         wait_queue_head_t       idle_queue;
471         bool            hdmi[RADEON_MAX_HDMI_BLOCKS];
472         spinlock_t sw_lock;
473         int sw_refcount;
474         union radeon_irq_stat_regs stat_regs;
475         spinlock_t pflip_lock[RADEON_MAX_CRTCS];
476         int pflip_refcount[RADEON_MAX_CRTCS];
477 };
478
479 int radeon_irq_kms_init(struct radeon_device *rdev);
480 void radeon_irq_kms_fini(struct radeon_device *rdev);
481 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev);
482 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev);
483 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc);
484 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc);
485
486 /*
487  * CP & ring.
488  */
489
490 /* max number of rings */
491 #define RADEON_NUM_RINGS 3
492
493 /* internal ring indices */
494 /* r1xx+ has gfx CP ring */
495 #define RADEON_RING_TYPE_GFX_INDEX  0
496
497 /* cayman has 2 compute CP rings */
498 #define CAYMAN_RING_TYPE_CP1_INDEX 1
499 #define CAYMAN_RING_TYPE_CP2_INDEX 2
500
501 struct radeon_ib {
502         struct list_head        list;
503         unsigned                idx;
504         uint64_t                gpu_addr;
505         struct radeon_fence     *fence;
506         uint32_t                *ptr;
507         uint32_t                length_dw;
508         bool                    free;
509 };
510
511 /*
512  * locking -
513  * mutex protects scheduled_ibs, ready, alloc_bm
514  */
515 struct radeon_ib_pool {
516         struct mutex            mutex;
517         struct radeon_bo        *robj;
518         struct list_head        bogus_ib;
519         struct radeon_ib        ibs[RADEON_IB_POOL_SIZE];
520         bool                    ready;
521         unsigned                head_id;
522 };
523
524 struct radeon_cp {
525         struct radeon_bo        *ring_obj;
526         volatile uint32_t       *ring;
527         unsigned                rptr;
528         unsigned                rptr_offs;
529         unsigned                rptr_reg;
530         unsigned                wptr;
531         unsigned                wptr_old;
532         unsigned                wptr_reg;
533         unsigned                ring_size;
534         unsigned                ring_free_dw;
535         int                     count_dw;
536         uint64_t                gpu_addr;
537         uint32_t                align_mask;
538         uint32_t                ptr_mask;
539         struct mutex            mutex;
540         bool                    ready;
541 };
542
543 /*
544  * R6xx+ IH ring
545  */
546 struct r600_ih {
547         struct radeon_bo        *ring_obj;
548         volatile uint32_t       *ring;
549         unsigned                rptr;
550         unsigned                rptr_offs;
551         unsigned                wptr;
552         unsigned                wptr_old;
553         unsigned                ring_size;
554         uint64_t                gpu_addr;
555         uint32_t                ptr_mask;
556         spinlock_t              lock;
557         bool                    enabled;
558 };
559
560 struct r600_blit_cp_primitives {
561         void (*set_render_target)(struct radeon_device *rdev, int format,
562                                   int w, int h, u64 gpu_addr);
563         void (*cp_set_surface_sync)(struct radeon_device *rdev,
564                                     u32 sync_type, u32 size,
565                                     u64 mc_addr);
566         void (*set_shaders)(struct radeon_device *rdev);
567         void (*set_vtx_resource)(struct radeon_device *rdev, u64 gpu_addr);
568         void (*set_tex_resource)(struct radeon_device *rdev,
569                                  int format, int w, int h, int pitch,
570                                  u64 gpu_addr, u32 size);
571         void (*set_scissors)(struct radeon_device *rdev, int x1, int y1,
572                              int x2, int y2);
573         void (*draw_auto)(struct radeon_device *rdev);
574         void (*set_default_state)(struct radeon_device *rdev);
575 };
576
577 struct r600_blit {
578         struct mutex            mutex;
579         struct radeon_bo        *shader_obj;
580         struct r600_blit_cp_primitives primitives;
581         int max_dim;
582         int ring_size_common;
583         int ring_size_per_loop;
584         u64 shader_gpu_addr;
585         u32 vs_offset, ps_offset;
586         u32 state_offset;
587         u32 state_len;
588         u32 vb_used, vb_total;
589         struct radeon_ib *vb_ib;
590 };
591
592 void r600_blit_suspend(struct radeon_device *rdev);
593
594 int radeon_ib_get(struct radeon_device *rdev, int ring, struct radeon_ib **ib);
595 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib);
596 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib);
597 int radeon_ib_pool_init(struct radeon_device *rdev);
598 void radeon_ib_pool_fini(struct radeon_device *rdev);
599 int radeon_ib_test(struct radeon_device *rdev);
600 extern void radeon_ib_bogus_add(struct radeon_device *rdev, struct radeon_ib *ib);
601 /* Ring access between begin & end cannot sleep */
602 int radeon_ring_index(struct radeon_device *rdev, struct radeon_cp *cp);
603 void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_cp *cp);
604 int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ndw);
605 int radeon_ring_lock(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ndw);
606 void radeon_ring_commit(struct radeon_device *rdev, struct radeon_cp *cp);
607 void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_cp *cp);
608 void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_cp *cp);
609 int radeon_ring_test(struct radeon_device *rdev, struct radeon_cp *cp);
610 int radeon_ring_init(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ring_size,
611                      unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg);
612 void radeon_ring_fini(struct radeon_device *rdev, struct radeon_cp *cp);
613
614
615 /*
616  * CS.
617  */
618 struct radeon_cs_reloc {
619         struct drm_gem_object           *gobj;
620         struct radeon_bo                *robj;
621         struct radeon_bo_list           lobj;
622         uint32_t                        handle;
623         uint32_t                        flags;
624 };
625
626 struct radeon_cs_chunk {
627         uint32_t                chunk_id;
628         uint32_t                length_dw;
629         int kpage_idx[2];
630         uint32_t                *kpage[2];
631         uint32_t                *kdata;
632         void __user *user_ptr;
633         int last_copied_page;
634         int last_page_index;
635 };
636
637 struct radeon_cs_parser {
638         struct device           *dev;
639         struct radeon_device    *rdev;
640         struct drm_file         *filp;
641         /* chunks */
642         unsigned                nchunks;
643         struct radeon_cs_chunk  *chunks;
644         uint64_t                *chunks_array;
645         /* IB */
646         unsigned                idx;
647         /* relocations */
648         unsigned                nrelocs;
649         struct radeon_cs_reloc  *relocs;
650         struct radeon_cs_reloc  **relocs_ptr;
651         struct list_head        validated;
652         /* indices of various chunks */
653         int                     chunk_ib_idx;
654         int                     chunk_relocs_idx;
655         struct radeon_ib        *ib;
656         void                    *track;
657         unsigned                family;
658         int                     parser_error;
659         bool                    keep_tiling_flags;
660 };
661
662 extern int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx);
663 extern int radeon_cs_finish_pages(struct radeon_cs_parser *p);
664 extern u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx);
665
666 struct radeon_cs_packet {
667         unsigned        idx;
668         unsigned        type;
669         unsigned        reg;
670         unsigned        opcode;
671         int             count;
672         unsigned        one_reg_wr;
673 };
674
675 typedef int (*radeon_packet0_check_t)(struct radeon_cs_parser *p,
676                                       struct radeon_cs_packet *pkt,
677                                       unsigned idx, unsigned reg);
678 typedef int (*radeon_packet3_check_t)(struct radeon_cs_parser *p,
679                                       struct radeon_cs_packet *pkt);
680
681
682 /*
683  * AGP
684  */
685 int radeon_agp_init(struct radeon_device *rdev);
686 void radeon_agp_resume(struct radeon_device *rdev);
687 void radeon_agp_suspend(struct radeon_device *rdev);
688 void radeon_agp_fini(struct radeon_device *rdev);
689
690
691 /*
692  * Writeback
693  */
694 struct radeon_wb {
695         struct radeon_bo        *wb_obj;
696         volatile uint32_t       *wb;
697         uint64_t                gpu_addr;
698         bool                    enabled;
699         bool                    use_event;
700 };
701
702 #define RADEON_WB_SCRATCH_OFFSET 0
703 #define RADEON_WB_CP_RPTR_OFFSET 1024
704 #define RADEON_WB_CP1_RPTR_OFFSET 1280
705 #define RADEON_WB_CP2_RPTR_OFFSET 1536
706 #define R600_WB_IH_WPTR_OFFSET   2048
707 #define R600_WB_EVENT_OFFSET     3072
708
709 /**
710  * struct radeon_pm - power management datas
711  * @max_bandwidth:      maximum bandwidth the gpu has (MByte/s)
712  * @igp_sideport_mclk:  sideport memory clock Mhz (rs690,rs740,rs780,rs880)
713  * @igp_system_mclk:    system clock Mhz (rs690,rs740,rs780,rs880)
714  * @igp_ht_link_clk:    ht link clock Mhz (rs690,rs740,rs780,rs880)
715  * @igp_ht_link_width:  ht link width in bits (rs690,rs740,rs780,rs880)
716  * @k8_bandwidth:       k8 bandwidth the gpu has (MByte/s) (IGP)
717  * @sideport_bandwidth: sideport bandwidth the gpu has (MByte/s) (IGP)
718  * @ht_bandwidth:       ht bandwidth the gpu has (MByte/s) (IGP)
719  * @core_bandwidth:     core GPU bandwidth the gpu has (MByte/s) (IGP)
720  * @sclk:               GPU clock Mhz (core bandwidth depends of this clock)
721  * @needed_bandwidth:   current bandwidth needs
722  *
723  * It keeps track of various data needed to take powermanagement decision.
724  * Bandwidth need is used to determine minimun clock of the GPU and memory.
725  * Equation between gpu/memory clock and available bandwidth is hw dependent
726  * (type of memory, bus size, efficiency, ...)
727  */
728
729 enum radeon_pm_method {
730         PM_METHOD_PROFILE,
731         PM_METHOD_DYNPM,
732 };
733
734 enum radeon_dynpm_state {
735         DYNPM_STATE_DISABLED,
736         DYNPM_STATE_MINIMUM,
737         DYNPM_STATE_PAUSED,
738         DYNPM_STATE_ACTIVE,
739         DYNPM_STATE_SUSPENDED,
740 };
741 enum radeon_dynpm_action {
742         DYNPM_ACTION_NONE,
743         DYNPM_ACTION_MINIMUM,
744         DYNPM_ACTION_DOWNCLOCK,
745         DYNPM_ACTION_UPCLOCK,
746         DYNPM_ACTION_DEFAULT
747 };
748
749 enum radeon_voltage_type {
750         VOLTAGE_NONE = 0,
751         VOLTAGE_GPIO,
752         VOLTAGE_VDDC,
753         VOLTAGE_SW
754 };
755
756 enum radeon_pm_state_type {
757         POWER_STATE_TYPE_DEFAULT,
758         POWER_STATE_TYPE_POWERSAVE,
759         POWER_STATE_TYPE_BATTERY,
760         POWER_STATE_TYPE_BALANCED,
761         POWER_STATE_TYPE_PERFORMANCE,
762 };
763
764 enum radeon_pm_profile_type {
765         PM_PROFILE_DEFAULT,
766         PM_PROFILE_AUTO,
767         PM_PROFILE_LOW,
768         PM_PROFILE_MID,
769         PM_PROFILE_HIGH,
770 };
771
772 #define PM_PROFILE_DEFAULT_IDX 0
773 #define PM_PROFILE_LOW_SH_IDX  1
774 #define PM_PROFILE_MID_SH_IDX  2
775 #define PM_PROFILE_HIGH_SH_IDX 3
776 #define PM_PROFILE_LOW_MH_IDX  4
777 #define PM_PROFILE_MID_MH_IDX  5
778 #define PM_PROFILE_HIGH_MH_IDX 6
779 #define PM_PROFILE_MAX         7
780
781 struct radeon_pm_profile {
782         int dpms_off_ps_idx;
783         int dpms_on_ps_idx;
784         int dpms_off_cm_idx;
785         int dpms_on_cm_idx;
786 };
787
788 enum radeon_int_thermal_type {
789         THERMAL_TYPE_NONE,
790         THERMAL_TYPE_RV6XX,
791         THERMAL_TYPE_RV770,
792         THERMAL_TYPE_EVERGREEN,
793         THERMAL_TYPE_SUMO,
794         THERMAL_TYPE_NI,
795 };
796
797 struct radeon_voltage {
798         enum radeon_voltage_type type;
799         /* gpio voltage */
800         struct radeon_gpio_rec gpio;
801         u32 delay; /* delay in usec from voltage drop to sclk change */
802         bool active_high; /* voltage drop is active when bit is high */
803         /* VDDC voltage */
804         u8 vddc_id; /* index into vddc voltage table */
805         u8 vddci_id; /* index into vddci voltage table */
806         bool vddci_enabled;
807         /* r6xx+ sw */
808         u16 voltage;
809         /* evergreen+ vddci */
810         u16 vddci;
811 };
812
813 /* clock mode flags */
814 #define RADEON_PM_MODE_NO_DISPLAY          (1 << 0)
815
816 struct radeon_pm_clock_info {
817         /* memory clock */
818         u32 mclk;
819         /* engine clock */
820         u32 sclk;
821         /* voltage info */
822         struct radeon_voltage voltage;
823         /* standardized clock flags */
824         u32 flags;
825 };
826
827 /* state flags */
828 #define RADEON_PM_STATE_SINGLE_DISPLAY_ONLY (1 << 0)
829
830 struct radeon_power_state {
831         enum radeon_pm_state_type type;
832         struct radeon_pm_clock_info *clock_info;
833         /* number of valid clock modes in this power state */
834         int num_clock_modes;
835         struct radeon_pm_clock_info *default_clock_mode;
836         /* standardized state flags */
837         u32 flags;
838         u32 misc; /* vbios specific flags */
839         u32 misc2; /* vbios specific flags */
840         int pcie_lanes; /* pcie lanes */
841 };
842
843 /*
844  * Some modes are overclocked by very low value, accept them
845  */
846 #define RADEON_MODE_OVERCLOCK_MARGIN 500 /* 5 MHz */
847
848 struct radeon_pm {
849         struct mutex            mutex;
850         u32                     active_crtcs;
851         int                     active_crtc_count;
852         int                     req_vblank;
853         bool                    vblank_sync;
854         bool                    gui_idle;
855         fixed20_12              max_bandwidth;
856         fixed20_12              igp_sideport_mclk;
857         fixed20_12              igp_system_mclk;
858         fixed20_12              igp_ht_link_clk;
859         fixed20_12              igp_ht_link_width;
860         fixed20_12              k8_bandwidth;
861         fixed20_12              sideport_bandwidth;
862         fixed20_12              ht_bandwidth;
863         fixed20_12              core_bandwidth;
864         fixed20_12              sclk;
865         fixed20_12              mclk;
866         fixed20_12              needed_bandwidth;
867         struct radeon_power_state *power_state;
868         /* number of valid power states */
869         int                     num_power_states;
870         int                     current_power_state_index;
871         int                     current_clock_mode_index;
872         int                     requested_power_state_index;
873         int                     requested_clock_mode_index;
874         int                     default_power_state_index;
875         u32                     current_sclk;
876         u32                     current_mclk;
877         u16                     current_vddc;
878         u16                     current_vddci;
879         u32                     default_sclk;
880         u32                     default_mclk;
881         u16                     default_vddc;
882         u16                     default_vddci;
883         struct radeon_i2c_chan *i2c_bus;
884         /* selected pm method */
885         enum radeon_pm_method     pm_method;
886         /* dynpm power management */
887         struct delayed_work     dynpm_idle_work;
888         enum radeon_dynpm_state dynpm_state;
889         enum radeon_dynpm_action        dynpm_planned_action;
890         unsigned long           dynpm_action_timeout;
891         bool                    dynpm_can_upclock;
892         bool                    dynpm_can_downclock;
893         /* profile-based power management */
894         enum radeon_pm_profile_type profile;
895         int                     profile_index;
896         struct radeon_pm_profile profiles[PM_PROFILE_MAX];
897         /* internal thermal controller on rv6xx+ */
898         enum radeon_int_thermal_type int_thermal_type;
899         struct device           *int_hwmon_dev;
900 };
901
902 int radeon_pm_get_type_index(struct radeon_device *rdev,
903                              enum radeon_pm_state_type ps_type,
904                              int instance);
905
906 /*
907  * Benchmarking
908  */
909 void radeon_benchmark(struct radeon_device *rdev, int test_number);
910
911
912 /*
913  * Testing
914  */
915 void radeon_test_moves(struct radeon_device *rdev);
916
917
918 /*
919  * Debugfs
920  */
921 struct radeon_debugfs {
922         struct drm_info_list    *files;
923         unsigned                num_files;
924 };
925
926 int radeon_debugfs_add_files(struct radeon_device *rdev,
927                              struct drm_info_list *files,
928                              unsigned nfiles);
929 int radeon_debugfs_fence_init(struct radeon_device *rdev);
930
931
932 /*
933  * ASIC specific functions.
934  */
935 struct radeon_asic {
936         int (*init)(struct radeon_device *rdev);
937         void (*fini)(struct radeon_device *rdev);
938         int (*resume)(struct radeon_device *rdev);
939         int (*suspend)(struct radeon_device *rdev);
940         void (*vga_set_state)(struct radeon_device *rdev, bool state);
941         bool (*gpu_is_lockup)(struct radeon_device *rdev, struct radeon_cp *cp);
942         int (*asic_reset)(struct radeon_device *rdev);
943         void (*gart_tlb_flush)(struct radeon_device *rdev);
944         int (*gart_set_page)(struct radeon_device *rdev, int i, uint64_t addr);
945         int (*cp_init)(struct radeon_device *rdev, unsigned ring_size);
946         void (*cp_fini)(struct radeon_device *rdev);
947         void (*cp_disable)(struct radeon_device *rdev);
948         void (*ring_start)(struct radeon_device *rdev);
949         int (*ring_test)(struct radeon_device *rdev, struct radeon_cp *cp);
950         void (*ring_ib_execute)(struct radeon_device *rdev, struct radeon_ib *ib);
951         int (*irq_set)(struct radeon_device *rdev);
952         int (*irq_process)(struct radeon_device *rdev);
953         u32 (*get_vblank_counter)(struct radeon_device *rdev, int crtc);
954         void (*fence_ring_emit)(struct radeon_device *rdev, struct radeon_fence *fence);
955         void (*semaphore_ring_emit)(struct radeon_device *rdev,
956                                     struct radeon_cp *cp,
957                                     struct radeon_semaphore *semaphore,
958                                     bool emit_wait);
959         int (*cs_parse)(struct radeon_cs_parser *p);
960         int (*copy_blit)(struct radeon_device *rdev,
961                          uint64_t src_offset,
962                          uint64_t dst_offset,
963                          unsigned num_gpu_pages,
964                          struct radeon_fence *fence);
965         int (*copy_dma)(struct radeon_device *rdev,
966                         uint64_t src_offset,
967                         uint64_t dst_offset,
968                         unsigned num_gpu_pages,
969                         struct radeon_fence *fence);
970         int (*copy)(struct radeon_device *rdev,
971                     uint64_t src_offset,
972                     uint64_t dst_offset,
973                     unsigned num_gpu_pages,
974                     struct radeon_fence *fence);
975         uint32_t (*get_engine_clock)(struct radeon_device *rdev);
976         void (*set_engine_clock)(struct radeon_device *rdev, uint32_t eng_clock);
977         uint32_t (*get_memory_clock)(struct radeon_device *rdev);
978         void (*set_memory_clock)(struct radeon_device *rdev, uint32_t mem_clock);
979         int (*get_pcie_lanes)(struct radeon_device *rdev);
980         void (*set_pcie_lanes)(struct radeon_device *rdev, int lanes);
981         void (*set_clock_gating)(struct radeon_device *rdev, int enable);
982         int (*set_surface_reg)(struct radeon_device *rdev, int reg,
983                                uint32_t tiling_flags, uint32_t pitch,
984                                uint32_t offset, uint32_t obj_size);
985         void (*clear_surface_reg)(struct radeon_device *rdev, int reg);
986         void (*bandwidth_update)(struct radeon_device *rdev);
987         void (*hpd_init)(struct radeon_device *rdev);
988         void (*hpd_fini)(struct radeon_device *rdev);
989         bool (*hpd_sense)(struct radeon_device *rdev, enum radeon_hpd_id hpd);
990         void (*hpd_set_polarity)(struct radeon_device *rdev, enum radeon_hpd_id hpd);
991         /* ioctl hw specific callback. Some hw might want to perform special
992          * operation on specific ioctl. For instance on wait idle some hw
993          * might want to perform and HDP flush through MMIO as it seems that
994          * some R6XX/R7XX hw doesn't take HDP flush into account if programmed
995          * through ring.
996          */
997         void (*ioctl_wait_idle)(struct radeon_device *rdev, struct radeon_bo *bo);
998         bool (*gui_idle)(struct radeon_device *rdev);
999         /* power management */
1000         void (*pm_misc)(struct radeon_device *rdev);
1001         void (*pm_prepare)(struct radeon_device *rdev);
1002         void (*pm_finish)(struct radeon_device *rdev);
1003         void (*pm_init_profile)(struct radeon_device *rdev);
1004         void (*pm_get_dynpm_state)(struct radeon_device *rdev);
1005         /* pageflipping */
1006         void (*pre_page_flip)(struct radeon_device *rdev, int crtc);
1007         u32 (*page_flip)(struct radeon_device *rdev, int crtc, u64 crtc_base);
1008         void (*post_page_flip)(struct radeon_device *rdev, int crtc);
1009 };
1010
1011 /*
1012  * Asic structures
1013  */
1014 struct r100_gpu_lockup {
1015         unsigned long   last_jiffies;
1016         u32             last_cp_rptr;
1017 };
1018
1019 struct r100_asic {
1020         const unsigned          *reg_safe_bm;
1021         unsigned                reg_safe_bm_size;
1022         u32                     hdp_cntl;
1023         struct r100_gpu_lockup  lockup;
1024 };
1025
1026 struct r300_asic {
1027         const unsigned          *reg_safe_bm;
1028         unsigned                reg_safe_bm_size;
1029         u32                     resync_scratch;
1030         u32                     hdp_cntl;
1031         struct r100_gpu_lockup  lockup;
1032 };
1033
1034 struct r600_asic {
1035         unsigned                max_pipes;
1036         unsigned                max_tile_pipes;
1037         unsigned                max_simds;
1038         unsigned                max_backends;
1039         unsigned                max_gprs;
1040         unsigned                max_threads;
1041         unsigned                max_stack_entries;
1042         unsigned                max_hw_contexts;
1043         unsigned                max_gs_threads;
1044         unsigned                sx_max_export_size;
1045         unsigned                sx_max_export_pos_size;
1046         unsigned                sx_max_export_smx_size;
1047         unsigned                sq_num_cf_insts;
1048         unsigned                tiling_nbanks;
1049         unsigned                tiling_npipes;
1050         unsigned                tiling_group_size;
1051         unsigned                tile_config;
1052         unsigned                backend_map;
1053         struct r100_gpu_lockup  lockup;
1054 };
1055
1056 struct rv770_asic {
1057         unsigned                max_pipes;
1058         unsigned                max_tile_pipes;
1059         unsigned                max_simds;
1060         unsigned                max_backends;
1061         unsigned                max_gprs;
1062         unsigned                max_threads;
1063         unsigned                max_stack_entries;
1064         unsigned                max_hw_contexts;
1065         unsigned                max_gs_threads;
1066         unsigned                sx_max_export_size;
1067         unsigned                sx_max_export_pos_size;
1068         unsigned                sx_max_export_smx_size;
1069         unsigned                sq_num_cf_insts;
1070         unsigned                sx_num_of_sets;
1071         unsigned                sc_prim_fifo_size;
1072         unsigned                sc_hiz_tile_fifo_size;
1073         unsigned                sc_earlyz_tile_fifo_fize;
1074         unsigned                tiling_nbanks;
1075         unsigned                tiling_npipes;
1076         unsigned                tiling_group_size;
1077         unsigned                tile_config;
1078         unsigned                backend_map;
1079         struct r100_gpu_lockup  lockup;
1080 };
1081
1082 struct evergreen_asic {
1083         unsigned num_ses;
1084         unsigned max_pipes;
1085         unsigned max_tile_pipes;
1086         unsigned max_simds;
1087         unsigned max_backends;
1088         unsigned max_gprs;
1089         unsigned max_threads;
1090         unsigned max_stack_entries;
1091         unsigned max_hw_contexts;
1092         unsigned max_gs_threads;
1093         unsigned sx_max_export_size;
1094         unsigned sx_max_export_pos_size;
1095         unsigned sx_max_export_smx_size;
1096         unsigned sq_num_cf_insts;
1097         unsigned sx_num_of_sets;
1098         unsigned sc_prim_fifo_size;
1099         unsigned sc_hiz_tile_fifo_size;
1100         unsigned sc_earlyz_tile_fifo_size;
1101         unsigned tiling_nbanks;
1102         unsigned tiling_npipes;
1103         unsigned tiling_group_size;
1104         unsigned tile_config;
1105         unsigned backend_map;
1106         struct r100_gpu_lockup  lockup;
1107 };
1108
1109 struct cayman_asic {
1110         unsigned max_shader_engines;
1111         unsigned max_pipes_per_simd;
1112         unsigned max_tile_pipes;
1113         unsigned max_simds_per_se;
1114         unsigned max_backends_per_se;
1115         unsigned max_texture_channel_caches;
1116         unsigned max_gprs;
1117         unsigned max_threads;
1118         unsigned max_gs_threads;
1119         unsigned max_stack_entries;
1120         unsigned sx_num_of_sets;
1121         unsigned sx_max_export_size;
1122         unsigned sx_max_export_pos_size;
1123         unsigned sx_max_export_smx_size;
1124         unsigned max_hw_contexts;
1125         unsigned sq_num_cf_insts;
1126         unsigned sc_prim_fifo_size;
1127         unsigned sc_hiz_tile_fifo_size;
1128         unsigned sc_earlyz_tile_fifo_size;
1129
1130         unsigned num_shader_engines;
1131         unsigned num_shader_pipes_per_simd;
1132         unsigned num_tile_pipes;
1133         unsigned num_simds_per_se;
1134         unsigned num_backends_per_se;
1135         unsigned backend_disable_mask_per_asic;
1136         unsigned backend_map;
1137         unsigned num_texture_channel_caches;
1138         unsigned mem_max_burst_length_bytes;
1139         unsigned mem_row_size_in_kb;
1140         unsigned shader_engine_tile_size;
1141         unsigned num_gpus;
1142         unsigned multi_gpu_tile_size;
1143
1144         unsigned tile_config;
1145         struct r100_gpu_lockup  lockup;
1146 };
1147
1148 union radeon_asic_config {
1149         struct r300_asic        r300;
1150         struct r100_asic        r100;
1151         struct r600_asic        r600;
1152         struct rv770_asic       rv770;
1153         struct evergreen_asic   evergreen;
1154         struct cayman_asic      cayman;
1155 };
1156
1157 /*
1158  * asic initizalization from radeon_asic.c
1159  */
1160 void radeon_agp_disable(struct radeon_device *rdev);
1161 int radeon_asic_init(struct radeon_device *rdev);
1162
1163
1164 /*
1165  * IOCTL.
1166  */
1167 int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
1168                           struct drm_file *filp);
1169 int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
1170                             struct drm_file *filp);
1171 int radeon_gem_pin_ioctl(struct drm_device *dev, void *data,
1172                          struct drm_file *file_priv);
1173 int radeon_gem_unpin_ioctl(struct drm_device *dev, void *data,
1174                            struct drm_file *file_priv);
1175 int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1176                             struct drm_file *file_priv);
1177 int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
1178                            struct drm_file *file_priv);
1179 int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1180                                 struct drm_file *filp);
1181 int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
1182                           struct drm_file *filp);
1183 int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
1184                           struct drm_file *filp);
1185 int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
1186                               struct drm_file *filp);
1187 int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
1188 int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
1189                                 struct drm_file *filp);
1190 int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
1191                                 struct drm_file *filp);
1192
1193 /* VRAM scratch page for HDP bug, default vram page */
1194 struct r600_vram_scratch {
1195         struct radeon_bo                *robj;
1196         volatile uint32_t               *ptr;
1197         u64                             gpu_addr;
1198 };
1199
1200
1201 /*
1202  * Mutex which allows recursive locking from the same process.
1203  */
1204 struct radeon_mutex {
1205         struct mutex            mutex;
1206         struct task_struct      *owner;
1207         int                     level;
1208 };
1209
1210 static inline void radeon_mutex_init(struct radeon_mutex *mutex)
1211 {
1212         mutex_init(&mutex->mutex);
1213         mutex->owner = NULL;
1214         mutex->level = 0;
1215 }
1216
1217 static inline void radeon_mutex_lock(struct radeon_mutex *mutex)
1218 {
1219         if (mutex_trylock(&mutex->mutex)) {
1220                 /* The mutex was unlocked before, so it's ours now */
1221                 mutex->owner = current;
1222         } else if (mutex->owner != current) {
1223                 /* Another process locked the mutex, take it */
1224                 mutex_lock(&mutex->mutex);
1225                 mutex->owner = current;
1226         }
1227         /* Otherwise the mutex was already locked by this process */
1228
1229         mutex->level++;
1230 }
1231
1232 static inline void radeon_mutex_unlock(struct radeon_mutex *mutex)
1233 {
1234         if (--mutex->level > 0)
1235                 return;
1236
1237         mutex->owner = NULL;
1238         mutex_unlock(&mutex->mutex);
1239 }
1240
1241
1242 /*
1243  * Core structure, functions and helpers.
1244  */
1245 typedef uint32_t (*radeon_rreg_t)(struct radeon_device*, uint32_t);
1246 typedef void (*radeon_wreg_t)(struct radeon_device*, uint32_t, uint32_t);
1247
1248 struct radeon_device {
1249         struct device                   *dev;
1250         struct drm_device               *ddev;
1251         struct pci_dev                  *pdev;
1252         /* ASIC */
1253         union radeon_asic_config        config;
1254         enum radeon_family              family;
1255         unsigned long                   flags;
1256         int                             usec_timeout;
1257         enum radeon_pll_errata          pll_errata;
1258         int                             num_gb_pipes;
1259         int                             num_z_pipes;
1260         int                             disp_priority;
1261         /* BIOS */
1262         uint8_t                         *bios;
1263         bool                            is_atom_bios;
1264         uint16_t                        bios_header_start;
1265         struct radeon_bo                *stollen_vga_memory;
1266         /* Register mmio */
1267         resource_size_t                 rmmio_base;
1268         resource_size_t                 rmmio_size;
1269         void __iomem                    *rmmio;
1270         radeon_rreg_t                   mc_rreg;
1271         radeon_wreg_t                   mc_wreg;
1272         radeon_rreg_t                   pll_rreg;
1273         radeon_wreg_t                   pll_wreg;
1274         uint32_t                        pcie_reg_mask;
1275         radeon_rreg_t                   pciep_rreg;
1276         radeon_wreg_t                   pciep_wreg;
1277         /* io port */
1278         void __iomem                    *rio_mem;
1279         resource_size_t                 rio_mem_size;
1280         struct radeon_clock             clock;
1281         struct radeon_mc                mc;
1282         struct radeon_gart              gart;
1283         struct radeon_mode_info         mode_info;
1284         struct radeon_scratch           scratch;
1285         struct radeon_mman              mman;
1286         rwlock_t                        fence_lock;
1287         struct radeon_fence_driver      fence_drv[RADEON_NUM_RINGS];
1288         struct radeon_semaphore_driver  semaphore_drv;
1289         struct radeon_cp                cp[RADEON_NUM_RINGS];
1290         struct radeon_ib_pool           ib_pool;
1291         struct radeon_irq               irq;
1292         struct radeon_asic              *asic;
1293         struct radeon_gem               gem;
1294         struct radeon_pm                pm;
1295         uint32_t                        bios_scratch[RADEON_BIOS_NUM_SCRATCH];
1296         struct radeon_mutex             cs_mutex;
1297         struct radeon_wb                wb;
1298         struct radeon_dummy_page        dummy_page;
1299         bool                            gpu_lockup;
1300         bool                            shutdown;
1301         bool                            suspend;
1302         bool                            need_dma32;
1303         bool                            accel_working;
1304         struct radeon_surface_reg surface_regs[RADEON_GEM_MAX_SURFACES];
1305         const struct firmware *me_fw;   /* all family ME firmware */
1306         const struct firmware *pfp_fw;  /* r6/700 PFP firmware */
1307         const struct firmware *rlc_fw;  /* r6/700 RLC firmware */
1308         const struct firmware *mc_fw;   /* NI MC firmware */
1309         struct r600_blit r600_blit;
1310         struct r600_vram_scratch vram_scratch;
1311         int msi_enabled; /* msi enabled */
1312         struct r600_ih ih; /* r6/700 interrupt ring */
1313         struct work_struct hotplug_work;
1314         int num_crtc; /* number of crtcs */
1315         struct mutex dc_hw_i2c_mutex; /* display controller hw i2c mutex */
1316         struct mutex vram_mutex;
1317
1318         /* audio stuff */
1319         bool                    audio_enabled;
1320         struct timer_list       audio_timer;
1321         int                     audio_channels;
1322         int                     audio_rate;
1323         int                     audio_bits_per_sample;
1324         uint8_t                 audio_status_bits;
1325         uint8_t                 audio_category_code;
1326
1327         struct notifier_block acpi_nb;
1328         /* only one userspace can use Hyperz features or CMASK at a time */
1329         struct drm_file *hyperz_filp;
1330         struct drm_file *cmask_filp;
1331         /* i2c buses */
1332         struct radeon_i2c_chan *i2c_bus[RADEON_MAX_I2C_BUS];
1333         /* debugfs */
1334         struct radeon_debugfs   debugfs[RADEON_DEBUGFS_MAX_COMPONENTS];
1335         unsigned                debugfs_count;
1336 };
1337
1338 int radeon_device_init(struct radeon_device *rdev,
1339                        struct drm_device *ddev,
1340                        struct pci_dev *pdev,
1341                        uint32_t flags);
1342 void radeon_device_fini(struct radeon_device *rdev);
1343 int radeon_gpu_wait_for_idle(struct radeon_device *rdev);
1344
1345 uint32_t r100_mm_rreg(struct radeon_device *rdev, uint32_t reg);
1346 void r100_mm_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v);
1347 u32 r100_io_rreg(struct radeon_device *rdev, u32 reg);
1348 void r100_io_wreg(struct radeon_device *rdev, u32 reg, u32 v);
1349
1350 /*
1351  * Cast helper
1352  */
1353 #define to_radeon_fence(p) ((struct radeon_fence *)(p))
1354
1355 /*
1356  * Registers read & write functions.
1357  */
1358 #define RREG8(reg) readb((rdev->rmmio) + (reg))
1359 #define WREG8(reg, v) writeb(v, (rdev->rmmio) + (reg))
1360 #define RREG16(reg) readw((rdev->rmmio) + (reg))
1361 #define WREG16(reg, v) writew(v, (rdev->rmmio) + (reg))
1362 #define RREG32(reg) r100_mm_rreg(rdev, (reg))
1363 #define DREG32(reg) printk(KERN_INFO "REGISTER: " #reg " : 0x%08X\n", r100_mm_rreg(rdev, (reg)))
1364 #define WREG32(reg, v) r100_mm_wreg(rdev, (reg), (v))
1365 #define REG_SET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
1366 #define REG_GET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
1367 #define RREG32_PLL(reg) rdev->pll_rreg(rdev, (reg))
1368 #define WREG32_PLL(reg, v) rdev->pll_wreg(rdev, (reg), (v))
1369 #define RREG32_MC(reg) rdev->mc_rreg(rdev, (reg))
1370 #define WREG32_MC(reg, v) rdev->mc_wreg(rdev, (reg), (v))
1371 #define RREG32_PCIE(reg) rv370_pcie_rreg(rdev, (reg))
1372 #define WREG32_PCIE(reg, v) rv370_pcie_wreg(rdev, (reg), (v))
1373 #define RREG32_PCIE_P(reg) rdev->pciep_rreg(rdev, (reg))
1374 #define WREG32_PCIE_P(reg, v) rdev->pciep_wreg(rdev, (reg), (v))
1375 #define WREG32_P(reg, val, mask)                                \
1376         do {                                                    \
1377                 uint32_t tmp_ = RREG32(reg);                    \
1378                 tmp_ &= (mask);                                 \
1379                 tmp_ |= ((val) & ~(mask));                      \
1380                 WREG32(reg, tmp_);                              \
1381         } while (0)
1382 #define WREG32_PLL_P(reg, val, mask)                            \
1383         do {                                                    \
1384                 uint32_t tmp_ = RREG32_PLL(reg);                \
1385                 tmp_ &= (mask);                                 \
1386                 tmp_ |= ((val) & ~(mask));                      \
1387                 WREG32_PLL(reg, tmp_);                          \
1388         } while (0)
1389 #define DREG32_SYS(sqf, rdev, reg) seq_printf((sqf), #reg " : 0x%08X\n", r100_mm_rreg((rdev), (reg)))
1390 #define RREG32_IO(reg) r100_io_rreg(rdev, (reg))
1391 #define WREG32_IO(reg, v) r100_io_wreg(rdev, (reg), (v))
1392
1393 /*
1394  * Indirect registers accessor
1395  */
1396 static inline uint32_t rv370_pcie_rreg(struct radeon_device *rdev, uint32_t reg)
1397 {
1398         uint32_t r;
1399
1400         WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
1401         r = RREG32(RADEON_PCIE_DATA);
1402         return r;
1403 }
1404
1405 static inline void rv370_pcie_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
1406 {
1407         WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
1408         WREG32(RADEON_PCIE_DATA, (v));
1409 }
1410
1411 void r100_pll_errata_after_index(struct radeon_device *rdev);
1412
1413
1414 /*
1415  * ASICs helpers.
1416  */
1417 #define ASIC_IS_RN50(rdev) ((rdev->pdev->device == 0x515e) || \
1418                             (rdev->pdev->device == 0x5969))
1419 #define ASIC_IS_RV100(rdev) ((rdev->family == CHIP_RV100) || \
1420                 (rdev->family == CHIP_RV200) || \
1421                 (rdev->family == CHIP_RS100) || \
1422                 (rdev->family == CHIP_RS200) || \
1423                 (rdev->family == CHIP_RV250) || \
1424                 (rdev->family == CHIP_RV280) || \
1425                 (rdev->family == CHIP_RS300))
1426 #define ASIC_IS_R300(rdev) ((rdev->family == CHIP_R300)  ||     \
1427                 (rdev->family == CHIP_RV350) ||                 \
1428                 (rdev->family == CHIP_R350)  ||                 \
1429                 (rdev->family == CHIP_RV380) ||                 \
1430                 (rdev->family == CHIP_R420)  ||                 \
1431                 (rdev->family == CHIP_R423)  ||                 \
1432                 (rdev->family == CHIP_RV410) ||                 \
1433                 (rdev->family == CHIP_RS400) ||                 \
1434                 (rdev->family == CHIP_RS480))
1435 #define ASIC_IS_X2(rdev) ((rdev->ddev->pdev->device == 0x9441) || \
1436                 (rdev->ddev->pdev->device == 0x9443) || \
1437                 (rdev->ddev->pdev->device == 0x944B) || \
1438                 (rdev->ddev->pdev->device == 0x9506) || \
1439                 (rdev->ddev->pdev->device == 0x9509) || \
1440                 (rdev->ddev->pdev->device == 0x950F) || \
1441                 (rdev->ddev->pdev->device == 0x689C) || \
1442                 (rdev->ddev->pdev->device == 0x689D))
1443 #define ASIC_IS_AVIVO(rdev) ((rdev->family >= CHIP_RS600))
1444 #define ASIC_IS_DCE2(rdev) ((rdev->family == CHIP_RS600)  ||    \
1445                             (rdev->family == CHIP_RS690)  ||    \
1446                             (rdev->family == CHIP_RS740)  ||    \
1447                             (rdev->family >= CHIP_R600))
1448 #define ASIC_IS_DCE3(rdev) ((rdev->family >= CHIP_RV620))
1449 #define ASIC_IS_DCE32(rdev) ((rdev->family >= CHIP_RV730))
1450 #define ASIC_IS_DCE4(rdev) ((rdev->family >= CHIP_CEDAR))
1451 #define ASIC_IS_DCE41(rdev) ((rdev->family >= CHIP_PALM) && \
1452                              (rdev->flags & RADEON_IS_IGP))
1453 #define ASIC_IS_DCE5(rdev) ((rdev->family >= CHIP_BARTS))
1454
1455 /*
1456  * BIOS helpers.
1457  */
1458 #define RBIOS8(i) (rdev->bios[i])
1459 #define RBIOS16(i) (RBIOS8(i) | (RBIOS8((i)+1) << 8))
1460 #define RBIOS32(i) ((RBIOS16(i)) | (RBIOS16((i)+2) << 16))
1461
1462 int radeon_combios_init(struct radeon_device *rdev);
1463 void radeon_combios_fini(struct radeon_device *rdev);
1464 int radeon_atombios_init(struct radeon_device *rdev);
1465 void radeon_atombios_fini(struct radeon_device *rdev);
1466
1467
1468 /*
1469  * RING helpers.
1470  */
1471 #if DRM_DEBUG_CODE == 0
1472 static inline void radeon_ring_write(struct radeon_cp *cp, uint32_t v)
1473 {
1474         cp->ring[cp->wptr++] = v;
1475         cp->wptr &= cp->ptr_mask;
1476         cp->count_dw--;
1477         cp->ring_free_dw--;
1478 }
1479 #else
1480 /* With debugging this is just too big to inline */
1481 void radeon_ring_write(struct radeon_cp *cp, uint32_t v);
1482 #endif
1483
1484 /*
1485  * ASICs macro.
1486  */
1487 #define radeon_init(rdev) (rdev)->asic->init((rdev))
1488 #define radeon_fini(rdev) (rdev)->asic->fini((rdev))
1489 #define radeon_resume(rdev) (rdev)->asic->resume((rdev))
1490 #define radeon_suspend(rdev) (rdev)->asic->suspend((rdev))
1491 #define radeon_cs_parse(p) rdev->asic->cs_parse((p))
1492 #define radeon_vga_set_state(rdev, state) (rdev)->asic->vga_set_state((rdev), (state))
1493 #define radeon_gpu_is_lockup(rdev, cp) (rdev)->asic->gpu_is_lockup((rdev), (cp))
1494 #define radeon_asic_reset(rdev) (rdev)->asic->asic_reset((rdev))
1495 #define radeon_gart_tlb_flush(rdev) (rdev)->asic->gart_tlb_flush((rdev))
1496 #define radeon_gart_set_page(rdev, i, p) (rdev)->asic->gart_set_page((rdev), (i), (p))
1497 #define radeon_ring_start(rdev) (rdev)->asic->ring_start((rdev))
1498 #define radeon_ring_test(rdev, cp) (rdev)->asic->ring_test((rdev), (cp))
1499 #define radeon_ring_ib_execute(rdev, ib) (rdev)->asic->ring_ib_execute((rdev), (ib))
1500 #define radeon_irq_set(rdev) (rdev)->asic->irq_set((rdev))
1501 #define radeon_irq_process(rdev) (rdev)->asic->irq_process((rdev))
1502 #define radeon_get_vblank_counter(rdev, crtc) (rdev)->asic->get_vblank_counter((rdev), (crtc))
1503 #define radeon_fence_ring_emit(rdev, fence) (rdev)->asic->fence_ring_emit((rdev), (fence))
1504 #define radeon_semaphore_ring_emit(rdev, cp, semaphore, emit_wait) (rdev)->asic->semaphore_ring_emit((rdev), (cp), (semaphore), (emit_wait))
1505 #define radeon_copy_blit(rdev, s, d, np, f) (rdev)->asic->copy_blit((rdev), (s), (d), (np), (f))
1506 #define radeon_copy_dma(rdev, s, d, np, f) (rdev)->asic->copy_dma((rdev), (s), (d), (np), (f))
1507 #define radeon_copy(rdev, s, d, np, f) (rdev)->asic->copy((rdev), (s), (d), (np), (f))
1508 #define radeon_get_engine_clock(rdev) (rdev)->asic->get_engine_clock((rdev))
1509 #define radeon_set_engine_clock(rdev, e) (rdev)->asic->set_engine_clock((rdev), (e))
1510 #define radeon_get_memory_clock(rdev) (rdev)->asic->get_memory_clock((rdev))
1511 #define radeon_set_memory_clock(rdev, e) (rdev)->asic->set_memory_clock((rdev), (e))
1512 #define radeon_get_pcie_lanes(rdev) (rdev)->asic->get_pcie_lanes((rdev))
1513 #define radeon_set_pcie_lanes(rdev, l) (rdev)->asic->set_pcie_lanes((rdev), (l))
1514 #define radeon_set_clock_gating(rdev, e) (rdev)->asic->set_clock_gating((rdev), (e))
1515 #define radeon_set_surface_reg(rdev, r, f, p, o, s) ((rdev)->asic->set_surface_reg((rdev), (r), (f), (p), (o), (s)))
1516 #define radeon_clear_surface_reg(rdev, r) ((rdev)->asic->clear_surface_reg((rdev), (r)))
1517 #define radeon_bandwidth_update(rdev) (rdev)->asic->bandwidth_update((rdev))
1518 #define radeon_hpd_init(rdev) (rdev)->asic->hpd_init((rdev))
1519 #define radeon_hpd_fini(rdev) (rdev)->asic->hpd_fini((rdev))
1520 #define radeon_hpd_sense(rdev, hpd) (rdev)->asic->hpd_sense((rdev), (hpd))
1521 #define radeon_hpd_set_polarity(rdev, hpd) (rdev)->asic->hpd_set_polarity((rdev), (hpd))
1522 #define radeon_gui_idle(rdev) (rdev)->asic->gui_idle((rdev))
1523 #define radeon_pm_misc(rdev) (rdev)->asic->pm_misc((rdev))
1524 #define radeon_pm_prepare(rdev) (rdev)->asic->pm_prepare((rdev))
1525 #define radeon_pm_finish(rdev) (rdev)->asic->pm_finish((rdev))
1526 #define radeon_pm_init_profile(rdev) (rdev)->asic->pm_init_profile((rdev))
1527 #define radeon_pm_get_dynpm_state(rdev) (rdev)->asic->pm_get_dynpm_state((rdev))
1528 #define radeon_pre_page_flip(rdev, crtc) rdev->asic->pre_page_flip((rdev), (crtc))
1529 #define radeon_page_flip(rdev, crtc, base) rdev->asic->page_flip((rdev), (crtc), (base))
1530 #define radeon_post_page_flip(rdev, crtc) rdev->asic->post_page_flip((rdev), (crtc))
1531
1532 /* Common functions */
1533 /* AGP */
1534 extern int radeon_gpu_reset(struct radeon_device *rdev);
1535 extern void radeon_agp_disable(struct radeon_device *rdev);
1536 extern int radeon_modeset_init(struct radeon_device *rdev);
1537 extern void radeon_modeset_fini(struct radeon_device *rdev);
1538 extern bool radeon_card_posted(struct radeon_device *rdev);
1539 extern void radeon_update_bandwidth_info(struct radeon_device *rdev);
1540 extern void radeon_update_display_priority(struct radeon_device *rdev);
1541 extern bool radeon_boot_test_post_card(struct radeon_device *rdev);
1542 extern void radeon_scratch_init(struct radeon_device *rdev);
1543 extern void radeon_wb_fini(struct radeon_device *rdev);
1544 extern int radeon_wb_init(struct radeon_device *rdev);
1545 extern void radeon_wb_disable(struct radeon_device *rdev);
1546 extern void radeon_surface_init(struct radeon_device *rdev);
1547 extern int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data);
1548 extern void radeon_legacy_set_clock_gating(struct radeon_device *rdev, int enable);
1549 extern void radeon_atom_set_clock_gating(struct radeon_device *rdev, int enable);
1550 extern void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain);
1551 extern bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo);
1552 extern void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base);
1553 extern void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc);
1554 extern int radeon_resume_kms(struct drm_device *dev);
1555 extern int radeon_suspend_kms(struct drm_device *dev, pm_message_t state);
1556 extern void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size);
1557
1558 /*
1559  * R600 vram scratch functions
1560  */
1561 int r600_vram_scratch_init(struct radeon_device *rdev);
1562 void r600_vram_scratch_fini(struct radeon_device *rdev);
1563
1564 /*
1565  * r600 functions used by radeon_encoder.c
1566  */
1567 extern void r600_hdmi_enable(struct drm_encoder *encoder);
1568 extern void r600_hdmi_disable(struct drm_encoder *encoder);
1569 extern void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode);
1570
1571 extern int ni_init_microcode(struct radeon_device *rdev);
1572 extern int ni_mc_load_microcode(struct radeon_device *rdev);
1573
1574 /* radeon_acpi.c */ 
1575 #if defined(CONFIG_ACPI) 
1576 extern int radeon_acpi_init(struct radeon_device *rdev); 
1577 #else 
1578 static inline int radeon_acpi_init(struct radeon_device *rdev) { return 0; } 
1579 #endif 
1580
1581 #include "radeon_object.h"
1582
1583 #endif