amdgpu_amdkfd_gfx_v7.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/fdtable.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/firmware.h>
  25. #include <drm/drmP.h>
  26. #include "amdgpu.h"
  27. #include "amdgpu_amdkfd.h"
  28. #include "cikd.h"
  29. #include "cik_sdma.h"
  30. #include "amdgpu_ucode.h"
  31. #include "gca/gfx_7_2_d.h"
  32. #include "gca/gfx_7_2_enum.h"
  33. #include "gca/gfx_7_2_sh_mask.h"
  34. #include "oss/oss_2_0_d.h"
  35. #include "oss/oss_2_0_sh_mask.h"
  36. #include "gmc/gmc_7_1_d.h"
  37. #include "gmc/gmc_7_1_sh_mask.h"
  38. #include "cik_structs.h"
  39. #define CIK_PIPE_PER_MEC (4)
  40. enum {
  41. MAX_TRAPID = 8, /* 3 bits in the bitfield. */
  42. MAX_WATCH_ADDRESSES = 4
  43. };
  44. enum {
  45. ADDRESS_WATCH_REG_ADDR_HI = 0,
  46. ADDRESS_WATCH_REG_ADDR_LO,
  47. ADDRESS_WATCH_REG_CNTL,
  48. ADDRESS_WATCH_REG_MAX
  49. };
  50. /* not defined in the CI/KV reg file */
  51. enum {
  52. ADDRESS_WATCH_REG_CNTL_ATC_BIT = 0x10000000UL,
  53. ADDRESS_WATCH_REG_CNTL_DEFAULT_MASK = 0x00FFFFFF,
  54. ADDRESS_WATCH_REG_ADDLOW_MASK_EXTENSION = 0x03000000,
  55. /* extend the mask to 26 bits to match the low address field */
  56. ADDRESS_WATCH_REG_ADDLOW_SHIFT = 6,
  57. ADDRESS_WATCH_REG_ADDHIGH_MASK = 0xFFFF
  58. };
  59. static const uint32_t watchRegs[MAX_WATCH_ADDRESSES * ADDRESS_WATCH_REG_MAX] = {
  60. mmTCP_WATCH0_ADDR_H, mmTCP_WATCH0_ADDR_L, mmTCP_WATCH0_CNTL,
  61. mmTCP_WATCH1_ADDR_H, mmTCP_WATCH1_ADDR_L, mmTCP_WATCH1_CNTL,
  62. mmTCP_WATCH2_ADDR_H, mmTCP_WATCH2_ADDR_L, mmTCP_WATCH2_CNTL,
  63. mmTCP_WATCH3_ADDR_H, mmTCP_WATCH3_ADDR_L, mmTCP_WATCH3_CNTL
  64. };
  65. union TCP_WATCH_CNTL_BITS {
  66. struct {
  67. uint32_t mask:24;
  68. uint32_t vmid:4;
  69. uint32_t atc:1;
  70. uint32_t mode:2;
  71. uint32_t valid:1;
  72. } bitfields, bits;
  73. uint32_t u32All;
  74. signed int i32All;
  75. float f32All;
  76. };
  77. /*
  78. * Register access functions
  79. */
  80. static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
  81. uint32_t sh_mem_config, uint32_t sh_mem_ape1_base,
  82. uint32_t sh_mem_ape1_limit, uint32_t sh_mem_bases);
  83. static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
  84. unsigned int vmid);
  85. static int kgd_init_pipeline(struct kgd_dev *kgd, uint32_t pipe_id,
  86. uint32_t hpd_size, uint64_t hpd_gpu_addr);
  87. static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id);
  88. static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
  89. uint32_t queue_id, uint32_t __user *wptr);
  90. static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd);
  91. static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
  92. uint32_t pipe_id, uint32_t queue_id);
  93. static int kgd_hqd_destroy(struct kgd_dev *kgd, uint32_t reset_type,
  94. unsigned int timeout, uint32_t pipe_id,
  95. uint32_t queue_id);
  96. static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd);
  97. static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
  98. unsigned int timeout);
  99. static int kgd_address_watch_disable(struct kgd_dev *kgd);
  100. static int kgd_address_watch_execute(struct kgd_dev *kgd,
  101. unsigned int watch_point_id,
  102. uint32_t cntl_val,
  103. uint32_t addr_hi,
  104. uint32_t addr_lo);
  105. static int kgd_wave_control_execute(struct kgd_dev *kgd,
  106. uint32_t gfx_index_val,
  107. uint32_t sq_cmd);
  108. static uint32_t kgd_address_watch_get_offset(struct kgd_dev *kgd,
  109. unsigned int watch_point_id,
  110. unsigned int reg_offset);
  111. static bool get_atc_vmid_pasid_mapping_valid(struct kgd_dev *kgd, uint8_t vmid);
  112. static uint16_t get_atc_vmid_pasid_mapping_pasid(struct kgd_dev *kgd,
  113. uint8_t vmid);
  114. static void write_vmid_invalidate_request(struct kgd_dev *kgd, uint8_t vmid);
  115. static uint16_t get_fw_version(struct kgd_dev *kgd, enum kgd_engine_type type);
  116. static const struct kfd2kgd_calls kfd2kgd = {
  117. .init_gtt_mem_allocation = alloc_gtt_mem,
  118. .free_gtt_mem = free_gtt_mem,
  119. .get_vmem_size = get_vmem_size,
  120. .get_gpu_clock_counter = get_gpu_clock_counter,
  121. .get_max_engine_clock_in_mhz = get_max_engine_clock_in_mhz,
  122. .program_sh_mem_settings = kgd_program_sh_mem_settings,
  123. .set_pasid_vmid_mapping = kgd_set_pasid_vmid_mapping,
  124. .init_pipeline = kgd_init_pipeline,
  125. .init_interrupts = kgd_init_interrupts,
  126. .hqd_load = kgd_hqd_load,
  127. .hqd_sdma_load = kgd_hqd_sdma_load,
  128. .hqd_is_occupied = kgd_hqd_is_occupied,
  129. .hqd_sdma_is_occupied = kgd_hqd_sdma_is_occupied,
  130. .hqd_destroy = kgd_hqd_destroy,
  131. .hqd_sdma_destroy = kgd_hqd_sdma_destroy,
  132. .address_watch_disable = kgd_address_watch_disable,
  133. .address_watch_execute = kgd_address_watch_execute,
  134. .wave_control_execute = kgd_wave_control_execute,
  135. .address_watch_get_offset = kgd_address_watch_get_offset,
  136. .get_atc_vmid_pasid_mapping_pasid = get_atc_vmid_pasid_mapping_pasid,
  137. .get_atc_vmid_pasid_mapping_valid = get_atc_vmid_pasid_mapping_valid,
  138. .write_vmid_invalidate_request = write_vmid_invalidate_request,
  139. .get_fw_version = get_fw_version
  140. };
  141. struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions()
  142. {
  143. return (struct kfd2kgd_calls *)&kfd2kgd;
  144. }
  145. static inline struct amdgpu_device *get_amdgpu_device(struct kgd_dev *kgd)
  146. {
  147. return (struct amdgpu_device *)kgd;
  148. }
  149. static void lock_srbm(struct kgd_dev *kgd, uint32_t mec, uint32_t pipe,
  150. uint32_t queue, uint32_t vmid)
  151. {
  152. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  153. uint32_t value = PIPEID(pipe) | MEID(mec) | VMID(vmid) | QUEUEID(queue);
  154. mutex_lock(&adev->srbm_mutex);
  155. WREG32(mmSRBM_GFX_CNTL, value);
  156. }
  157. static void unlock_srbm(struct kgd_dev *kgd)
  158. {
  159. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  160. WREG32(mmSRBM_GFX_CNTL, 0);
  161. mutex_unlock(&adev->srbm_mutex);
  162. }
  163. static void acquire_queue(struct kgd_dev *kgd, uint32_t pipe_id,
  164. uint32_t queue_id)
  165. {
  166. uint32_t mec = (++pipe_id / CIK_PIPE_PER_MEC) + 1;
  167. uint32_t pipe = (pipe_id % CIK_PIPE_PER_MEC);
  168. lock_srbm(kgd, mec, pipe, queue_id, 0);
  169. }
  170. static void release_queue(struct kgd_dev *kgd)
  171. {
  172. unlock_srbm(kgd);
  173. }
  174. static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
  175. uint32_t sh_mem_config,
  176. uint32_t sh_mem_ape1_base,
  177. uint32_t sh_mem_ape1_limit,
  178. uint32_t sh_mem_bases)
  179. {
  180. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  181. lock_srbm(kgd, 0, 0, 0, vmid);
  182. WREG32(mmSH_MEM_CONFIG, sh_mem_config);
  183. WREG32(mmSH_MEM_APE1_BASE, sh_mem_ape1_base);
  184. WREG32(mmSH_MEM_APE1_LIMIT, sh_mem_ape1_limit);
  185. WREG32(mmSH_MEM_BASES, sh_mem_bases);
  186. unlock_srbm(kgd);
  187. }
  188. static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
  189. unsigned int vmid)
  190. {
  191. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  192. /*
  193. * We have to assume that there is no outstanding mapping.
  194. * The ATC_VMID_PASID_MAPPING_UPDATE_STATUS bit could be 0 because
  195. * a mapping is in progress or because a mapping finished and the
  196. * SW cleared it. So the protocol is to always wait & clear.
  197. */
  198. uint32_t pasid_mapping = (pasid == 0) ? 0 : (uint32_t)pasid |
  199. ATC_VMID0_PASID_MAPPING__VALID_MASK;
  200. WREG32(mmATC_VMID0_PASID_MAPPING + vmid, pasid_mapping);
  201. while (!(RREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS) & (1U << vmid)))
  202. cpu_relax();
  203. WREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS, 1U << vmid);
  204. /* Mapping vmid to pasid also for IH block */
  205. WREG32(mmIH_VMID_0_LUT + vmid, pasid_mapping);
  206. return 0;
  207. }
  208. static int kgd_init_pipeline(struct kgd_dev *kgd, uint32_t pipe_id,
  209. uint32_t hpd_size, uint64_t hpd_gpu_addr)
  210. {
  211. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  212. uint32_t mec = (++pipe_id / CIK_PIPE_PER_MEC) + 1;
  213. uint32_t pipe = (pipe_id % CIK_PIPE_PER_MEC);
  214. lock_srbm(kgd, mec, pipe, 0, 0);
  215. WREG32(mmCP_HPD_EOP_BASE_ADDR, lower_32_bits(hpd_gpu_addr >> 8));
  216. WREG32(mmCP_HPD_EOP_BASE_ADDR_HI, upper_32_bits(hpd_gpu_addr >> 8));
  217. WREG32(mmCP_HPD_EOP_VMID, 0);
  218. WREG32(mmCP_HPD_EOP_CONTROL, hpd_size);
  219. unlock_srbm(kgd);
  220. return 0;
  221. }
  222. static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id)
  223. {
  224. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  225. uint32_t mec;
  226. uint32_t pipe;
  227. mec = (pipe_id / CIK_PIPE_PER_MEC) + 1;
  228. pipe = (pipe_id % CIK_PIPE_PER_MEC);
  229. lock_srbm(kgd, mec, pipe, 0, 0);
  230. WREG32(mmCPC_INT_CNTL, CP_INT_CNTL_RING0__TIME_STAMP_INT_ENABLE_MASK |
  231. CP_INT_CNTL_RING0__OPCODE_ERROR_INT_ENABLE_MASK);
  232. unlock_srbm(kgd);
  233. return 0;
  234. }
  235. static inline uint32_t get_sdma_base_addr(struct cik_sdma_rlc_registers *m)
  236. {
  237. uint32_t retval;
  238. retval = m->sdma_engine_id * SDMA1_REGISTER_OFFSET +
  239. m->sdma_queue_id * KFD_CIK_SDMA_QUEUE_OFFSET;
  240. pr_debug("kfd: sdma base address: 0x%x\n", retval);
  241. return retval;
  242. }
  243. static inline struct cik_mqd *get_mqd(void *mqd)
  244. {
  245. return (struct cik_mqd *)mqd;
  246. }
  247. static inline struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd)
  248. {
  249. return (struct cik_sdma_rlc_registers *)mqd;
  250. }
  251. static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
  252. uint32_t queue_id, uint32_t __user *wptr)
  253. {
  254. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  255. uint32_t wptr_shadow, is_wptr_shadow_valid;
  256. struct cik_mqd *m;
  257. m = get_mqd(mqd);
  258. is_wptr_shadow_valid = !get_user(wptr_shadow, wptr);
  259. acquire_queue(kgd, pipe_id, queue_id);
  260. WREG32(mmCP_MQD_BASE_ADDR, m->cp_mqd_base_addr_lo);
  261. WREG32(mmCP_MQD_BASE_ADDR_HI, m->cp_mqd_base_addr_hi);
  262. WREG32(mmCP_MQD_CONTROL, m->cp_mqd_control);
  263. WREG32(mmCP_HQD_PQ_BASE, m->cp_hqd_pq_base_lo);
  264. WREG32(mmCP_HQD_PQ_BASE_HI, m->cp_hqd_pq_base_hi);
  265. WREG32(mmCP_HQD_PQ_CONTROL, m->cp_hqd_pq_control);
  266. WREG32(mmCP_HQD_IB_CONTROL, m->cp_hqd_ib_control);
  267. WREG32(mmCP_HQD_IB_BASE_ADDR, m->cp_hqd_ib_base_addr_lo);
  268. WREG32(mmCP_HQD_IB_BASE_ADDR_HI, m->cp_hqd_ib_base_addr_hi);
  269. WREG32(mmCP_HQD_IB_RPTR, m->cp_hqd_ib_rptr);
  270. WREG32(mmCP_HQD_PERSISTENT_STATE, m->cp_hqd_persistent_state);
  271. WREG32(mmCP_HQD_SEMA_CMD, m->cp_hqd_sema_cmd);
  272. WREG32(mmCP_HQD_MSG_TYPE, m->cp_hqd_msg_type);
  273. WREG32(mmCP_HQD_ATOMIC0_PREOP_LO, m->cp_hqd_atomic0_preop_lo);
  274. WREG32(mmCP_HQD_ATOMIC0_PREOP_HI, m->cp_hqd_atomic0_preop_hi);
  275. WREG32(mmCP_HQD_ATOMIC1_PREOP_LO, m->cp_hqd_atomic1_preop_lo);
  276. WREG32(mmCP_HQD_ATOMIC1_PREOP_HI, m->cp_hqd_atomic1_preop_hi);
  277. WREG32(mmCP_HQD_PQ_RPTR_REPORT_ADDR, m->cp_hqd_pq_rptr_report_addr_lo);
  278. WREG32(mmCP_HQD_PQ_RPTR_REPORT_ADDR_HI,
  279. m->cp_hqd_pq_rptr_report_addr_hi);
  280. WREG32(mmCP_HQD_PQ_RPTR, m->cp_hqd_pq_rptr);
  281. WREG32(mmCP_HQD_PQ_WPTR_POLL_ADDR, m->cp_hqd_pq_wptr_poll_addr_lo);
  282. WREG32(mmCP_HQD_PQ_WPTR_POLL_ADDR_HI, m->cp_hqd_pq_wptr_poll_addr_hi);
  283. WREG32(mmCP_HQD_PQ_DOORBELL_CONTROL, m->cp_hqd_pq_doorbell_control);
  284. WREG32(mmCP_HQD_VMID, m->cp_hqd_vmid);
  285. WREG32(mmCP_HQD_QUANTUM, m->cp_hqd_quantum);
  286. WREG32(mmCP_HQD_PIPE_PRIORITY, m->cp_hqd_pipe_priority);
  287. WREG32(mmCP_HQD_QUEUE_PRIORITY, m->cp_hqd_queue_priority);
  288. WREG32(mmCP_HQD_IQ_RPTR, m->cp_hqd_iq_rptr);
  289. if (is_wptr_shadow_valid)
  290. WREG32(mmCP_HQD_PQ_WPTR, wptr_shadow);
  291. WREG32(mmCP_HQD_ACTIVE, m->cp_hqd_active);
  292. release_queue(kgd);
  293. return 0;
  294. }
  295. static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd)
  296. {
  297. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  298. struct cik_sdma_rlc_registers *m;
  299. unsigned long end_jiffies;
  300. uint32_t sdma_base_addr;
  301. uint32_t data;
  302. m = get_sdma_mqd(mqd);
  303. sdma_base_addr = get_sdma_base_addr(m);
  304. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL,
  305. m->sdma_rlc_rb_cntl & (~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK));
  306. end_jiffies = msecs_to_jiffies(2000) + jiffies;
  307. while (true) {
  308. data = RREG32(sdma_base_addr + mmSDMA0_RLC0_CONTEXT_STATUS);
  309. if (data & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
  310. break;
  311. if (time_after(jiffies, end_jiffies))
  312. return -ETIME;
  313. usleep_range(500, 1000);
  314. }
  315. if (m->sdma_engine_id) {
  316. data = RREG32(mmSDMA1_GFX_CONTEXT_CNTL);
  317. data = REG_SET_FIELD(data, SDMA1_GFX_CONTEXT_CNTL,
  318. RESUME_CTX, 0);
  319. WREG32(mmSDMA1_GFX_CONTEXT_CNTL, data);
  320. } else {
  321. data = RREG32(mmSDMA0_GFX_CONTEXT_CNTL);
  322. data = REG_SET_FIELD(data, SDMA0_GFX_CONTEXT_CNTL,
  323. RESUME_CTX, 0);
  324. WREG32(mmSDMA0_GFX_CONTEXT_CNTL, data);
  325. }
  326. WREG32(sdma_base_addr + mmSDMA0_RLC0_DOORBELL,
  327. m->sdma_rlc_doorbell);
  328. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR, 0);
  329. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_WPTR, 0);
  330. WREG32(sdma_base_addr + mmSDMA0_RLC0_VIRTUAL_ADDR,
  331. m->sdma_rlc_virtual_addr);
  332. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_BASE, m->sdma_rlc_rb_base);
  333. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_BASE_HI,
  334. m->sdma_rlc_rb_base_hi);
  335. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR_ADDR_LO,
  336. m->sdma_rlc_rb_rptr_addr_lo);
  337. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR_ADDR_HI,
  338. m->sdma_rlc_rb_rptr_addr_hi);
  339. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL,
  340. m->sdma_rlc_rb_cntl);
  341. return 0;
  342. }
  343. static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
  344. uint32_t pipe_id, uint32_t queue_id)
  345. {
  346. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  347. uint32_t act;
  348. bool retval = false;
  349. uint32_t low, high;
  350. acquire_queue(kgd, pipe_id, queue_id);
  351. act = RREG32(mmCP_HQD_ACTIVE);
  352. if (act) {
  353. low = lower_32_bits(queue_address >> 8);
  354. high = upper_32_bits(queue_address >> 8);
  355. if (low == RREG32(mmCP_HQD_PQ_BASE) &&
  356. high == RREG32(mmCP_HQD_PQ_BASE_HI))
  357. retval = true;
  358. }
  359. release_queue(kgd);
  360. return retval;
  361. }
  362. static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd)
  363. {
  364. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  365. struct cik_sdma_rlc_registers *m;
  366. uint32_t sdma_base_addr;
  367. uint32_t sdma_rlc_rb_cntl;
  368. m = get_sdma_mqd(mqd);
  369. sdma_base_addr = get_sdma_base_addr(m);
  370. sdma_rlc_rb_cntl = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL);
  371. if (sdma_rlc_rb_cntl & SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK)
  372. return true;
  373. return false;
  374. }
  375. static int kgd_hqd_destroy(struct kgd_dev *kgd, uint32_t reset_type,
  376. unsigned int timeout, uint32_t pipe_id,
  377. uint32_t queue_id)
  378. {
  379. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  380. uint32_t temp;
  381. acquire_queue(kgd, pipe_id, queue_id);
  382. WREG32(mmCP_HQD_PQ_DOORBELL_CONTROL, 0);
  383. WREG32(mmCP_HQD_DEQUEUE_REQUEST, reset_type);
  384. while (true) {
  385. temp = RREG32(mmCP_HQD_ACTIVE);
  386. if (temp & CP_HQD_ACTIVE__ACTIVE_MASK)
  387. break;
  388. if (timeout == 0) {
  389. pr_err("kfd: cp queue preemption time out (%dms)\n",
  390. temp);
  391. release_queue(kgd);
  392. return -ETIME;
  393. }
  394. msleep(20);
  395. timeout -= 20;
  396. }
  397. release_queue(kgd);
  398. return 0;
  399. }
  400. static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
  401. unsigned int timeout)
  402. {
  403. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  404. struct cik_sdma_rlc_registers *m;
  405. uint32_t sdma_base_addr;
  406. uint32_t temp;
  407. m = get_sdma_mqd(mqd);
  408. sdma_base_addr = get_sdma_base_addr(m);
  409. temp = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL);
  410. temp = temp & ~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK;
  411. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL, temp);
  412. while (true) {
  413. temp = RREG32(sdma_base_addr + mmSDMA0_RLC0_CONTEXT_STATUS);
  414. if (temp & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
  415. break;
  416. if (timeout == 0)
  417. return -ETIME;
  418. msleep(20);
  419. timeout -= 20;
  420. }
  421. WREG32(sdma_base_addr + mmSDMA0_RLC0_DOORBELL, 0);
  422. WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL,
  423. RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL) |
  424. SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK);
  425. return 0;
  426. }
  427. static int kgd_address_watch_disable(struct kgd_dev *kgd)
  428. {
  429. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  430. union TCP_WATCH_CNTL_BITS cntl;
  431. unsigned int i;
  432. cntl.u32All = 0;
  433. cntl.bitfields.valid = 0;
  434. cntl.bitfields.mask = ADDRESS_WATCH_REG_CNTL_DEFAULT_MASK;
  435. cntl.bitfields.atc = 1;
  436. /* Turning off this address until we set all the registers */
  437. for (i = 0; i < MAX_WATCH_ADDRESSES; i++)
  438. WREG32(watchRegs[i * ADDRESS_WATCH_REG_MAX +
  439. ADDRESS_WATCH_REG_CNTL], cntl.u32All);
  440. return 0;
  441. }
  442. static int kgd_address_watch_execute(struct kgd_dev *kgd,
  443. unsigned int watch_point_id,
  444. uint32_t cntl_val,
  445. uint32_t addr_hi,
  446. uint32_t addr_lo)
  447. {
  448. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  449. union TCP_WATCH_CNTL_BITS cntl;
  450. cntl.u32All = cntl_val;
  451. /* Turning off this watch point until we set all the registers */
  452. cntl.bitfields.valid = 0;
  453. WREG32(watchRegs[watch_point_id * ADDRESS_WATCH_REG_MAX +
  454. ADDRESS_WATCH_REG_CNTL], cntl.u32All);
  455. WREG32(watchRegs[watch_point_id * ADDRESS_WATCH_REG_MAX +
  456. ADDRESS_WATCH_REG_ADDR_HI], addr_hi);
  457. WREG32(watchRegs[watch_point_id * ADDRESS_WATCH_REG_MAX +
  458. ADDRESS_WATCH_REG_ADDR_LO], addr_lo);
  459. /* Enable the watch point */
  460. cntl.bitfields.valid = 1;
  461. WREG32(watchRegs[watch_point_id * ADDRESS_WATCH_REG_MAX +
  462. ADDRESS_WATCH_REG_CNTL], cntl.u32All);
  463. return 0;
  464. }
  465. static int kgd_wave_control_execute(struct kgd_dev *kgd,
  466. uint32_t gfx_index_val,
  467. uint32_t sq_cmd)
  468. {
  469. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  470. uint32_t data;
  471. mutex_lock(&adev->grbm_idx_mutex);
  472. WREG32(mmGRBM_GFX_INDEX, gfx_index_val);
  473. WREG32(mmSQ_CMD, sq_cmd);
  474. /* Restore the GRBM_GFX_INDEX register */
  475. data = GRBM_GFX_INDEX__INSTANCE_BROADCAST_WRITES_MASK |
  476. GRBM_GFX_INDEX__SH_BROADCAST_WRITES_MASK |
  477. GRBM_GFX_INDEX__SE_BROADCAST_WRITES_MASK;
  478. WREG32(mmGRBM_GFX_INDEX, data);
  479. mutex_unlock(&adev->grbm_idx_mutex);
  480. return 0;
  481. }
  482. static uint32_t kgd_address_watch_get_offset(struct kgd_dev *kgd,
  483. unsigned int watch_point_id,
  484. unsigned int reg_offset)
  485. {
  486. return watchRegs[watch_point_id * ADDRESS_WATCH_REG_MAX + reg_offset];
  487. }
  488. static bool get_atc_vmid_pasid_mapping_valid(struct kgd_dev *kgd,
  489. uint8_t vmid)
  490. {
  491. uint32_t reg;
  492. struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
  493. reg = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
  494. return reg & ATC_VMID0_PASID_MAPPING__VALID_MASK;
  495. }
  496. static uint16_t get_atc_vmid_pasid_mapping_pasid(struct kgd_dev *kgd,
  497. uint8_t vmid)
  498. {
  499. uint32_t reg;
  500. struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
  501. reg = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
  502. return reg & ATC_VMID0_PASID_MAPPING__VALID_MASK;
  503. }
  504. static void write_vmid_invalidate_request(struct kgd_dev *kgd, uint8_t vmid)
  505. {
  506. struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
  507. WREG32(mmVM_INVALIDATE_REQUEST, 1 << vmid);
  508. }
  509. static uint16_t get_fw_version(struct kgd_dev *kgd, enum kgd_engine_type type)
  510. {
  511. struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
  512. const union amdgpu_firmware_header *hdr;
  513. BUG_ON(kgd == NULL);
  514. switch (type) {
  515. case KGD_ENGINE_PFP:
  516. hdr = (const union amdgpu_firmware_header *)
  517. adev->gfx.pfp_fw->data;
  518. break;
  519. case KGD_ENGINE_ME:
  520. hdr = (const union amdgpu_firmware_header *)
  521. adev->gfx.me_fw->data;
  522. break;
  523. case KGD_ENGINE_CE:
  524. hdr = (const union amdgpu_firmware_header *)
  525. adev->gfx.ce_fw->data;
  526. break;
  527. case KGD_ENGINE_MEC1:
  528. hdr = (const union amdgpu_firmware_header *)
  529. adev->gfx.mec_fw->data;
  530. break;
  531. case KGD_ENGINE_MEC2:
  532. hdr = (const union amdgpu_firmware_header *)
  533. adev->gfx.mec2_fw->data;
  534. break;
  535. case KGD_ENGINE_RLC:
  536. hdr = (const union amdgpu_firmware_header *)
  537. adev->gfx.rlc_fw->data;
  538. break;
  539. case KGD_ENGINE_SDMA1:
  540. hdr = (const union amdgpu_firmware_header *)
  541. adev->sdma.instance[0].fw->data;
  542. break;
  543. case KGD_ENGINE_SDMA2:
  544. hdr = (const union amdgpu_firmware_header *)
  545. adev->sdma.instance[1].fw->data;
  546. break;
  547. default:
  548. return 0;
  549. }
  550. if (hdr == NULL)
  551. return 0;
  552. /* Only 12 bit in use*/
  553. return hdr->common.ucode_version;
  554. }