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