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