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