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