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