drm/radeon: add indirect accessors for UVD CTX registers
[linux-2.6-block.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 extern int radeon_fastfb;
99 extern int radeon_dpm;
100
101 /*
102  * Copy from radeon_drv.h so we don't have to include both and have conflicting
103  * symbol;
104  */
105 #define RADEON_MAX_USEC_TIMEOUT                 100000  /* 100 ms */
106 #define RADEON_FENCE_JIFFIES_TIMEOUT            (HZ / 2)
107 /* RADEON_IB_POOL_SIZE must be a power of 2 */
108 #define RADEON_IB_POOL_SIZE                     16
109 #define RADEON_DEBUGFS_MAX_COMPONENTS           32
110 #define RADEONFB_CONN_LIMIT                     4
111 #define RADEON_BIOS_NUM_SCRATCH                 8
112
113 /* max number of rings */
114 #define RADEON_NUM_RINGS                        6
115
116 /* fence seq are set to this number when signaled */
117 #define RADEON_FENCE_SIGNALED_SEQ               0LL
118
119 /* internal ring indices */
120 /* r1xx+ has gfx CP ring */
121 #define RADEON_RING_TYPE_GFX_INDEX      0
122
123 /* cayman has 2 compute CP rings */
124 #define CAYMAN_RING_TYPE_CP1_INDEX      1
125 #define CAYMAN_RING_TYPE_CP2_INDEX      2
126
127 /* R600+ has an async dma ring */
128 #define R600_RING_TYPE_DMA_INDEX                3
129 /* cayman add a second async dma ring */
130 #define CAYMAN_RING_TYPE_DMA1_INDEX             4
131
132 /* R600+ */
133 #define R600_RING_TYPE_UVD_INDEX        5
134
135 /* hardcode those limit for now */
136 #define RADEON_VA_IB_OFFSET                     (1 << 20)
137 #define RADEON_VA_RESERVED_SIZE                 (8 << 20)
138 #define RADEON_IB_VM_MAX_SIZE                   (64 << 10)
139
140 /* reset flags */
141 #define RADEON_RESET_GFX                        (1 << 0)
142 #define RADEON_RESET_COMPUTE                    (1 << 1)
143 #define RADEON_RESET_DMA                        (1 << 2)
144 #define RADEON_RESET_CP                         (1 << 3)
145 #define RADEON_RESET_GRBM                       (1 << 4)
146 #define RADEON_RESET_DMA1                       (1 << 5)
147 #define RADEON_RESET_RLC                        (1 << 6)
148 #define RADEON_RESET_SEM                        (1 << 7)
149 #define RADEON_RESET_IH                         (1 << 8)
150 #define RADEON_RESET_VMC                        (1 << 9)
151 #define RADEON_RESET_MC                         (1 << 10)
152 #define RADEON_RESET_DISPLAY                    (1 << 11)
153
154 /* max cursor sizes (in pixels) */
155 #define CURSOR_WIDTH 64
156 #define CURSOR_HEIGHT 64
157
158 #define CIK_CURSOR_WIDTH 128
159 #define CIK_CURSOR_HEIGHT 128
160
161 /*
162  * Errata workarounds.
163  */
164 enum radeon_pll_errata {
165         CHIP_ERRATA_R300_CG             = 0x00000001,
166         CHIP_ERRATA_PLL_DUMMYREADS      = 0x00000002,
167         CHIP_ERRATA_PLL_DELAY           = 0x00000004
168 };
169
170
171 struct radeon_device;
172
173
174 /*
175  * BIOS.
176  */
177 bool radeon_get_bios(struct radeon_device *rdev);
178
179 /*
180  * Dummy page
181  */
182 struct radeon_dummy_page {
183         struct page     *page;
184         dma_addr_t      addr;
185 };
186 int radeon_dummy_page_init(struct radeon_device *rdev);
187 void radeon_dummy_page_fini(struct radeon_device *rdev);
188
189
190 /*
191  * Clocks
192  */
193 struct radeon_clock {
194         struct radeon_pll p1pll;
195         struct radeon_pll p2pll;
196         struct radeon_pll dcpll;
197         struct radeon_pll spll;
198         struct radeon_pll mpll;
199         /* 10 Khz units */
200         uint32_t default_mclk;
201         uint32_t default_sclk;
202         uint32_t default_dispclk;
203         uint32_t dp_extclk;
204         uint32_t max_pixel_clock;
205 };
206
207 /*
208  * Power management
209  */
210 int radeon_pm_init(struct radeon_device *rdev);
211 void radeon_pm_fini(struct radeon_device *rdev);
212 void radeon_pm_compute_clocks(struct radeon_device *rdev);
213 void radeon_pm_suspend(struct radeon_device *rdev);
214 void radeon_pm_resume(struct radeon_device *rdev);
215 void radeon_combios_get_power_modes(struct radeon_device *rdev);
216 void radeon_atombios_get_power_modes(struct radeon_device *rdev);
217 int radeon_atom_get_clock_dividers(struct radeon_device *rdev,
218                                    u8 clock_type,
219                                    u32 clock,
220                                    bool strobe_mode,
221                                    struct atom_clock_dividers *dividers);
222 int radeon_atom_get_memory_pll_dividers(struct radeon_device *rdev,
223                                         u32 clock,
224                                         bool strobe_mode,
225                                         struct atom_mpll_param *mpll_param);
226 void radeon_atom_set_voltage(struct radeon_device *rdev, u16 voltage_level, u8 voltage_type);
227 int radeon_atom_get_voltage_gpio_settings(struct radeon_device *rdev,
228                                           u16 voltage_level, u8 voltage_type,
229                                           u32 *gpio_value, u32 *gpio_mask);
230 void radeon_atom_set_engine_dram_timings(struct radeon_device *rdev,
231                                          u32 eng_clock, u32 mem_clock);
232 int radeon_atom_get_voltage_step(struct radeon_device *rdev,
233                                  u8 voltage_type, u16 *voltage_step);
234 int radeon_atom_get_max_vddc(struct radeon_device *rdev, u8 voltage_type,
235                              u16 voltage_id, u16 *voltage);
236 int radeon_atom_get_leakage_vddc_based_on_leakage_idx(struct radeon_device *rdev,
237                                                       u16 *voltage,
238                                                       u16 leakage_idx);
239 int radeon_atom_round_to_true_voltage(struct radeon_device *rdev,
240                                       u8 voltage_type,
241                                       u16 nominal_voltage,
242                                       u16 *true_voltage);
243 int radeon_atom_get_min_voltage(struct radeon_device *rdev,
244                                 u8 voltage_type, u16 *min_voltage);
245 int radeon_atom_get_max_voltage(struct radeon_device *rdev,
246                                 u8 voltage_type, u16 *max_voltage);
247 int radeon_atom_get_voltage_table(struct radeon_device *rdev,
248                                   u8 voltage_type, u8 voltage_mode,
249                                   struct atom_voltage_table *voltage_table);
250 bool radeon_atom_is_voltage_gpio(struct radeon_device *rdev,
251                                  u8 voltage_type, u8 voltage_mode);
252 void radeon_atom_update_memory_dll(struct radeon_device *rdev,
253                                    u32 mem_clock);
254 void radeon_atom_set_ac_timing(struct radeon_device *rdev,
255                                u32 mem_clock);
256 int radeon_atom_init_mc_reg_table(struct radeon_device *rdev,
257                                   u8 module_index,
258                                   struct atom_mc_reg_table *reg_table);
259 int radeon_atom_get_memory_info(struct radeon_device *rdev,
260                                 u8 module_index, struct atom_memory_info *mem_info);
261 int radeon_atom_get_mclk_range_table(struct radeon_device *rdev,
262                                      bool gddr5, u8 module_index,
263                                      struct atom_memory_clock_range_table *mclk_range_table);
264 int radeon_atom_get_max_vddc(struct radeon_device *rdev, u8 voltage_type,
265                              u16 voltage_id, u16 *voltage);
266 void rs690_pm_info(struct radeon_device *rdev);
267 extern void evergreen_tiling_fields(unsigned tiling_flags, unsigned *bankw,
268                                     unsigned *bankh, unsigned *mtaspect,
269                                     unsigned *tile_split);
270
271 /*
272  * Fences.
273  */
274 struct radeon_fence_driver {
275         uint32_t                        scratch_reg;
276         uint64_t                        gpu_addr;
277         volatile uint32_t               *cpu_addr;
278         /* sync_seq is protected by ring emission lock */
279         uint64_t                        sync_seq[RADEON_NUM_RINGS];
280         atomic64_t                      last_seq;
281         unsigned long                   last_activity;
282         bool                            initialized;
283 };
284
285 struct radeon_fence {
286         struct radeon_device            *rdev;
287         struct kref                     kref;
288         /* protected by radeon_fence.lock */
289         uint64_t                        seq;
290         /* RB, DMA, etc. */
291         unsigned                        ring;
292 };
293
294 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring);
295 int radeon_fence_driver_init(struct radeon_device *rdev);
296 void radeon_fence_driver_fini(struct radeon_device *rdev);
297 void radeon_fence_driver_force_completion(struct radeon_device *rdev);
298 int radeon_fence_emit(struct radeon_device *rdev, struct radeon_fence **fence, int ring);
299 void radeon_fence_process(struct radeon_device *rdev, int ring);
300 bool radeon_fence_signaled(struct radeon_fence *fence);
301 int radeon_fence_wait(struct radeon_fence *fence, bool interruptible);
302 int radeon_fence_wait_next_locked(struct radeon_device *rdev, int ring);
303 int radeon_fence_wait_empty_locked(struct radeon_device *rdev, int ring);
304 int radeon_fence_wait_any(struct radeon_device *rdev,
305                           struct radeon_fence **fences,
306                           bool intr);
307 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence);
308 void radeon_fence_unref(struct radeon_fence **fence);
309 unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring);
310 bool radeon_fence_need_sync(struct radeon_fence *fence, int ring);
311 void radeon_fence_note_sync(struct radeon_fence *fence, int ring);
312 static inline struct radeon_fence *radeon_fence_later(struct radeon_fence *a,
313                                                       struct radeon_fence *b)
314 {
315         if (!a) {
316                 return b;
317         }
318
319         if (!b) {
320                 return a;
321         }
322
323         BUG_ON(a->ring != b->ring);
324
325         if (a->seq > b->seq) {
326                 return a;
327         } else {
328                 return b;
329         }
330 }
331
332 static inline bool radeon_fence_is_earlier(struct radeon_fence *a,
333                                            struct radeon_fence *b)
334 {
335         if (!a) {
336                 return false;
337         }
338
339         if (!b) {
340                 return true;
341         }
342
343         BUG_ON(a->ring != b->ring);
344
345         return a->seq < b->seq;
346 }
347
348 /*
349  * Tiling registers
350  */
351 struct radeon_surface_reg {
352         struct radeon_bo *bo;
353 };
354
355 #define RADEON_GEM_MAX_SURFACES 8
356
357 /*
358  * TTM.
359  */
360 struct radeon_mman {
361         struct ttm_bo_global_ref        bo_global_ref;
362         struct drm_global_reference     mem_global_ref;
363         struct ttm_bo_device            bdev;
364         bool                            mem_global_referenced;
365         bool                            initialized;
366 };
367
368 /* bo virtual address in a specific vm */
369 struct radeon_bo_va {
370         /* protected by bo being reserved */
371         struct list_head                bo_list;
372         uint64_t                        soffset;
373         uint64_t                        eoffset;
374         uint32_t                        flags;
375         bool                            valid;
376         unsigned                        ref_count;
377
378         /* protected by vm mutex */
379         struct list_head                vm_list;
380
381         /* constant after initialization */
382         struct radeon_vm                *vm;
383         struct radeon_bo                *bo;
384 };
385
386 struct radeon_bo {
387         /* Protected by gem.mutex */
388         struct list_head                list;
389         /* Protected by tbo.reserved */
390         u32                             placements[3];
391         struct ttm_placement            placement;
392         struct ttm_buffer_object        tbo;
393         struct ttm_bo_kmap_obj          kmap;
394         unsigned                        pin_count;
395         void                            *kptr;
396         u32                             tiling_flags;
397         u32                             pitch;
398         int                             surface_reg;
399         /* list of all virtual address to which this bo
400          * is associated to
401          */
402         struct list_head                va;
403         /* Constant after initialization */
404         struct radeon_device            *rdev;
405         struct drm_gem_object           gem_base;
406
407         struct ttm_bo_kmap_obj          dma_buf_vmap;
408         pid_t                           pid;
409 };
410 #define gem_to_radeon_bo(gobj) container_of((gobj), struct radeon_bo, gem_base)
411
412 struct radeon_bo_list {
413         struct ttm_validate_buffer tv;
414         struct radeon_bo        *bo;
415         uint64_t                gpu_offset;
416         bool                    written;
417         unsigned                domain;
418         unsigned                alt_domain;
419         u32                     tiling_flags;
420 };
421
422 int radeon_gem_debugfs_init(struct radeon_device *rdev);
423
424 /* sub-allocation manager, it has to be protected by another lock.
425  * By conception this is an helper for other part of the driver
426  * like the indirect buffer or semaphore, which both have their
427  * locking.
428  *
429  * Principe is simple, we keep a list of sub allocation in offset
430  * order (first entry has offset == 0, last entry has the highest
431  * offset).
432  *
433  * When allocating new object we first check if there is room at
434  * the end total_size - (last_object_offset + last_object_size) >=
435  * alloc_size. If so we allocate new object there.
436  *
437  * When there is not enough room at the end, we start waiting for
438  * each sub object until we reach object_offset+object_size >=
439  * alloc_size, this object then become the sub object we return.
440  *
441  * Alignment can't be bigger than page size.
442  *
443  * Hole are not considered for allocation to keep things simple.
444  * Assumption is that there won't be hole (all object on same
445  * alignment).
446  */
447 struct radeon_sa_manager {
448         wait_queue_head_t       wq;
449         struct radeon_bo        *bo;
450         struct list_head        *hole;
451         struct list_head        flist[RADEON_NUM_RINGS];
452         struct list_head        olist;
453         unsigned                size;
454         uint64_t                gpu_addr;
455         void                    *cpu_ptr;
456         uint32_t                domain;
457 };
458
459 struct radeon_sa_bo;
460
461 /* sub-allocation buffer */
462 struct radeon_sa_bo {
463         struct list_head                olist;
464         struct list_head                flist;
465         struct radeon_sa_manager        *manager;
466         unsigned                        soffset;
467         unsigned                        eoffset;
468         struct radeon_fence             *fence;
469 };
470
471 /*
472  * GEM objects.
473  */
474 struct radeon_gem {
475         struct mutex            mutex;
476         struct list_head        objects;
477 };
478
479 int radeon_gem_init(struct radeon_device *rdev);
480 void radeon_gem_fini(struct radeon_device *rdev);
481 int radeon_gem_object_create(struct radeon_device *rdev, int size,
482                                 int alignment, int initial_domain,
483                                 bool discardable, bool kernel,
484                                 struct drm_gem_object **obj);
485
486 int radeon_mode_dumb_create(struct drm_file *file_priv,
487                             struct drm_device *dev,
488                             struct drm_mode_create_dumb *args);
489 int radeon_mode_dumb_mmap(struct drm_file *filp,
490                           struct drm_device *dev,
491                           uint32_t handle, uint64_t *offset_p);
492 int radeon_mode_dumb_destroy(struct drm_file *file_priv,
493                              struct drm_device *dev,
494                              uint32_t handle);
495
496 /*
497  * Semaphores.
498  */
499 /* everything here is constant */
500 struct radeon_semaphore {
501         struct radeon_sa_bo             *sa_bo;
502         signed                          waiters;
503         uint64_t                        gpu_addr;
504 };
505
506 int radeon_semaphore_create(struct radeon_device *rdev,
507                             struct radeon_semaphore **semaphore);
508 void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
509                                   struct radeon_semaphore *semaphore);
510 void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
511                                 struct radeon_semaphore *semaphore);
512 int radeon_semaphore_sync_rings(struct radeon_device *rdev,
513                                 struct radeon_semaphore *semaphore,
514                                 int signaler, int waiter);
515 void radeon_semaphore_free(struct radeon_device *rdev,
516                            struct radeon_semaphore **semaphore,
517                            struct radeon_fence *fence);
518
519 /*
520  * GART structures, functions & helpers
521  */
522 struct radeon_mc;
523
524 #define RADEON_GPU_PAGE_SIZE 4096
525 #define RADEON_GPU_PAGE_MASK (RADEON_GPU_PAGE_SIZE - 1)
526 #define RADEON_GPU_PAGE_SHIFT 12
527 #define RADEON_GPU_PAGE_ALIGN(a) (((a) + RADEON_GPU_PAGE_MASK) & ~RADEON_GPU_PAGE_MASK)
528
529 struct radeon_gart {
530         dma_addr_t                      table_addr;
531         struct radeon_bo                *robj;
532         void                            *ptr;
533         unsigned                        num_gpu_pages;
534         unsigned                        num_cpu_pages;
535         unsigned                        table_size;
536         struct page                     **pages;
537         dma_addr_t                      *pages_addr;
538         bool                            ready;
539 };
540
541 int radeon_gart_table_ram_alloc(struct radeon_device *rdev);
542 void radeon_gart_table_ram_free(struct radeon_device *rdev);
543 int radeon_gart_table_vram_alloc(struct radeon_device *rdev);
544 void radeon_gart_table_vram_free(struct radeon_device *rdev);
545 int radeon_gart_table_vram_pin(struct radeon_device *rdev);
546 void radeon_gart_table_vram_unpin(struct radeon_device *rdev);
547 int radeon_gart_init(struct radeon_device *rdev);
548 void radeon_gart_fini(struct radeon_device *rdev);
549 void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
550                         int pages);
551 int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
552                      int pages, struct page **pagelist,
553                      dma_addr_t *dma_addr);
554 void radeon_gart_restore(struct radeon_device *rdev);
555
556
557 /*
558  * GPU MC structures, functions & helpers
559  */
560 struct radeon_mc {
561         resource_size_t         aper_size;
562         resource_size_t         aper_base;
563         resource_size_t         agp_base;
564         /* for some chips with <= 32MB we need to lie
565          * about vram size near mc fb location */
566         u64                     mc_vram_size;
567         u64                     visible_vram_size;
568         u64                     gtt_size;
569         u64                     gtt_start;
570         u64                     gtt_end;
571         u64                     vram_start;
572         u64                     vram_end;
573         unsigned                vram_width;
574         u64                     real_vram_size;
575         int                     vram_mtrr;
576         bool                    vram_is_ddr;
577         bool                    igp_sideport_enabled;
578         u64                     gtt_base_align;
579         u64                     mc_mask;
580 };
581
582 bool radeon_combios_sideport_present(struct radeon_device *rdev);
583 bool radeon_atombios_sideport_present(struct radeon_device *rdev);
584
585 /*
586  * GPU scratch registers structures, functions & helpers
587  */
588 struct radeon_scratch {
589         unsigned                num_reg;
590         uint32_t                reg_base;
591         bool                    free[32];
592         uint32_t                reg[32];
593 };
594
595 int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg);
596 void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg);
597
598 /*
599  * GPU doorbell structures, functions & helpers
600  */
601 struct radeon_doorbell {
602         u32                     num_pages;
603         bool                    free[1024];
604         /* doorbell mmio */
605         resource_size_t                 base;
606         resource_size_t                 size;
607         void __iomem                    *ptr;
608 };
609
610 int radeon_doorbell_get(struct radeon_device *rdev, u32 *page);
611 void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell);
612
613 /*
614  * IRQS.
615  */
616
617 struct radeon_unpin_work {
618         struct work_struct work;
619         struct radeon_device *rdev;
620         int crtc_id;
621         struct radeon_fence *fence;
622         struct drm_pending_vblank_event *event;
623         struct radeon_bo *old_rbo;
624         u64 new_crtc_base;
625 };
626
627 struct r500_irq_stat_regs {
628         u32 disp_int;
629         u32 hdmi0_status;
630 };
631
632 struct r600_irq_stat_regs {
633         u32 disp_int;
634         u32 disp_int_cont;
635         u32 disp_int_cont2;
636         u32 d1grph_int;
637         u32 d2grph_int;
638         u32 hdmi0_status;
639         u32 hdmi1_status;
640 };
641
642 struct evergreen_irq_stat_regs {
643         u32 disp_int;
644         u32 disp_int_cont;
645         u32 disp_int_cont2;
646         u32 disp_int_cont3;
647         u32 disp_int_cont4;
648         u32 disp_int_cont5;
649         u32 d1grph_int;
650         u32 d2grph_int;
651         u32 d3grph_int;
652         u32 d4grph_int;
653         u32 d5grph_int;
654         u32 d6grph_int;
655         u32 afmt_status1;
656         u32 afmt_status2;
657         u32 afmt_status3;
658         u32 afmt_status4;
659         u32 afmt_status5;
660         u32 afmt_status6;
661 };
662
663 struct cik_irq_stat_regs {
664         u32 disp_int;
665         u32 disp_int_cont;
666         u32 disp_int_cont2;
667         u32 disp_int_cont3;
668         u32 disp_int_cont4;
669         u32 disp_int_cont5;
670         u32 disp_int_cont6;
671 };
672
673 union radeon_irq_stat_regs {
674         struct r500_irq_stat_regs r500;
675         struct r600_irq_stat_regs r600;
676         struct evergreen_irq_stat_regs evergreen;
677         struct cik_irq_stat_regs cik;
678 };
679
680 #define RADEON_MAX_HPD_PINS 6
681 #define RADEON_MAX_CRTCS 6
682 #define RADEON_MAX_AFMT_BLOCKS 6
683
684 struct radeon_irq {
685         bool                            installed;
686         spinlock_t                      lock;
687         atomic_t                        ring_int[RADEON_NUM_RINGS];
688         bool                            crtc_vblank_int[RADEON_MAX_CRTCS];
689         atomic_t                        pflip[RADEON_MAX_CRTCS];
690         wait_queue_head_t               vblank_queue;
691         bool                            hpd[RADEON_MAX_HPD_PINS];
692         bool                            afmt[RADEON_MAX_AFMT_BLOCKS];
693         union radeon_irq_stat_regs      stat_regs;
694         bool                            dpm_thermal;
695 };
696
697 int radeon_irq_kms_init(struct radeon_device *rdev);
698 void radeon_irq_kms_fini(struct radeon_device *rdev);
699 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring);
700 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring);
701 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc);
702 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc);
703 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block);
704 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block);
705 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask);
706 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask);
707
708 /*
709  * CP & rings.
710  */
711
712 struct radeon_ib {
713         struct radeon_sa_bo             *sa_bo;
714         uint32_t                        length_dw;
715         uint64_t                        gpu_addr;
716         uint32_t                        *ptr;
717         int                             ring;
718         struct radeon_fence             *fence;
719         struct radeon_vm                *vm;
720         bool                            is_const_ib;
721         struct radeon_fence             *sync_to[RADEON_NUM_RINGS];
722         struct radeon_semaphore         *semaphore;
723 };
724
725 struct radeon_ring {
726         struct radeon_bo        *ring_obj;
727         volatile uint32_t       *ring;
728         unsigned                rptr;
729         unsigned                rptr_offs;
730         unsigned                rptr_reg;
731         unsigned                rptr_save_reg;
732         u64                     next_rptr_gpu_addr;
733         volatile u32            *next_rptr_cpu_addr;
734         unsigned                wptr;
735         unsigned                wptr_old;
736         unsigned                wptr_reg;
737         unsigned                ring_size;
738         unsigned                ring_free_dw;
739         int                     count_dw;
740         unsigned long           last_activity;
741         unsigned                last_rptr;
742         uint64_t                gpu_addr;
743         uint32_t                align_mask;
744         uint32_t                ptr_mask;
745         bool                    ready;
746         u32                     ptr_reg_shift;
747         u32                     ptr_reg_mask;
748         u32                     nop;
749         u32                     idx;
750         u64                     last_semaphore_signal_addr;
751         u64                     last_semaphore_wait_addr;
752         /* for CIK queues */
753         u32 me;
754         u32 pipe;
755         u32 queue;
756         struct radeon_bo        *mqd_obj;
757         u32 doorbell_page_num;
758         u32 doorbell_offset;
759         unsigned                wptr_offs;
760 };
761
762 struct radeon_mec {
763         struct radeon_bo        *hpd_eop_obj;
764         u64                     hpd_eop_gpu_addr;
765         u32 num_pipe;
766         u32 num_mec;
767         u32 num_queue;
768 };
769
770 /*
771  * VM
772  */
773
774 /* maximum number of VMIDs */
775 #define RADEON_NUM_VM   16
776
777 /* defines number of bits in page table versus page directory,
778  * a page is 4KB so we have 12 bits offset, 9 bits in the page
779  * table and the remaining 19 bits are in the page directory */
780 #define RADEON_VM_BLOCK_SIZE   9
781
782 /* number of entries in page table */
783 #define RADEON_VM_PTE_COUNT (1 << RADEON_VM_BLOCK_SIZE)
784
785 struct radeon_vm {
786         struct list_head                list;
787         struct list_head                va;
788         unsigned                        id;
789
790         /* contains the page directory */
791         struct radeon_sa_bo             *page_directory;
792         uint64_t                        pd_gpu_addr;
793
794         /* array of page tables, one for each page directory entry */
795         struct radeon_sa_bo             **page_tables;
796
797         struct mutex                    mutex;
798         /* last fence for cs using this vm */
799         struct radeon_fence             *fence;
800         /* last flush or NULL if we still need to flush */
801         struct radeon_fence             *last_flush;
802 };
803
804 struct radeon_vm_manager {
805         struct mutex                    lock;
806         struct list_head                lru_vm;
807         struct radeon_fence             *active[RADEON_NUM_VM];
808         struct radeon_sa_manager        sa_manager;
809         uint32_t                        max_pfn;
810         /* number of VMIDs */
811         unsigned                        nvm;
812         /* vram base address for page table entry  */
813         u64                             vram_base_offset;
814         /* is vm enabled? */
815         bool                            enabled;
816 };
817
818 /*
819  * file private structure
820  */
821 struct radeon_fpriv {
822         struct radeon_vm                vm;
823 };
824
825 /*
826  * R6xx+ IH ring
827  */
828 struct r600_ih {
829         struct radeon_bo        *ring_obj;
830         volatile uint32_t       *ring;
831         unsigned                rptr;
832         unsigned                ring_size;
833         uint64_t                gpu_addr;
834         uint32_t                ptr_mask;
835         atomic_t                lock;
836         bool                    enabled;
837 };
838
839 struct r600_blit_cp_primitives {
840         void (*set_render_target)(struct radeon_device *rdev, int format,
841                                   int w, int h, u64 gpu_addr);
842         void (*cp_set_surface_sync)(struct radeon_device *rdev,
843                                     u32 sync_type, u32 size,
844                                     u64 mc_addr);
845         void (*set_shaders)(struct radeon_device *rdev);
846         void (*set_vtx_resource)(struct radeon_device *rdev, u64 gpu_addr);
847         void (*set_tex_resource)(struct radeon_device *rdev,
848                                  int format, int w, int h, int pitch,
849                                  u64 gpu_addr, u32 size);
850         void (*set_scissors)(struct radeon_device *rdev, int x1, int y1,
851                              int x2, int y2);
852         void (*draw_auto)(struct radeon_device *rdev);
853         void (*set_default_state)(struct radeon_device *rdev);
854 };
855
856 struct r600_blit {
857         struct radeon_bo        *shader_obj;
858         struct r600_blit_cp_primitives primitives;
859         int max_dim;
860         int ring_size_common;
861         int ring_size_per_loop;
862         u64 shader_gpu_addr;
863         u32 vs_offset, ps_offset;
864         u32 state_offset;
865         u32 state_len;
866 };
867
868 /*
869  * RLC stuff
870  */
871 #include "clearstate_defs.h"
872
873 struct radeon_rlc {
874         /* for power gating */
875         struct radeon_bo        *save_restore_obj;
876         uint64_t                save_restore_gpu_addr;
877         volatile uint32_t       *sr_ptr;
878         u32                     *reg_list;
879         u32                     reg_list_size;
880         /* for clear state */
881         struct radeon_bo        *clear_state_obj;
882         uint64_t                clear_state_gpu_addr;
883         volatile uint32_t       *cs_ptr;
884         struct cs_section_def   *cs_data;
885 };
886
887 int radeon_ib_get(struct radeon_device *rdev, int ring,
888                   struct radeon_ib *ib, struct radeon_vm *vm,
889                   unsigned size);
890 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib);
891 void radeon_ib_sync_to(struct radeon_ib *ib, struct radeon_fence *fence);
892 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
893                        struct radeon_ib *const_ib);
894 int radeon_ib_pool_init(struct radeon_device *rdev);
895 void radeon_ib_pool_fini(struct radeon_device *rdev);
896 int radeon_ib_ring_tests(struct radeon_device *rdev);
897 /* Ring access between begin & end cannot sleep */
898 bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
899                                       struct radeon_ring *ring);
900 void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *cp);
901 int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *cp, unsigned ndw);
902 int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *cp, unsigned ndw);
903 void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *cp);
904 void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *cp);
905 void radeon_ring_undo(struct radeon_ring *ring);
906 void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *cp);
907 int radeon_ring_test(struct radeon_device *rdev, struct radeon_ring *cp);
908 void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring);
909 void radeon_ring_lockup_update(struct radeon_ring *ring);
910 bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring);
911 unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
912                             uint32_t **data);
913 int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
914                         unsigned size, uint32_t *data);
915 int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *cp, unsigned ring_size,
916                      unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
917                      u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop);
918 void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *cp);
919
920
921 /* r600 async dma */
922 void r600_dma_stop(struct radeon_device *rdev);
923 int r600_dma_resume(struct radeon_device *rdev);
924 void r600_dma_fini(struct radeon_device *rdev);
925
926 void cayman_dma_stop(struct radeon_device *rdev);
927 int cayman_dma_resume(struct radeon_device *rdev);
928 void cayman_dma_fini(struct radeon_device *rdev);
929
930 /*
931  * CS.
932  */
933 struct radeon_cs_reloc {
934         struct drm_gem_object           *gobj;
935         struct radeon_bo                *robj;
936         struct radeon_bo_list           lobj;
937         uint32_t                        handle;
938         uint32_t                        flags;
939 };
940
941 struct radeon_cs_chunk {
942         uint32_t                chunk_id;
943         uint32_t                length_dw;
944         int                     kpage_idx[2];
945         uint32_t                *kpage[2];
946         uint32_t                *kdata;
947         void __user             *user_ptr;
948         int                     last_copied_page;
949         int                     last_page_index;
950 };
951
952 struct radeon_cs_parser {
953         struct device           *dev;
954         struct radeon_device    *rdev;
955         struct drm_file         *filp;
956         /* chunks */
957         unsigned                nchunks;
958         struct radeon_cs_chunk  *chunks;
959         uint64_t                *chunks_array;
960         /* IB */
961         unsigned                idx;
962         /* relocations */
963         unsigned                nrelocs;
964         struct radeon_cs_reloc  *relocs;
965         struct radeon_cs_reloc  **relocs_ptr;
966         struct list_head        validated;
967         unsigned                dma_reloc_idx;
968         /* indices of various chunks */
969         int                     chunk_ib_idx;
970         int                     chunk_relocs_idx;
971         int                     chunk_flags_idx;
972         int                     chunk_const_ib_idx;
973         struct radeon_ib        ib;
974         struct radeon_ib        const_ib;
975         void                    *track;
976         unsigned                family;
977         int                     parser_error;
978         u32                     cs_flags;
979         u32                     ring;
980         s32                     priority;
981 };
982
983 extern int radeon_cs_finish_pages(struct radeon_cs_parser *p);
984 extern u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx);
985
986 struct radeon_cs_packet {
987         unsigned        idx;
988         unsigned        type;
989         unsigned        reg;
990         unsigned        opcode;
991         int             count;
992         unsigned        one_reg_wr;
993 };
994
995 typedef int (*radeon_packet0_check_t)(struct radeon_cs_parser *p,
996                                       struct radeon_cs_packet *pkt,
997                                       unsigned idx, unsigned reg);
998 typedef int (*radeon_packet3_check_t)(struct radeon_cs_parser *p,
999                                       struct radeon_cs_packet *pkt);
1000
1001
1002 /*
1003  * AGP
1004  */
1005 int radeon_agp_init(struct radeon_device *rdev);
1006 void radeon_agp_resume(struct radeon_device *rdev);
1007 void radeon_agp_suspend(struct radeon_device *rdev);
1008 void radeon_agp_fini(struct radeon_device *rdev);
1009
1010
1011 /*
1012  * Writeback
1013  */
1014 struct radeon_wb {
1015         struct radeon_bo        *wb_obj;
1016         volatile uint32_t       *wb;
1017         uint64_t                gpu_addr;
1018         bool                    enabled;
1019         bool                    use_event;
1020 };
1021
1022 #define RADEON_WB_SCRATCH_OFFSET 0
1023 #define RADEON_WB_RING0_NEXT_RPTR 256
1024 #define RADEON_WB_CP_RPTR_OFFSET 1024
1025 #define RADEON_WB_CP1_RPTR_OFFSET 1280
1026 #define RADEON_WB_CP2_RPTR_OFFSET 1536
1027 #define R600_WB_DMA_RPTR_OFFSET   1792
1028 #define R600_WB_IH_WPTR_OFFSET   2048
1029 #define CAYMAN_WB_DMA1_RPTR_OFFSET   2304
1030 #define R600_WB_UVD_RPTR_OFFSET  2560
1031 #define R600_WB_EVENT_OFFSET     3072
1032 #define CIK_WB_CP1_WPTR_OFFSET     3328
1033 #define CIK_WB_CP2_WPTR_OFFSET     3584
1034
1035 /**
1036  * struct radeon_pm - power management datas
1037  * @max_bandwidth:      maximum bandwidth the gpu has (MByte/s)
1038  * @igp_sideport_mclk:  sideport memory clock Mhz (rs690,rs740,rs780,rs880)
1039  * @igp_system_mclk:    system clock Mhz (rs690,rs740,rs780,rs880)
1040  * @igp_ht_link_clk:    ht link clock Mhz (rs690,rs740,rs780,rs880)
1041  * @igp_ht_link_width:  ht link width in bits (rs690,rs740,rs780,rs880)
1042  * @k8_bandwidth:       k8 bandwidth the gpu has (MByte/s) (IGP)
1043  * @sideport_bandwidth: sideport bandwidth the gpu has (MByte/s) (IGP)
1044  * @ht_bandwidth:       ht bandwidth the gpu has (MByte/s) (IGP)
1045  * @core_bandwidth:     core GPU bandwidth the gpu has (MByte/s) (IGP)
1046  * @sclk:               GPU clock Mhz (core bandwidth depends of this clock)
1047  * @needed_bandwidth:   current bandwidth needs
1048  *
1049  * It keeps track of various data needed to take powermanagement decision.
1050  * Bandwidth need is used to determine minimun clock of the GPU and memory.
1051  * Equation between gpu/memory clock and available bandwidth is hw dependent
1052  * (type of memory, bus size, efficiency, ...)
1053  */
1054
1055 enum radeon_pm_method {
1056         PM_METHOD_PROFILE,
1057         PM_METHOD_DYNPM,
1058         PM_METHOD_DPM,
1059 };
1060
1061 enum radeon_dynpm_state {
1062         DYNPM_STATE_DISABLED,
1063         DYNPM_STATE_MINIMUM,
1064         DYNPM_STATE_PAUSED,
1065         DYNPM_STATE_ACTIVE,
1066         DYNPM_STATE_SUSPENDED,
1067 };
1068 enum radeon_dynpm_action {
1069         DYNPM_ACTION_NONE,
1070         DYNPM_ACTION_MINIMUM,
1071         DYNPM_ACTION_DOWNCLOCK,
1072         DYNPM_ACTION_UPCLOCK,
1073         DYNPM_ACTION_DEFAULT
1074 };
1075
1076 enum radeon_voltage_type {
1077         VOLTAGE_NONE = 0,
1078         VOLTAGE_GPIO,
1079         VOLTAGE_VDDC,
1080         VOLTAGE_SW
1081 };
1082
1083 enum radeon_pm_state_type {
1084         /* not used for dpm */
1085         POWER_STATE_TYPE_DEFAULT,
1086         POWER_STATE_TYPE_POWERSAVE,
1087         /* user selectable states */
1088         POWER_STATE_TYPE_BATTERY,
1089         POWER_STATE_TYPE_BALANCED,
1090         POWER_STATE_TYPE_PERFORMANCE,
1091         /* internal states */
1092         POWER_STATE_TYPE_INTERNAL_UVD,
1093         POWER_STATE_TYPE_INTERNAL_UVD_SD,
1094         POWER_STATE_TYPE_INTERNAL_UVD_HD,
1095         POWER_STATE_TYPE_INTERNAL_UVD_HD2,
1096         POWER_STATE_TYPE_INTERNAL_UVD_MVC,
1097         POWER_STATE_TYPE_INTERNAL_BOOT,
1098         POWER_STATE_TYPE_INTERNAL_THERMAL,
1099         POWER_STATE_TYPE_INTERNAL_ACPI,
1100         POWER_STATE_TYPE_INTERNAL_ULV,
1101 };
1102
1103 enum radeon_pm_profile_type {
1104         PM_PROFILE_DEFAULT,
1105         PM_PROFILE_AUTO,
1106         PM_PROFILE_LOW,
1107         PM_PROFILE_MID,
1108         PM_PROFILE_HIGH,
1109 };
1110
1111 #define PM_PROFILE_DEFAULT_IDX 0
1112 #define PM_PROFILE_LOW_SH_IDX  1
1113 #define PM_PROFILE_MID_SH_IDX  2
1114 #define PM_PROFILE_HIGH_SH_IDX 3
1115 #define PM_PROFILE_LOW_MH_IDX  4
1116 #define PM_PROFILE_MID_MH_IDX  5
1117 #define PM_PROFILE_HIGH_MH_IDX 6
1118 #define PM_PROFILE_MAX         7
1119
1120 struct radeon_pm_profile {
1121         int dpms_off_ps_idx;
1122         int dpms_on_ps_idx;
1123         int dpms_off_cm_idx;
1124         int dpms_on_cm_idx;
1125 };
1126
1127 enum radeon_int_thermal_type {
1128         THERMAL_TYPE_NONE,
1129         THERMAL_TYPE_EXTERNAL,
1130         THERMAL_TYPE_EXTERNAL_GPIO,
1131         THERMAL_TYPE_RV6XX,
1132         THERMAL_TYPE_RV770,
1133         THERMAL_TYPE_ADT7473_WITH_INTERNAL,
1134         THERMAL_TYPE_EVERGREEN,
1135         THERMAL_TYPE_SUMO,
1136         THERMAL_TYPE_NI,
1137         THERMAL_TYPE_SI,
1138         THERMAL_TYPE_EMC2103_WITH_INTERNAL,
1139         THERMAL_TYPE_CI,
1140 };
1141
1142 struct radeon_voltage {
1143         enum radeon_voltage_type type;
1144         /* gpio voltage */
1145         struct radeon_gpio_rec gpio;
1146         u32 delay; /* delay in usec from voltage drop to sclk change */
1147         bool active_high; /* voltage drop is active when bit is high */
1148         /* VDDC voltage */
1149         u8 vddc_id; /* index into vddc voltage table */
1150         u8 vddci_id; /* index into vddci voltage table */
1151         bool vddci_enabled;
1152         /* r6xx+ sw */
1153         u16 voltage;
1154         /* evergreen+ vddci */
1155         u16 vddci;
1156 };
1157
1158 /* clock mode flags */
1159 #define RADEON_PM_MODE_NO_DISPLAY          (1 << 0)
1160
1161 struct radeon_pm_clock_info {
1162         /* memory clock */
1163         u32 mclk;
1164         /* engine clock */
1165         u32 sclk;
1166         /* voltage info */
1167         struct radeon_voltage voltage;
1168         /* standardized clock flags */
1169         u32 flags;
1170 };
1171
1172 /* state flags */
1173 #define RADEON_PM_STATE_SINGLE_DISPLAY_ONLY (1 << 0)
1174
1175 struct radeon_power_state {
1176         enum radeon_pm_state_type type;
1177         struct radeon_pm_clock_info *clock_info;
1178         /* number of valid clock modes in this power state */
1179         int num_clock_modes;
1180         struct radeon_pm_clock_info *default_clock_mode;
1181         /* standardized state flags */
1182         u32 flags;
1183         u32 misc; /* vbios specific flags */
1184         u32 misc2; /* vbios specific flags */
1185         int pcie_lanes; /* pcie lanes */
1186 };
1187
1188 /*
1189  * Some modes are overclocked by very low value, accept them
1190  */
1191 #define RADEON_MODE_OVERCLOCK_MARGIN 500 /* 5 MHz */
1192
1193 enum radeon_dpm_auto_throttle_src {
1194         RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL,
1195         RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL
1196 };
1197
1198 enum radeon_dpm_event_src {
1199         RADEON_DPM_EVENT_SRC_ANALOG = 0,
1200         RADEON_DPM_EVENT_SRC_EXTERNAL = 1,
1201         RADEON_DPM_EVENT_SRC_DIGITAL = 2,
1202         RADEON_DPM_EVENT_SRC_ANALOG_OR_EXTERNAL = 3,
1203         RADEON_DPM_EVENT_SRC_DIGIAL_OR_EXTERNAL = 4
1204 };
1205
1206 struct radeon_ps {
1207         u32 caps; /* vbios flags */
1208         u32 class; /* vbios flags */
1209         u32 class2; /* vbios flags */
1210         /* UVD clocks */
1211         u32 vclk;
1212         u32 dclk;
1213         /* asic priv */
1214         void *ps_priv;
1215 };
1216
1217 struct radeon_dpm_thermal {
1218         /* thermal interrupt work */
1219         struct work_struct work;
1220         /* low temperature threshold */
1221         int                min_temp;
1222         /* high temperature threshold */
1223         int                max_temp;
1224         /* was interrupt low to high or high to low */
1225         bool               high_to_low;
1226 };
1227
1228 enum radeon_clk_action
1229 {
1230         RADEON_SCLK_UP = 1,
1231         RADEON_SCLK_DOWN
1232 };
1233
1234 struct radeon_blacklist_clocks
1235 {
1236         u32 sclk;
1237         u32 mclk;
1238         enum radeon_clk_action action;
1239 };
1240
1241 struct radeon_clock_and_voltage_limits {
1242         u32 sclk;
1243         u32 mclk;
1244         u32 vddc;
1245         u32 vddci;
1246 };
1247
1248 struct radeon_clock_array {
1249         u32 count;
1250         u32 *values;
1251 };
1252
1253 struct radeon_clock_voltage_dependency_entry {
1254         u32 clk;
1255         u16 v;
1256 };
1257
1258 struct radeon_clock_voltage_dependency_table {
1259         u32 count;
1260         struct radeon_clock_voltage_dependency_entry *entries;
1261 };
1262
1263 struct radeon_cac_leakage_entry {
1264         u16 vddc;
1265         u32 leakage;
1266 };
1267
1268 struct radeon_cac_leakage_table {
1269         u32 count;
1270         struct radeon_cac_leakage_entry *entries;
1271 };
1272
1273 struct radeon_dpm_dynamic_state {
1274         struct radeon_clock_voltage_dependency_table vddc_dependency_on_sclk;
1275         struct radeon_clock_voltage_dependency_table vddci_dependency_on_mclk;
1276         struct radeon_clock_voltage_dependency_table vddc_dependency_on_mclk;
1277         struct radeon_clock_array valid_sclk_values;
1278         struct radeon_clock_array valid_mclk_values;
1279         struct radeon_clock_and_voltage_limits max_clock_voltage_on_dc;
1280         struct radeon_clock_and_voltage_limits max_clock_voltage_on_ac;
1281         u32 mclk_sclk_ratio;
1282         u32 sclk_mclk_delta;
1283         u16 vddc_vddci_delta;
1284         u16 min_vddc_for_pcie_gen2;
1285         struct radeon_cac_leakage_table cac_leakage_table;
1286 };
1287
1288 struct radeon_dpm_fan {
1289         u16 t_min;
1290         u16 t_med;
1291         u16 t_high;
1292         u16 pwm_min;
1293         u16 pwm_med;
1294         u16 pwm_high;
1295         u8 t_hyst;
1296         u32 cycle_delay;
1297         u16 t_max;
1298         bool ucode_fan_control;
1299 };
1300
1301 struct radeon_dpm {
1302         struct radeon_ps        *ps;
1303         /* number of valid power states */
1304         int                     num_ps;
1305         /* current power state that is active */
1306         struct radeon_ps        *current_ps;
1307         /* requested power state */
1308         struct radeon_ps        *requested_ps;
1309         /* boot up power state */
1310         struct radeon_ps        *boot_ps;
1311         /* default uvd power state */
1312         struct radeon_ps        *uvd_ps;
1313         enum radeon_pm_state_type state;
1314         enum radeon_pm_state_type user_state;
1315         u32                     platform_caps;
1316         u32                     voltage_response_time;
1317         u32                     backbias_response_time;
1318         void                    *priv;
1319         u32                     new_active_crtcs;
1320         int                     new_active_crtc_count;
1321         u32                     current_active_crtcs;
1322         int                     current_active_crtc_count;
1323         struct radeon_dpm_dynamic_state dyn_state;
1324         struct radeon_dpm_fan fan;
1325         u32 tdp_limit;
1326         u32 near_tdp_limit;
1327         u32 sq_ramping_threshold;
1328         u32 cac_leakage;
1329         u16 tdp_od_limit;
1330         u32 tdp_adjustment;
1331         u16 load_line_slope;
1332         bool power_control;
1333         bool ac_power;
1334         /* special states active */
1335         bool                    thermal_active;
1336         bool                    uvd_active;
1337         /* thermal handling */
1338         struct radeon_dpm_thermal thermal;
1339 };
1340
1341 void radeon_dpm_enable_power_state(struct radeon_device *rdev,
1342                                     enum radeon_pm_state_type dpm_state);
1343
1344
1345 struct radeon_pm {
1346         struct mutex            mutex;
1347         /* write locked while reprogramming mclk */
1348         struct rw_semaphore     mclk_lock;
1349         u32                     active_crtcs;
1350         int                     active_crtc_count;
1351         int                     req_vblank;
1352         bool                    vblank_sync;
1353         fixed20_12              max_bandwidth;
1354         fixed20_12              igp_sideport_mclk;
1355         fixed20_12              igp_system_mclk;
1356         fixed20_12              igp_ht_link_clk;
1357         fixed20_12              igp_ht_link_width;
1358         fixed20_12              k8_bandwidth;
1359         fixed20_12              sideport_bandwidth;
1360         fixed20_12              ht_bandwidth;
1361         fixed20_12              core_bandwidth;
1362         fixed20_12              sclk;
1363         fixed20_12              mclk;
1364         fixed20_12              needed_bandwidth;
1365         struct radeon_power_state *power_state;
1366         /* number of valid power states */
1367         int                     num_power_states;
1368         int                     current_power_state_index;
1369         int                     current_clock_mode_index;
1370         int                     requested_power_state_index;
1371         int                     requested_clock_mode_index;
1372         int                     default_power_state_index;
1373         u32                     current_sclk;
1374         u32                     current_mclk;
1375         u16                     current_vddc;
1376         u16                     current_vddci;
1377         u32                     default_sclk;
1378         u32                     default_mclk;
1379         u16                     default_vddc;
1380         u16                     default_vddci;
1381         struct radeon_i2c_chan *i2c_bus;
1382         /* selected pm method */
1383         enum radeon_pm_method     pm_method;
1384         /* dynpm power management */
1385         struct delayed_work     dynpm_idle_work;
1386         enum radeon_dynpm_state dynpm_state;
1387         enum radeon_dynpm_action        dynpm_planned_action;
1388         unsigned long           dynpm_action_timeout;
1389         bool                    dynpm_can_upclock;
1390         bool                    dynpm_can_downclock;
1391         /* profile-based power management */
1392         enum radeon_pm_profile_type profile;
1393         int                     profile_index;
1394         struct radeon_pm_profile profiles[PM_PROFILE_MAX];
1395         /* internal thermal controller on rv6xx+ */
1396         enum radeon_int_thermal_type int_thermal_type;
1397         struct device           *int_hwmon_dev;
1398         /* dpm */
1399         bool                    dpm_enabled;
1400         struct radeon_dpm       dpm;
1401 };
1402
1403 int radeon_pm_get_type_index(struct radeon_device *rdev,
1404                              enum radeon_pm_state_type ps_type,
1405                              int instance);
1406 /*
1407  * UVD
1408  */
1409 #define RADEON_MAX_UVD_HANDLES  10
1410 #define RADEON_UVD_STACK_SIZE   (1024*1024)
1411 #define RADEON_UVD_HEAP_SIZE    (1024*1024)
1412
1413 struct radeon_uvd {
1414         struct radeon_bo        *vcpu_bo;
1415         void                    *cpu_addr;
1416         uint64_t                gpu_addr;
1417         atomic_t                handles[RADEON_MAX_UVD_HANDLES];
1418         struct drm_file         *filp[RADEON_MAX_UVD_HANDLES];
1419         struct delayed_work     idle_work;
1420 };
1421
1422 int radeon_uvd_init(struct radeon_device *rdev);
1423 void radeon_uvd_fini(struct radeon_device *rdev);
1424 int radeon_uvd_suspend(struct radeon_device *rdev);
1425 int radeon_uvd_resume(struct radeon_device *rdev);
1426 int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
1427                               uint32_t handle, struct radeon_fence **fence);
1428 int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
1429                                uint32_t handle, struct radeon_fence **fence);
1430 void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo);
1431 void radeon_uvd_free_handles(struct radeon_device *rdev,
1432                              struct drm_file *filp);
1433 int radeon_uvd_cs_parse(struct radeon_cs_parser *parser);
1434 void radeon_uvd_note_usage(struct radeon_device *rdev);
1435 int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
1436                                   unsigned vclk, unsigned dclk,
1437                                   unsigned vco_min, unsigned vco_max,
1438                                   unsigned fb_factor, unsigned fb_mask,
1439                                   unsigned pd_min, unsigned pd_max,
1440                                   unsigned pd_even,
1441                                   unsigned *optimal_fb_div,
1442                                   unsigned *optimal_vclk_div,
1443                                   unsigned *optimal_dclk_div);
1444 int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
1445                                 unsigned cg_upll_func_cntl);
1446
1447 struct r600_audio {
1448         int                     channels;
1449         int                     rate;
1450         int                     bits_per_sample;
1451         u8                      status_bits;
1452         u8                      category_code;
1453 };
1454
1455 /*
1456  * Benchmarking
1457  */
1458 void radeon_benchmark(struct radeon_device *rdev, int test_number);
1459
1460
1461 /*
1462  * Testing
1463  */
1464 void radeon_test_moves(struct radeon_device *rdev);
1465 void radeon_test_ring_sync(struct radeon_device *rdev,
1466                            struct radeon_ring *cpA,
1467                            struct radeon_ring *cpB);
1468 void radeon_test_syncing(struct radeon_device *rdev);
1469
1470
1471 /*
1472  * Debugfs
1473  */
1474 struct radeon_debugfs {
1475         struct drm_info_list    *files;
1476         unsigned                num_files;
1477 };
1478
1479 int radeon_debugfs_add_files(struct radeon_device *rdev,
1480                              struct drm_info_list *files,
1481                              unsigned nfiles);
1482 int radeon_debugfs_fence_init(struct radeon_device *rdev);
1483
1484
1485 /*
1486  * ASIC specific functions.
1487  */
1488 struct radeon_asic {
1489         int (*init)(struct radeon_device *rdev);
1490         void (*fini)(struct radeon_device *rdev);
1491         int (*resume)(struct radeon_device *rdev);
1492         int (*suspend)(struct radeon_device *rdev);
1493         void (*vga_set_state)(struct radeon_device *rdev, bool state);
1494         int (*asic_reset)(struct radeon_device *rdev);
1495         /* ioctl hw specific callback. Some hw might want to perform special
1496          * operation on specific ioctl. For instance on wait idle some hw
1497          * might want to perform and HDP flush through MMIO as it seems that
1498          * some R6XX/R7XX hw doesn't take HDP flush into account if programmed
1499          * through ring.
1500          */
1501         void (*ioctl_wait_idle)(struct radeon_device *rdev, struct radeon_bo *bo);
1502         /* check if 3D engine is idle */
1503         bool (*gui_idle)(struct radeon_device *rdev);
1504         /* wait for mc_idle */
1505         int (*mc_wait_for_idle)(struct radeon_device *rdev);
1506         /* get the reference clock */
1507         u32 (*get_xclk)(struct radeon_device *rdev);
1508         /* get the gpu clock counter */
1509         uint64_t (*get_gpu_clock_counter)(struct radeon_device *rdev);
1510         /* gart */
1511         struct {
1512                 void (*tlb_flush)(struct radeon_device *rdev);
1513                 int (*set_page)(struct radeon_device *rdev, int i, uint64_t addr);
1514         } gart;
1515         struct {
1516                 int (*init)(struct radeon_device *rdev);
1517                 void (*fini)(struct radeon_device *rdev);
1518
1519                 u32 pt_ring_index;
1520                 void (*set_page)(struct radeon_device *rdev,
1521                                  struct radeon_ib *ib,
1522                                  uint64_t pe,
1523                                  uint64_t addr, unsigned count,
1524                                  uint32_t incr, uint32_t flags);
1525         } vm;
1526         /* ring specific callbacks */
1527         struct {
1528                 void (*ib_execute)(struct radeon_device *rdev, struct radeon_ib *ib);
1529                 int (*ib_parse)(struct radeon_device *rdev, struct radeon_ib *ib);
1530                 void (*emit_fence)(struct radeon_device *rdev, struct radeon_fence *fence);
1531                 void (*emit_semaphore)(struct radeon_device *rdev, struct radeon_ring *cp,
1532                                        struct radeon_semaphore *semaphore, bool emit_wait);
1533                 int (*cs_parse)(struct radeon_cs_parser *p);
1534                 void (*ring_start)(struct radeon_device *rdev, struct radeon_ring *cp);
1535                 int (*ring_test)(struct radeon_device *rdev, struct radeon_ring *cp);
1536                 int (*ib_test)(struct radeon_device *rdev, struct radeon_ring *cp);
1537                 bool (*is_lockup)(struct radeon_device *rdev, struct radeon_ring *cp);
1538                 void (*vm_flush)(struct radeon_device *rdev, int ridx, struct radeon_vm *vm);
1539
1540                 u32 (*get_rptr)(struct radeon_device *rdev, struct radeon_ring *ring);
1541                 u32 (*get_wptr)(struct radeon_device *rdev, struct radeon_ring *ring);
1542                 void (*set_wptr)(struct radeon_device *rdev, struct radeon_ring *ring);
1543         } ring[RADEON_NUM_RINGS];
1544         /* irqs */
1545         struct {
1546                 int (*set)(struct radeon_device *rdev);
1547                 int (*process)(struct radeon_device *rdev);
1548         } irq;
1549         /* displays */
1550         struct {
1551                 /* display watermarks */
1552                 void (*bandwidth_update)(struct radeon_device *rdev);
1553                 /* get frame count */
1554                 u32 (*get_vblank_counter)(struct radeon_device *rdev, int crtc);
1555                 /* wait for vblank */
1556                 void (*wait_for_vblank)(struct radeon_device *rdev, int crtc);
1557                 /* set backlight level */
1558                 void (*set_backlight_level)(struct radeon_encoder *radeon_encoder, u8 level);
1559                 /* get backlight level */
1560                 u8 (*get_backlight_level)(struct radeon_encoder *radeon_encoder);
1561                 /* audio callbacks */
1562                 void (*hdmi_enable)(struct drm_encoder *encoder, bool enable);
1563                 void (*hdmi_setmode)(struct drm_encoder *encoder, struct drm_display_mode *mode);
1564         } display;
1565         /* copy functions for bo handling */
1566         struct {
1567                 int (*blit)(struct radeon_device *rdev,
1568                             uint64_t src_offset,
1569                             uint64_t dst_offset,
1570                             unsigned num_gpu_pages,
1571                             struct radeon_fence **fence);
1572                 u32 blit_ring_index;
1573                 int (*dma)(struct radeon_device *rdev,
1574                            uint64_t src_offset,
1575                            uint64_t dst_offset,
1576                            unsigned num_gpu_pages,
1577                            struct radeon_fence **fence);
1578                 u32 dma_ring_index;
1579                 /* method used for bo copy */
1580                 int (*copy)(struct radeon_device *rdev,
1581                             uint64_t src_offset,
1582                             uint64_t dst_offset,
1583                             unsigned num_gpu_pages,
1584                             struct radeon_fence **fence);
1585                 /* ring used for bo copies */
1586                 u32 copy_ring_index;
1587         } copy;
1588         /* surfaces */
1589         struct {
1590                 int (*set_reg)(struct radeon_device *rdev, int reg,
1591                                        uint32_t tiling_flags, uint32_t pitch,
1592                                        uint32_t offset, uint32_t obj_size);
1593                 void (*clear_reg)(struct radeon_device *rdev, int reg);
1594         } surface;
1595         /* hotplug detect */
1596         struct {
1597                 void (*init)(struct radeon_device *rdev);
1598                 void (*fini)(struct radeon_device *rdev);
1599                 bool (*sense)(struct radeon_device *rdev, enum radeon_hpd_id hpd);
1600                 void (*set_polarity)(struct radeon_device *rdev, enum radeon_hpd_id hpd);
1601         } hpd;
1602         /* static power management */
1603         struct {
1604                 void (*misc)(struct radeon_device *rdev);
1605                 void (*prepare)(struct radeon_device *rdev);
1606                 void (*finish)(struct radeon_device *rdev);
1607                 void (*init_profile)(struct radeon_device *rdev);
1608                 void (*get_dynpm_state)(struct radeon_device *rdev);
1609                 uint32_t (*get_engine_clock)(struct radeon_device *rdev);
1610                 void (*set_engine_clock)(struct radeon_device *rdev, uint32_t eng_clock);
1611                 uint32_t (*get_memory_clock)(struct radeon_device *rdev);
1612                 void (*set_memory_clock)(struct radeon_device *rdev, uint32_t mem_clock);
1613                 int (*get_pcie_lanes)(struct radeon_device *rdev);
1614                 void (*set_pcie_lanes)(struct radeon_device *rdev, int lanes);
1615                 void (*set_clock_gating)(struct radeon_device *rdev, int enable);
1616                 int (*set_uvd_clocks)(struct radeon_device *rdev, u32 vclk, u32 dclk);
1617                 int (*get_temperature)(struct radeon_device *rdev);
1618         } pm;
1619         /* dynamic power management */
1620         struct {
1621                 int (*init)(struct radeon_device *rdev);
1622                 void (*setup_asic)(struct radeon_device *rdev);
1623                 int (*enable)(struct radeon_device *rdev);
1624                 void (*disable)(struct radeon_device *rdev);
1625                 int (*pre_set_power_state)(struct radeon_device *rdev);
1626                 int (*set_power_state)(struct radeon_device *rdev);
1627                 void (*post_set_power_state)(struct radeon_device *rdev);
1628                 void (*display_configuration_changed)(struct radeon_device *rdev);
1629                 void (*fini)(struct radeon_device *rdev);
1630                 u32 (*get_sclk)(struct radeon_device *rdev, bool low);
1631                 u32 (*get_mclk)(struct radeon_device *rdev, bool low);
1632                 void (*print_power_state)(struct radeon_device *rdev, struct radeon_ps *ps);
1633         } dpm;
1634         /* pageflipping */
1635         struct {
1636                 void (*pre_page_flip)(struct radeon_device *rdev, int crtc);
1637                 u32 (*page_flip)(struct radeon_device *rdev, int crtc, u64 crtc_base);
1638                 void (*post_page_flip)(struct radeon_device *rdev, int crtc);
1639         } pflip;
1640 };
1641
1642 /*
1643  * Asic structures
1644  */
1645 struct r100_asic {
1646         const unsigned          *reg_safe_bm;
1647         unsigned                reg_safe_bm_size;
1648         u32                     hdp_cntl;
1649 };
1650
1651 struct r300_asic {
1652         const unsigned          *reg_safe_bm;
1653         unsigned                reg_safe_bm_size;
1654         u32                     resync_scratch;
1655         u32                     hdp_cntl;
1656 };
1657
1658 struct r600_asic {
1659         unsigned                max_pipes;
1660         unsigned                max_tile_pipes;
1661         unsigned                max_simds;
1662         unsigned                max_backends;
1663         unsigned                max_gprs;
1664         unsigned                max_threads;
1665         unsigned                max_stack_entries;
1666         unsigned                max_hw_contexts;
1667         unsigned                max_gs_threads;
1668         unsigned                sx_max_export_size;
1669         unsigned                sx_max_export_pos_size;
1670         unsigned                sx_max_export_smx_size;
1671         unsigned                sq_num_cf_insts;
1672         unsigned                tiling_nbanks;
1673         unsigned                tiling_npipes;
1674         unsigned                tiling_group_size;
1675         unsigned                tile_config;
1676         unsigned                backend_map;
1677 };
1678
1679 struct rv770_asic {
1680         unsigned                max_pipes;
1681         unsigned                max_tile_pipes;
1682         unsigned                max_simds;
1683         unsigned                max_backends;
1684         unsigned                max_gprs;
1685         unsigned                max_threads;
1686         unsigned                max_stack_entries;
1687         unsigned                max_hw_contexts;
1688         unsigned                max_gs_threads;
1689         unsigned                sx_max_export_size;
1690         unsigned                sx_max_export_pos_size;
1691         unsigned                sx_max_export_smx_size;
1692         unsigned                sq_num_cf_insts;
1693         unsigned                sx_num_of_sets;
1694         unsigned                sc_prim_fifo_size;
1695         unsigned                sc_hiz_tile_fifo_size;
1696         unsigned                sc_earlyz_tile_fifo_fize;
1697         unsigned                tiling_nbanks;
1698         unsigned                tiling_npipes;
1699         unsigned                tiling_group_size;
1700         unsigned                tile_config;
1701         unsigned                backend_map;
1702 };
1703
1704 struct evergreen_asic {
1705         unsigned num_ses;
1706         unsigned max_pipes;
1707         unsigned max_tile_pipes;
1708         unsigned max_simds;
1709         unsigned max_backends;
1710         unsigned max_gprs;
1711         unsigned max_threads;
1712         unsigned max_stack_entries;
1713         unsigned max_hw_contexts;
1714         unsigned max_gs_threads;
1715         unsigned sx_max_export_size;
1716         unsigned sx_max_export_pos_size;
1717         unsigned sx_max_export_smx_size;
1718         unsigned sq_num_cf_insts;
1719         unsigned sx_num_of_sets;
1720         unsigned sc_prim_fifo_size;
1721         unsigned sc_hiz_tile_fifo_size;
1722         unsigned sc_earlyz_tile_fifo_size;
1723         unsigned tiling_nbanks;
1724         unsigned tiling_npipes;
1725         unsigned tiling_group_size;
1726         unsigned tile_config;
1727         unsigned backend_map;
1728 };
1729
1730 struct cayman_asic {
1731         unsigned max_shader_engines;
1732         unsigned max_pipes_per_simd;
1733         unsigned max_tile_pipes;
1734         unsigned max_simds_per_se;
1735         unsigned max_backends_per_se;
1736         unsigned max_texture_channel_caches;
1737         unsigned max_gprs;
1738         unsigned max_threads;
1739         unsigned max_gs_threads;
1740         unsigned max_stack_entries;
1741         unsigned sx_num_of_sets;
1742         unsigned sx_max_export_size;
1743         unsigned sx_max_export_pos_size;
1744         unsigned sx_max_export_smx_size;
1745         unsigned max_hw_contexts;
1746         unsigned sq_num_cf_insts;
1747         unsigned sc_prim_fifo_size;
1748         unsigned sc_hiz_tile_fifo_size;
1749         unsigned sc_earlyz_tile_fifo_size;
1750
1751         unsigned num_shader_engines;
1752         unsigned num_shader_pipes_per_simd;
1753         unsigned num_tile_pipes;
1754         unsigned num_simds_per_se;
1755         unsigned num_backends_per_se;
1756         unsigned backend_disable_mask_per_asic;
1757         unsigned backend_map;
1758         unsigned num_texture_channel_caches;
1759         unsigned mem_max_burst_length_bytes;
1760         unsigned mem_row_size_in_kb;
1761         unsigned shader_engine_tile_size;
1762         unsigned num_gpus;
1763         unsigned multi_gpu_tile_size;
1764
1765         unsigned tile_config;
1766 };
1767
1768 struct si_asic {
1769         unsigned max_shader_engines;
1770         unsigned max_tile_pipes;
1771         unsigned max_cu_per_sh;
1772         unsigned max_sh_per_se;
1773         unsigned max_backends_per_se;
1774         unsigned max_texture_channel_caches;
1775         unsigned max_gprs;
1776         unsigned max_gs_threads;
1777         unsigned max_hw_contexts;
1778         unsigned sc_prim_fifo_size_frontend;
1779         unsigned sc_prim_fifo_size_backend;
1780         unsigned sc_hiz_tile_fifo_size;
1781         unsigned sc_earlyz_tile_fifo_size;
1782
1783         unsigned num_tile_pipes;
1784         unsigned num_backends_per_se;
1785         unsigned backend_disable_mask_per_asic;
1786         unsigned backend_map;
1787         unsigned num_texture_channel_caches;
1788         unsigned mem_max_burst_length_bytes;
1789         unsigned mem_row_size_in_kb;
1790         unsigned shader_engine_tile_size;
1791         unsigned num_gpus;
1792         unsigned multi_gpu_tile_size;
1793
1794         unsigned tile_config;
1795         uint32_t tile_mode_array[32];
1796 };
1797
1798 struct cik_asic {
1799         unsigned max_shader_engines;
1800         unsigned max_tile_pipes;
1801         unsigned max_cu_per_sh;
1802         unsigned max_sh_per_se;
1803         unsigned max_backends_per_se;
1804         unsigned max_texture_channel_caches;
1805         unsigned max_gprs;
1806         unsigned max_gs_threads;
1807         unsigned max_hw_contexts;
1808         unsigned sc_prim_fifo_size_frontend;
1809         unsigned sc_prim_fifo_size_backend;
1810         unsigned sc_hiz_tile_fifo_size;
1811         unsigned sc_earlyz_tile_fifo_size;
1812
1813         unsigned num_tile_pipes;
1814         unsigned num_backends_per_se;
1815         unsigned backend_disable_mask_per_asic;
1816         unsigned backend_map;
1817         unsigned num_texture_channel_caches;
1818         unsigned mem_max_burst_length_bytes;
1819         unsigned mem_row_size_in_kb;
1820         unsigned shader_engine_tile_size;
1821         unsigned num_gpus;
1822         unsigned multi_gpu_tile_size;
1823
1824         unsigned tile_config;
1825         uint32_t tile_mode_array[32];
1826 };
1827
1828 union radeon_asic_config {
1829         struct r300_asic        r300;
1830         struct r100_asic        r100;
1831         struct r600_asic        r600;
1832         struct rv770_asic       rv770;
1833         struct evergreen_asic   evergreen;
1834         struct cayman_asic      cayman;
1835         struct si_asic          si;
1836         struct cik_asic         cik;
1837 };
1838
1839 /*
1840  * asic initizalization from radeon_asic.c
1841  */
1842 void radeon_agp_disable(struct radeon_device *rdev);
1843 int radeon_asic_init(struct radeon_device *rdev);
1844
1845
1846 /*
1847  * IOCTL.
1848  */
1849 int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
1850                           struct drm_file *filp);
1851 int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
1852                             struct drm_file *filp);
1853 int radeon_gem_pin_ioctl(struct drm_device *dev, void *data,
1854                          struct drm_file *file_priv);
1855 int radeon_gem_unpin_ioctl(struct drm_device *dev, void *data,
1856                            struct drm_file *file_priv);
1857 int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1858                             struct drm_file *file_priv);
1859 int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
1860                            struct drm_file *file_priv);
1861 int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1862                                 struct drm_file *filp);
1863 int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
1864                           struct drm_file *filp);
1865 int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
1866                           struct drm_file *filp);
1867 int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
1868                               struct drm_file *filp);
1869 int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
1870                           struct drm_file *filp);
1871 int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
1872 int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
1873                                 struct drm_file *filp);
1874 int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
1875                                 struct drm_file *filp);
1876
1877 /* VRAM scratch page for HDP bug, default vram page */
1878 struct r600_vram_scratch {
1879         struct radeon_bo                *robj;
1880         volatile uint32_t               *ptr;
1881         u64                             gpu_addr;
1882 };
1883
1884 /*
1885  * ACPI
1886  */
1887 struct radeon_atif_notification_cfg {
1888         bool enabled;
1889         int command_code;
1890 };
1891
1892 struct radeon_atif_notifications {
1893         bool display_switch;
1894         bool expansion_mode_change;
1895         bool thermal_state;
1896         bool forced_power_state;
1897         bool system_power_state;
1898         bool display_conf_change;
1899         bool px_gfx_switch;
1900         bool brightness_change;
1901         bool dgpu_display_event;
1902 };
1903
1904 struct radeon_atif_functions {
1905         bool system_params;
1906         bool sbios_requests;
1907         bool select_active_disp;
1908         bool lid_state;
1909         bool get_tv_standard;
1910         bool set_tv_standard;
1911         bool get_panel_expansion_mode;
1912         bool set_panel_expansion_mode;
1913         bool temperature_change;
1914         bool graphics_device_types;
1915 };
1916
1917 struct radeon_atif {
1918         struct radeon_atif_notifications notifications;
1919         struct radeon_atif_functions functions;
1920         struct radeon_atif_notification_cfg notification_cfg;
1921         struct radeon_encoder *encoder_for_bl;
1922 };
1923
1924 struct radeon_atcs_functions {
1925         bool get_ext_state;
1926         bool pcie_perf_req;
1927         bool pcie_dev_rdy;
1928         bool pcie_bus_width;
1929 };
1930
1931 struct radeon_atcs {
1932         struct radeon_atcs_functions functions;
1933 };
1934
1935 /*
1936  * Core structure, functions and helpers.
1937  */
1938 typedef uint32_t (*radeon_rreg_t)(struct radeon_device*, uint32_t);
1939 typedef void (*radeon_wreg_t)(struct radeon_device*, uint32_t, uint32_t);
1940
1941 struct radeon_device {
1942         struct device                   *dev;
1943         struct drm_device               *ddev;
1944         struct pci_dev                  *pdev;
1945         struct rw_semaphore             exclusive_lock;
1946         /* ASIC */
1947         union radeon_asic_config        config;
1948         enum radeon_family              family;
1949         unsigned long                   flags;
1950         int                             usec_timeout;
1951         enum radeon_pll_errata          pll_errata;
1952         int                             num_gb_pipes;
1953         int                             num_z_pipes;
1954         int                             disp_priority;
1955         /* BIOS */
1956         uint8_t                         *bios;
1957         bool                            is_atom_bios;
1958         uint16_t                        bios_header_start;
1959         struct radeon_bo                *stollen_vga_memory;
1960         /* Register mmio */
1961         resource_size_t                 rmmio_base;
1962         resource_size_t                 rmmio_size;
1963         /* protects concurrent MM_INDEX/DATA based register access */
1964         spinlock_t mmio_idx_lock;
1965         void __iomem                    *rmmio;
1966         radeon_rreg_t                   mc_rreg;
1967         radeon_wreg_t                   mc_wreg;
1968         radeon_rreg_t                   pll_rreg;
1969         radeon_wreg_t                   pll_wreg;
1970         uint32_t                        pcie_reg_mask;
1971         radeon_rreg_t                   pciep_rreg;
1972         radeon_wreg_t                   pciep_wreg;
1973         /* io port */
1974         void __iomem                    *rio_mem;
1975         resource_size_t                 rio_mem_size;
1976         struct radeon_clock             clock;
1977         struct radeon_mc                mc;
1978         struct radeon_gart              gart;
1979         struct radeon_mode_info         mode_info;
1980         struct radeon_scratch           scratch;
1981         struct radeon_doorbell          doorbell;
1982         struct radeon_mman              mman;
1983         struct radeon_fence_driver      fence_drv[RADEON_NUM_RINGS];
1984         wait_queue_head_t               fence_queue;
1985         struct mutex                    ring_lock;
1986         struct radeon_ring              ring[RADEON_NUM_RINGS];
1987         bool                            ib_pool_ready;
1988         struct radeon_sa_manager        ring_tmp_bo;
1989         struct radeon_irq               irq;
1990         struct radeon_asic              *asic;
1991         struct radeon_gem               gem;
1992         struct radeon_pm                pm;
1993         struct radeon_uvd               uvd;
1994         uint32_t                        bios_scratch[RADEON_BIOS_NUM_SCRATCH];
1995         struct radeon_wb                wb;
1996         struct radeon_dummy_page        dummy_page;
1997         bool                            shutdown;
1998         bool                            suspend;
1999         bool                            need_dma32;
2000         bool                            accel_working;
2001         bool                            fastfb_working; /* IGP feature*/
2002         struct radeon_surface_reg surface_regs[RADEON_GEM_MAX_SURFACES];
2003         const struct firmware *me_fw;   /* all family ME firmware */
2004         const struct firmware *pfp_fw;  /* r6/700 PFP firmware */
2005         const struct firmware *rlc_fw;  /* r6/700 RLC firmware */
2006         const struct firmware *mc_fw;   /* NI MC firmware */
2007         const struct firmware *ce_fw;   /* SI CE firmware */
2008         const struct firmware *uvd_fw;  /* UVD firmware */
2009         const struct firmware *mec_fw;  /* CIK MEC firmware */
2010         const struct firmware *sdma_fw; /* CIK SDMA firmware */
2011         const struct firmware *smc_fw;  /* SMC firmware */
2012         struct r600_blit r600_blit;
2013         struct r600_vram_scratch vram_scratch;
2014         int msi_enabled; /* msi enabled */
2015         struct r600_ih ih; /* r6/700 interrupt ring */
2016         struct radeon_rlc rlc;
2017         struct radeon_mec mec;
2018         struct work_struct hotplug_work;
2019         struct work_struct audio_work;
2020         struct work_struct reset_work;
2021         int num_crtc; /* number of crtcs */
2022         struct mutex dc_hw_i2c_mutex; /* display controller hw i2c mutex */
2023         bool audio_enabled;
2024         bool has_uvd;
2025         struct r600_audio audio_status; /* audio stuff */
2026         struct notifier_block acpi_nb;
2027         /* only one userspace can use Hyperz features or CMASK at a time */
2028         struct drm_file *hyperz_filp;
2029         struct drm_file *cmask_filp;
2030         /* i2c buses */
2031         struct radeon_i2c_chan *i2c_bus[RADEON_MAX_I2C_BUS];
2032         /* debugfs */
2033         struct radeon_debugfs   debugfs[RADEON_DEBUGFS_MAX_COMPONENTS];
2034         unsigned                debugfs_count;
2035         /* virtual memory */
2036         struct radeon_vm_manager        vm_manager;
2037         struct mutex                    gpu_clock_mutex;
2038         /* ACPI interface */
2039         struct radeon_atif              atif;
2040         struct radeon_atcs              atcs;
2041 };
2042
2043 int radeon_device_init(struct radeon_device *rdev,
2044                        struct drm_device *ddev,
2045                        struct pci_dev *pdev,
2046                        uint32_t flags);
2047 void radeon_device_fini(struct radeon_device *rdev);
2048 int radeon_gpu_wait_for_idle(struct radeon_device *rdev);
2049
2050 uint32_t r100_mm_rreg(struct radeon_device *rdev, uint32_t reg,
2051                       bool always_indirect);
2052 void r100_mm_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v,
2053                   bool always_indirect);
2054 u32 r100_io_rreg(struct radeon_device *rdev, u32 reg);
2055 void r100_io_wreg(struct radeon_device *rdev, u32 reg, u32 v);
2056
2057 u32 cik_mm_rdoorbell(struct radeon_device *rdev, u32 offset);
2058 void cik_mm_wdoorbell(struct radeon_device *rdev, u32 offset, u32 v);
2059
2060 /*
2061  * Cast helper
2062  */
2063 #define to_radeon_fence(p) ((struct radeon_fence *)(p))
2064
2065 /*
2066  * Registers read & write functions.
2067  */
2068 #define RREG8(reg) readb((rdev->rmmio) + (reg))
2069 #define WREG8(reg, v) writeb(v, (rdev->rmmio) + (reg))
2070 #define RREG16(reg) readw((rdev->rmmio) + (reg))
2071 #define WREG16(reg, v) writew(v, (rdev->rmmio) + (reg))
2072 #define RREG32(reg) r100_mm_rreg(rdev, (reg), false)
2073 #define RREG32_IDX(reg) r100_mm_rreg(rdev, (reg), true)
2074 #define DREG32(reg) printk(KERN_INFO "REGISTER: " #reg " : 0x%08X\n", r100_mm_rreg(rdev, (reg), false))
2075 #define WREG32(reg, v) r100_mm_wreg(rdev, (reg), (v), false)
2076 #define WREG32_IDX(reg, v) r100_mm_wreg(rdev, (reg), (v), true)
2077 #define REG_SET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
2078 #define REG_GET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
2079 #define RREG32_PLL(reg) rdev->pll_rreg(rdev, (reg))
2080 #define WREG32_PLL(reg, v) rdev->pll_wreg(rdev, (reg), (v))
2081 #define RREG32_MC(reg) rdev->mc_rreg(rdev, (reg))
2082 #define WREG32_MC(reg, v) rdev->mc_wreg(rdev, (reg), (v))
2083 #define RREG32_PCIE(reg) rv370_pcie_rreg(rdev, (reg))
2084 #define WREG32_PCIE(reg, v) rv370_pcie_wreg(rdev, (reg), (v))
2085 #define RREG32_PCIE_PORT(reg) rdev->pciep_rreg(rdev, (reg))
2086 #define WREG32_PCIE_PORT(reg, v) rdev->pciep_wreg(rdev, (reg), (v))
2087 #define RREG32_SMC(reg) tn_smc_rreg(rdev, (reg))
2088 #define WREG32_SMC(reg, v) tn_smc_wreg(rdev, (reg), (v))
2089 #define RREG32_RCU(reg) r600_rcu_rreg(rdev, (reg))
2090 #define WREG32_RCU(reg, v) r600_rcu_wreg(rdev, (reg), (v))
2091 #define RREG32_CG(reg) eg_cg_rreg(rdev, (reg))
2092 #define WREG32_CG(reg, v) eg_cg_wreg(rdev, (reg), (v))
2093 #define RREG32_PIF_PHY0(reg) eg_pif_phy0_rreg(rdev, (reg))
2094 #define WREG32_PIF_PHY0(reg, v) eg_pif_phy0_wreg(rdev, (reg), (v))
2095 #define RREG32_PIF_PHY1(reg) eg_pif_phy1_rreg(rdev, (reg))
2096 #define WREG32_PIF_PHY1(reg, v) eg_pif_phy1_wreg(rdev, (reg), (v))
2097 #define RREG32_UVD_CTX(reg) r600_uvd_ctx_rreg(rdev, (reg))
2098 #define WREG32_UVD_CTX(reg, v) r600_uvd_ctx_wreg(rdev, (reg), (v))
2099 #define WREG32_P(reg, val, mask)                                \
2100         do {                                                    \
2101                 uint32_t tmp_ = RREG32(reg);                    \
2102                 tmp_ &= (mask);                                 \
2103                 tmp_ |= ((val) & ~(mask));                      \
2104                 WREG32(reg, tmp_);                              \
2105         } while (0)
2106 #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
2107 #define WREG32_OR(reg, or) WREG32_P(reg, or, ~or)
2108 #define WREG32_PLL_P(reg, val, mask)                            \
2109         do {                                                    \
2110                 uint32_t tmp_ = RREG32_PLL(reg);                \
2111                 tmp_ &= (mask);                                 \
2112                 tmp_ |= ((val) & ~(mask));                      \
2113                 WREG32_PLL(reg, tmp_);                          \
2114         } while (0)
2115 #define DREG32_SYS(sqf, rdev, reg) seq_printf((sqf), #reg " : 0x%08X\n", r100_mm_rreg((rdev), (reg), false))
2116 #define RREG32_IO(reg) r100_io_rreg(rdev, (reg))
2117 #define WREG32_IO(reg, v) r100_io_wreg(rdev, (reg), (v))
2118
2119 #define RDOORBELL32(offset) cik_mm_rdoorbell(rdev, (offset))
2120 #define WDOORBELL32(offset, v) cik_mm_wdoorbell(rdev, (offset), (v))
2121
2122 /*
2123  * Indirect registers accessor
2124  */
2125 static inline uint32_t rv370_pcie_rreg(struct radeon_device *rdev, uint32_t reg)
2126 {
2127         uint32_t r;
2128
2129         WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
2130         r = RREG32(RADEON_PCIE_DATA);
2131         return r;
2132 }
2133
2134 static inline void rv370_pcie_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
2135 {
2136         WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
2137         WREG32(RADEON_PCIE_DATA, (v));
2138 }
2139
2140 static inline u32 tn_smc_rreg(struct radeon_device *rdev, u32 reg)
2141 {
2142         u32 r;
2143
2144         WREG32(TN_SMC_IND_INDEX_0, (reg));
2145         r = RREG32(TN_SMC_IND_DATA_0);
2146         return r;
2147 }
2148
2149 static inline void tn_smc_wreg(struct radeon_device *rdev, u32 reg, u32 v)
2150 {
2151         WREG32(TN_SMC_IND_INDEX_0, (reg));
2152         WREG32(TN_SMC_IND_DATA_0, (v));
2153 }
2154
2155 static inline u32 r600_rcu_rreg(struct radeon_device *rdev, u32 reg)
2156 {
2157         u32 r;
2158
2159         WREG32(R600_RCU_INDEX, ((reg) & 0x1fff));
2160         r = RREG32(R600_RCU_DATA);
2161         return r;
2162 }
2163
2164 static inline void r600_rcu_wreg(struct radeon_device *rdev, u32 reg, u32 v)
2165 {
2166         WREG32(R600_RCU_INDEX, ((reg) & 0x1fff));
2167         WREG32(R600_RCU_DATA, (v));
2168 }
2169
2170 static inline u32 eg_cg_rreg(struct radeon_device *rdev, u32 reg)
2171 {
2172         u32 r;
2173
2174         WREG32(EVERGREEN_CG_IND_ADDR, ((reg) & 0xffff));
2175         r = RREG32(EVERGREEN_CG_IND_DATA);
2176         return r;
2177 }
2178
2179 static inline void eg_cg_wreg(struct radeon_device *rdev, u32 reg, u32 v)
2180 {
2181         WREG32(EVERGREEN_CG_IND_ADDR, ((reg) & 0xffff));
2182         WREG32(EVERGREEN_CG_IND_DATA, (v));
2183 }
2184
2185 static inline u32 eg_pif_phy0_rreg(struct radeon_device *rdev, u32 reg)
2186 {
2187         u32 r;
2188
2189         WREG32(EVERGREEN_PIF_PHY0_INDEX, ((reg) & 0xffff));
2190         r = RREG32(EVERGREEN_PIF_PHY0_DATA);
2191         return r;
2192 }
2193
2194 static inline void eg_pif_phy0_wreg(struct radeon_device *rdev, u32 reg, u32 v)
2195 {
2196         WREG32(EVERGREEN_PIF_PHY0_INDEX, ((reg) & 0xffff));
2197         WREG32(EVERGREEN_PIF_PHY0_DATA, (v));
2198 }
2199
2200 static inline u32 eg_pif_phy1_rreg(struct radeon_device *rdev, u32 reg)
2201 {
2202         u32 r;
2203
2204         WREG32(EVERGREEN_PIF_PHY1_INDEX, ((reg) & 0xffff));
2205         r = RREG32(EVERGREEN_PIF_PHY1_DATA);
2206         return r;
2207 }
2208
2209 static inline void eg_pif_phy1_wreg(struct radeon_device *rdev, u32 reg, u32 v)
2210 {
2211         WREG32(EVERGREEN_PIF_PHY1_INDEX, ((reg) & 0xffff));
2212         WREG32(EVERGREEN_PIF_PHY1_DATA, (v));
2213 }
2214
2215 static inline u32 r600_uvd_ctx_rreg(struct radeon_device *rdev, u32 reg)
2216 {
2217         u32 r;
2218
2219         WREG32(R600_UVD_CTX_INDEX, ((reg) & 0x1ff));
2220         r = RREG32(R600_UVD_CTX_DATA);
2221         return r;
2222 }
2223
2224 static inline void r600_uvd_ctx_wreg(struct radeon_device *rdev, u32 reg, u32 v)
2225 {
2226         WREG32(R600_UVD_CTX_INDEX, ((reg) & 0x1ff));
2227         WREG32(R600_UVD_CTX_DATA, (v));
2228 }
2229
2230 void r100_pll_errata_after_index(struct radeon_device *rdev);
2231
2232
2233 /*
2234  * ASICs helpers.
2235  */
2236 #define ASIC_IS_RN50(rdev) ((rdev->pdev->device == 0x515e) || \
2237                             (rdev->pdev->device == 0x5969))
2238 #define ASIC_IS_RV100(rdev) ((rdev->family == CHIP_RV100) || \
2239                 (rdev->family == CHIP_RV200) || \
2240                 (rdev->family == CHIP_RS100) || \
2241                 (rdev->family == CHIP_RS200) || \
2242                 (rdev->family == CHIP_RV250) || \
2243                 (rdev->family == CHIP_RV280) || \
2244                 (rdev->family == CHIP_RS300))
2245 #define ASIC_IS_R300(rdev) ((rdev->family == CHIP_R300)  ||     \
2246                 (rdev->family == CHIP_RV350) ||                 \
2247                 (rdev->family == CHIP_R350)  ||                 \
2248                 (rdev->family == CHIP_RV380) ||                 \
2249                 (rdev->family == CHIP_R420)  ||                 \
2250                 (rdev->family == CHIP_R423)  ||                 \
2251                 (rdev->family == CHIP_RV410) ||                 \
2252                 (rdev->family == CHIP_RS400) ||                 \
2253                 (rdev->family == CHIP_RS480))
2254 #define ASIC_IS_X2(rdev) ((rdev->ddev->pdev->device == 0x9441) || \
2255                 (rdev->ddev->pdev->device == 0x9443) || \
2256                 (rdev->ddev->pdev->device == 0x944B) || \
2257                 (rdev->ddev->pdev->device == 0x9506) || \
2258                 (rdev->ddev->pdev->device == 0x9509) || \
2259                 (rdev->ddev->pdev->device == 0x950F) || \
2260                 (rdev->ddev->pdev->device == 0x689C) || \
2261                 (rdev->ddev->pdev->device == 0x689D))
2262 #define ASIC_IS_AVIVO(rdev) ((rdev->family >= CHIP_RS600))
2263 #define ASIC_IS_DCE2(rdev) ((rdev->family == CHIP_RS600)  ||    \
2264                             (rdev->family == CHIP_RS690)  ||    \
2265                             (rdev->family == CHIP_RS740)  ||    \
2266                             (rdev->family >= CHIP_R600))
2267 #define ASIC_IS_DCE3(rdev) ((rdev->family >= CHIP_RV620))
2268 #define ASIC_IS_DCE32(rdev) ((rdev->family >= CHIP_RV730))
2269 #define ASIC_IS_DCE4(rdev) ((rdev->family >= CHIP_CEDAR))
2270 #define ASIC_IS_DCE41(rdev) ((rdev->family >= CHIP_PALM) && \
2271                              (rdev->flags & RADEON_IS_IGP))
2272 #define ASIC_IS_DCE5(rdev) ((rdev->family >= CHIP_BARTS))
2273 #define ASIC_IS_DCE6(rdev) ((rdev->family >= CHIP_ARUBA))
2274 #define ASIC_IS_DCE61(rdev) ((rdev->family >= CHIP_ARUBA) && \
2275                              (rdev->flags & RADEON_IS_IGP))
2276 #define ASIC_IS_DCE64(rdev) ((rdev->family == CHIP_OLAND))
2277 #define ASIC_IS_NODCE(rdev) ((rdev->family == CHIP_HAINAN))
2278 #define ASIC_IS_DCE8(rdev) ((rdev->family >= CHIP_BONAIRE))
2279
2280 #define ASIC_IS_LOMBOK(rdev) ((rdev->ddev->pdev->device == 0x6849) || \
2281                               (rdev->ddev->pdev->device == 0x6850) || \
2282                               (rdev->ddev->pdev->device == 0x6858) || \
2283                               (rdev->ddev->pdev->device == 0x6859) || \
2284                               (rdev->ddev->pdev->device == 0x6840) || \
2285                               (rdev->ddev->pdev->device == 0x6841) || \
2286                               (rdev->ddev->pdev->device == 0x6842) || \
2287                               (rdev->ddev->pdev->device == 0x6843))
2288
2289 /*
2290  * BIOS helpers.
2291  */
2292 #define RBIOS8(i) (rdev->bios[i])
2293 #define RBIOS16(i) (RBIOS8(i) | (RBIOS8((i)+1) << 8))
2294 #define RBIOS32(i) ((RBIOS16(i)) | (RBIOS16((i)+2) << 16))
2295
2296 int radeon_combios_init(struct radeon_device *rdev);
2297 void radeon_combios_fini(struct radeon_device *rdev);
2298 int radeon_atombios_init(struct radeon_device *rdev);
2299 void radeon_atombios_fini(struct radeon_device *rdev);
2300
2301
2302 /*
2303  * RING helpers.
2304  */
2305 #if DRM_DEBUG_CODE == 0
2306 static inline void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
2307 {
2308         ring->ring[ring->wptr++] = v;
2309         ring->wptr &= ring->ptr_mask;
2310         ring->count_dw--;
2311         ring->ring_free_dw--;
2312 }
2313 #else
2314 /* With debugging this is just too big to inline */
2315 void radeon_ring_write(struct radeon_ring *ring, uint32_t v);
2316 #endif
2317
2318 /*
2319  * ASICs macro.
2320  */
2321 #define radeon_init(rdev) (rdev)->asic->init((rdev))
2322 #define radeon_fini(rdev) (rdev)->asic->fini((rdev))
2323 #define radeon_resume(rdev) (rdev)->asic->resume((rdev))
2324 #define radeon_suspend(rdev) (rdev)->asic->suspend((rdev))
2325 #define radeon_cs_parse(rdev, r, p) (rdev)->asic->ring[(r)].cs_parse((p))
2326 #define radeon_vga_set_state(rdev, state) (rdev)->asic->vga_set_state((rdev), (state))
2327 #define radeon_asic_reset(rdev) (rdev)->asic->asic_reset((rdev))
2328 #define radeon_gart_tlb_flush(rdev) (rdev)->asic->gart.tlb_flush((rdev))
2329 #define radeon_gart_set_page(rdev, i, p) (rdev)->asic->gart.set_page((rdev), (i), (p))
2330 #define radeon_asic_vm_init(rdev) (rdev)->asic->vm.init((rdev))
2331 #define radeon_asic_vm_fini(rdev) (rdev)->asic->vm.fini((rdev))
2332 #define radeon_asic_vm_set_page(rdev, ib, pe, addr, count, incr, flags) ((rdev)->asic->vm.set_page((rdev), (ib), (pe), (addr), (count), (incr), (flags)))
2333 #define radeon_ring_start(rdev, r, cp) (rdev)->asic->ring[(r)].ring_start((rdev), (cp))
2334 #define radeon_ring_test(rdev, r, cp) (rdev)->asic->ring[(r)].ring_test((rdev), (cp))
2335 #define radeon_ib_test(rdev, r, cp) (rdev)->asic->ring[(r)].ib_test((rdev), (cp))
2336 #define radeon_ring_ib_execute(rdev, r, ib) (rdev)->asic->ring[(r)].ib_execute((rdev), (ib))
2337 #define radeon_ring_ib_parse(rdev, r, ib) (rdev)->asic->ring[(r)].ib_parse((rdev), (ib))
2338 #define radeon_ring_is_lockup(rdev, r, cp) (rdev)->asic->ring[(r)].is_lockup((rdev), (cp))
2339 #define radeon_ring_vm_flush(rdev, r, vm) (rdev)->asic->ring[(r)].vm_flush((rdev), (r), (vm))
2340 #define radeon_ring_get_rptr(rdev, r) (rdev)->asic->ring[(r)->idx].get_rptr((rdev), (r))
2341 #define radeon_ring_get_wptr(rdev, r) (rdev)->asic->ring[(r)->idx].get_wptr((rdev), (r))
2342 #define radeon_ring_set_wptr(rdev, r) (rdev)->asic->ring[(r)->idx].set_wptr((rdev), (r))
2343 #define radeon_irq_set(rdev) (rdev)->asic->irq.set((rdev))
2344 #define radeon_irq_process(rdev) (rdev)->asic->irq.process((rdev))
2345 #define radeon_get_vblank_counter(rdev, crtc) (rdev)->asic->display.get_vblank_counter((rdev), (crtc))
2346 #define radeon_set_backlight_level(rdev, e, l) (rdev)->asic->display.set_backlight_level((e), (l))
2347 #define radeon_get_backlight_level(rdev, e) (rdev)->asic->display.get_backlight_level((e))
2348 #define radeon_hdmi_enable(rdev, e, b) (rdev)->asic->display.hdmi_enable((e), (b))
2349 #define radeon_hdmi_setmode(rdev, e, m) (rdev)->asic->display.hdmi_setmode((e), (m))
2350 #define radeon_fence_ring_emit(rdev, r, fence) (rdev)->asic->ring[(r)].emit_fence((rdev), (fence))
2351 #define radeon_semaphore_ring_emit(rdev, r, cp, semaphore, emit_wait) (rdev)->asic->ring[(r)].emit_semaphore((rdev), (cp), (semaphore), (emit_wait))
2352 #define radeon_copy_blit(rdev, s, d, np, f) (rdev)->asic->copy.blit((rdev), (s), (d), (np), (f))
2353 #define radeon_copy_dma(rdev, s, d, np, f) (rdev)->asic->copy.dma((rdev), (s), (d), (np), (f))
2354 #define radeon_copy(rdev, s, d, np, f) (rdev)->asic->copy.copy((rdev), (s), (d), (np), (f))
2355 #define radeon_copy_blit_ring_index(rdev) (rdev)->asic->copy.blit_ring_index
2356 #define radeon_copy_dma_ring_index(rdev) (rdev)->asic->copy.dma_ring_index
2357 #define radeon_copy_ring_index(rdev) (rdev)->asic->copy.copy_ring_index
2358 #define radeon_get_engine_clock(rdev) (rdev)->asic->pm.get_engine_clock((rdev))
2359 #define radeon_set_engine_clock(rdev, e) (rdev)->asic->pm.set_engine_clock((rdev), (e))
2360 #define radeon_get_memory_clock(rdev) (rdev)->asic->pm.get_memory_clock((rdev))
2361 #define radeon_set_memory_clock(rdev, e) (rdev)->asic->pm.set_memory_clock((rdev), (e))
2362 #define radeon_get_pcie_lanes(rdev) (rdev)->asic->pm.get_pcie_lanes((rdev))
2363 #define radeon_set_pcie_lanes(rdev, l) (rdev)->asic->pm.set_pcie_lanes((rdev), (l))
2364 #define radeon_set_clock_gating(rdev, e) (rdev)->asic->pm.set_clock_gating((rdev), (e))
2365 #define radeon_set_uvd_clocks(rdev, v, d) (rdev)->asic->pm.set_uvd_clocks((rdev), (v), (d))
2366 #define radeon_get_temperature(rdev) (rdev)->asic->pm.get_temperature((rdev))
2367 #define radeon_set_surface_reg(rdev, r, f, p, o, s) ((rdev)->asic->surface.set_reg((rdev), (r), (f), (p), (o), (s)))
2368 #define radeon_clear_surface_reg(rdev, r) ((rdev)->asic->surface.clear_reg((rdev), (r)))
2369 #define radeon_bandwidth_update(rdev) (rdev)->asic->display.bandwidth_update((rdev))
2370 #define radeon_hpd_init(rdev) (rdev)->asic->hpd.init((rdev))
2371 #define radeon_hpd_fini(rdev) (rdev)->asic->hpd.fini((rdev))
2372 #define radeon_hpd_sense(rdev, h) (rdev)->asic->hpd.sense((rdev), (h))
2373 #define radeon_hpd_set_polarity(rdev, h) (rdev)->asic->hpd.set_polarity((rdev), (h))
2374 #define radeon_gui_idle(rdev) (rdev)->asic->gui_idle((rdev))
2375 #define radeon_pm_misc(rdev) (rdev)->asic->pm.misc((rdev))
2376 #define radeon_pm_prepare(rdev) (rdev)->asic->pm.prepare((rdev))
2377 #define radeon_pm_finish(rdev) (rdev)->asic->pm.finish((rdev))
2378 #define radeon_pm_init_profile(rdev) (rdev)->asic->pm.init_profile((rdev))
2379 #define radeon_pm_get_dynpm_state(rdev) (rdev)->asic->pm.get_dynpm_state((rdev))
2380 #define radeon_pre_page_flip(rdev, crtc) (rdev)->asic->pflip.pre_page_flip((rdev), (crtc))
2381 #define radeon_page_flip(rdev, crtc, base) (rdev)->asic->pflip.page_flip((rdev), (crtc), (base))
2382 #define radeon_post_page_flip(rdev, crtc) (rdev)->asic->pflip.post_page_flip((rdev), (crtc))
2383 #define radeon_wait_for_vblank(rdev, crtc) (rdev)->asic->display.wait_for_vblank((rdev), (crtc))
2384 #define radeon_mc_wait_for_idle(rdev) (rdev)->asic->mc_wait_for_idle((rdev))
2385 #define radeon_get_xclk(rdev) (rdev)->asic->get_xclk((rdev))
2386 #define radeon_get_gpu_clock_counter(rdev) (rdev)->asic->get_gpu_clock_counter((rdev))
2387 #define radeon_dpm_init(rdev) rdev->asic->dpm.init((rdev))
2388 #define radeon_dpm_setup_asic(rdev) rdev->asic->dpm.setup_asic((rdev))
2389 #define radeon_dpm_enable(rdev) rdev->asic->dpm.enable((rdev))
2390 #define radeon_dpm_disable(rdev) rdev->asic->dpm.disable((rdev))
2391 #define radeon_dpm_pre_set_power_state(rdev) rdev->asic->dpm.pre_set_power_state((rdev))
2392 #define radeon_dpm_set_power_state(rdev) rdev->asic->dpm.set_power_state((rdev))
2393 #define radeon_dpm_post_set_power_state(rdev) rdev->asic->dpm.post_set_power_state((rdev))
2394 #define radeon_dpm_display_configuration_changed(rdev) rdev->asic->dpm.display_configuration_changed((rdev))
2395 #define radeon_dpm_fini(rdev) rdev->asic->dpm.fini((rdev))
2396 #define radeon_dpm_get_sclk(rdev, l) rdev->asic->dpm.get_sclk((rdev), (l))
2397 #define radeon_dpm_get_mclk(rdev, l) rdev->asic->dpm.get_mclk((rdev), (l))
2398 #define radeon_dpm_print_power_state(rdev, ps) rdev->asic->dpm.print_power_state((rdev), (ps))
2399
2400 /* Common functions */
2401 /* AGP */
2402 extern int radeon_gpu_reset(struct radeon_device *rdev);
2403 extern void r600_set_bios_scratch_engine_hung(struct radeon_device *rdev, bool hung);
2404 extern void radeon_agp_disable(struct radeon_device *rdev);
2405 extern int radeon_modeset_init(struct radeon_device *rdev);
2406 extern void radeon_modeset_fini(struct radeon_device *rdev);
2407 extern bool radeon_card_posted(struct radeon_device *rdev);
2408 extern void radeon_update_bandwidth_info(struct radeon_device *rdev);
2409 extern void radeon_update_display_priority(struct radeon_device *rdev);
2410 extern bool radeon_boot_test_post_card(struct radeon_device *rdev);
2411 extern void radeon_scratch_init(struct radeon_device *rdev);
2412 extern void radeon_wb_fini(struct radeon_device *rdev);
2413 extern int radeon_wb_init(struct radeon_device *rdev);
2414 extern void radeon_wb_disable(struct radeon_device *rdev);
2415 extern void radeon_surface_init(struct radeon_device *rdev);
2416 extern int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data);
2417 extern void radeon_legacy_set_clock_gating(struct radeon_device *rdev, int enable);
2418 extern void radeon_atom_set_clock_gating(struct radeon_device *rdev, int enable);
2419 extern void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain);
2420 extern bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo);
2421 extern void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base);
2422 extern void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc);
2423 extern int radeon_resume_kms(struct drm_device *dev);
2424 extern int radeon_suspend_kms(struct drm_device *dev, pm_message_t state);
2425 extern void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size);
2426 extern void radeon_program_register_sequence(struct radeon_device *rdev,
2427                                              const u32 *registers,
2428                                              const u32 array_size);
2429
2430 /*
2431  * vm
2432  */
2433 int radeon_vm_manager_init(struct radeon_device *rdev);
2434 void radeon_vm_manager_fini(struct radeon_device *rdev);
2435 void radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm);
2436 void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm);
2437 int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm);
2438 void radeon_vm_add_to_lru(struct radeon_device *rdev, struct radeon_vm *vm);
2439 struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
2440                                        struct radeon_vm *vm, int ring);
2441 void radeon_vm_fence(struct radeon_device *rdev,
2442                      struct radeon_vm *vm,
2443                      struct radeon_fence *fence);
2444 uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr);
2445 int radeon_vm_bo_update_pte(struct radeon_device *rdev,
2446                             struct radeon_vm *vm,
2447                             struct radeon_bo *bo,
2448                             struct ttm_mem_reg *mem);
2449 void radeon_vm_bo_invalidate(struct radeon_device *rdev,
2450                              struct radeon_bo *bo);
2451 struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
2452                                        struct radeon_bo *bo);
2453 struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
2454                                       struct radeon_vm *vm,
2455                                       struct radeon_bo *bo);
2456 int radeon_vm_bo_set_addr(struct radeon_device *rdev,
2457                           struct radeon_bo_va *bo_va,
2458                           uint64_t offset,
2459                           uint32_t flags);
2460 int radeon_vm_bo_rmv(struct radeon_device *rdev,
2461                      struct radeon_bo_va *bo_va);
2462
2463 /* audio */
2464 void r600_audio_update_hdmi(struct work_struct *work);
2465
2466 /*
2467  * R600 vram scratch functions
2468  */
2469 int r600_vram_scratch_init(struct radeon_device *rdev);
2470 void r600_vram_scratch_fini(struct radeon_device *rdev);
2471
2472 /*
2473  * r600 cs checking helper
2474  */
2475 unsigned r600_mip_minify(unsigned size, unsigned level);
2476 bool r600_fmt_is_valid_color(u32 format);
2477 bool r600_fmt_is_valid_texture(u32 format, enum radeon_family family);
2478 int r600_fmt_get_blocksize(u32 format);
2479 int r600_fmt_get_nblocksx(u32 format, u32 w);
2480 int r600_fmt_get_nblocksy(u32 format, u32 h);
2481
2482 /*
2483  * r600 functions used by radeon_encoder.c
2484  */
2485 struct radeon_hdmi_acr {
2486         u32 clock;
2487
2488         int n_32khz;
2489         int cts_32khz;
2490
2491         int n_44_1khz;
2492         int cts_44_1khz;
2493
2494         int n_48khz;
2495         int cts_48khz;
2496
2497 };
2498
2499 extern struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock);
2500
2501 extern u32 r6xx_remap_render_backend(struct radeon_device *rdev,
2502                                      u32 tiling_pipe_num,
2503                                      u32 max_rb_num,
2504                                      u32 total_max_rb_num,
2505                                      u32 enabled_rb_mask);
2506
2507 /*
2508  * evergreen functions used by radeon_encoder.c
2509  */
2510
2511 extern int ni_init_microcode(struct radeon_device *rdev);
2512 extern int ni_mc_load_microcode(struct radeon_device *rdev);
2513
2514 /* radeon_acpi.c */
2515 #if defined(CONFIG_ACPI)
2516 extern int radeon_acpi_init(struct radeon_device *rdev);
2517 extern void radeon_acpi_fini(struct radeon_device *rdev);
2518 extern bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev);
2519 extern int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
2520                                                 u8 perf_req, bool advertise);
2521 extern int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev);
2522 #else
2523 static inline int radeon_acpi_init(struct radeon_device *rdev) { return 0; }
2524 static inline void radeon_acpi_fini(struct radeon_device *rdev) { }
2525 #endif
2526
2527 int radeon_cs_packet_parse(struct radeon_cs_parser *p,
2528                            struct radeon_cs_packet *pkt,
2529                            unsigned idx);
2530 bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p);
2531 void radeon_cs_dump_packet(struct radeon_cs_parser *p,
2532                            struct radeon_cs_packet *pkt);
2533 int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
2534                                 struct radeon_cs_reloc **cs_reloc,
2535                                 int nomm);
2536 int r600_cs_common_vline_parse(struct radeon_cs_parser *p,
2537                                uint32_t *vline_start_end,
2538                                uint32_t *vline_status);
2539
2540 #include "radeon_object.h"
2541
2542 #endif