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