Subversion Repositories Kolibri OS

Rev

Rev 2007 | Rev 2175 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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