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