amdgpu.h 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  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 __AMDGPU_H__
  29. #define __AMDGPU_H__
  30. #include <linux/atomic.h>
  31. #include <linux/wait.h>
  32. #include <linux/list.h>
  33. #include <linux/kref.h>
  34. #include <linux/interval_tree.h>
  35. #include <linux/hashtable.h>
  36. #include <linux/dma-fence.h>
  37. #include <ttm/ttm_bo_api.h>
  38. #include <ttm/ttm_bo_driver.h>
  39. #include <ttm/ttm_placement.h>
  40. #include <ttm/ttm_module.h>
  41. #include <ttm/ttm_execbuf_util.h>
  42. #include <drm/drmP.h>
  43. #include <drm/drm_gem.h>
  44. #include <drm/amdgpu_drm.h>
  45. #include "amd_shared.h"
  46. #include "amdgpu_mode.h"
  47. #include "amdgpu_ih.h"
  48. #include "amdgpu_irq.h"
  49. #include "amdgpu_ucode.h"
  50. #include "amdgpu_ttm.h"
  51. #include "amdgpu_gds.h"
  52. #include "amdgpu_sync.h"
  53. #include "amdgpu_ring.h"
  54. #include "amdgpu_vm.h"
  55. #include "amd_powerplay.h"
  56. #include "amdgpu_dpm.h"
  57. #include "amdgpu_acp.h"
  58. #include "gpu_scheduler.h"
  59. #include "amdgpu_virt.h"
  60. /*
  61. * Modules parameters.
  62. */
  63. extern int amdgpu_modeset;
  64. extern int amdgpu_vram_limit;
  65. extern int amdgpu_gart_size;
  66. extern int amdgpu_moverate;
  67. extern int amdgpu_benchmarking;
  68. extern int amdgpu_testing;
  69. extern int amdgpu_audio;
  70. extern int amdgpu_disp_priority;
  71. extern int amdgpu_hw_i2c;
  72. extern int amdgpu_pcie_gen2;
  73. extern int amdgpu_msi;
  74. extern int amdgpu_lockup_timeout;
  75. extern int amdgpu_dpm;
  76. extern int amdgpu_smc_load_fw;
  77. extern int amdgpu_aspm;
  78. extern int amdgpu_runtime_pm;
  79. extern unsigned amdgpu_ip_block_mask;
  80. extern int amdgpu_bapm;
  81. extern int amdgpu_deep_color;
  82. extern int amdgpu_vm_size;
  83. extern int amdgpu_vm_block_size;
  84. extern int amdgpu_vm_fault_stop;
  85. extern int amdgpu_vm_debug;
  86. extern int amdgpu_sched_jobs;
  87. extern int amdgpu_sched_hw_submission;
  88. extern int amdgpu_no_evict;
  89. extern int amdgpu_direct_gma_size;
  90. extern unsigned amdgpu_pcie_gen_cap;
  91. extern unsigned amdgpu_pcie_lane_cap;
  92. extern unsigned amdgpu_cg_mask;
  93. extern unsigned amdgpu_pg_mask;
  94. extern char *amdgpu_disable_cu;
  95. extern char *amdgpu_virtual_display;
  96. extern unsigned amdgpu_pp_feature_mask;
  97. extern int amdgpu_vram_page_split;
  98. #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS 3000
  99. #define AMDGPU_MAX_USEC_TIMEOUT 100000 /* 100 ms */
  100. #define AMDGPU_FENCE_JIFFIES_TIMEOUT (HZ / 2)
  101. /* AMDGPU_IB_POOL_SIZE must be a power of 2 */
  102. #define AMDGPU_IB_POOL_SIZE 16
  103. #define AMDGPU_DEBUGFS_MAX_COMPONENTS 32
  104. #define AMDGPUFB_CONN_LIMIT 4
  105. #define AMDGPU_BIOS_NUM_SCRATCH 8
  106. /* max number of IP instances */
  107. #define AMDGPU_MAX_SDMA_INSTANCES 2
  108. /* hardcode that limit for now */
  109. #define AMDGPU_VA_RESERVED_SIZE (8 << 20)
  110. /* hard reset data */
  111. #define AMDGPU_ASIC_RESET_DATA 0x39d5e86b
  112. /* reset flags */
  113. #define AMDGPU_RESET_GFX (1 << 0)
  114. #define AMDGPU_RESET_COMPUTE (1 << 1)
  115. #define AMDGPU_RESET_DMA (1 << 2)
  116. #define AMDGPU_RESET_CP (1 << 3)
  117. #define AMDGPU_RESET_GRBM (1 << 4)
  118. #define AMDGPU_RESET_DMA1 (1 << 5)
  119. #define AMDGPU_RESET_RLC (1 << 6)
  120. #define AMDGPU_RESET_SEM (1 << 7)
  121. #define AMDGPU_RESET_IH (1 << 8)
  122. #define AMDGPU_RESET_VMC (1 << 9)
  123. #define AMDGPU_RESET_MC (1 << 10)
  124. #define AMDGPU_RESET_DISPLAY (1 << 11)
  125. #define AMDGPU_RESET_UVD (1 << 12)
  126. #define AMDGPU_RESET_VCE (1 << 13)
  127. #define AMDGPU_RESET_VCE1 (1 << 14)
  128. /* GFX current status */
  129. #define AMDGPU_GFX_NORMAL_MODE 0x00000000L
  130. #define AMDGPU_GFX_SAFE_MODE 0x00000001L
  131. #define AMDGPU_GFX_PG_DISABLED_MODE 0x00000002L
  132. #define AMDGPU_GFX_CG_DISABLED_MODE 0x00000004L
  133. #define AMDGPU_GFX_LBPW_DISABLED_MODE 0x00000008L
  134. /* max cursor sizes (in pixels) */
  135. #define CIK_CURSOR_WIDTH 128
  136. #define CIK_CURSOR_HEIGHT 128
  137. struct amdgpu_device;
  138. struct amdgpu_ib;
  139. struct amdgpu_cs_parser;
  140. struct amdgpu_job;
  141. struct amdgpu_irq_src;
  142. struct amdgpu_fpriv;
  143. enum amdgpu_cp_irq {
  144. AMDGPU_CP_IRQ_GFX_EOP = 0,
  145. AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP,
  146. AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE1_EOP,
  147. AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE2_EOP,
  148. AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE3_EOP,
  149. AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE0_EOP,
  150. AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE1_EOP,
  151. AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE2_EOP,
  152. AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE3_EOP,
  153. AMDGPU_CP_IRQ_LAST
  154. };
  155. enum amdgpu_sdma_irq {
  156. AMDGPU_SDMA_IRQ_TRAP0 = 0,
  157. AMDGPU_SDMA_IRQ_TRAP1,
  158. AMDGPU_SDMA_IRQ_LAST
  159. };
  160. enum amdgpu_thermal_irq {
  161. AMDGPU_THERMAL_IRQ_LOW_TO_HIGH = 0,
  162. AMDGPU_THERMAL_IRQ_HIGH_TO_LOW,
  163. AMDGPU_THERMAL_IRQ_LAST
  164. };
  165. enum amdgpu_kiq_irq {
  166. AMDGPU_CP_KIQ_IRQ_DRIVER0 = 0,
  167. AMDGPU_CP_KIQ_IRQ_LAST
  168. };
  169. int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
  170. enum amd_ip_block_type block_type,
  171. enum amd_clockgating_state state);
  172. int amdgpu_set_powergating_state(struct amdgpu_device *adev,
  173. enum amd_ip_block_type block_type,
  174. enum amd_powergating_state state);
  175. void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags);
  176. int amdgpu_wait_for_idle(struct amdgpu_device *adev,
  177. enum amd_ip_block_type block_type);
  178. bool amdgpu_is_idle(struct amdgpu_device *adev,
  179. enum amd_ip_block_type block_type);
  180. #define AMDGPU_MAX_IP_NUM 16
  181. struct amdgpu_ip_block_status {
  182. bool valid;
  183. bool sw;
  184. bool hw;
  185. bool late_initialized;
  186. bool hang;
  187. };
  188. struct amdgpu_ip_block_version {
  189. const enum amd_ip_block_type type;
  190. const u32 major;
  191. const u32 minor;
  192. const u32 rev;
  193. const struct amd_ip_funcs *funcs;
  194. };
  195. struct amdgpu_ip_block {
  196. struct amdgpu_ip_block_status status;
  197. const struct amdgpu_ip_block_version *version;
  198. };
  199. int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
  200. enum amd_ip_block_type type,
  201. u32 major, u32 minor);
  202. struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
  203. enum amd_ip_block_type type);
  204. int amdgpu_ip_block_add(struct amdgpu_device *adev,
  205. const struct amdgpu_ip_block_version *ip_block_version);
  206. /* provided by hw blocks that can move/clear data. e.g., gfx or sdma */
  207. struct amdgpu_buffer_funcs {
  208. /* maximum bytes in a single operation */
  209. uint32_t copy_max_bytes;
  210. /* number of dw to reserve per operation */
  211. unsigned copy_num_dw;
  212. /* used for buffer migration */
  213. void (*emit_copy_buffer)(struct amdgpu_ib *ib,
  214. /* src addr in bytes */
  215. uint64_t src_offset,
  216. /* dst addr in bytes */
  217. uint64_t dst_offset,
  218. /* number of byte to transfer */
  219. uint32_t byte_count);
  220. /* maximum bytes in a single operation */
  221. uint32_t fill_max_bytes;
  222. /* number of dw to reserve per operation */
  223. unsigned fill_num_dw;
  224. /* used for buffer clearing */
  225. void (*emit_fill_buffer)(struct amdgpu_ib *ib,
  226. /* value to write to memory */
  227. uint32_t src_data,
  228. /* dst addr in bytes */
  229. uint64_t dst_offset,
  230. /* number of byte to fill */
  231. uint32_t byte_count);
  232. };
  233. /* provided by hw blocks that can write ptes, e.g., sdma */
  234. struct amdgpu_vm_pte_funcs {
  235. /* copy pte entries from GART */
  236. void (*copy_pte)(struct amdgpu_ib *ib,
  237. uint64_t pe, uint64_t src,
  238. unsigned count);
  239. /* write pte one entry at a time with addr mapping */
  240. void (*write_pte)(struct amdgpu_ib *ib, uint64_t pe,
  241. uint64_t value, unsigned count,
  242. uint32_t incr);
  243. /* for linear pte/pde updates without addr mapping */
  244. void (*set_pte_pde)(struct amdgpu_ib *ib,
  245. uint64_t pe,
  246. uint64_t addr, unsigned count,
  247. uint32_t incr, uint32_t flags);
  248. };
  249. /* provided by the gmc block */
  250. struct amdgpu_gart_funcs {
  251. /* flush the vm tlb via mmio */
  252. void (*flush_gpu_tlb)(struct amdgpu_device *adev,
  253. uint32_t vmid);
  254. /* write pte/pde updates using the cpu */
  255. int (*set_pte_pde)(struct amdgpu_device *adev,
  256. void *cpu_pt_addr, /* cpu addr of page table */
  257. uint32_t gpu_page_idx, /* pte/pde to update */
  258. uint64_t addr, /* addr to write into pte/pde */
  259. uint32_t flags); /* access flags */
  260. /* enable/disable PRT support */
  261. void (*set_prt)(struct amdgpu_device *adev, bool enable);
  262. };
  263. /* provided by the ih block */
  264. struct amdgpu_ih_funcs {
  265. /* ring read/write ptr handling, called from interrupt context */
  266. u32 (*get_wptr)(struct amdgpu_device *adev);
  267. void (*decode_iv)(struct amdgpu_device *adev,
  268. struct amdgpu_iv_entry *entry);
  269. void (*set_rptr)(struct amdgpu_device *adev);
  270. };
  271. /*
  272. * BIOS.
  273. */
  274. bool amdgpu_get_bios(struct amdgpu_device *adev);
  275. bool amdgpu_read_bios(struct amdgpu_device *adev);
  276. /*
  277. * Dummy page
  278. */
  279. struct amdgpu_dummy_page {
  280. struct page *page;
  281. dma_addr_t addr;
  282. };
  283. int amdgpu_dummy_page_init(struct amdgpu_device *adev);
  284. void amdgpu_dummy_page_fini(struct amdgpu_device *adev);
  285. /*
  286. * Clocks
  287. */
  288. #define AMDGPU_MAX_PPLL 3
  289. struct amdgpu_clock {
  290. struct amdgpu_pll ppll[AMDGPU_MAX_PPLL];
  291. struct amdgpu_pll spll;
  292. struct amdgpu_pll mpll;
  293. /* 10 Khz units */
  294. uint32_t default_mclk;
  295. uint32_t default_sclk;
  296. uint32_t default_dispclk;
  297. uint32_t current_dispclk;
  298. uint32_t dp_extclk;
  299. uint32_t max_pixel_clock;
  300. };
  301. /*
  302. * BO.
  303. */
  304. struct amdgpu_bo_list_entry {
  305. struct amdgpu_bo *robj;
  306. struct ttm_validate_buffer tv;
  307. struct amdgpu_bo_va *bo_va;
  308. uint32_t priority;
  309. struct page **user_pages;
  310. int user_invalidated;
  311. };
  312. struct amdgpu_bo_va_mapping {
  313. struct list_head list;
  314. struct interval_tree_node it;
  315. uint64_t offset;
  316. uint64_t flags;
  317. };
  318. /* bo virtual addresses in a specific vm */
  319. struct amdgpu_bo_va {
  320. /* protected by bo being reserved */
  321. struct list_head bo_list;
  322. struct dma_fence *last_pt_update;
  323. unsigned ref_count;
  324. /* protected by vm mutex and spinlock */
  325. struct list_head vm_status;
  326. /* mappings for this bo_va */
  327. struct list_head invalids;
  328. struct list_head valids;
  329. /* constant after initialization */
  330. struct amdgpu_vm *vm;
  331. struct amdgpu_bo *bo;
  332. };
  333. #define AMDGPU_GEM_DOMAIN_MAX 0x3
  334. struct amdgpu_bo {
  335. /* Protected by tbo.reserved */
  336. u32 prefered_domains;
  337. u32 allowed_domains;
  338. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  339. struct ttm_placement placement;
  340. struct ttm_buffer_object tbo;
  341. struct ttm_bo_kmap_obj kmap;
  342. u64 flags;
  343. unsigned pin_count;
  344. void *kptr;
  345. u64 tiling_flags;
  346. u64 metadata_flags;
  347. void *metadata;
  348. u32 metadata_size;
  349. unsigned prime_shared_count;
  350. /* list of all virtual address to which this bo
  351. * is associated to
  352. */
  353. struct list_head va;
  354. /* Constant after initialization */
  355. struct drm_gem_object gem_base;
  356. struct amdgpu_bo *parent;
  357. struct amdgpu_bo *shadow;
  358. struct ttm_bo_kmap_obj dma_buf_vmap;
  359. struct amdgpu_mn *mn;
  360. struct list_head mn_list;
  361. struct list_head shadow_list;
  362. };
  363. #define gem_to_amdgpu_bo(gobj) container_of((gobj), struct amdgpu_bo, gem_base)
  364. void amdgpu_gem_object_free(struct drm_gem_object *obj);
  365. int amdgpu_gem_object_open(struct drm_gem_object *obj,
  366. struct drm_file *file_priv);
  367. void amdgpu_gem_object_close(struct drm_gem_object *obj,
  368. struct drm_file *file_priv);
  369. unsigned long amdgpu_gem_timeout(uint64_t timeout_ns);
  370. struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj);
  371. struct drm_gem_object *
  372. amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
  373. struct dma_buf_attachment *attach,
  374. struct sg_table *sg);
  375. struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
  376. struct drm_gem_object *gobj,
  377. int flags);
  378. int amdgpu_gem_prime_pin(struct drm_gem_object *obj);
  379. void amdgpu_gem_prime_unpin(struct drm_gem_object *obj);
  380. struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *);
  381. void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj);
  382. void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
  383. int amdgpu_gem_debugfs_init(struct amdgpu_device *adev);
  384. /* sub-allocation manager, it has to be protected by another lock.
  385. * By conception this is an helper for other part of the driver
  386. * like the indirect buffer or semaphore, which both have their
  387. * locking.
  388. *
  389. * Principe is simple, we keep a list of sub allocation in offset
  390. * order (first entry has offset == 0, last entry has the highest
  391. * offset).
  392. *
  393. * When allocating new object we first check if there is room at
  394. * the end total_size - (last_object_offset + last_object_size) >=
  395. * alloc_size. If so we allocate new object there.
  396. *
  397. * When there is not enough room at the end, we start waiting for
  398. * each sub object until we reach object_offset+object_size >=
  399. * alloc_size, this object then become the sub object we return.
  400. *
  401. * Alignment can't be bigger than page size.
  402. *
  403. * Hole are not considered for allocation to keep things simple.
  404. * Assumption is that there won't be hole (all object on same
  405. * alignment).
  406. */
  407. #define AMDGPU_SA_NUM_FENCE_LISTS 32
  408. struct amdgpu_sa_manager {
  409. wait_queue_head_t wq;
  410. struct amdgpu_bo *bo;
  411. struct list_head *hole;
  412. struct list_head flist[AMDGPU_SA_NUM_FENCE_LISTS];
  413. struct list_head olist;
  414. unsigned size;
  415. uint64_t gpu_addr;
  416. void *cpu_ptr;
  417. uint32_t domain;
  418. uint32_t align;
  419. };
  420. /* sub-allocation buffer */
  421. struct amdgpu_sa_bo {
  422. struct list_head olist;
  423. struct list_head flist;
  424. struct amdgpu_sa_manager *manager;
  425. unsigned soffset;
  426. unsigned eoffset;
  427. struct dma_fence *fence;
  428. };
  429. /*
  430. * GEM objects.
  431. */
  432. void amdgpu_gem_force_release(struct amdgpu_device *adev);
  433. int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
  434. int alignment, u32 initial_domain,
  435. u64 flags, bool kernel,
  436. struct drm_gem_object **obj);
  437. int amdgpu_mode_dumb_create(struct drm_file *file_priv,
  438. struct drm_device *dev,
  439. struct drm_mode_create_dumb *args);
  440. int amdgpu_mode_dumb_mmap(struct drm_file *filp,
  441. struct drm_device *dev,
  442. uint32_t handle, uint64_t *offset_p);
  443. int amdgpu_fence_slab_init(void);
  444. void amdgpu_fence_slab_fini(void);
  445. /*
  446. * GART structures, functions & helpers
  447. */
  448. struct amdgpu_mc;
  449. #define AMDGPU_GPU_PAGE_SIZE 4096
  450. #define AMDGPU_GPU_PAGE_MASK (AMDGPU_GPU_PAGE_SIZE - 1)
  451. #define AMDGPU_GPU_PAGE_SHIFT 12
  452. #define AMDGPU_GPU_PAGE_ALIGN(a) (((a) + AMDGPU_GPU_PAGE_MASK) & ~AMDGPU_GPU_PAGE_MASK)
  453. struct amdgpu_gart {
  454. dma_addr_t table_addr;
  455. struct amdgpu_bo *robj;
  456. void *ptr;
  457. unsigned num_gpu_pages;
  458. unsigned num_cpu_pages;
  459. unsigned table_size;
  460. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  461. struct page **pages;
  462. #endif
  463. bool ready;
  464. const struct amdgpu_gart_funcs *gart_funcs;
  465. };
  466. int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev);
  467. void amdgpu_gart_table_ram_free(struct amdgpu_device *adev);
  468. int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev);
  469. void amdgpu_gart_table_vram_free(struct amdgpu_device *adev);
  470. int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
  471. void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
  472. int amdgpu_gart_init(struct amdgpu_device *adev);
  473. void amdgpu_gart_fini(struct amdgpu_device *adev);
  474. void amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  475. int pages);
  476. int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
  477. int pages, struct page **pagelist,
  478. dma_addr_t *dma_addr, uint32_t flags);
  479. int amdgpu_ttm_recover_gart(struct amdgpu_device *adev);
  480. /*
  481. * GPU MC structures, functions & helpers
  482. */
  483. struct amdgpu_mc {
  484. resource_size_t aper_size;
  485. resource_size_t aper_base;
  486. resource_size_t agp_base;
  487. /* for some chips with <= 32MB we need to lie
  488. * about vram size near mc fb location */
  489. u64 mc_vram_size;
  490. u64 visible_vram_size;
  491. u64 gtt_size;
  492. u64 gtt_start;
  493. u64 gtt_end;
  494. u64 vram_start;
  495. u64 vram_end;
  496. unsigned vram_width;
  497. u64 real_vram_size;
  498. int vram_mtrr;
  499. u64 gtt_base_align;
  500. u64 mc_mask;
  501. const struct firmware *fw; /* MC firmware */
  502. uint32_t fw_version;
  503. struct amdgpu_irq_src vm_fault;
  504. uint32_t vram_type;
  505. uint32_t srbm_soft_reset;
  506. struct amdgpu_mode_mc_save save;
  507. bool prt_warning;
  508. /* apertures */
  509. u64 shared_aperture_start;
  510. u64 shared_aperture_end;
  511. u64 private_aperture_start;
  512. u64 private_aperture_end;
  513. };
  514. /*
  515. * GPU doorbell structures, functions & helpers
  516. */
  517. typedef enum _AMDGPU_DOORBELL_ASSIGNMENT
  518. {
  519. AMDGPU_DOORBELL_KIQ = 0x000,
  520. AMDGPU_DOORBELL_HIQ = 0x001,
  521. AMDGPU_DOORBELL_DIQ = 0x002,
  522. AMDGPU_DOORBELL_MEC_RING0 = 0x010,
  523. AMDGPU_DOORBELL_MEC_RING1 = 0x011,
  524. AMDGPU_DOORBELL_MEC_RING2 = 0x012,
  525. AMDGPU_DOORBELL_MEC_RING3 = 0x013,
  526. AMDGPU_DOORBELL_MEC_RING4 = 0x014,
  527. AMDGPU_DOORBELL_MEC_RING5 = 0x015,
  528. AMDGPU_DOORBELL_MEC_RING6 = 0x016,
  529. AMDGPU_DOORBELL_MEC_RING7 = 0x017,
  530. AMDGPU_DOORBELL_GFX_RING0 = 0x020,
  531. AMDGPU_DOORBELL_sDMA_ENGINE0 = 0x1E0,
  532. AMDGPU_DOORBELL_sDMA_ENGINE1 = 0x1E1,
  533. AMDGPU_DOORBELL_IH = 0x1E8,
  534. AMDGPU_DOORBELL_MAX_ASSIGNMENT = 0x3FF,
  535. AMDGPU_DOORBELL_INVALID = 0xFFFF
  536. } AMDGPU_DOORBELL_ASSIGNMENT;
  537. struct amdgpu_doorbell {
  538. /* doorbell mmio */
  539. resource_size_t base;
  540. resource_size_t size;
  541. u32 __iomem *ptr;
  542. u32 num_doorbells; /* Number of doorbells actually reserved for amdgpu. */
  543. };
  544. void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
  545. phys_addr_t *aperture_base,
  546. size_t *aperture_size,
  547. size_t *start_offset);
  548. /*
  549. * IRQS.
  550. */
  551. struct amdgpu_flip_work {
  552. struct delayed_work flip_work;
  553. struct work_struct unpin_work;
  554. struct amdgpu_device *adev;
  555. int crtc_id;
  556. u32 target_vblank;
  557. uint64_t base;
  558. struct drm_pending_vblank_event *event;
  559. struct amdgpu_bo *old_abo;
  560. struct dma_fence *excl;
  561. unsigned shared_count;
  562. struct dma_fence **shared;
  563. struct dma_fence_cb cb;
  564. bool async;
  565. };
  566. /*
  567. * CP & rings.
  568. */
  569. struct amdgpu_ib {
  570. struct amdgpu_sa_bo *sa_bo;
  571. uint32_t length_dw;
  572. uint64_t gpu_addr;
  573. uint32_t *ptr;
  574. uint32_t flags;
  575. };
  576. extern const struct amd_sched_backend_ops amdgpu_sched_ops;
  577. int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs,
  578. struct amdgpu_job **job, struct amdgpu_vm *vm);
  579. int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size,
  580. struct amdgpu_job **job);
  581. void amdgpu_job_free_resources(struct amdgpu_job *job);
  582. void amdgpu_job_free(struct amdgpu_job *job);
  583. int amdgpu_job_submit(struct amdgpu_job *job, struct amdgpu_ring *ring,
  584. struct amd_sched_entity *entity, void *owner,
  585. struct dma_fence **f);
  586. /*
  587. * context related structures
  588. */
  589. struct amdgpu_ctx_ring {
  590. uint64_t sequence;
  591. struct dma_fence **fences;
  592. struct amd_sched_entity entity;
  593. };
  594. struct amdgpu_ctx {
  595. struct kref refcount;
  596. struct amdgpu_device *adev;
  597. unsigned reset_counter;
  598. spinlock_t ring_lock;
  599. struct dma_fence **fences;
  600. struct amdgpu_ctx_ring rings[AMDGPU_MAX_RINGS];
  601. bool preamble_presented;
  602. };
  603. struct amdgpu_ctx_mgr {
  604. struct amdgpu_device *adev;
  605. struct mutex lock;
  606. /* protected by lock */
  607. struct idr ctx_handles;
  608. };
  609. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id);
  610. int amdgpu_ctx_put(struct amdgpu_ctx *ctx);
  611. uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
  612. struct dma_fence *fence);
  613. struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  614. struct amdgpu_ring *ring, uint64_t seq);
  615. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  616. struct drm_file *filp);
  617. void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
  618. void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
  619. /*
  620. * file private structure
  621. */
  622. struct amdgpu_fpriv {
  623. struct amdgpu_vm vm;
  624. struct amdgpu_bo_va *prt_va;
  625. struct mutex bo_list_lock;
  626. struct idr bo_list_handles;
  627. struct amdgpu_ctx_mgr ctx_mgr;
  628. };
  629. /*
  630. * residency list
  631. */
  632. struct amdgpu_bo_list {
  633. struct mutex lock;
  634. struct amdgpu_bo *gds_obj;
  635. struct amdgpu_bo *gws_obj;
  636. struct amdgpu_bo *oa_obj;
  637. unsigned first_userptr;
  638. unsigned num_entries;
  639. struct amdgpu_bo_list_entry *array;
  640. };
  641. struct amdgpu_bo_list *
  642. amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id);
  643. void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
  644. struct list_head *validated);
  645. void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
  646. void amdgpu_bo_list_free(struct amdgpu_bo_list *list);
  647. /*
  648. * GFX stuff
  649. */
  650. #include "clearstate_defs.h"
  651. struct amdgpu_rlc_funcs {
  652. void (*enter_safe_mode)(struct amdgpu_device *adev);
  653. void (*exit_safe_mode)(struct amdgpu_device *adev);
  654. };
  655. struct amdgpu_rlc {
  656. /* for power gating */
  657. struct amdgpu_bo *save_restore_obj;
  658. uint64_t save_restore_gpu_addr;
  659. volatile uint32_t *sr_ptr;
  660. const u32 *reg_list;
  661. u32 reg_list_size;
  662. /* for clear state */
  663. struct amdgpu_bo *clear_state_obj;
  664. uint64_t clear_state_gpu_addr;
  665. volatile uint32_t *cs_ptr;
  666. const struct cs_section_def *cs_data;
  667. u32 clear_state_size;
  668. /* for cp tables */
  669. struct amdgpu_bo *cp_table_obj;
  670. uint64_t cp_table_gpu_addr;
  671. volatile uint32_t *cp_table_ptr;
  672. u32 cp_table_size;
  673. /* safe mode for updating CG/PG state */
  674. bool in_safe_mode;
  675. const struct amdgpu_rlc_funcs *funcs;
  676. /* for firmware data */
  677. u32 save_and_restore_offset;
  678. u32 clear_state_descriptor_offset;
  679. u32 avail_scratch_ram_locations;
  680. u32 reg_restore_list_size;
  681. u32 reg_list_format_start;
  682. u32 reg_list_format_separate_start;
  683. u32 starting_offsets_start;
  684. u32 reg_list_format_size_bytes;
  685. u32 reg_list_size_bytes;
  686. u32 *register_list_format;
  687. u32 *register_restore;
  688. };
  689. struct amdgpu_mec {
  690. struct amdgpu_bo *hpd_eop_obj;
  691. u64 hpd_eop_gpu_addr;
  692. u32 num_pipe;
  693. u32 num_mec;
  694. u32 num_queue;
  695. void *mqd_backup[AMDGPU_MAX_COMPUTE_RINGS + 1];
  696. };
  697. struct amdgpu_kiq {
  698. u64 eop_gpu_addr;
  699. struct amdgpu_bo *eop_obj;
  700. struct amdgpu_ring ring;
  701. struct amdgpu_irq_src irq;
  702. };
  703. /*
  704. * GPU scratch registers structures, functions & helpers
  705. */
  706. struct amdgpu_scratch {
  707. unsigned num_reg;
  708. uint32_t reg_base;
  709. uint32_t free_mask;
  710. };
  711. /*
  712. * GFX configurations
  713. */
  714. #define AMDGPU_GFX_MAX_SE 4
  715. #define AMDGPU_GFX_MAX_SH_PER_SE 2
  716. struct amdgpu_rb_config {
  717. uint32_t rb_backend_disable;
  718. uint32_t user_rb_backend_disable;
  719. uint32_t raster_config;
  720. uint32_t raster_config_1;
  721. };
  722. struct amdgpu_gfx_config {
  723. unsigned max_shader_engines;
  724. unsigned max_tile_pipes;
  725. unsigned max_cu_per_sh;
  726. unsigned max_sh_per_se;
  727. unsigned max_backends_per_se;
  728. unsigned max_texture_channel_caches;
  729. unsigned max_gprs;
  730. unsigned max_gs_threads;
  731. unsigned max_hw_contexts;
  732. unsigned sc_prim_fifo_size_frontend;
  733. unsigned sc_prim_fifo_size_backend;
  734. unsigned sc_hiz_tile_fifo_size;
  735. unsigned sc_earlyz_tile_fifo_size;
  736. unsigned num_tile_pipes;
  737. unsigned backend_enable_mask;
  738. unsigned mem_max_burst_length_bytes;
  739. unsigned mem_row_size_in_kb;
  740. unsigned shader_engine_tile_size;
  741. unsigned num_gpus;
  742. unsigned multi_gpu_tile_size;
  743. unsigned mc_arb_ramcfg;
  744. unsigned gb_addr_config;
  745. unsigned num_rbs;
  746. uint32_t tile_mode_array[32];
  747. uint32_t macrotile_mode_array[16];
  748. struct amdgpu_rb_config rb_config[AMDGPU_GFX_MAX_SE][AMDGPU_GFX_MAX_SH_PER_SE];
  749. /* gfx configure feature */
  750. uint32_t double_offchip_lds_buf;
  751. };
  752. struct amdgpu_cu_info {
  753. uint32_t number; /* total active CU number */
  754. uint32_t ao_cu_mask;
  755. uint32_t bitmap[4][4];
  756. };
  757. struct amdgpu_gfx_funcs {
  758. /* get the gpu clock counter */
  759. uint64_t (*get_gpu_clock_counter)(struct amdgpu_device *adev);
  760. void (*select_se_sh)(struct amdgpu_device *adev, u32 se_num, u32 sh_num, u32 instance);
  761. void (*read_wave_data)(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t *dst, int *no_fields);
  762. void (*read_wave_vgprs)(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t thread, uint32_t start, uint32_t size, uint32_t *dst);
  763. void (*read_wave_sgprs)(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t start, uint32_t size, uint32_t *dst);
  764. };
  765. struct amdgpu_gfx {
  766. struct mutex gpu_clock_mutex;
  767. struct amdgpu_gfx_config config;
  768. struct amdgpu_rlc rlc;
  769. struct amdgpu_mec mec;
  770. struct amdgpu_kiq kiq;
  771. struct amdgpu_scratch scratch;
  772. const struct firmware *me_fw; /* ME firmware */
  773. uint32_t me_fw_version;
  774. const struct firmware *pfp_fw; /* PFP firmware */
  775. uint32_t pfp_fw_version;
  776. const struct firmware *ce_fw; /* CE firmware */
  777. uint32_t ce_fw_version;
  778. const struct firmware *rlc_fw; /* RLC firmware */
  779. uint32_t rlc_fw_version;
  780. const struct firmware *mec_fw; /* MEC firmware */
  781. uint32_t mec_fw_version;
  782. const struct firmware *mec2_fw; /* MEC2 firmware */
  783. uint32_t mec2_fw_version;
  784. uint32_t me_feature_version;
  785. uint32_t ce_feature_version;
  786. uint32_t pfp_feature_version;
  787. uint32_t rlc_feature_version;
  788. uint32_t mec_feature_version;
  789. uint32_t mec2_feature_version;
  790. struct amdgpu_ring gfx_ring[AMDGPU_MAX_GFX_RINGS];
  791. unsigned num_gfx_rings;
  792. struct amdgpu_ring compute_ring[AMDGPU_MAX_COMPUTE_RINGS];
  793. unsigned num_compute_rings;
  794. struct amdgpu_irq_src eop_irq;
  795. struct amdgpu_irq_src priv_reg_irq;
  796. struct amdgpu_irq_src priv_inst_irq;
  797. /* gfx status */
  798. uint32_t gfx_current_status;
  799. /* ce ram size*/
  800. unsigned ce_ram_size;
  801. struct amdgpu_cu_info cu_info;
  802. const struct amdgpu_gfx_funcs *funcs;
  803. /* reset mask */
  804. uint32_t grbm_soft_reset;
  805. uint32_t srbm_soft_reset;
  806. bool in_reset;
  807. };
  808. int amdgpu_ib_get(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  809. unsigned size, struct amdgpu_ib *ib);
  810. void amdgpu_ib_free(struct amdgpu_device *adev, struct amdgpu_ib *ib,
  811. struct dma_fence *f);
  812. int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned num_ibs,
  813. struct amdgpu_ib *ibs, struct amdgpu_job *job,
  814. struct dma_fence **f);
  815. int amdgpu_ib_pool_init(struct amdgpu_device *adev);
  816. void amdgpu_ib_pool_fini(struct amdgpu_device *adev);
  817. int amdgpu_ib_ring_tests(struct amdgpu_device *adev);
  818. /*
  819. * CS.
  820. */
  821. struct amdgpu_cs_chunk {
  822. uint32_t chunk_id;
  823. uint32_t length_dw;
  824. void *kdata;
  825. };
  826. struct amdgpu_cs_parser {
  827. struct amdgpu_device *adev;
  828. struct drm_file *filp;
  829. struct amdgpu_ctx *ctx;
  830. /* chunks */
  831. unsigned nchunks;
  832. struct amdgpu_cs_chunk *chunks;
  833. /* scheduler job object */
  834. struct amdgpu_job *job;
  835. /* buffer objects */
  836. struct ww_acquire_ctx ticket;
  837. struct amdgpu_bo_list *bo_list;
  838. struct amdgpu_bo_list_entry vm_pd;
  839. struct list_head validated;
  840. struct dma_fence *fence;
  841. uint64_t bytes_moved_threshold;
  842. uint64_t bytes_moved;
  843. struct amdgpu_bo_list_entry *evictable;
  844. /* user fence */
  845. struct amdgpu_bo_list_entry uf_entry;
  846. };
  847. #define AMDGPU_PREAMBLE_IB_PRESENT (1 << 0) /* bit set means command submit involves a preamble IB */
  848. #define AMDGPU_PREAMBLE_IB_PRESENT_FIRST (1 << 1) /* bit set means preamble IB is first presented in belonging context */
  849. #define AMDGPU_HAVE_CTX_SWITCH (1 << 2) /* bit set means context switch occured */
  850. #define AMDGPU_VM_DOMAIN (1 << 3) /* bit set means in virtual memory context */
  851. struct amdgpu_job {
  852. struct amd_sched_job base;
  853. struct amdgpu_device *adev;
  854. struct amdgpu_vm *vm;
  855. struct amdgpu_ring *ring;
  856. struct amdgpu_sync sync;
  857. struct amdgpu_ib *ibs;
  858. struct dma_fence *fence; /* the hw fence */
  859. uint32_t preamble_status;
  860. uint32_t num_ibs;
  861. void *owner;
  862. uint64_t fence_ctx; /* the fence_context this job uses */
  863. bool vm_needs_flush;
  864. unsigned vm_id;
  865. uint64_t vm_pd_addr;
  866. uint32_t gds_base, gds_size;
  867. uint32_t gws_base, gws_size;
  868. uint32_t oa_base, oa_size;
  869. /* user fence handling */
  870. uint64_t uf_addr;
  871. uint64_t uf_sequence;
  872. };
  873. #define to_amdgpu_job(sched_job) \
  874. container_of((sched_job), struct amdgpu_job, base)
  875. static inline u32 amdgpu_get_ib_value(struct amdgpu_cs_parser *p,
  876. uint32_t ib_idx, int idx)
  877. {
  878. return p->job->ibs[ib_idx].ptr[idx];
  879. }
  880. static inline void amdgpu_set_ib_value(struct amdgpu_cs_parser *p,
  881. uint32_t ib_idx, int idx,
  882. uint32_t value)
  883. {
  884. p->job->ibs[ib_idx].ptr[idx] = value;
  885. }
  886. /*
  887. * Writeback
  888. */
  889. #define AMDGPU_MAX_WB 1024 /* Reserve at most 1024 WB slots for amdgpu-owned rings. */
  890. struct amdgpu_wb {
  891. struct amdgpu_bo *wb_obj;
  892. volatile uint32_t *wb;
  893. uint64_t gpu_addr;
  894. u32 num_wb; /* Number of wb slots actually reserved for amdgpu. */
  895. unsigned long used[DIV_ROUND_UP(AMDGPU_MAX_WB, BITS_PER_LONG)];
  896. };
  897. int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb);
  898. void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb);
  899. int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb);
  900. void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb);
  901. void amdgpu_get_pcie_info(struct amdgpu_device *adev);
  902. /*
  903. * UVD
  904. */
  905. #define AMDGPU_DEFAULT_UVD_HANDLES 10
  906. #define AMDGPU_MAX_UVD_HANDLES 40
  907. #define AMDGPU_UVD_STACK_SIZE (200*1024)
  908. #define AMDGPU_UVD_HEAP_SIZE (256*1024)
  909. #define AMDGPU_UVD_SESSION_SIZE (50*1024)
  910. #define AMDGPU_UVD_FIRMWARE_OFFSET 256
  911. struct amdgpu_uvd {
  912. struct amdgpu_bo *vcpu_bo;
  913. void *cpu_addr;
  914. uint64_t gpu_addr;
  915. unsigned fw_version;
  916. void *saved_bo;
  917. unsigned max_handles;
  918. atomic_t handles[AMDGPU_MAX_UVD_HANDLES];
  919. struct drm_file *filp[AMDGPU_MAX_UVD_HANDLES];
  920. struct delayed_work idle_work;
  921. const struct firmware *fw; /* UVD firmware */
  922. struct amdgpu_ring ring;
  923. struct amdgpu_irq_src irq;
  924. bool address_64_bit;
  925. bool use_ctx_buf;
  926. struct amd_sched_entity entity;
  927. uint32_t srbm_soft_reset;
  928. };
  929. /*
  930. * VCE
  931. */
  932. #define AMDGPU_MAX_VCE_HANDLES 16
  933. #define AMDGPU_VCE_FIRMWARE_OFFSET 256
  934. #define AMDGPU_VCE_HARVEST_VCE0 (1 << 0)
  935. #define AMDGPU_VCE_HARVEST_VCE1 (1 << 1)
  936. struct amdgpu_vce {
  937. struct amdgpu_bo *vcpu_bo;
  938. uint64_t gpu_addr;
  939. unsigned fw_version;
  940. unsigned fb_version;
  941. atomic_t handles[AMDGPU_MAX_VCE_HANDLES];
  942. struct drm_file *filp[AMDGPU_MAX_VCE_HANDLES];
  943. uint32_t img_size[AMDGPU_MAX_VCE_HANDLES];
  944. struct delayed_work idle_work;
  945. struct mutex idle_mutex;
  946. const struct firmware *fw; /* VCE firmware */
  947. struct amdgpu_ring ring[AMDGPU_MAX_VCE_RINGS];
  948. struct amdgpu_irq_src irq;
  949. unsigned harvest_config;
  950. struct amd_sched_entity entity;
  951. uint32_t srbm_soft_reset;
  952. unsigned num_rings;
  953. };
  954. /*
  955. * SDMA
  956. */
  957. struct amdgpu_sdma_instance {
  958. /* SDMA firmware */
  959. const struct firmware *fw;
  960. uint32_t fw_version;
  961. uint32_t feature_version;
  962. struct amdgpu_ring ring;
  963. bool burst_nop;
  964. };
  965. struct amdgpu_sdma {
  966. struct amdgpu_sdma_instance instance[AMDGPU_MAX_SDMA_INSTANCES];
  967. #ifdef CONFIG_DRM_AMDGPU_SI
  968. //SI DMA has a difference trap irq number for the second engine
  969. struct amdgpu_irq_src trap_irq_1;
  970. #endif
  971. struct amdgpu_irq_src trap_irq;
  972. struct amdgpu_irq_src illegal_inst_irq;
  973. int num_instances;
  974. uint32_t srbm_soft_reset;
  975. };
  976. /*
  977. * Firmware
  978. */
  979. struct amdgpu_firmware {
  980. struct amdgpu_firmware_info ucode[AMDGPU_UCODE_ID_MAXIMUM];
  981. bool smu_load;
  982. struct amdgpu_bo *fw_buf;
  983. unsigned int fw_size;
  984. };
  985. /*
  986. * Benchmarking
  987. */
  988. void amdgpu_benchmark(struct amdgpu_device *adev, int test_number);
  989. /*
  990. * Testing
  991. */
  992. void amdgpu_test_moves(struct amdgpu_device *adev);
  993. /*
  994. * MMU Notifier
  995. */
  996. #if defined(CONFIG_MMU_NOTIFIER)
  997. int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr);
  998. void amdgpu_mn_unregister(struct amdgpu_bo *bo);
  999. #else
  1000. static inline int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
  1001. {
  1002. return -ENODEV;
  1003. }
  1004. static inline void amdgpu_mn_unregister(struct amdgpu_bo *bo) {}
  1005. #endif
  1006. /*
  1007. * Debugfs
  1008. */
  1009. struct amdgpu_debugfs {
  1010. const struct drm_info_list *files;
  1011. unsigned num_files;
  1012. };
  1013. int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
  1014. const struct drm_info_list *files,
  1015. unsigned nfiles);
  1016. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev);
  1017. #if defined(CONFIG_DEBUG_FS)
  1018. int amdgpu_debugfs_init(struct drm_minor *minor);
  1019. #endif
  1020. int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev);
  1021. /*
  1022. * amdgpu smumgr functions
  1023. */
  1024. struct amdgpu_smumgr_funcs {
  1025. int (*check_fw_load_finish)(struct amdgpu_device *adev, uint32_t fwtype);
  1026. int (*request_smu_load_fw)(struct amdgpu_device *adev);
  1027. int (*request_smu_specific_fw)(struct amdgpu_device *adev, uint32_t fwtype);
  1028. };
  1029. /*
  1030. * amdgpu smumgr
  1031. */
  1032. struct amdgpu_smumgr {
  1033. struct amdgpu_bo *toc_buf;
  1034. struct amdgpu_bo *smu_buf;
  1035. /* asic priv smu data */
  1036. void *priv;
  1037. spinlock_t smu_lock;
  1038. /* smumgr functions */
  1039. const struct amdgpu_smumgr_funcs *smumgr_funcs;
  1040. /* ucode loading complete flag */
  1041. uint32_t fw_flags;
  1042. };
  1043. /*
  1044. * ASIC specific register table accessible by UMD
  1045. */
  1046. struct amdgpu_allowed_register_entry {
  1047. uint32_t reg_offset;
  1048. bool untouched;
  1049. bool grbm_indexed;
  1050. };
  1051. /*
  1052. * ASIC specific functions.
  1053. */
  1054. struct amdgpu_asic_funcs {
  1055. bool (*read_disabled_bios)(struct amdgpu_device *adev);
  1056. bool (*read_bios_from_rom)(struct amdgpu_device *adev,
  1057. u8 *bios, u32 length_bytes);
  1058. int (*read_register)(struct amdgpu_device *adev, u32 se_num,
  1059. u32 sh_num, u32 reg_offset, u32 *value);
  1060. void (*set_vga_state)(struct amdgpu_device *adev, bool state);
  1061. int (*reset)(struct amdgpu_device *adev);
  1062. /* get the reference clock */
  1063. u32 (*get_xclk)(struct amdgpu_device *adev);
  1064. /* MM block clocks */
  1065. int (*set_uvd_clocks)(struct amdgpu_device *adev, u32 vclk, u32 dclk);
  1066. int (*set_vce_clocks)(struct amdgpu_device *adev, u32 evclk, u32 ecclk);
  1067. /* static power management */
  1068. int (*get_pcie_lanes)(struct amdgpu_device *adev);
  1069. void (*set_pcie_lanes)(struct amdgpu_device *adev, int lanes);
  1070. };
  1071. /*
  1072. * IOCTL.
  1073. */
  1074. int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
  1075. struct drm_file *filp);
  1076. int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
  1077. struct drm_file *filp);
  1078. int amdgpu_gem_info_ioctl(struct drm_device *dev, void *data,
  1079. struct drm_file *filp);
  1080. int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
  1081. struct drm_file *filp);
  1082. int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1083. struct drm_file *filp);
  1084. int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
  1085. struct drm_file *filp);
  1086. int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
  1087. struct drm_file *filp);
  1088. int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
  1089. struct drm_file *filp);
  1090. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  1091. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  1092. int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
  1093. struct drm_file *filp);
  1094. int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
  1095. struct drm_file *filp);
  1096. /* VRAM scratch page for HDP bug, default vram page */
  1097. struct amdgpu_vram_scratch {
  1098. struct amdgpu_bo *robj;
  1099. volatile uint32_t *ptr;
  1100. u64 gpu_addr;
  1101. };
  1102. /*
  1103. * ACPI
  1104. */
  1105. struct amdgpu_atif_notification_cfg {
  1106. bool enabled;
  1107. int command_code;
  1108. };
  1109. struct amdgpu_atif_notifications {
  1110. bool display_switch;
  1111. bool expansion_mode_change;
  1112. bool thermal_state;
  1113. bool forced_power_state;
  1114. bool system_power_state;
  1115. bool display_conf_change;
  1116. bool px_gfx_switch;
  1117. bool brightness_change;
  1118. bool dgpu_display_event;
  1119. };
  1120. struct amdgpu_atif_functions {
  1121. bool system_params;
  1122. bool sbios_requests;
  1123. bool select_active_disp;
  1124. bool lid_state;
  1125. bool get_tv_standard;
  1126. bool set_tv_standard;
  1127. bool get_panel_expansion_mode;
  1128. bool set_panel_expansion_mode;
  1129. bool temperature_change;
  1130. bool graphics_device_types;
  1131. };
  1132. struct amdgpu_atif {
  1133. struct amdgpu_atif_notifications notifications;
  1134. struct amdgpu_atif_functions functions;
  1135. struct amdgpu_atif_notification_cfg notification_cfg;
  1136. struct amdgpu_encoder *encoder_for_bl;
  1137. };
  1138. struct amdgpu_atcs_functions {
  1139. bool get_ext_state;
  1140. bool pcie_perf_req;
  1141. bool pcie_dev_rdy;
  1142. bool pcie_bus_width;
  1143. };
  1144. struct amdgpu_atcs {
  1145. struct amdgpu_atcs_functions functions;
  1146. };
  1147. /*
  1148. * CGS
  1149. */
  1150. struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev);
  1151. void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device);
  1152. /*
  1153. * Core structure, functions and helpers.
  1154. */
  1155. typedef uint32_t (*amdgpu_rreg_t)(struct amdgpu_device*, uint32_t);
  1156. typedef void (*amdgpu_wreg_t)(struct amdgpu_device*, uint32_t, uint32_t);
  1157. typedef uint32_t (*amdgpu_block_rreg_t)(struct amdgpu_device*, uint32_t, uint32_t);
  1158. typedef void (*amdgpu_block_wreg_t)(struct amdgpu_device*, uint32_t, uint32_t, uint32_t);
  1159. struct amdgpu_device {
  1160. struct device *dev;
  1161. struct drm_device *ddev;
  1162. struct pci_dev *pdev;
  1163. #ifdef CONFIG_DRM_AMD_ACP
  1164. struct amdgpu_acp acp;
  1165. #endif
  1166. /* ASIC */
  1167. enum amd_asic_type asic_type;
  1168. uint32_t family;
  1169. uint32_t rev_id;
  1170. uint32_t external_rev_id;
  1171. unsigned long flags;
  1172. int usec_timeout;
  1173. const struct amdgpu_asic_funcs *asic_funcs;
  1174. bool shutdown;
  1175. bool need_dma32;
  1176. bool accel_working;
  1177. struct work_struct reset_work;
  1178. struct notifier_block acpi_nb;
  1179. struct amdgpu_i2c_chan *i2c_bus[AMDGPU_MAX_I2C_BUS];
  1180. struct amdgpu_debugfs debugfs[AMDGPU_DEBUGFS_MAX_COMPONENTS];
  1181. unsigned debugfs_count;
  1182. #if defined(CONFIG_DEBUG_FS)
  1183. struct dentry *debugfs_regs[AMDGPU_DEBUGFS_MAX_COMPONENTS];
  1184. #endif
  1185. struct amdgpu_atif atif;
  1186. struct amdgpu_atcs atcs;
  1187. struct mutex srbm_mutex;
  1188. /* GRBM index mutex. Protects concurrent access to GRBM index */
  1189. struct mutex grbm_idx_mutex;
  1190. struct dev_pm_domain vga_pm_domain;
  1191. bool have_disp_power_ref;
  1192. /* BIOS */
  1193. uint8_t *bios;
  1194. uint32_t bios_size;
  1195. struct amdgpu_bo *stollen_vga_memory;
  1196. uint32_t bios_scratch[AMDGPU_BIOS_NUM_SCRATCH];
  1197. /* Register/doorbell mmio */
  1198. resource_size_t rmmio_base;
  1199. resource_size_t rmmio_size;
  1200. void __iomem *rmmio;
  1201. /* protects concurrent MM_INDEX/DATA based register access */
  1202. spinlock_t mmio_idx_lock;
  1203. /* protects concurrent SMC based register access */
  1204. spinlock_t smc_idx_lock;
  1205. amdgpu_rreg_t smc_rreg;
  1206. amdgpu_wreg_t smc_wreg;
  1207. /* protects concurrent PCIE register access */
  1208. spinlock_t pcie_idx_lock;
  1209. amdgpu_rreg_t pcie_rreg;
  1210. amdgpu_wreg_t pcie_wreg;
  1211. amdgpu_rreg_t pciep_rreg;
  1212. amdgpu_wreg_t pciep_wreg;
  1213. /* protects concurrent UVD register access */
  1214. spinlock_t uvd_ctx_idx_lock;
  1215. amdgpu_rreg_t uvd_ctx_rreg;
  1216. amdgpu_wreg_t uvd_ctx_wreg;
  1217. /* protects concurrent DIDT register access */
  1218. spinlock_t didt_idx_lock;
  1219. amdgpu_rreg_t didt_rreg;
  1220. amdgpu_wreg_t didt_wreg;
  1221. /* protects concurrent gc_cac register access */
  1222. spinlock_t gc_cac_idx_lock;
  1223. amdgpu_rreg_t gc_cac_rreg;
  1224. amdgpu_wreg_t gc_cac_wreg;
  1225. /* protects concurrent ENDPOINT (audio) register access */
  1226. spinlock_t audio_endpt_idx_lock;
  1227. amdgpu_block_rreg_t audio_endpt_rreg;
  1228. amdgpu_block_wreg_t audio_endpt_wreg;
  1229. void __iomem *rio_mem;
  1230. resource_size_t rio_mem_size;
  1231. struct amdgpu_doorbell doorbell;
  1232. /* clock/pll info */
  1233. struct amdgpu_clock clock;
  1234. /* MC */
  1235. struct amdgpu_mc mc;
  1236. struct amdgpu_gart gart;
  1237. struct amdgpu_dummy_page dummy_page;
  1238. struct amdgpu_vm_manager vm_manager;
  1239. /* memory management */
  1240. struct amdgpu_mman mman;
  1241. struct amdgpu_vram_scratch vram_scratch;
  1242. struct amdgpu_wb wb;
  1243. atomic64_t vram_usage;
  1244. atomic64_t vram_vis_usage;
  1245. atomic64_t gtt_usage;
  1246. atomic64_t num_bytes_moved;
  1247. atomic64_t num_evictions;
  1248. atomic_t gpu_reset_counter;
  1249. /* data for buffer migration throttling */
  1250. struct {
  1251. spinlock_t lock;
  1252. s64 last_update_us;
  1253. s64 accum_us; /* accumulated microseconds */
  1254. u32 log2_max_MBps;
  1255. } mm_stats;
  1256. /* display */
  1257. bool enable_virtual_display;
  1258. struct amdgpu_mode_info mode_info;
  1259. struct work_struct hotplug_work;
  1260. struct amdgpu_irq_src crtc_irq;
  1261. struct amdgpu_irq_src pageflip_irq;
  1262. struct amdgpu_irq_src hpd_irq;
  1263. /* rings */
  1264. u64 fence_context;
  1265. unsigned num_rings;
  1266. struct amdgpu_ring *rings[AMDGPU_MAX_RINGS];
  1267. bool ib_pool_ready;
  1268. struct amdgpu_sa_manager ring_tmp_bo;
  1269. /* interrupts */
  1270. struct amdgpu_irq irq;
  1271. /* powerplay */
  1272. struct amd_powerplay powerplay;
  1273. bool pp_enabled;
  1274. bool pp_force_state_enabled;
  1275. /* dpm */
  1276. struct amdgpu_pm pm;
  1277. u32 cg_flags;
  1278. u32 pg_flags;
  1279. /* amdgpu smumgr */
  1280. struct amdgpu_smumgr smu;
  1281. /* gfx */
  1282. struct amdgpu_gfx gfx;
  1283. /* sdma */
  1284. struct amdgpu_sdma sdma;
  1285. /* uvd */
  1286. struct amdgpu_uvd uvd;
  1287. /* vce */
  1288. struct amdgpu_vce vce;
  1289. /* firmwares */
  1290. struct amdgpu_firmware firmware;
  1291. /* GDS */
  1292. struct amdgpu_gds gds;
  1293. struct amdgpu_ip_block ip_blocks[AMDGPU_MAX_IP_NUM];
  1294. int num_ip_blocks;
  1295. struct mutex mn_lock;
  1296. DECLARE_HASHTABLE(mn_hash, 7);
  1297. /* tracking pinned memory */
  1298. u64 vram_pin_size;
  1299. u64 invisible_pin_size;
  1300. u64 gart_pin_size;
  1301. /* amdkfd interface */
  1302. struct kfd_dev *kfd;
  1303. struct amdgpu_virt virt;
  1304. /* link all shadow bo */
  1305. struct list_head shadow_list;
  1306. struct mutex shadow_list_lock;
  1307. /* link all gtt */
  1308. spinlock_t gtt_list_lock;
  1309. struct list_head gtt_list;
  1310. /* record hw reset is performed */
  1311. bool has_hw_reset;
  1312. };
  1313. static inline struct amdgpu_device *amdgpu_ttm_adev(struct ttm_bo_device *bdev)
  1314. {
  1315. return container_of(bdev, struct amdgpu_device, mman.bdev);
  1316. }
  1317. bool amdgpu_device_is_px(struct drm_device *dev);
  1318. int amdgpu_device_init(struct amdgpu_device *adev,
  1319. struct drm_device *ddev,
  1320. struct pci_dev *pdev,
  1321. uint32_t flags);
  1322. void amdgpu_device_fini(struct amdgpu_device *adev);
  1323. int amdgpu_gpu_wait_for_idle(struct amdgpu_device *adev);
  1324. uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
  1325. uint32_t acc_flags);
  1326. void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
  1327. uint32_t acc_flags);
  1328. u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg);
  1329. void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v);
  1330. u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index);
  1331. void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v);
  1332. /*
  1333. * Registers read & write functions.
  1334. */
  1335. #define AMDGPU_REGS_IDX (1<<0)
  1336. #define AMDGPU_REGS_NO_KIQ (1<<1)
  1337. #define RREG32_NO_KIQ(reg) amdgpu_mm_rreg(adev, (reg), AMDGPU_REGS_NO_KIQ)
  1338. #define WREG32_NO_KIQ(reg, v) amdgpu_mm_wreg(adev, (reg), (v), AMDGPU_REGS_NO_KIQ)
  1339. #define RREG32(reg) amdgpu_mm_rreg(adev, (reg), 0)
  1340. #define RREG32_IDX(reg) amdgpu_mm_rreg(adev, (reg), AMDGPU_REGS_IDX)
  1341. #define DREG32(reg) printk(KERN_INFO "REGISTER: " #reg " : 0x%08X\n", amdgpu_mm_rreg(adev, (reg), 0))
  1342. #define WREG32(reg, v) amdgpu_mm_wreg(adev, (reg), (v), 0)
  1343. #define WREG32_IDX(reg, v) amdgpu_mm_wreg(adev, (reg), (v), AMDGPU_REGS_IDX)
  1344. #define REG_SET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
  1345. #define REG_GET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
  1346. #define RREG32_PCIE(reg) adev->pcie_rreg(adev, (reg))
  1347. #define WREG32_PCIE(reg, v) adev->pcie_wreg(adev, (reg), (v))
  1348. #define RREG32_PCIE_PORT(reg) adev->pciep_rreg(adev, (reg))
  1349. #define WREG32_PCIE_PORT(reg, v) adev->pciep_wreg(adev, (reg), (v))
  1350. #define RREG32_SMC(reg) adev->smc_rreg(adev, (reg))
  1351. #define WREG32_SMC(reg, v) adev->smc_wreg(adev, (reg), (v))
  1352. #define RREG32_UVD_CTX(reg) adev->uvd_ctx_rreg(adev, (reg))
  1353. #define WREG32_UVD_CTX(reg, v) adev->uvd_ctx_wreg(adev, (reg), (v))
  1354. #define RREG32_DIDT(reg) adev->didt_rreg(adev, (reg))
  1355. #define WREG32_DIDT(reg, v) adev->didt_wreg(adev, (reg), (v))
  1356. #define RREG32_GC_CAC(reg) adev->gc_cac_rreg(adev, (reg))
  1357. #define WREG32_GC_CAC(reg, v) adev->gc_cac_wreg(adev, (reg), (v))
  1358. #define RREG32_AUDIO_ENDPT(block, reg) adev->audio_endpt_rreg(adev, (block), (reg))
  1359. #define WREG32_AUDIO_ENDPT(block, reg, v) adev->audio_endpt_wreg(adev, (block), (reg), (v))
  1360. #define WREG32_P(reg, val, mask) \
  1361. do { \
  1362. uint32_t tmp_ = RREG32(reg); \
  1363. tmp_ &= (mask); \
  1364. tmp_ |= ((val) & ~(mask)); \
  1365. WREG32(reg, tmp_); \
  1366. } while (0)
  1367. #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
  1368. #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
  1369. #define WREG32_PLL_P(reg, val, mask) \
  1370. do { \
  1371. uint32_t tmp_ = RREG32_PLL(reg); \
  1372. tmp_ &= (mask); \
  1373. tmp_ |= ((val) & ~(mask)); \
  1374. WREG32_PLL(reg, tmp_); \
  1375. } while (0)
  1376. #define DREG32_SYS(sqf, adev, reg) seq_printf((sqf), #reg " : 0x%08X\n", amdgpu_mm_rreg((adev), (reg), false))
  1377. #define RREG32_IO(reg) amdgpu_io_rreg(adev, (reg))
  1378. #define WREG32_IO(reg, v) amdgpu_io_wreg(adev, (reg), (v))
  1379. #define RDOORBELL32(index) amdgpu_mm_rdoorbell(adev, (index))
  1380. #define WDOORBELL32(index, v) amdgpu_mm_wdoorbell(adev, (index), (v))
  1381. #define REG_FIELD_SHIFT(reg, field) reg##__##field##__SHIFT
  1382. #define REG_FIELD_MASK(reg, field) reg##__##field##_MASK
  1383. #define REG_SET_FIELD(orig_val, reg, field, field_val) \
  1384. (((orig_val) & ~REG_FIELD_MASK(reg, field)) | \
  1385. (REG_FIELD_MASK(reg, field) & ((field_val) << REG_FIELD_SHIFT(reg, field))))
  1386. #define REG_GET_FIELD(value, reg, field) \
  1387. (((value) & REG_FIELD_MASK(reg, field)) >> REG_FIELD_SHIFT(reg, field))
  1388. #define WREG32_FIELD(reg, field, val) \
  1389. WREG32(mm##reg, (RREG32(mm##reg) & ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field))
  1390. /*
  1391. * BIOS helpers.
  1392. */
  1393. #define RBIOS8(i) (adev->bios[i])
  1394. #define RBIOS16(i) (RBIOS8(i) | (RBIOS8((i)+1) << 8))
  1395. #define RBIOS32(i) ((RBIOS16(i)) | (RBIOS16((i)+2) << 16))
  1396. /*
  1397. * RING helpers.
  1398. */
  1399. static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
  1400. {
  1401. if (ring->count_dw <= 0)
  1402. DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
  1403. ring->ring[ring->wptr++ & ring->buf_mask] = v;
  1404. ring->wptr &= ring->ptr_mask;
  1405. ring->count_dw--;
  1406. }
  1407. static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring, void *src, int count_dw)
  1408. {
  1409. unsigned occupied, chunk1, chunk2;
  1410. void *dst;
  1411. if (ring->count_dw < count_dw) {
  1412. DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
  1413. } else {
  1414. occupied = ring->wptr & ring->ptr_mask;
  1415. dst = (void *)&ring->ring[occupied];
  1416. chunk1 = ring->ptr_mask + 1 - occupied;
  1417. chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
  1418. chunk2 = count_dw - chunk1;
  1419. chunk1 <<= 2;
  1420. chunk2 <<= 2;
  1421. if (chunk1)
  1422. memcpy(dst, src, chunk1);
  1423. if (chunk2) {
  1424. src += chunk1;
  1425. dst = (void *)ring->ring;
  1426. memcpy(dst, src, chunk2);
  1427. }
  1428. ring->wptr += count_dw;
  1429. ring->wptr &= ring->ptr_mask;
  1430. ring->count_dw -= count_dw;
  1431. }
  1432. }
  1433. static inline struct amdgpu_sdma_instance *
  1434. amdgpu_get_sdma_instance(struct amdgpu_ring *ring)
  1435. {
  1436. struct amdgpu_device *adev = ring->adev;
  1437. int i;
  1438. for (i = 0; i < adev->sdma.num_instances; i++)
  1439. if (&adev->sdma.instance[i].ring == ring)
  1440. break;
  1441. if (i < AMDGPU_MAX_SDMA_INSTANCES)
  1442. return &adev->sdma.instance[i];
  1443. else
  1444. return NULL;
  1445. }
  1446. /*
  1447. * ASICs macro.
  1448. */
  1449. #define amdgpu_asic_set_vga_state(adev, state) (adev)->asic_funcs->set_vga_state((adev), (state))
  1450. #define amdgpu_asic_reset(adev) (adev)->asic_funcs->reset((adev))
  1451. #define amdgpu_asic_get_xclk(adev) (adev)->asic_funcs->get_xclk((adev))
  1452. #define amdgpu_asic_set_uvd_clocks(adev, v, d) (adev)->asic_funcs->set_uvd_clocks((adev), (v), (d))
  1453. #define amdgpu_asic_set_vce_clocks(adev, ev, ec) (adev)->asic_funcs->set_vce_clocks((adev), (ev), (ec))
  1454. #define amdgpu_get_pcie_lanes(adev) (adev)->asic_funcs->get_pcie_lanes((adev))
  1455. #define amdgpu_set_pcie_lanes(adev, l) (adev)->asic_funcs->set_pcie_lanes((adev), (l))
  1456. #define amdgpu_asic_get_gpu_clock_counter(adev) (adev)->asic_funcs->get_gpu_clock_counter((adev))
  1457. #define amdgpu_asic_read_disabled_bios(adev) (adev)->asic_funcs->read_disabled_bios((adev))
  1458. #define amdgpu_asic_read_bios_from_rom(adev, b, l) (adev)->asic_funcs->read_bios_from_rom((adev), (b), (l))
  1459. #define amdgpu_asic_read_register(adev, se, sh, offset, v)((adev)->asic_funcs->read_register((adev), (se), (sh), (offset), (v)))
  1460. #define amdgpu_gart_flush_gpu_tlb(adev, vmid) (adev)->gart.gart_funcs->flush_gpu_tlb((adev), (vmid))
  1461. #define amdgpu_gart_set_pte_pde(adev, pt, idx, addr, flags) (adev)->gart.gart_funcs->set_pte_pde((adev), (pt), (idx), (addr), (flags))
  1462. #define amdgpu_vm_copy_pte(adev, ib, pe, src, count) ((adev)->vm_manager.vm_pte_funcs->copy_pte((ib), (pe), (src), (count)))
  1463. #define amdgpu_vm_write_pte(adev, ib, pe, value, count, incr) ((adev)->vm_manager.vm_pte_funcs->write_pte((ib), (pe), (value), (count), (incr)))
  1464. #define amdgpu_vm_set_pte_pde(adev, ib, pe, addr, count, incr, flags) ((adev)->vm_manager.vm_pte_funcs->set_pte_pde((ib), (pe), (addr), (count), (incr), (flags)))
  1465. #define amdgpu_ring_parse_cs(r, p, ib) ((r)->funcs->parse_cs((p), (ib)))
  1466. #define amdgpu_ring_test_ring(r) (r)->funcs->test_ring((r))
  1467. #define amdgpu_ring_test_ib(r, t) (r)->funcs->test_ib((r), (t))
  1468. #define amdgpu_ring_get_rptr(r) (r)->funcs->get_rptr((r))
  1469. #define amdgpu_ring_get_wptr(r) (r)->funcs->get_wptr((r))
  1470. #define amdgpu_ring_set_wptr(r) (r)->funcs->set_wptr((r))
  1471. #define amdgpu_ring_emit_ib(r, ib, vm_id, c) (r)->funcs->emit_ib((r), (ib), (vm_id), (c))
  1472. #define amdgpu_ring_emit_pipeline_sync(r) (r)->funcs->emit_pipeline_sync((r))
  1473. #define amdgpu_ring_emit_vm_flush(r, vmid, addr) (r)->funcs->emit_vm_flush((r), (vmid), (addr))
  1474. #define amdgpu_ring_emit_fence(r, addr, seq, flags) (r)->funcs->emit_fence((r), (addr), (seq), (flags))
  1475. #define amdgpu_ring_emit_gds_switch(r, v, db, ds, wb, ws, ab, as) (r)->funcs->emit_gds_switch((r), (v), (db), (ds), (wb), (ws), (ab), (as))
  1476. #define amdgpu_ring_emit_hdp_flush(r) (r)->funcs->emit_hdp_flush((r))
  1477. #define amdgpu_ring_emit_hdp_invalidate(r) (r)->funcs->emit_hdp_invalidate((r))
  1478. #define amdgpu_ring_emit_switch_buffer(r) (r)->funcs->emit_switch_buffer((r))
  1479. #define amdgpu_ring_emit_cntxcntl(r, d) (r)->funcs->emit_cntxcntl((r), (d))
  1480. #define amdgpu_ring_emit_rreg(r, d) (r)->funcs->emit_rreg((r), (d))
  1481. #define amdgpu_ring_emit_wreg(r, d, v) (r)->funcs->emit_wreg((r), (d), (v))
  1482. #define amdgpu_ring_pad_ib(r, ib) ((r)->funcs->pad_ib((r), (ib)))
  1483. #define amdgpu_ring_init_cond_exec(r) (r)->funcs->init_cond_exec((r))
  1484. #define amdgpu_ring_patch_cond_exec(r,o) (r)->funcs->patch_cond_exec((r),(o))
  1485. #define amdgpu_ih_get_wptr(adev) (adev)->irq.ih_funcs->get_wptr((adev))
  1486. #define amdgpu_ih_decode_iv(adev, iv) (adev)->irq.ih_funcs->decode_iv((adev), (iv))
  1487. #define amdgpu_ih_set_rptr(adev) (adev)->irq.ih_funcs->set_rptr((adev))
  1488. #define amdgpu_display_set_vga_render_state(adev, r) (adev)->mode_info.funcs->set_vga_render_state((adev), (r))
  1489. #define amdgpu_display_vblank_get_counter(adev, crtc) (adev)->mode_info.funcs->vblank_get_counter((adev), (crtc))
  1490. #define amdgpu_display_vblank_wait(adev, crtc) (adev)->mode_info.funcs->vblank_wait((adev), (crtc))
  1491. #define amdgpu_display_backlight_set_level(adev, e, l) (adev)->mode_info.funcs->backlight_set_level((e), (l))
  1492. #define amdgpu_display_backlight_get_level(adev, e) (adev)->mode_info.funcs->backlight_get_level((e))
  1493. #define amdgpu_display_hpd_sense(adev, h) (adev)->mode_info.funcs->hpd_sense((adev), (h))
  1494. #define amdgpu_display_hpd_set_polarity(adev, h) (adev)->mode_info.funcs->hpd_set_polarity((adev), (h))
  1495. #define amdgpu_display_hpd_get_gpio_reg(adev) (adev)->mode_info.funcs->hpd_get_gpio_reg((adev))
  1496. #define amdgpu_display_bandwidth_update(adev) (adev)->mode_info.funcs->bandwidth_update((adev))
  1497. #define amdgpu_display_page_flip(adev, crtc, base, async) (adev)->mode_info.funcs->page_flip((adev), (crtc), (base), (async))
  1498. #define amdgpu_display_page_flip_get_scanoutpos(adev, crtc, vbl, pos) (adev)->mode_info.funcs->page_flip_get_scanoutpos((adev), (crtc), (vbl), (pos))
  1499. #define amdgpu_display_add_encoder(adev, e, s, c) (adev)->mode_info.funcs->add_encoder((adev), (e), (s), (c))
  1500. #define amdgpu_display_add_connector(adev, ci, sd, ct, ib, coi, h, r) (adev)->mode_info.funcs->add_connector((adev), (ci), (sd), (ct), (ib), (coi), (h), (r))
  1501. #define amdgpu_display_stop_mc_access(adev, s) (adev)->mode_info.funcs->stop_mc_access((adev), (s))
  1502. #define amdgpu_display_resume_mc_access(adev, s) (adev)->mode_info.funcs->resume_mc_access((adev), (s))
  1503. #define amdgpu_emit_copy_buffer(adev, ib, s, d, b) (adev)->mman.buffer_funcs->emit_copy_buffer((ib), (s), (d), (b))
  1504. #define amdgpu_emit_fill_buffer(adev, ib, s, d, b) (adev)->mman.buffer_funcs->emit_fill_buffer((ib), (s), (d), (b))
  1505. #define amdgpu_gfx_get_gpu_clock_counter(adev) (adev)->gfx.funcs->get_gpu_clock_counter((adev))
  1506. #define amdgpu_gfx_select_se_sh(adev, se, sh, instance) (adev)->gfx.funcs->select_se_sh((adev), (se), (sh), (instance))
  1507. #define amdgpu_gds_switch(adev, r, v, d, w, a) (adev)->gds.funcs->patch_gds_switch((r), (v), (d), (w), (a))
  1508. /* Common functions */
  1509. int amdgpu_gpu_reset(struct amdgpu_device *adev);
  1510. bool amdgpu_need_backup(struct amdgpu_device *adev);
  1511. void amdgpu_pci_config_reset(struct amdgpu_device *adev);
  1512. bool amdgpu_need_post(struct amdgpu_device *adev);
  1513. void amdgpu_update_display_priority(struct amdgpu_device *adev);
  1514. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data);
  1515. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  1516. u32 ip_instance, u32 ring,
  1517. struct amdgpu_ring **out_ring);
  1518. void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes);
  1519. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain);
  1520. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo);
  1521. int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages);
  1522. int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
  1523. uint32_t flags);
  1524. bool amdgpu_ttm_tt_has_userptr(struct ttm_tt *ttm);
  1525. struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm);
  1526. bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
  1527. unsigned long end);
  1528. bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
  1529. int *last_invalidated);
  1530. bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
  1531. uint32_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
  1532. struct ttm_mem_reg *mem);
  1533. void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base);
  1534. void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc);
  1535. void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size);
  1536. int amdgpu_ttm_init(struct amdgpu_device *adev);
  1537. void amdgpu_ttm_fini(struct amdgpu_device *adev);
  1538. void amdgpu_program_register_sequence(struct amdgpu_device *adev,
  1539. const u32 *registers,
  1540. const u32 array_size);
  1541. bool amdgpu_device_is_px(struct drm_device *dev);
  1542. /* atpx handler */
  1543. #if defined(CONFIG_VGA_SWITCHEROO)
  1544. void amdgpu_register_atpx_handler(void);
  1545. void amdgpu_unregister_atpx_handler(void);
  1546. bool amdgpu_has_atpx_dgpu_power_cntl(void);
  1547. bool amdgpu_is_atpx_hybrid(void);
  1548. bool amdgpu_atpx_dgpu_req_power_for_displays(void);
  1549. #else
  1550. static inline void amdgpu_register_atpx_handler(void) {}
  1551. static inline void amdgpu_unregister_atpx_handler(void) {}
  1552. static inline bool amdgpu_has_atpx_dgpu_power_cntl(void) { return false; }
  1553. static inline bool amdgpu_is_atpx_hybrid(void) { return false; }
  1554. static inline bool amdgpu_atpx_dgpu_req_power_for_displays(void) { return false; }
  1555. #endif
  1556. /*
  1557. * KMS
  1558. */
  1559. extern const struct drm_ioctl_desc amdgpu_ioctls_kms[];
  1560. extern const int amdgpu_max_kms_ioctl;
  1561. int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags);
  1562. void amdgpu_driver_unload_kms(struct drm_device *dev);
  1563. void amdgpu_driver_lastclose_kms(struct drm_device *dev);
  1564. int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv);
  1565. void amdgpu_driver_postclose_kms(struct drm_device *dev,
  1566. struct drm_file *file_priv);
  1567. int amdgpu_suspend(struct amdgpu_device *adev);
  1568. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon);
  1569. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon);
  1570. u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe);
  1571. int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe);
  1572. void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe);
  1573. int amdgpu_get_vblank_timestamp_kms(struct drm_device *dev, unsigned int pipe,
  1574. int *max_error,
  1575. struct timeval *vblank_time,
  1576. unsigned flags);
  1577. long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd,
  1578. unsigned long arg);
  1579. /*
  1580. * functions used by amdgpu_encoder.c
  1581. */
  1582. struct amdgpu_afmt_acr {
  1583. u32 clock;
  1584. int n_32khz;
  1585. int cts_32khz;
  1586. int n_44_1khz;
  1587. int cts_44_1khz;
  1588. int n_48khz;
  1589. int cts_48khz;
  1590. };
  1591. struct amdgpu_afmt_acr amdgpu_afmt_acr(uint32_t clock);
  1592. /* amdgpu_acpi.c */
  1593. #if defined(CONFIG_ACPI)
  1594. int amdgpu_acpi_init(struct amdgpu_device *adev);
  1595. void amdgpu_acpi_fini(struct amdgpu_device *adev);
  1596. bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev);
  1597. int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
  1598. u8 perf_req, bool advertise);
  1599. int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev);
  1600. #else
  1601. static inline int amdgpu_acpi_init(struct amdgpu_device *adev) { return 0; }
  1602. static inline void amdgpu_acpi_fini(struct amdgpu_device *adev) { }
  1603. #endif
  1604. struct amdgpu_bo_va_mapping *
  1605. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  1606. uint64_t addr, struct amdgpu_bo **bo);
  1607. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser);
  1608. #include "amdgpu_object.h"
  1609. #endif