radeon.h 48 KB

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