drm/radeon/kms: Add initial support for async DMA on r6xx/r7xx
[firefly-linux-kernel-4.4.55.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 extern int radeon_lockup_timeout;
98
99 /*
100  * Copy from radeon_drv.h so we don't have to include both and have conflicting
101  * symbol;
102  */
103 #define RADEON_MAX_USEC_TIMEOUT                 100000  /* 100 ms */
104 #define RADEON_FENCE_JIFFIES_TIMEOUT            (HZ / 2)
105 /* RADEON_IB_POOL_SIZE must be a power of 2 */
106 #define RADEON_IB_POOL_SIZE                     16
107 #define RADEON_DEBUGFS_MAX_COMPONENTS           32
108 #define RADEONFB_CONN_LIMIT                     4
109 #define RADEON_BIOS_NUM_SCRATCH                 8
110
111 /* max number of rings */
112 #define RADEON_NUM_RINGS                        4
113
114 /* fence seq are set to this number when signaled */
115 #define RADEON_FENCE_SIGNALED_SEQ               0LL
116
117 /* internal ring indices */
118 /* r1xx+ has gfx CP ring */
119 #define RADEON_RING_TYPE_GFX_INDEX              0
120
121 /* cayman has 2 compute CP rings */
122 #define CAYMAN_RING_TYPE_CP1_INDEX              1
123 #define CAYMAN_RING_TYPE_CP2_INDEX              2
124
125 /* R600+ has an async dma ring */
126 #define R600_RING_TYPE_DMA_INDEX                3
127
128 /* hardcode those limit for now */
129 #define RADEON_VA_IB_OFFSET                     (1 << 20)
130 #define RADEON_VA_RESERVED_SIZE                 (8 << 20)
131 #define RADEON_IB_VM_MAX_SIZE                   (64 << 10)
132
133 /*
134  * Errata workarounds.
135  */
136 enum radeon_pll_errata {
137         CHIP_ERRATA_R300_CG             = 0x00000001,
138         CHIP_ERRATA_PLL_DUMMYREADS      = 0x00000002,
139         CHIP_ERRATA_PLL_DELAY           = 0x00000004
140 };
141
142
143 struct radeon_device;
144
145
146 /*
147  * BIOS.
148  */
149 bool radeon_get_bios(struct radeon_device *rdev);
150
151 /*
152  * Dummy page
153  */
154 struct radeon_dummy_page {
155         struct page     *page;
156         dma_addr_t      addr;
157 };
158 int radeon_dummy_page_init(struct radeon_device *rdev);
159 void radeon_dummy_page_fini(struct radeon_device *rdev);
160
161
162 /*
163  * Clocks
164  */
165 struct radeon_clock {
166         struct radeon_pll p1pll;
167         struct radeon_pll p2pll;
168         struct radeon_pll dcpll;
169         struct radeon_pll spll;
170         struct radeon_pll mpll;
171         /* 10 Khz units */
172         uint32_t default_mclk;
173         uint32_t default_sclk;
174         uint32_t default_dispclk;
175         uint32_t dp_extclk;
176         uint32_t max_pixel_clock;
177 };
178
179 /*
180  * Power management
181  */
182 int radeon_pm_init(struct radeon_device *rdev);
183 void radeon_pm_fini(struct radeon_device *rdev);
184 void radeon_pm_compute_clocks(struct radeon_device *rdev);
185 void radeon_pm_suspend(struct radeon_device *rdev);
186 void radeon_pm_resume(struct radeon_device *rdev);
187 void radeon_combios_get_power_modes(struct radeon_device *rdev);
188 void radeon_atombios_get_power_modes(struct radeon_device *rdev);
189 void radeon_atom_set_voltage(struct radeon_device *rdev, u16 voltage_level, u8 voltage_type);
190 void rs690_pm_info(struct radeon_device *rdev);
191 extern int rv6xx_get_temp(struct radeon_device *rdev);
192 extern int rv770_get_temp(struct radeon_device *rdev);
193 extern int evergreen_get_temp(struct radeon_device *rdev);
194 extern int sumo_get_temp(struct radeon_device *rdev);
195 extern int si_get_temp(struct radeon_device *rdev);
196 extern void evergreen_tiling_fields(unsigned tiling_flags, unsigned *bankw,
197                                     unsigned *bankh, unsigned *mtaspect,
198                                     unsigned *tile_split);
199
200 /*
201  * Fences.
202  */
203 struct radeon_fence_driver {
204         uint32_t                        scratch_reg;
205         uint64_t                        gpu_addr;
206         volatile uint32_t               *cpu_addr;
207         /* sync_seq is protected by ring emission lock */
208         uint64_t                        sync_seq[RADEON_NUM_RINGS];
209         atomic64_t                      last_seq;
210         unsigned long                   last_activity;
211         bool                            initialized;
212 };
213
214 struct radeon_fence {
215         struct radeon_device            *rdev;
216         struct kref                     kref;
217         /* protected by radeon_fence.lock */
218         uint64_t                        seq;
219         /* RB, DMA, etc. */
220         unsigned                        ring;
221 };
222
223 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring);
224 int radeon_fence_driver_init(struct radeon_device *rdev);
225 void radeon_fence_driver_fini(struct radeon_device *rdev);
226 int radeon_fence_emit(struct radeon_device *rdev, struct radeon_fence **fence, int ring);
227 void radeon_fence_process(struct radeon_device *rdev, int ring);
228 bool radeon_fence_signaled(struct radeon_fence *fence);
229 int radeon_fence_wait(struct radeon_fence *fence, bool interruptible);
230 int radeon_fence_wait_next_locked(struct radeon_device *rdev, int ring);
231 void radeon_fence_wait_empty_locked(struct radeon_device *rdev, int ring);
232 int radeon_fence_wait_any(struct radeon_device *rdev,
233                           struct radeon_fence **fences,
234                           bool intr);
235 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence);
236 void radeon_fence_unref(struct radeon_fence **fence);
237 unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring);
238 bool radeon_fence_need_sync(struct radeon_fence *fence, int ring);
239 void radeon_fence_note_sync(struct radeon_fence *fence, int ring);
240 static inline struct radeon_fence *radeon_fence_later(struct radeon_fence *a,
241                                                       struct radeon_fence *b)
242 {
243         if (!a) {
244                 return b;
245         }
246
247         if (!b) {
248                 return a;
249         }
250
251         BUG_ON(a->ring != b->ring);
252
253         if (a->seq > b->seq) {
254                 return a;
255         } else {
256                 return b;
257         }
258 }
259
260 static inline bool radeon_fence_is_earlier(struct radeon_fence *a,
261                                            struct radeon_fence *b)
262 {
263         if (!a) {
264                 return false;
265         }
266
267         if (!b) {
268                 return true;
269         }
270
271         BUG_ON(a->ring != b->ring);
272
273         return a->seq < b->seq;
274 }
275
276 /*
277  * Tiling registers
278  */
279 struct radeon_surface_reg {
280         struct radeon_bo *bo;
281 };
282
283 #define RADEON_GEM_MAX_SURFACES 8
284
285 /*
286  * TTM.
287  */
288 struct radeon_mman {
289         struct ttm_bo_global_ref        bo_global_ref;
290         struct drm_global_reference     mem_global_ref;
291         struct ttm_bo_device            bdev;
292         bool                            mem_global_referenced;
293         bool                            initialized;
294 };
295
296 /* bo virtual address in a specific vm */
297 struct radeon_bo_va {
298         /* protected by bo being reserved */
299         struct list_head                bo_list;
300         uint64_t                        soffset;
301         uint64_t                        eoffset;
302         uint32_t                        flags;
303         bool                            valid;
304         unsigned                        ref_count;
305
306         /* protected by vm mutex */
307         struct list_head                vm_list;
308
309         /* constant after initialization */
310         struct radeon_vm                *vm;
311         struct radeon_bo                *bo;
312 };
313
314 struct radeon_bo {
315         /* Protected by gem.mutex */
316         struct list_head                list;
317         /* Protected by tbo.reserved */
318         u32                             placements[3];
319         struct ttm_placement            placement;
320         struct ttm_buffer_object        tbo;
321         struct ttm_bo_kmap_obj          kmap;
322         unsigned                        pin_count;
323         void                            *kptr;
324         u32                             tiling_flags;
325         u32                             pitch;
326         int                             surface_reg;
327         /* list of all virtual address to which this bo
328          * is associated to
329          */
330         struct list_head                va;
331         /* Constant after initialization */
332         struct radeon_device            *rdev;
333         struct drm_gem_object           gem_base;
334
335         struct ttm_bo_kmap_obj dma_buf_vmap;
336         int vmapping_count;
337 };
338 #define gem_to_radeon_bo(gobj) container_of((gobj), struct radeon_bo, gem_base)
339
340 struct radeon_bo_list {
341         struct ttm_validate_buffer tv;
342         struct radeon_bo        *bo;
343         uint64_t                gpu_offset;
344         unsigned                rdomain;
345         unsigned                wdomain;
346         u32                     tiling_flags;
347 };
348
349 /* sub-allocation manager, it has to be protected by another lock.
350  * By conception this is an helper for other part of the driver
351  * like the indirect buffer or semaphore, which both have their
352  * locking.
353  *
354  * Principe is simple, we keep a list of sub allocation in offset
355  * order (first entry has offset == 0, last entry has the highest
356  * offset).
357  *
358  * When allocating new object we first check if there is room at
359  * the end total_size - (last_object_offset + last_object_size) >=
360  * alloc_size. If so we allocate new object there.
361  *
362  * When there is not enough room at the end, we start waiting for
363  * each sub object until we reach object_offset+object_size >=
364  * alloc_size, this object then become the sub object we return.
365  *
366  * Alignment can't be bigger than page size.
367  *
368  * Hole are not considered for allocation to keep things simple.
369  * Assumption is that there won't be hole (all object on same
370  * alignment).
371  */
372 struct radeon_sa_manager {
373         wait_queue_head_t       wq;
374         struct radeon_bo        *bo;
375         struct list_head        *hole;
376         struct list_head        flist[RADEON_NUM_RINGS];
377         struct list_head        olist;
378         unsigned                size;
379         uint64_t                gpu_addr;
380         void                    *cpu_ptr;
381         uint32_t                domain;
382 };
383
384 struct radeon_sa_bo;
385
386 /* sub-allocation buffer */
387 struct radeon_sa_bo {
388         struct list_head                olist;
389         struct list_head                flist;
390         struct radeon_sa_manager        *manager;
391         unsigned                        soffset;
392         unsigned                        eoffset;
393         struct radeon_fence             *fence;
394 };
395
396 /*
397  * GEM objects.
398  */
399 struct radeon_gem {
400         struct mutex            mutex;
401         struct list_head        objects;
402 };
403
404 int radeon_gem_init(struct radeon_device *rdev);
405 void radeon_gem_fini(struct radeon_device *rdev);
406 int radeon_gem_object_create(struct radeon_device *rdev, int size,
407                                 int alignment, int initial_domain,
408                                 bool discardable, bool kernel,
409                                 struct drm_gem_object **obj);
410
411 int radeon_mode_dumb_create(struct drm_file *file_priv,
412                             struct drm_device *dev,
413                             struct drm_mode_create_dumb *args);
414 int radeon_mode_dumb_mmap(struct drm_file *filp,
415                           struct drm_device *dev,
416                           uint32_t handle, uint64_t *offset_p);
417 int radeon_mode_dumb_destroy(struct drm_file *file_priv,
418                              struct drm_device *dev,
419                              uint32_t handle);
420
421 /*
422  * Semaphores.
423  */
424 /* everything here is constant */
425 struct radeon_semaphore {
426         struct radeon_sa_bo             *sa_bo;
427         signed                          waiters;
428         uint64_t                        gpu_addr;
429 };
430
431 int radeon_semaphore_create(struct radeon_device *rdev,
432                             struct radeon_semaphore **semaphore);
433 void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
434                                   struct radeon_semaphore *semaphore);
435 void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
436                                 struct radeon_semaphore *semaphore);
437 int radeon_semaphore_sync_rings(struct radeon_device *rdev,
438                                 struct radeon_semaphore *semaphore,
439                                 int signaler, int waiter);
440 void radeon_semaphore_free(struct radeon_device *rdev,
441                            struct radeon_semaphore **semaphore,
442                            struct radeon_fence *fence);
443
444 /*
445  * GART structures, functions & helpers
446  */
447 struct radeon_mc;
448
449 #define RADEON_GPU_PAGE_SIZE 4096
450 #define RADEON_GPU_PAGE_MASK (RADEON_GPU_PAGE_SIZE - 1)
451 #define RADEON_GPU_PAGE_SHIFT 12
452 #define RADEON_GPU_PAGE_ALIGN(a) (((a) + RADEON_GPU_PAGE_MASK) & ~RADEON_GPU_PAGE_MASK)
453
454 struct radeon_gart {
455         dma_addr_t                      table_addr;
456         struct radeon_bo                *robj;
457         void                            *ptr;
458         unsigned                        num_gpu_pages;
459         unsigned                        num_cpu_pages;
460         unsigned                        table_size;
461         struct page                     **pages;
462         dma_addr_t                      *pages_addr;
463         bool                            ready;
464 };
465
466 int radeon_gart_table_ram_alloc(struct radeon_device *rdev);
467 void radeon_gart_table_ram_free(struct radeon_device *rdev);
468 int radeon_gart_table_vram_alloc(struct radeon_device *rdev);
469 void radeon_gart_table_vram_free(struct radeon_device *rdev);
470 int radeon_gart_table_vram_pin(struct radeon_device *rdev);
471 void radeon_gart_table_vram_unpin(struct radeon_device *rdev);
472 int radeon_gart_init(struct radeon_device *rdev);
473 void radeon_gart_fini(struct radeon_device *rdev);
474 void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
475                         int pages);
476 int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
477                      int pages, struct page **pagelist,
478                      dma_addr_t *dma_addr);
479 void radeon_gart_restore(struct radeon_device *rdev);
480
481
482 /*
483  * GPU MC structures, functions & helpers
484  */
485 struct radeon_mc {
486         resource_size_t         aper_size;
487         resource_size_t         aper_base;
488         resource_size_t         agp_base;
489         /* for some chips with <= 32MB we need to lie
490          * about vram size near mc fb location */
491         u64                     mc_vram_size;
492         u64                     visible_vram_size;
493         u64                     gtt_size;
494         u64                     gtt_start;
495         u64                     gtt_end;
496         u64                     vram_start;
497         u64                     vram_end;
498         unsigned                vram_width;
499         u64                     real_vram_size;
500         int                     vram_mtrr;
501         bool                    vram_is_ddr;
502         bool                    igp_sideport_enabled;
503         u64                     gtt_base_align;
504 };
505
506 bool radeon_combios_sideport_present(struct radeon_device *rdev);
507 bool radeon_atombios_sideport_present(struct radeon_device *rdev);
508
509 /*
510  * GPU scratch registers structures, functions & helpers
511  */
512 struct radeon_scratch {
513         unsigned                num_reg;
514         uint32_t                reg_base;
515         bool                    free[32];
516         uint32_t                reg[32];
517 };
518
519 int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg);
520 void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg);
521
522
523 /*
524  * IRQS.
525  */
526
527 struct radeon_unpin_work {
528         struct work_struct work;
529         struct radeon_device *rdev;
530         int crtc_id;
531         struct radeon_fence *fence;
532         struct drm_pending_vblank_event *event;
533         struct radeon_bo *old_rbo;
534         u64 new_crtc_base;
535 };
536
537 struct r500_irq_stat_regs {
538         u32 disp_int;
539         u32 hdmi0_status;
540 };
541
542 struct r600_irq_stat_regs {
543         u32 disp_int;
544         u32 disp_int_cont;
545         u32 disp_int_cont2;
546         u32 d1grph_int;
547         u32 d2grph_int;
548         u32 hdmi0_status;
549         u32 hdmi1_status;
550 };
551
552 struct evergreen_irq_stat_regs {
553         u32 disp_int;
554         u32 disp_int_cont;
555         u32 disp_int_cont2;
556         u32 disp_int_cont3;
557         u32 disp_int_cont4;
558         u32 disp_int_cont5;
559         u32 d1grph_int;
560         u32 d2grph_int;
561         u32 d3grph_int;
562         u32 d4grph_int;
563         u32 d5grph_int;
564         u32 d6grph_int;
565         u32 afmt_status1;
566         u32 afmt_status2;
567         u32 afmt_status3;
568         u32 afmt_status4;
569         u32 afmt_status5;
570         u32 afmt_status6;
571 };
572
573 union radeon_irq_stat_regs {
574         struct r500_irq_stat_regs r500;
575         struct r600_irq_stat_regs r600;
576         struct evergreen_irq_stat_regs evergreen;
577 };
578
579 #define RADEON_MAX_HPD_PINS 6
580 #define RADEON_MAX_CRTCS 6
581 #define RADEON_MAX_AFMT_BLOCKS 6
582
583 struct radeon_irq {
584         bool                            installed;
585         spinlock_t                      lock;
586         atomic_t                        ring_int[RADEON_NUM_RINGS];
587         bool                            crtc_vblank_int[RADEON_MAX_CRTCS];
588         atomic_t                        pflip[RADEON_MAX_CRTCS];
589         wait_queue_head_t               vblank_queue;
590         bool                            hpd[RADEON_MAX_HPD_PINS];
591         bool                            afmt[RADEON_MAX_AFMT_BLOCKS];
592         union radeon_irq_stat_regs      stat_regs;
593 };
594
595 int radeon_irq_kms_init(struct radeon_device *rdev);
596 void radeon_irq_kms_fini(struct radeon_device *rdev);
597 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring);
598 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring);
599 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc);
600 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc);
601 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block);
602 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block);
603 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask);
604 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask);
605
606 /*
607  * CP & rings.
608  */
609
610 struct radeon_ib {
611         struct radeon_sa_bo             *sa_bo;
612         uint32_t                        length_dw;
613         uint64_t                        gpu_addr;
614         uint32_t                        *ptr;
615         int                             ring;
616         struct radeon_fence             *fence;
617         struct radeon_vm                *vm;
618         bool                            is_const_ib;
619         struct radeon_fence             *sync_to[RADEON_NUM_RINGS];
620         struct radeon_semaphore         *semaphore;
621 };
622
623 struct radeon_ring {
624         struct radeon_bo        *ring_obj;
625         volatile uint32_t       *ring;
626         unsigned                rptr;
627         unsigned                rptr_offs;
628         unsigned                rptr_reg;
629         unsigned                rptr_save_reg;
630         u64                     next_rptr_gpu_addr;
631         volatile u32            *next_rptr_cpu_addr;
632         unsigned                wptr;
633         unsigned                wptr_old;
634         unsigned                wptr_reg;
635         unsigned                ring_size;
636         unsigned                ring_free_dw;
637         int                     count_dw;
638         unsigned long           last_activity;
639         unsigned                last_rptr;
640         uint64_t                gpu_addr;
641         uint32_t                align_mask;
642         uint32_t                ptr_mask;
643         bool                    ready;
644         u32                     ptr_reg_shift;
645         u32                     ptr_reg_mask;
646         u32                     nop;
647         u32                     idx;
648 };
649
650 /*
651  * VM
652  */
653
654 /* maximum number of VMIDs */
655 #define RADEON_NUM_VM   16
656
657 /* defines number of bits in page table versus page directory,
658  * a page is 4KB so we have 12 bits offset, 9 bits in the page
659  * table and the remaining 19 bits are in the page directory */
660 #define RADEON_VM_BLOCK_SIZE   9
661
662 /* number of entries in page table */
663 #define RADEON_VM_PTE_COUNT (1 << RADEON_VM_BLOCK_SIZE)
664
665 struct radeon_vm {
666         struct list_head                list;
667         struct list_head                va;
668         unsigned                        id;
669
670         /* contains the page directory */
671         struct radeon_sa_bo             *page_directory;
672         uint64_t                        pd_gpu_addr;
673
674         /* array of page tables, one for each page directory entry */
675         struct radeon_sa_bo             **page_tables;
676
677         struct mutex                    mutex;
678         /* last fence for cs using this vm */
679         struct radeon_fence             *fence;
680         /* last flush or NULL if we still need to flush */
681         struct radeon_fence             *last_flush;
682 };
683
684 struct radeon_vm_manager {
685         struct mutex                    lock;
686         struct list_head                lru_vm;
687         struct radeon_fence             *active[RADEON_NUM_VM];
688         struct radeon_sa_manager        sa_manager;
689         uint32_t                        max_pfn;
690         /* number of VMIDs */
691         unsigned                        nvm;
692         /* vram base address for page table entry  */
693         u64                             vram_base_offset;
694         /* is vm enabled? */
695         bool                            enabled;
696 };
697
698 /*
699  * file private structure
700  */
701 struct radeon_fpriv {
702         struct radeon_vm                vm;
703 };
704
705 /*
706  * R6xx+ IH ring
707  */
708 struct r600_ih {
709         struct radeon_bo        *ring_obj;
710         volatile uint32_t       *ring;
711         unsigned                rptr;
712         unsigned                ring_size;
713         uint64_t                gpu_addr;
714         uint32_t                ptr_mask;
715         atomic_t                lock;
716         bool                    enabled;
717 };
718
719 struct r600_blit_cp_primitives {
720         void (*set_render_target)(struct radeon_device *rdev, int format,
721                                   int w, int h, u64 gpu_addr);
722         void (*cp_set_surface_sync)(struct radeon_device *rdev,
723                                     u32 sync_type, u32 size,
724                                     u64 mc_addr);
725         void (*set_shaders)(struct radeon_device *rdev);
726         void (*set_vtx_resource)(struct radeon_device *rdev, u64 gpu_addr);
727         void (*set_tex_resource)(struct radeon_device *rdev,
728                                  int format, int w, int h, int pitch,
729                                  u64 gpu_addr, u32 size);
730         void (*set_scissors)(struct radeon_device *rdev, int x1, int y1,
731                              int x2, int y2);
732         void (*draw_auto)(struct radeon_device *rdev);
733         void (*set_default_state)(struct radeon_device *rdev);
734 };
735
736 struct r600_blit {
737         struct radeon_bo        *shader_obj;
738         struct r600_blit_cp_primitives primitives;
739         int max_dim;
740         int ring_size_common;
741         int ring_size_per_loop;
742         u64 shader_gpu_addr;
743         u32 vs_offset, ps_offset;
744         u32 state_offset;
745         u32 state_len;
746 };
747
748 /*
749  * SI RLC stuff
750  */
751 struct si_rlc {
752         /* for power gating */
753         struct radeon_bo        *save_restore_obj;
754         uint64_t                save_restore_gpu_addr;
755         /* for clear state */
756         struct radeon_bo        *clear_state_obj;
757         uint64_t                clear_state_gpu_addr;
758 };
759
760 int radeon_ib_get(struct radeon_device *rdev, int ring,
761                   struct radeon_ib *ib, struct radeon_vm *vm,
762                   unsigned size);
763 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib);
764 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
765                        struct radeon_ib *const_ib);
766 int radeon_ib_pool_init(struct radeon_device *rdev);
767 void radeon_ib_pool_fini(struct radeon_device *rdev);
768 int radeon_ib_ring_tests(struct radeon_device *rdev);
769 /* Ring access between begin & end cannot sleep */
770 bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
771                                       struct radeon_ring *ring);
772 void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *cp);
773 int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *cp, unsigned ndw);
774 int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *cp, unsigned ndw);
775 void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *cp);
776 void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *cp);
777 void radeon_ring_undo(struct radeon_ring *ring);
778 void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *cp);
779 int radeon_ring_test(struct radeon_device *rdev, struct radeon_ring *cp);
780 void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring);
781 void radeon_ring_lockup_update(struct radeon_ring *ring);
782 bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring);
783 unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
784                             uint32_t **data);
785 int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
786                         unsigned size, uint32_t *data);
787 int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *cp, unsigned ring_size,
788                      unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
789                      u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop);
790 void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *cp);
791
792
793 /* r600 async dma */
794 void r600_dma_stop(struct radeon_device *rdev);
795 int r600_dma_resume(struct radeon_device *rdev);
796 void r600_dma_fini(struct radeon_device *rdev);
797
798 /*
799  * CS.
800  */
801 struct radeon_cs_reloc {
802         struct drm_gem_object           *gobj;
803         struct radeon_bo                *robj;
804         struct radeon_bo_list           lobj;
805         uint32_t                        handle;
806         uint32_t                        flags;
807 };
808
809 struct radeon_cs_chunk {
810         uint32_t                chunk_id;
811         uint32_t                length_dw;
812         int                     kpage_idx[2];
813         uint32_t                *kpage[2];
814         uint32_t                *kdata;
815         void __user             *user_ptr;
816         int                     last_copied_page;
817         int                     last_page_index;
818 };
819
820 struct radeon_cs_parser {
821         struct device           *dev;
822         struct radeon_device    *rdev;
823         struct drm_file         *filp;
824         /* chunks */
825         unsigned                nchunks;
826         struct radeon_cs_chunk  *chunks;
827         uint64_t                *chunks_array;
828         /* IB */
829         unsigned                idx;
830         /* relocations */
831         unsigned                nrelocs;
832         struct radeon_cs_reloc  *relocs;
833         struct radeon_cs_reloc  **relocs_ptr;
834         struct list_head        validated;
835         /* indices of various chunks */
836         int                     chunk_ib_idx;
837         int                     chunk_relocs_idx;
838         int                     chunk_flags_idx;
839         int                     chunk_const_ib_idx;
840         struct radeon_ib        ib;
841         struct radeon_ib        const_ib;
842         void                    *track;
843         unsigned                family;
844         int                     parser_error;
845         u32                     cs_flags;
846         u32                     ring;
847         s32                     priority;
848 };
849
850 extern int radeon_cs_finish_pages(struct radeon_cs_parser *p);
851 extern u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx);
852
853 struct radeon_cs_packet {
854         unsigned        idx;
855         unsigned        type;
856         unsigned        reg;
857         unsigned        opcode;
858         int             count;
859         unsigned        one_reg_wr;
860 };
861
862 typedef int (*radeon_packet0_check_t)(struct radeon_cs_parser *p,
863                                       struct radeon_cs_packet *pkt,
864                                       unsigned idx, unsigned reg);
865 typedef int (*radeon_packet3_check_t)(struct radeon_cs_parser *p,
866                                       struct radeon_cs_packet *pkt);
867
868
869 /*
870  * AGP
871  */
872 int radeon_agp_init(struct radeon_device *rdev);
873 void radeon_agp_resume(struct radeon_device *rdev);
874 void radeon_agp_suspend(struct radeon_device *rdev);
875 void radeon_agp_fini(struct radeon_device *rdev);
876
877
878 /*
879  * Writeback
880  */
881 struct radeon_wb {
882         struct radeon_bo        *wb_obj;
883         volatile uint32_t       *wb;
884         uint64_t                gpu_addr;
885         bool                    enabled;
886         bool                    use_event;
887 };
888
889 #define RADEON_WB_SCRATCH_OFFSET 0
890 #define RADEON_WB_RING0_NEXT_RPTR 256
891 #define RADEON_WB_CP_RPTR_OFFSET 1024
892 #define RADEON_WB_CP1_RPTR_OFFSET 1280
893 #define RADEON_WB_CP2_RPTR_OFFSET 1536
894 #define R600_WB_DMA_RPTR_OFFSET   1792
895 #define R600_WB_IH_WPTR_OFFSET   2048
896 #define R600_WB_EVENT_OFFSET     3072
897
898 /**
899  * struct radeon_pm - power management datas
900  * @max_bandwidth:      maximum bandwidth the gpu has (MByte/s)
901  * @igp_sideport_mclk:  sideport memory clock Mhz (rs690,rs740,rs780,rs880)
902  * @igp_system_mclk:    system clock Mhz (rs690,rs740,rs780,rs880)
903  * @igp_ht_link_clk:    ht link clock Mhz (rs690,rs740,rs780,rs880)
904  * @igp_ht_link_width:  ht link width in bits (rs690,rs740,rs780,rs880)
905  * @k8_bandwidth:       k8 bandwidth the gpu has (MByte/s) (IGP)
906  * @sideport_bandwidth: sideport bandwidth the gpu has (MByte/s) (IGP)
907  * @ht_bandwidth:       ht bandwidth the gpu has (MByte/s) (IGP)
908  * @core_bandwidth:     core GPU bandwidth the gpu has (MByte/s) (IGP)
909  * @sclk:               GPU clock Mhz (core bandwidth depends of this clock)
910  * @needed_bandwidth:   current bandwidth needs
911  *
912  * It keeps track of various data needed to take powermanagement decision.
913  * Bandwidth need is used to determine minimun clock of the GPU and memory.
914  * Equation between gpu/memory clock and available bandwidth is hw dependent
915  * (type of memory, bus size, efficiency, ...)
916  */
917
918 enum radeon_pm_method {
919         PM_METHOD_PROFILE,
920         PM_METHOD_DYNPM,
921 };
922
923 enum radeon_dynpm_state {
924         DYNPM_STATE_DISABLED,
925         DYNPM_STATE_MINIMUM,
926         DYNPM_STATE_PAUSED,
927         DYNPM_STATE_ACTIVE,
928         DYNPM_STATE_SUSPENDED,
929 };
930 enum radeon_dynpm_action {
931         DYNPM_ACTION_NONE,
932         DYNPM_ACTION_MINIMUM,
933         DYNPM_ACTION_DOWNCLOCK,
934         DYNPM_ACTION_UPCLOCK,
935         DYNPM_ACTION_DEFAULT
936 };
937
938 enum radeon_voltage_type {
939         VOLTAGE_NONE = 0,
940         VOLTAGE_GPIO,
941         VOLTAGE_VDDC,
942         VOLTAGE_SW
943 };
944
945 enum radeon_pm_state_type {
946         POWER_STATE_TYPE_DEFAULT,
947         POWER_STATE_TYPE_POWERSAVE,
948         POWER_STATE_TYPE_BATTERY,
949         POWER_STATE_TYPE_BALANCED,
950         POWER_STATE_TYPE_PERFORMANCE,
951 };
952
953 enum radeon_pm_profile_type {
954         PM_PROFILE_DEFAULT,
955         PM_PROFILE_AUTO,
956         PM_PROFILE_LOW,
957         PM_PROFILE_MID,
958         PM_PROFILE_HIGH,
959 };
960
961 #define PM_PROFILE_DEFAULT_IDX 0
962 #define PM_PROFILE_LOW_SH_IDX  1
963 #define PM_PROFILE_MID_SH_IDX  2
964 #define PM_PROFILE_HIGH_SH_IDX 3
965 #define PM_PROFILE_LOW_MH_IDX  4
966 #define PM_PROFILE_MID_MH_IDX  5
967 #define PM_PROFILE_HIGH_MH_IDX 6
968 #define PM_PROFILE_MAX         7
969
970 struct radeon_pm_profile {
971         int dpms_off_ps_idx;
972         int dpms_on_ps_idx;
973         int dpms_off_cm_idx;
974         int dpms_on_cm_idx;
975 };
976
977 enum radeon_int_thermal_type {
978         THERMAL_TYPE_NONE,
979         THERMAL_TYPE_RV6XX,
980         THERMAL_TYPE_RV770,
981         THERMAL_TYPE_EVERGREEN,
982         THERMAL_TYPE_SUMO,
983         THERMAL_TYPE_NI,
984         THERMAL_TYPE_SI,
985 };
986
987 struct radeon_voltage {
988         enum radeon_voltage_type type;
989         /* gpio voltage */
990         struct radeon_gpio_rec gpio;
991         u32 delay; /* delay in usec from voltage drop to sclk change */
992         bool active_high; /* voltage drop is active when bit is high */
993         /* VDDC voltage */
994         u8 vddc_id; /* index into vddc voltage table */
995         u8 vddci_id; /* index into vddci voltage table */
996         bool vddci_enabled;
997         /* r6xx+ sw */
998         u16 voltage;
999         /* evergreen+ vddci */
1000         u16 vddci;
1001 };
1002
1003 /* clock mode flags */
1004 #define RADEON_PM_MODE_NO_DISPLAY          (1 << 0)
1005
1006 struct radeon_pm_clock_info {
1007         /* memory clock */
1008         u32 mclk;
1009         /* engine clock */
1010         u32 sclk;
1011         /* voltage info */
1012         struct radeon_voltage voltage;
1013         /* standardized clock flags */
1014         u32 flags;
1015 };
1016
1017 /* state flags */
1018 #define RADEON_PM_STATE_SINGLE_DISPLAY_ONLY (1 << 0)
1019
1020 struct radeon_power_state {
1021         enum radeon_pm_state_type type;
1022         struct radeon_pm_clock_info *clock_info;
1023         /* number of valid clock modes in this power state */
1024         int num_clock_modes;
1025         struct radeon_pm_clock_info *default_clock_mode;
1026         /* standardized state flags */
1027         u32 flags;
1028         u32 misc; /* vbios specific flags */
1029         u32 misc2; /* vbios specific flags */
1030         int pcie_lanes; /* pcie lanes */
1031 };
1032
1033 /*
1034  * Some modes are overclocked by very low value, accept them
1035  */
1036 #define RADEON_MODE_OVERCLOCK_MARGIN 500 /* 5 MHz */
1037
1038 struct radeon_pm {
1039         struct mutex            mutex;
1040         /* write locked while reprogramming mclk */
1041         struct rw_semaphore     mclk_lock;
1042         u32                     active_crtcs;
1043         int                     active_crtc_count;
1044         int                     req_vblank;
1045         bool                    vblank_sync;
1046         fixed20_12              max_bandwidth;
1047         fixed20_12              igp_sideport_mclk;
1048         fixed20_12              igp_system_mclk;
1049         fixed20_12              igp_ht_link_clk;
1050         fixed20_12              igp_ht_link_width;
1051         fixed20_12              k8_bandwidth;
1052         fixed20_12              sideport_bandwidth;
1053         fixed20_12              ht_bandwidth;
1054         fixed20_12              core_bandwidth;
1055         fixed20_12              sclk;
1056         fixed20_12              mclk;
1057         fixed20_12              needed_bandwidth;
1058         struct radeon_power_state *power_state;
1059         /* number of valid power states */
1060         int                     num_power_states;
1061         int                     current_power_state_index;
1062         int                     current_clock_mode_index;
1063         int                     requested_power_state_index;
1064         int                     requested_clock_mode_index;
1065         int                     default_power_state_index;
1066         u32                     current_sclk;
1067         u32                     current_mclk;
1068         u16                     current_vddc;
1069         u16                     current_vddci;
1070         u32                     default_sclk;
1071         u32                     default_mclk;
1072         u16                     default_vddc;
1073         u16                     default_vddci;
1074         struct radeon_i2c_chan *i2c_bus;
1075         /* selected pm method */
1076         enum radeon_pm_method     pm_method;
1077         /* dynpm power management */
1078         struct delayed_work     dynpm_idle_work;
1079         enum radeon_dynpm_state dynpm_state;
1080         enum radeon_dynpm_action        dynpm_planned_action;
1081         unsigned long           dynpm_action_timeout;
1082         bool                    dynpm_can_upclock;
1083         bool                    dynpm_can_downclock;
1084         /* profile-based power management */
1085         enum radeon_pm_profile_type profile;
1086         int                     profile_index;
1087         struct radeon_pm_profile profiles[PM_PROFILE_MAX];
1088         /* internal thermal controller on rv6xx+ */
1089         enum radeon_int_thermal_type int_thermal_type;
1090         struct device           *int_hwmon_dev;
1091 };
1092
1093 int radeon_pm_get_type_index(struct radeon_device *rdev,
1094                              enum radeon_pm_state_type ps_type,
1095                              int instance);
1096
1097 struct r600_audio {
1098         int                     channels;
1099         int                     rate;
1100         int                     bits_per_sample;
1101         u8                      status_bits;
1102         u8                      category_code;
1103 };
1104
1105 /*
1106  * Benchmarking
1107  */
1108 void radeon_benchmark(struct radeon_device *rdev, int test_number);
1109
1110
1111 /*
1112  * Testing
1113  */
1114 void radeon_test_moves(struct radeon_device *rdev);
1115 void radeon_test_ring_sync(struct radeon_device *rdev,
1116                            struct radeon_ring *cpA,
1117                            struct radeon_ring *cpB);
1118 void radeon_test_syncing(struct radeon_device *rdev);
1119
1120
1121 /*
1122  * Debugfs
1123  */
1124 struct radeon_debugfs {
1125         struct drm_info_list    *files;
1126         unsigned                num_files;
1127 };
1128
1129 int radeon_debugfs_add_files(struct radeon_device *rdev,
1130                              struct drm_info_list *files,
1131                              unsigned nfiles);
1132 int radeon_debugfs_fence_init(struct radeon_device *rdev);
1133
1134
1135 /*
1136  * ASIC specific functions.
1137  */
1138 struct radeon_asic {
1139         int (*init)(struct radeon_device *rdev);
1140         void (*fini)(struct radeon_device *rdev);
1141         int (*resume)(struct radeon_device *rdev);
1142         int (*suspend)(struct radeon_device *rdev);
1143         void (*vga_set_state)(struct radeon_device *rdev, bool state);
1144         int (*asic_reset)(struct radeon_device *rdev);
1145         /* ioctl hw specific callback. Some hw might want to perform special
1146          * operation on specific ioctl. For instance on wait idle some hw
1147          * might want to perform and HDP flush through MMIO as it seems that
1148          * some R6XX/R7XX hw doesn't take HDP flush into account if programmed
1149          * through ring.
1150          */
1151         void (*ioctl_wait_idle)(struct radeon_device *rdev, struct radeon_bo *bo);
1152         /* check if 3D engine is idle */
1153         bool (*gui_idle)(struct radeon_device *rdev);
1154         /* wait for mc_idle */
1155         int (*mc_wait_for_idle)(struct radeon_device *rdev);
1156         /* gart */
1157         struct {
1158                 void (*tlb_flush)(struct radeon_device *rdev);
1159                 int (*set_page)(struct radeon_device *rdev, int i, uint64_t addr);
1160         } gart;
1161         struct {
1162                 int (*init)(struct radeon_device *rdev);
1163                 void (*fini)(struct radeon_device *rdev);
1164
1165                 u32 pt_ring_index;
1166                 void (*set_page)(struct radeon_device *rdev, uint64_t pe,
1167                                  uint64_t addr, unsigned count,
1168                                  uint32_t incr, uint32_t flags);
1169         } vm;
1170         /* ring specific callbacks */
1171         struct {
1172                 void (*ib_execute)(struct radeon_device *rdev, struct radeon_ib *ib);
1173                 int (*ib_parse)(struct radeon_device *rdev, struct radeon_ib *ib);
1174                 void (*emit_fence)(struct radeon_device *rdev, struct radeon_fence *fence);
1175                 void (*emit_semaphore)(struct radeon_device *rdev, struct radeon_ring *cp,
1176                                        struct radeon_semaphore *semaphore, bool emit_wait);
1177                 int (*cs_parse)(struct radeon_cs_parser *p);
1178                 void (*ring_start)(struct radeon_device *rdev, struct radeon_ring *cp);
1179                 int (*ring_test)(struct radeon_device *rdev, struct radeon_ring *cp);
1180                 int (*ib_test)(struct radeon_device *rdev, struct radeon_ring *cp);
1181                 bool (*is_lockup)(struct radeon_device *rdev, struct radeon_ring *cp);
1182                 void (*vm_flush)(struct radeon_device *rdev, int ridx, struct radeon_vm *vm);
1183         } ring[RADEON_NUM_RINGS];
1184         /* irqs */
1185         struct {
1186                 int (*set)(struct radeon_device *rdev);
1187                 int (*process)(struct radeon_device *rdev);
1188         } irq;
1189         /* displays */
1190         struct {
1191                 /* display watermarks */
1192                 void (*bandwidth_update)(struct radeon_device *rdev);
1193                 /* get frame count */
1194                 u32 (*get_vblank_counter)(struct radeon_device *rdev, int crtc);
1195                 /* wait for vblank */
1196                 void (*wait_for_vblank)(struct radeon_device *rdev, int crtc);
1197                 /* set backlight level */
1198                 void (*set_backlight_level)(struct radeon_encoder *radeon_encoder, u8 level);
1199                 /* get backlight level */
1200                 u8 (*get_backlight_level)(struct radeon_encoder *radeon_encoder);
1201         } display;
1202         /* copy functions for bo handling */
1203         struct {
1204                 int (*blit)(struct radeon_device *rdev,
1205                             uint64_t src_offset,
1206                             uint64_t dst_offset,
1207                             unsigned num_gpu_pages,
1208                             struct radeon_fence **fence);
1209                 u32 blit_ring_index;
1210                 int (*dma)(struct radeon_device *rdev,
1211                            uint64_t src_offset,
1212                            uint64_t dst_offset,
1213                            unsigned num_gpu_pages,
1214                            struct radeon_fence **fence);
1215                 u32 dma_ring_index;
1216                 /* method used for bo copy */
1217                 int (*copy)(struct radeon_device *rdev,
1218                             uint64_t src_offset,
1219                             uint64_t dst_offset,
1220                             unsigned num_gpu_pages,
1221                             struct radeon_fence **fence);
1222                 /* ring used for bo copies */
1223                 u32 copy_ring_index;
1224         } copy;
1225         /* surfaces */
1226         struct {
1227                 int (*set_reg)(struct radeon_device *rdev, int reg,
1228                                        uint32_t tiling_flags, uint32_t pitch,
1229                                        uint32_t offset, uint32_t obj_size);
1230                 void (*clear_reg)(struct radeon_device *rdev, int reg);
1231         } surface;
1232         /* hotplug detect */
1233         struct {
1234                 void (*init)(struct radeon_device *rdev);
1235                 void (*fini)(struct radeon_device *rdev);
1236                 bool (*sense)(struct radeon_device *rdev, enum radeon_hpd_id hpd);
1237                 void (*set_polarity)(struct radeon_device *rdev, enum radeon_hpd_id hpd);
1238         } hpd;
1239         /* power management */
1240         struct {
1241                 void (*misc)(struct radeon_device *rdev);
1242                 void (*prepare)(struct radeon_device *rdev);
1243                 void (*finish)(struct radeon_device *rdev);
1244                 void (*init_profile)(struct radeon_device *rdev);
1245                 void (*get_dynpm_state)(struct radeon_device *rdev);
1246                 uint32_t (*get_engine_clock)(struct radeon_device *rdev);
1247                 void (*set_engine_clock)(struct radeon_device *rdev, uint32_t eng_clock);
1248                 uint32_t (*get_memory_clock)(struct radeon_device *rdev);
1249                 void (*set_memory_clock)(struct radeon_device *rdev, uint32_t mem_clock);
1250                 int (*get_pcie_lanes)(struct radeon_device *rdev);
1251                 void (*set_pcie_lanes)(struct radeon_device *rdev, int lanes);
1252                 void (*set_clock_gating)(struct radeon_device *rdev, int enable);
1253         } pm;
1254         /* pageflipping */
1255         struct {
1256                 void (*pre_page_flip)(struct radeon_device *rdev, int crtc);
1257                 u32 (*page_flip)(struct radeon_device *rdev, int crtc, u64 crtc_base);
1258                 void (*post_page_flip)(struct radeon_device *rdev, int crtc);
1259         } pflip;
1260 };
1261
1262 /*
1263  * Asic structures
1264  */
1265 struct r100_asic {
1266         const unsigned          *reg_safe_bm;
1267         unsigned                reg_safe_bm_size;
1268         u32                     hdp_cntl;
1269 };
1270
1271 struct r300_asic {
1272         const unsigned          *reg_safe_bm;
1273         unsigned                reg_safe_bm_size;
1274         u32                     resync_scratch;
1275         u32                     hdp_cntl;
1276 };
1277
1278 struct r600_asic {
1279         unsigned                max_pipes;
1280         unsigned                max_tile_pipes;
1281         unsigned                max_simds;
1282         unsigned                max_backends;
1283         unsigned                max_gprs;
1284         unsigned                max_threads;
1285         unsigned                max_stack_entries;
1286         unsigned                max_hw_contexts;
1287         unsigned                max_gs_threads;
1288         unsigned                sx_max_export_size;
1289         unsigned                sx_max_export_pos_size;
1290         unsigned                sx_max_export_smx_size;
1291         unsigned                sq_num_cf_insts;
1292         unsigned                tiling_nbanks;
1293         unsigned                tiling_npipes;
1294         unsigned                tiling_group_size;
1295         unsigned                tile_config;
1296         unsigned                backend_map;
1297 };
1298
1299 struct rv770_asic {
1300         unsigned                max_pipes;
1301         unsigned                max_tile_pipes;
1302         unsigned                max_simds;
1303         unsigned                max_backends;
1304         unsigned                max_gprs;
1305         unsigned                max_threads;
1306         unsigned                max_stack_entries;
1307         unsigned                max_hw_contexts;
1308         unsigned                max_gs_threads;
1309         unsigned                sx_max_export_size;
1310         unsigned                sx_max_export_pos_size;
1311         unsigned                sx_max_export_smx_size;
1312         unsigned                sq_num_cf_insts;
1313         unsigned                sx_num_of_sets;
1314         unsigned                sc_prim_fifo_size;
1315         unsigned                sc_hiz_tile_fifo_size;
1316         unsigned                sc_earlyz_tile_fifo_fize;
1317         unsigned                tiling_nbanks;
1318         unsigned                tiling_npipes;
1319         unsigned                tiling_group_size;
1320         unsigned                tile_config;
1321         unsigned                backend_map;
1322 };
1323
1324 struct evergreen_asic {
1325         unsigned num_ses;
1326         unsigned max_pipes;
1327         unsigned max_tile_pipes;
1328         unsigned max_simds;
1329         unsigned max_backends;
1330         unsigned max_gprs;
1331         unsigned max_threads;
1332         unsigned max_stack_entries;
1333         unsigned max_hw_contexts;
1334         unsigned max_gs_threads;
1335         unsigned sx_max_export_size;
1336         unsigned sx_max_export_pos_size;
1337         unsigned sx_max_export_smx_size;
1338         unsigned sq_num_cf_insts;
1339         unsigned sx_num_of_sets;
1340         unsigned sc_prim_fifo_size;
1341         unsigned sc_hiz_tile_fifo_size;
1342         unsigned sc_earlyz_tile_fifo_size;
1343         unsigned tiling_nbanks;
1344         unsigned tiling_npipes;
1345         unsigned tiling_group_size;
1346         unsigned tile_config;
1347         unsigned backend_map;
1348 };
1349
1350 struct cayman_asic {
1351         unsigned max_shader_engines;
1352         unsigned max_pipes_per_simd;
1353         unsigned max_tile_pipes;
1354         unsigned max_simds_per_se;
1355         unsigned max_backends_per_se;
1356         unsigned max_texture_channel_caches;
1357         unsigned max_gprs;
1358         unsigned max_threads;
1359         unsigned max_gs_threads;
1360         unsigned max_stack_entries;
1361         unsigned sx_num_of_sets;
1362         unsigned sx_max_export_size;
1363         unsigned sx_max_export_pos_size;
1364         unsigned sx_max_export_smx_size;
1365         unsigned max_hw_contexts;
1366         unsigned sq_num_cf_insts;
1367         unsigned sc_prim_fifo_size;
1368         unsigned sc_hiz_tile_fifo_size;
1369         unsigned sc_earlyz_tile_fifo_size;
1370
1371         unsigned num_shader_engines;
1372         unsigned num_shader_pipes_per_simd;
1373         unsigned num_tile_pipes;
1374         unsigned num_simds_per_se;
1375         unsigned num_backends_per_se;
1376         unsigned backend_disable_mask_per_asic;
1377         unsigned backend_map;
1378         unsigned num_texture_channel_caches;
1379         unsigned mem_max_burst_length_bytes;
1380         unsigned mem_row_size_in_kb;
1381         unsigned shader_engine_tile_size;
1382         unsigned num_gpus;
1383         unsigned multi_gpu_tile_size;
1384
1385         unsigned tile_config;
1386 };
1387
1388 struct si_asic {
1389         unsigned max_shader_engines;
1390         unsigned max_tile_pipes;
1391         unsigned max_cu_per_sh;
1392         unsigned max_sh_per_se;
1393         unsigned max_backends_per_se;
1394         unsigned max_texture_channel_caches;
1395         unsigned max_gprs;
1396         unsigned max_gs_threads;
1397         unsigned max_hw_contexts;
1398         unsigned sc_prim_fifo_size_frontend;
1399         unsigned sc_prim_fifo_size_backend;
1400         unsigned sc_hiz_tile_fifo_size;
1401         unsigned sc_earlyz_tile_fifo_size;
1402
1403         unsigned num_tile_pipes;
1404         unsigned num_backends_per_se;
1405         unsigned backend_disable_mask_per_asic;
1406         unsigned backend_map;
1407         unsigned num_texture_channel_caches;
1408         unsigned mem_max_burst_length_bytes;
1409         unsigned mem_row_size_in_kb;
1410         unsigned shader_engine_tile_size;
1411         unsigned num_gpus;
1412         unsigned multi_gpu_tile_size;
1413
1414         unsigned tile_config;
1415 };
1416
1417 union radeon_asic_config {
1418         struct r300_asic        r300;
1419         struct r100_asic        r100;
1420         struct r600_asic        r600;
1421         struct rv770_asic       rv770;
1422         struct evergreen_asic   evergreen;
1423         struct cayman_asic      cayman;
1424         struct si_asic          si;
1425 };
1426
1427 /*
1428  * asic initizalization from radeon_asic.c
1429  */
1430 void radeon_agp_disable(struct radeon_device *rdev);
1431 int radeon_asic_init(struct radeon_device *rdev);
1432
1433
1434 /*
1435  * IOCTL.
1436  */
1437 int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
1438                           struct drm_file *filp);
1439 int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
1440                             struct drm_file *filp);
1441 int radeon_gem_pin_ioctl(struct drm_device *dev, void *data,
1442                          struct drm_file *file_priv);
1443 int radeon_gem_unpin_ioctl(struct drm_device *dev, void *data,
1444                            struct drm_file *file_priv);
1445 int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1446                             struct drm_file *file_priv);
1447 int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
1448                            struct drm_file *file_priv);
1449 int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1450                                 struct drm_file *filp);
1451 int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
1452                           struct drm_file *filp);
1453 int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
1454                           struct drm_file *filp);
1455 int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
1456                               struct drm_file *filp);
1457 int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
1458                           struct drm_file *filp);
1459 int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
1460 int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
1461                                 struct drm_file *filp);
1462 int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
1463                                 struct drm_file *filp);
1464
1465 /* VRAM scratch page for HDP bug, default vram page */
1466 struct r600_vram_scratch {
1467         struct radeon_bo                *robj;
1468         volatile uint32_t               *ptr;
1469         u64                             gpu_addr;
1470 };
1471
1472 /*
1473  * ACPI
1474  */
1475 struct radeon_atif_notification_cfg {
1476         bool enabled;
1477         int command_code;
1478 };
1479
1480 struct radeon_atif_notifications {
1481         bool display_switch;
1482         bool expansion_mode_change;
1483         bool thermal_state;
1484         bool forced_power_state;
1485         bool system_power_state;
1486         bool display_conf_change;
1487         bool px_gfx_switch;
1488         bool brightness_change;
1489         bool dgpu_display_event;
1490 };
1491
1492 struct radeon_atif_functions {
1493         bool system_params;
1494         bool sbios_requests;
1495         bool select_active_disp;
1496         bool lid_state;
1497         bool get_tv_standard;
1498         bool set_tv_standard;
1499         bool get_panel_expansion_mode;
1500         bool set_panel_expansion_mode;
1501         bool temperature_change;
1502         bool graphics_device_types;
1503 };
1504
1505 struct radeon_atif {
1506         struct radeon_atif_notifications notifications;
1507         struct radeon_atif_functions functions;
1508         struct radeon_atif_notification_cfg notification_cfg;
1509         struct radeon_encoder *encoder_for_bl;
1510 };
1511
1512 struct radeon_atcs_functions {
1513         bool get_ext_state;
1514         bool pcie_perf_req;
1515         bool pcie_dev_rdy;
1516         bool pcie_bus_width;
1517 };
1518
1519 struct radeon_atcs {
1520         struct radeon_atcs_functions functions;
1521 };
1522
1523 /*
1524  * Core structure, functions and helpers.
1525  */
1526 typedef uint32_t (*radeon_rreg_t)(struct radeon_device*, uint32_t);
1527 typedef void (*radeon_wreg_t)(struct radeon_device*, uint32_t, uint32_t);
1528
1529 struct radeon_device {
1530         struct device                   *dev;
1531         struct drm_device               *ddev;
1532         struct pci_dev                  *pdev;
1533         struct rw_semaphore             exclusive_lock;
1534         /* ASIC */
1535         union radeon_asic_config        config;
1536         enum radeon_family              family;
1537         unsigned long                   flags;
1538         int                             usec_timeout;
1539         enum radeon_pll_errata          pll_errata;
1540         int                             num_gb_pipes;
1541         int                             num_z_pipes;
1542         int                             disp_priority;
1543         /* BIOS */
1544         uint8_t                         *bios;
1545         bool                            is_atom_bios;
1546         uint16_t                        bios_header_start;
1547         struct radeon_bo                *stollen_vga_memory;
1548         /* Register mmio */
1549         resource_size_t                 rmmio_base;
1550         resource_size_t                 rmmio_size;
1551         void __iomem                    *rmmio;
1552         radeon_rreg_t                   mc_rreg;
1553         radeon_wreg_t                   mc_wreg;
1554         radeon_rreg_t                   pll_rreg;
1555         radeon_wreg_t                   pll_wreg;
1556         uint32_t                        pcie_reg_mask;
1557         radeon_rreg_t                   pciep_rreg;
1558         radeon_wreg_t                   pciep_wreg;
1559         /* io port */
1560         void __iomem                    *rio_mem;
1561         resource_size_t                 rio_mem_size;
1562         struct radeon_clock             clock;
1563         struct radeon_mc                mc;
1564         struct radeon_gart              gart;
1565         struct radeon_mode_info         mode_info;
1566         struct radeon_scratch           scratch;
1567         struct radeon_mman              mman;
1568         struct radeon_fence_driver      fence_drv[RADEON_NUM_RINGS];
1569         wait_queue_head_t               fence_queue;
1570         struct mutex                    ring_lock;
1571         struct radeon_ring              ring[RADEON_NUM_RINGS];
1572         bool                            ib_pool_ready;
1573         struct radeon_sa_manager        ring_tmp_bo;
1574         struct radeon_irq               irq;
1575         struct radeon_asic              *asic;
1576         struct radeon_gem               gem;
1577         struct radeon_pm                pm;
1578         uint32_t                        bios_scratch[RADEON_BIOS_NUM_SCRATCH];
1579         struct radeon_wb                wb;
1580         struct radeon_dummy_page        dummy_page;
1581         bool                            shutdown;
1582         bool                            suspend;
1583         bool                            need_dma32;
1584         bool                            accel_working;
1585         struct radeon_surface_reg surface_regs[RADEON_GEM_MAX_SURFACES];
1586         const struct firmware *me_fw;   /* all family ME firmware */
1587         const struct firmware *pfp_fw;  /* r6/700 PFP firmware */
1588         const struct firmware *rlc_fw;  /* r6/700 RLC firmware */
1589         const struct firmware *mc_fw;   /* NI MC firmware */
1590         const struct firmware *ce_fw;   /* SI CE firmware */
1591         struct r600_blit r600_blit;
1592         struct r600_vram_scratch vram_scratch;
1593         int msi_enabled; /* msi enabled */
1594         struct r600_ih ih; /* r6/700 interrupt ring */
1595         struct si_rlc rlc;
1596         struct work_struct hotplug_work;
1597         struct work_struct audio_work;
1598         int num_crtc; /* number of crtcs */
1599         struct mutex dc_hw_i2c_mutex; /* display controller hw i2c mutex */
1600         bool audio_enabled;
1601         struct r600_audio audio_status; /* audio stuff */
1602         struct notifier_block acpi_nb;
1603         /* only one userspace can use Hyperz features or CMASK at a time */
1604         struct drm_file *hyperz_filp;
1605         struct drm_file *cmask_filp;
1606         /* i2c buses */
1607         struct radeon_i2c_chan *i2c_bus[RADEON_MAX_I2C_BUS];
1608         /* debugfs */
1609         struct radeon_debugfs   debugfs[RADEON_DEBUGFS_MAX_COMPONENTS];
1610         unsigned                debugfs_count;
1611         /* virtual memory */
1612         struct radeon_vm_manager        vm_manager;
1613         struct mutex                    gpu_clock_mutex;
1614         /* ACPI interface */
1615         struct radeon_atif              atif;
1616         struct radeon_atcs              atcs;
1617 };
1618
1619 int radeon_device_init(struct radeon_device *rdev,
1620                        struct drm_device *ddev,
1621                        struct pci_dev *pdev,
1622                        uint32_t flags);
1623 void radeon_device_fini(struct radeon_device *rdev);
1624 int radeon_gpu_wait_for_idle(struct radeon_device *rdev);
1625
1626 uint32_t r100_mm_rreg(struct radeon_device *rdev, uint32_t reg);
1627 void r100_mm_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v);
1628 u32 r100_io_rreg(struct radeon_device *rdev, u32 reg);
1629 void r100_io_wreg(struct radeon_device *rdev, u32 reg, u32 v);
1630
1631 /*
1632  * Cast helper
1633  */
1634 #define to_radeon_fence(p) ((struct radeon_fence *)(p))
1635
1636 /*
1637  * Registers read & write functions.
1638  */
1639 #define RREG8(reg) readb((rdev->rmmio) + (reg))
1640 #define WREG8(reg, v) writeb(v, (rdev->rmmio) + (reg))
1641 #define RREG16(reg) readw((rdev->rmmio) + (reg))
1642 #define WREG16(reg, v) writew(v, (rdev->rmmio) + (reg))
1643 #define RREG32(reg) r100_mm_rreg(rdev, (reg))
1644 #define DREG32(reg) printk(KERN_INFO "REGISTER: " #reg " : 0x%08X\n", r100_mm_rreg(rdev, (reg)))
1645 #define WREG32(reg, v) r100_mm_wreg(rdev, (reg), (v))
1646 #define REG_SET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
1647 #define REG_GET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
1648 #define RREG32_PLL(reg) rdev->pll_rreg(rdev, (reg))
1649 #define WREG32_PLL(reg, v) rdev->pll_wreg(rdev, (reg), (v))
1650 #define RREG32_MC(reg) rdev->mc_rreg(rdev, (reg))
1651 #define WREG32_MC(reg, v) rdev->mc_wreg(rdev, (reg), (v))
1652 #define RREG32_PCIE(reg) rv370_pcie_rreg(rdev, (reg))
1653 #define WREG32_PCIE(reg, v) rv370_pcie_wreg(rdev, (reg), (v))
1654 #define RREG32_PCIE_P(reg) rdev->pciep_rreg(rdev, (reg))
1655 #define WREG32_PCIE_P(reg, v) rdev->pciep_wreg(rdev, (reg), (v))
1656 #define WREG32_P(reg, val, mask)                                \
1657         do {                                                    \
1658                 uint32_t tmp_ = RREG32(reg);                    \
1659                 tmp_ &= (mask);                                 \
1660                 tmp_ |= ((val) & ~(mask));                      \
1661                 WREG32(reg, tmp_);                              \
1662         } while (0)
1663 #define WREG32_PLL_P(reg, val, mask)                            \
1664         do {                                                    \
1665                 uint32_t tmp_ = RREG32_PLL(reg);                \
1666                 tmp_ &= (mask);                                 \
1667                 tmp_ |= ((val) & ~(mask));                      \
1668                 WREG32_PLL(reg, tmp_);                          \
1669         } while (0)
1670 #define DREG32_SYS(sqf, rdev, reg) seq_printf((sqf), #reg " : 0x%08X\n", r100_mm_rreg((rdev), (reg)))
1671 #define RREG32_IO(reg) r100_io_rreg(rdev, (reg))
1672 #define WREG32_IO(reg, v) r100_io_wreg(rdev, (reg), (v))
1673
1674 /*
1675  * Indirect registers accessor
1676  */
1677 static inline uint32_t rv370_pcie_rreg(struct radeon_device *rdev, uint32_t reg)
1678 {
1679         uint32_t r;
1680
1681         WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
1682         r = RREG32(RADEON_PCIE_DATA);
1683         return r;
1684 }
1685
1686 static inline void rv370_pcie_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
1687 {
1688         WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
1689         WREG32(RADEON_PCIE_DATA, (v));
1690 }
1691
1692 void r100_pll_errata_after_index(struct radeon_device *rdev);
1693
1694
1695 /*
1696  * ASICs helpers.
1697  */
1698 #define ASIC_IS_RN50(rdev) ((rdev->pdev->device == 0x515e) || \
1699                             (rdev->pdev->device == 0x5969))
1700 #define ASIC_IS_RV100(rdev) ((rdev->family == CHIP_RV100) || \
1701                 (rdev->family == CHIP_RV200) || \
1702                 (rdev->family == CHIP_RS100) || \
1703                 (rdev->family == CHIP_RS200) || \
1704                 (rdev->family == CHIP_RV250) || \
1705                 (rdev->family == CHIP_RV280) || \
1706                 (rdev->family == CHIP_RS300))
1707 #define ASIC_IS_R300(rdev) ((rdev->family == CHIP_R300)  ||     \
1708                 (rdev->family == CHIP_RV350) ||                 \
1709                 (rdev->family == CHIP_R350)  ||                 \
1710                 (rdev->family == CHIP_RV380) ||                 \
1711                 (rdev->family == CHIP_R420)  ||                 \
1712                 (rdev->family == CHIP_R423)  ||                 \
1713                 (rdev->family == CHIP_RV410) ||                 \
1714                 (rdev->family == CHIP_RS400) ||                 \
1715                 (rdev->family == CHIP_RS480))
1716 #define ASIC_IS_X2(rdev) ((rdev->ddev->pdev->device == 0x9441) || \
1717                 (rdev->ddev->pdev->device == 0x9443) || \
1718                 (rdev->ddev->pdev->device == 0x944B) || \
1719                 (rdev->ddev->pdev->device == 0x9506) || \
1720                 (rdev->ddev->pdev->device == 0x9509) || \
1721                 (rdev->ddev->pdev->device == 0x950F) || \
1722                 (rdev->ddev->pdev->device == 0x689C) || \
1723                 (rdev->ddev->pdev->device == 0x689D))
1724 #define ASIC_IS_AVIVO(rdev) ((rdev->family >= CHIP_RS600))
1725 #define ASIC_IS_DCE2(rdev) ((rdev->family == CHIP_RS600)  ||    \
1726                             (rdev->family == CHIP_RS690)  ||    \
1727                             (rdev->family == CHIP_RS740)  ||    \
1728                             (rdev->family >= CHIP_R600))
1729 #define ASIC_IS_DCE3(rdev) ((rdev->family >= CHIP_RV620))
1730 #define ASIC_IS_DCE32(rdev) ((rdev->family >= CHIP_RV730))
1731 #define ASIC_IS_DCE4(rdev) ((rdev->family >= CHIP_CEDAR))
1732 #define ASIC_IS_DCE41(rdev) ((rdev->family >= CHIP_PALM) && \
1733                              (rdev->flags & RADEON_IS_IGP))
1734 #define ASIC_IS_DCE5(rdev) ((rdev->family >= CHIP_BARTS))
1735 #define ASIC_IS_DCE6(rdev) ((rdev->family >= CHIP_ARUBA))
1736 #define ASIC_IS_DCE61(rdev) ((rdev->family >= CHIP_ARUBA) && \
1737                              (rdev->flags & RADEON_IS_IGP))
1738
1739 /*
1740  * BIOS helpers.
1741  */
1742 #define RBIOS8(i) (rdev->bios[i])
1743 #define RBIOS16(i) (RBIOS8(i) | (RBIOS8((i)+1) << 8))
1744 #define RBIOS32(i) ((RBIOS16(i)) | (RBIOS16((i)+2) << 16))
1745
1746 int radeon_combios_init(struct radeon_device *rdev);
1747 void radeon_combios_fini(struct radeon_device *rdev);
1748 int radeon_atombios_init(struct radeon_device *rdev);
1749 void radeon_atombios_fini(struct radeon_device *rdev);
1750
1751
1752 /*
1753  * RING helpers.
1754  */
1755 #if DRM_DEBUG_CODE == 0
1756 static inline void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
1757 {
1758         ring->ring[ring->wptr++] = v;
1759         ring->wptr &= ring->ptr_mask;
1760         ring->count_dw--;
1761         ring->ring_free_dw--;
1762 }
1763 #else
1764 /* With debugging this is just too big to inline */
1765 void radeon_ring_write(struct radeon_ring *ring, uint32_t v);
1766 #endif
1767
1768 /*
1769  * ASICs macro.
1770  */
1771 #define radeon_init(rdev) (rdev)->asic->init((rdev))
1772 #define radeon_fini(rdev) (rdev)->asic->fini((rdev))
1773 #define radeon_resume(rdev) (rdev)->asic->resume((rdev))
1774 #define radeon_suspend(rdev) (rdev)->asic->suspend((rdev))
1775 #define radeon_cs_parse(rdev, r, p) (rdev)->asic->ring[(r)].cs_parse((p))
1776 #define radeon_vga_set_state(rdev, state) (rdev)->asic->vga_set_state((rdev), (state))
1777 #define radeon_asic_reset(rdev) (rdev)->asic->asic_reset((rdev))
1778 #define radeon_gart_tlb_flush(rdev) (rdev)->asic->gart.tlb_flush((rdev))
1779 #define radeon_gart_set_page(rdev, i, p) (rdev)->asic->gart.set_page((rdev), (i), (p))
1780 #define radeon_asic_vm_init(rdev) (rdev)->asic->vm.init((rdev))
1781 #define radeon_asic_vm_fini(rdev) (rdev)->asic->vm.fini((rdev))
1782 #define radeon_asic_vm_set_page(rdev, pe, addr, count, incr, flags) ((rdev)->asic->vm.set_page((rdev), (pe), (addr), (count), (incr), (flags)))
1783 #define radeon_ring_start(rdev, r, cp) (rdev)->asic->ring[(r)].ring_start((rdev), (cp))
1784 #define radeon_ring_test(rdev, r, cp) (rdev)->asic->ring[(r)].ring_test((rdev), (cp))
1785 #define radeon_ib_test(rdev, r, cp) (rdev)->asic->ring[(r)].ib_test((rdev), (cp))
1786 #define radeon_ring_ib_execute(rdev, r, ib) (rdev)->asic->ring[(r)].ib_execute((rdev), (ib))
1787 #define radeon_ring_ib_parse(rdev, r, ib) (rdev)->asic->ring[(r)].ib_parse((rdev), (ib))
1788 #define radeon_ring_is_lockup(rdev, r, cp) (rdev)->asic->ring[(r)].is_lockup((rdev), (cp))
1789 #define radeon_ring_vm_flush(rdev, r, vm) (rdev)->asic->ring[(r)].vm_flush((rdev), (r), (vm))
1790 #define radeon_irq_set(rdev) (rdev)->asic->irq.set((rdev))
1791 #define radeon_irq_process(rdev) (rdev)->asic->irq.process((rdev))
1792 #define radeon_get_vblank_counter(rdev, crtc) (rdev)->asic->display.get_vblank_counter((rdev), (crtc))
1793 #define radeon_set_backlight_level(rdev, e, l) (rdev)->asic->display.set_backlight_level((e), (l))
1794 #define radeon_get_backlight_level(rdev, e) (rdev)->asic->display.get_backlight_level((e))
1795 #define radeon_fence_ring_emit(rdev, r, fence) (rdev)->asic->ring[(r)].emit_fence((rdev), (fence))
1796 #define radeon_semaphore_ring_emit(rdev, r, cp, semaphore, emit_wait) (rdev)->asic->ring[(r)].emit_semaphore((rdev), (cp), (semaphore), (emit_wait))
1797 #define radeon_copy_blit(rdev, s, d, np, f) (rdev)->asic->copy.blit((rdev), (s), (d), (np), (f))
1798 #define radeon_copy_dma(rdev, s, d, np, f) (rdev)->asic->copy.dma((rdev), (s), (d), (np), (f))
1799 #define radeon_copy(rdev, s, d, np, f) (rdev)->asic->copy.copy((rdev), (s), (d), (np), (f))
1800 #define radeon_copy_blit_ring_index(rdev) (rdev)->asic->copy.blit_ring_index
1801 #define radeon_copy_dma_ring_index(rdev) (rdev)->asic->copy.dma_ring_index
1802 #define radeon_copy_ring_index(rdev) (rdev)->asic->copy.copy_ring_index
1803 #define radeon_get_engine_clock(rdev) (rdev)->asic->pm.get_engine_clock((rdev))
1804 #define radeon_set_engine_clock(rdev, e) (rdev)->asic->pm.set_engine_clock((rdev), (e))
1805 #define radeon_get_memory_clock(rdev) (rdev)->asic->pm.get_memory_clock((rdev))
1806 #define radeon_set_memory_clock(rdev, e) (rdev)->asic->pm.set_memory_clock((rdev), (e))
1807 #define radeon_get_pcie_lanes(rdev) (rdev)->asic->pm.get_pcie_lanes((rdev))
1808 #define radeon_set_pcie_lanes(rdev, l) (rdev)->asic->pm.set_pcie_lanes((rdev), (l))
1809 #define radeon_set_clock_gating(rdev, e) (rdev)->asic->pm.set_clock_gating((rdev), (e))
1810 #define radeon_set_surface_reg(rdev, r, f, p, o, s) ((rdev)->asic->surface.set_reg((rdev), (r), (f), (p), (o), (s)))
1811 #define radeon_clear_surface_reg(rdev, r) ((rdev)->asic->surface.clear_reg((rdev), (r)))
1812 #define radeon_bandwidth_update(rdev) (rdev)->asic->display.bandwidth_update((rdev))
1813 #define radeon_hpd_init(rdev) (rdev)->asic->hpd.init((rdev))
1814 #define radeon_hpd_fini(rdev) (rdev)->asic->hpd.fini((rdev))
1815 #define radeon_hpd_sense(rdev, h) (rdev)->asic->hpd.sense((rdev), (h))
1816 #define radeon_hpd_set_polarity(rdev, h) (rdev)->asic->hpd.set_polarity((rdev), (h))
1817 #define radeon_gui_idle(rdev) (rdev)->asic->gui_idle((rdev))
1818 #define radeon_pm_misc(rdev) (rdev)->asic->pm.misc((rdev))
1819 #define radeon_pm_prepare(rdev) (rdev)->asic->pm.prepare((rdev))
1820 #define radeon_pm_finish(rdev) (rdev)->asic->pm.finish((rdev))
1821 #define radeon_pm_init_profile(rdev) (rdev)->asic->pm.init_profile((rdev))
1822 #define radeon_pm_get_dynpm_state(rdev) (rdev)->asic->pm.get_dynpm_state((rdev))
1823 #define radeon_pre_page_flip(rdev, crtc) (rdev)->asic->pflip.pre_page_flip((rdev), (crtc))
1824 #define radeon_page_flip(rdev, crtc, base) (rdev)->asic->pflip.page_flip((rdev), (crtc), (base))
1825 #define radeon_post_page_flip(rdev, crtc) (rdev)->asic->pflip.post_page_flip((rdev), (crtc))
1826 #define radeon_wait_for_vblank(rdev, crtc) (rdev)->asic->display.wait_for_vblank((rdev), (crtc))
1827 #define radeon_mc_wait_for_idle(rdev) (rdev)->asic->mc_wait_for_idle((rdev))
1828
1829 /* Common functions */
1830 /* AGP */
1831 extern int radeon_gpu_reset(struct radeon_device *rdev);
1832 extern void radeon_agp_disable(struct radeon_device *rdev);
1833 extern int radeon_modeset_init(struct radeon_device *rdev);
1834 extern void radeon_modeset_fini(struct radeon_device *rdev);
1835 extern bool radeon_card_posted(struct radeon_device *rdev);
1836 extern void radeon_update_bandwidth_info(struct radeon_device *rdev);
1837 extern void radeon_update_display_priority(struct radeon_device *rdev);
1838 extern bool radeon_boot_test_post_card(struct radeon_device *rdev);
1839 extern void radeon_scratch_init(struct radeon_device *rdev);
1840 extern void radeon_wb_fini(struct radeon_device *rdev);
1841 extern int radeon_wb_init(struct radeon_device *rdev);
1842 extern void radeon_wb_disable(struct radeon_device *rdev);
1843 extern void radeon_surface_init(struct radeon_device *rdev);
1844 extern int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data);
1845 extern void radeon_legacy_set_clock_gating(struct radeon_device *rdev, int enable);
1846 extern void radeon_atom_set_clock_gating(struct radeon_device *rdev, int enable);
1847 extern void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain);
1848 extern bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo);
1849 extern void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base);
1850 extern void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc);
1851 extern int radeon_resume_kms(struct drm_device *dev);
1852 extern int radeon_suspend_kms(struct drm_device *dev, pm_message_t state);
1853 extern void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size);
1854
1855 /*
1856  * vm
1857  */
1858 int radeon_vm_manager_init(struct radeon_device *rdev);
1859 void radeon_vm_manager_fini(struct radeon_device *rdev);
1860 void radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm);
1861 void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm);
1862 int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm);
1863 void radeon_vm_add_to_lru(struct radeon_device *rdev, struct radeon_vm *vm);
1864 struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
1865                                        struct radeon_vm *vm, int ring);
1866 void radeon_vm_fence(struct radeon_device *rdev,
1867                      struct radeon_vm *vm,
1868                      struct radeon_fence *fence);
1869 uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr);
1870 int radeon_vm_bo_update_pte(struct radeon_device *rdev,
1871                             struct radeon_vm *vm,
1872                             struct radeon_bo *bo,
1873                             struct ttm_mem_reg *mem);
1874 void radeon_vm_bo_invalidate(struct radeon_device *rdev,
1875                              struct radeon_bo *bo);
1876 struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
1877                                        struct radeon_bo *bo);
1878 struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
1879                                       struct radeon_vm *vm,
1880                                       struct radeon_bo *bo);
1881 int radeon_vm_bo_set_addr(struct radeon_device *rdev,
1882                           struct radeon_bo_va *bo_va,
1883                           uint64_t offset,
1884                           uint32_t flags);
1885 int radeon_vm_bo_rmv(struct radeon_device *rdev,
1886                      struct radeon_bo_va *bo_va);
1887
1888 /* audio */
1889 void r600_audio_update_hdmi(struct work_struct *work);
1890
1891 /*
1892  * R600 vram scratch functions
1893  */
1894 int r600_vram_scratch_init(struct radeon_device *rdev);
1895 void r600_vram_scratch_fini(struct radeon_device *rdev);
1896
1897 /*
1898  * r600 cs checking helper
1899  */
1900 unsigned r600_mip_minify(unsigned size, unsigned level);
1901 bool r600_fmt_is_valid_color(u32 format);
1902 bool r600_fmt_is_valid_texture(u32 format, enum radeon_family family);
1903 int r600_fmt_get_blocksize(u32 format);
1904 int r600_fmt_get_nblocksx(u32 format, u32 w);
1905 int r600_fmt_get_nblocksy(u32 format, u32 h);
1906
1907 /*
1908  * r600 functions used by radeon_encoder.c
1909  */
1910 struct radeon_hdmi_acr {
1911         u32 clock;
1912
1913         int n_32khz;
1914         int cts_32khz;
1915
1916         int n_44_1khz;
1917         int cts_44_1khz;
1918
1919         int n_48khz;
1920         int cts_48khz;
1921
1922 };
1923
1924 extern struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock);
1925
1926 extern void r600_hdmi_enable(struct drm_encoder *encoder);
1927 extern void r600_hdmi_disable(struct drm_encoder *encoder);
1928 extern void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode);
1929 extern u32 r6xx_remap_render_backend(struct radeon_device *rdev,
1930                                      u32 tiling_pipe_num,
1931                                      u32 max_rb_num,
1932                                      u32 total_max_rb_num,
1933                                      u32 enabled_rb_mask);
1934
1935 /*
1936  * evergreen functions used by radeon_encoder.c
1937  */
1938
1939 extern void evergreen_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode);
1940
1941 extern int ni_init_microcode(struct radeon_device *rdev);
1942 extern int ni_mc_load_microcode(struct radeon_device *rdev);
1943
1944 /* radeon_acpi.c */
1945 #if defined(CONFIG_ACPI)
1946 extern int radeon_acpi_init(struct radeon_device *rdev);
1947 extern void radeon_acpi_fini(struct radeon_device *rdev);
1948 #else
1949 static inline int radeon_acpi_init(struct radeon_device *rdev) { return 0; }
1950 static inline void radeon_acpi_fini(struct radeon_device *rdev) { }
1951 #endif
1952
1953 #include "radeon_object.h"
1954
1955 #endif