mxgpu_ai.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. */
  23. #include "amdgpu.h"
  24. #include "nbio/nbio_6_1_offset.h"
  25. #include "nbio/nbio_6_1_sh_mask.h"
  26. #include "gc/gc_9_0_offset.h"
  27. #include "gc/gc_9_0_sh_mask.h"
  28. #include "soc15.h"
  29. #include "vega10_ih.h"
  30. #include "soc15_common.h"
  31. #include "mxgpu_ai.h"
  32. static void xgpu_ai_mailbox_send_ack(struct amdgpu_device *adev)
  33. {
  34. WREG8(AI_MAIBOX_CONTROL_RCV_OFFSET_BYTE, 2);
  35. }
  36. static void xgpu_ai_mailbox_set_valid(struct amdgpu_device *adev, bool val)
  37. {
  38. WREG8(AI_MAIBOX_CONTROL_TRN_OFFSET_BYTE, val ? 1 : 0);
  39. }
  40. /*
  41. * this peek_msg could *only* be called in IRQ routine becuase in IRQ routine
  42. * RCV_MSG_VALID filed of BIF_BX_PF0_MAILBOX_CONTROL must already be set to 1
  43. * by host.
  44. *
  45. * if called no in IRQ routine, this peek_msg cannot guaranteed to return the
  46. * correct value since it doesn't return the RCV_DW0 under the case that
  47. * RCV_MSG_VALID is set by host.
  48. */
  49. static enum idh_event xgpu_ai_mailbox_peek_msg(struct amdgpu_device *adev)
  50. {
  51. return RREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0,
  52. mmBIF_BX_PF0_MAILBOX_MSGBUF_RCV_DW0));
  53. }
  54. static int xgpu_ai_mailbox_rcv_msg(struct amdgpu_device *adev,
  55. enum idh_event event)
  56. {
  57. u32 reg;
  58. reg = RREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0,
  59. mmBIF_BX_PF0_MAILBOX_MSGBUF_RCV_DW0));
  60. if (reg != event)
  61. return -ENOENT;
  62. xgpu_ai_mailbox_send_ack(adev);
  63. return 0;
  64. }
  65. static uint8_t xgpu_ai_peek_ack(struct amdgpu_device *adev) {
  66. return RREG8(AI_MAIBOX_CONTROL_TRN_OFFSET_BYTE) & 2;
  67. }
  68. static int xgpu_ai_poll_ack(struct amdgpu_device *adev)
  69. {
  70. int timeout = AI_MAILBOX_POLL_ACK_TIMEDOUT;
  71. u8 reg;
  72. do {
  73. reg = RREG8(AI_MAIBOX_CONTROL_TRN_OFFSET_BYTE);
  74. if (reg & 2)
  75. return 0;
  76. mdelay(5);
  77. timeout -= 5;
  78. } while (timeout > 1);
  79. pr_err("Doesn't get TRN_MSG_ACK from pf in %d msec\n", AI_MAILBOX_POLL_ACK_TIMEDOUT);
  80. return -ETIME;
  81. }
  82. static int xgpu_ai_poll_msg(struct amdgpu_device *adev, enum idh_event event)
  83. {
  84. int r, timeout = AI_MAILBOX_POLL_MSG_TIMEDOUT;
  85. do {
  86. r = xgpu_ai_mailbox_rcv_msg(adev, event);
  87. if (!r)
  88. return 0;
  89. msleep(10);
  90. timeout -= 10;
  91. } while (timeout > 1);
  92. pr_err("Doesn't get msg:%d from pf, error=%d\n", event, r);
  93. return -ETIME;
  94. }
  95. static void xgpu_ai_mailbox_trans_msg (struct amdgpu_device *adev,
  96. enum idh_request req, u32 data1, u32 data2, u32 data3) {
  97. u32 reg;
  98. int r;
  99. uint8_t trn;
  100. /* IMPORTANT:
  101. * clear TRN_MSG_VALID valid to clear host's RCV_MSG_ACK
  102. * and with host's RCV_MSG_ACK cleared hw automatically clear host's RCV_MSG_ACK
  103. * which lead to VF's TRN_MSG_ACK cleared, otherwise below xgpu_ai_poll_ack()
  104. * will return immediatly
  105. */
  106. do {
  107. xgpu_ai_mailbox_set_valid(adev, false);
  108. trn = xgpu_ai_peek_ack(adev);
  109. if (trn) {
  110. pr_err("trn=%x ACK should not asssert! wait again !\n", trn);
  111. msleep(1);
  112. }
  113. } while(trn);
  114. reg = RREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0,
  115. mmBIF_BX_PF0_MAILBOX_MSGBUF_TRN_DW0));
  116. reg = REG_SET_FIELD(reg, BIF_BX_PF0_MAILBOX_MSGBUF_TRN_DW0,
  117. MSGBUF_DATA, req);
  118. WREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_MSGBUF_TRN_DW0),
  119. reg);
  120. WREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_MSGBUF_TRN_DW1),
  121. data1);
  122. WREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_MSGBUF_TRN_DW2),
  123. data2);
  124. WREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_MSGBUF_TRN_DW3),
  125. data3);
  126. xgpu_ai_mailbox_set_valid(adev, true);
  127. /* start to poll ack */
  128. r = xgpu_ai_poll_ack(adev);
  129. if (r)
  130. pr_err("Doesn't get ack from pf, continue\n");
  131. xgpu_ai_mailbox_set_valid(adev, false);
  132. }
  133. static int xgpu_ai_send_access_requests(struct amdgpu_device *adev,
  134. enum idh_request req)
  135. {
  136. int r;
  137. xgpu_ai_mailbox_trans_msg(adev, req, 0, 0, 0);
  138. /* start to check msg if request is idh_req_gpu_init_access */
  139. if (req == IDH_REQ_GPU_INIT_ACCESS ||
  140. req == IDH_REQ_GPU_FINI_ACCESS ||
  141. req == IDH_REQ_GPU_RESET_ACCESS) {
  142. r = xgpu_ai_poll_msg(adev, IDH_READY_TO_ACCESS_GPU);
  143. if (r) {
  144. pr_err("Doesn't get READY_TO_ACCESS_GPU from pf, give up\n");
  145. return r;
  146. }
  147. /* Retrieve checksum from mailbox2 */
  148. if (req == IDH_REQ_GPU_INIT_ACCESS) {
  149. adev->virt.fw_reserve.checksum_key =
  150. RREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0,
  151. mmBIF_BX_PF0_MAILBOX_MSGBUF_RCV_DW2));
  152. }
  153. }
  154. return 0;
  155. }
  156. static int xgpu_ai_request_reset(struct amdgpu_device *adev)
  157. {
  158. return xgpu_ai_send_access_requests(adev, IDH_REQ_GPU_RESET_ACCESS);
  159. }
  160. static int xgpu_ai_request_full_gpu_access(struct amdgpu_device *adev,
  161. bool init)
  162. {
  163. enum idh_request req;
  164. req = init ? IDH_REQ_GPU_INIT_ACCESS : IDH_REQ_GPU_FINI_ACCESS;
  165. return xgpu_ai_send_access_requests(adev, req);
  166. }
  167. static int xgpu_ai_release_full_gpu_access(struct amdgpu_device *adev,
  168. bool init)
  169. {
  170. enum idh_request req;
  171. int r = 0;
  172. req = init ? IDH_REL_GPU_INIT_ACCESS : IDH_REL_GPU_FINI_ACCESS;
  173. r = xgpu_ai_send_access_requests(adev, req);
  174. return r;
  175. }
  176. static int xgpu_ai_mailbox_ack_irq(struct amdgpu_device *adev,
  177. struct amdgpu_irq_src *source,
  178. struct amdgpu_iv_entry *entry)
  179. {
  180. DRM_DEBUG("get ack intr and do nothing.\n");
  181. return 0;
  182. }
  183. static int xgpu_ai_set_mailbox_ack_irq(struct amdgpu_device *adev,
  184. struct amdgpu_irq_src *source,
  185. unsigned type,
  186. enum amdgpu_interrupt_state state)
  187. {
  188. u32 tmp = RREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_INT_CNTL));
  189. tmp = REG_SET_FIELD(tmp, BIF_BX_PF0_MAILBOX_INT_CNTL, ACK_INT_EN,
  190. (state == AMDGPU_IRQ_STATE_ENABLE) ? 1 : 0);
  191. WREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_INT_CNTL), tmp);
  192. return 0;
  193. }
  194. static void xgpu_ai_mailbox_flr_work(struct work_struct *work)
  195. {
  196. struct amdgpu_virt *virt = container_of(work, struct amdgpu_virt, flr_work);
  197. struct amdgpu_device *adev = container_of(virt, struct amdgpu_device, virt);
  198. int timeout = AI_MAILBOX_POLL_FLR_TIMEDOUT;
  199. int locked;
  200. /* block amdgpu_gpu_recover till msg FLR COMPLETE received,
  201. * otherwise the mailbox msg will be ruined/reseted by
  202. * the VF FLR.
  203. *
  204. * we can unlock the lock_reset to allow "amdgpu_job_timedout"
  205. * to run gpu_recover() after FLR_NOTIFICATION_CMPL received
  206. * which means host side had finished this VF's FLR.
  207. */
  208. locked = mutex_trylock(&adev->lock_reset);
  209. if (locked)
  210. adev->in_gpu_reset = 1;
  211. do {
  212. if (xgpu_ai_mailbox_peek_msg(adev) == IDH_FLR_NOTIFICATION_CMPL)
  213. goto flr_done;
  214. msleep(10);
  215. timeout -= 10;
  216. } while (timeout > 1);
  217. flr_done:
  218. if (locked)
  219. mutex_unlock(&adev->lock_reset);
  220. /* Trigger recovery for world switch failure if no TDR */
  221. if (amdgpu_lockup_timeout == 0)
  222. amdgpu_device_gpu_recover(adev, NULL, true);
  223. }
  224. static int xgpu_ai_set_mailbox_rcv_irq(struct amdgpu_device *adev,
  225. struct amdgpu_irq_src *src,
  226. unsigned type,
  227. enum amdgpu_interrupt_state state)
  228. {
  229. u32 tmp = RREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_INT_CNTL));
  230. tmp = REG_SET_FIELD(tmp, BIF_BX_PF0_MAILBOX_INT_CNTL, VALID_INT_EN,
  231. (state == AMDGPU_IRQ_STATE_ENABLE) ? 1 : 0);
  232. WREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_INT_CNTL), tmp);
  233. return 0;
  234. }
  235. static int xgpu_ai_mailbox_rcv_irq(struct amdgpu_device *adev,
  236. struct amdgpu_irq_src *source,
  237. struct amdgpu_iv_entry *entry)
  238. {
  239. enum idh_event event = xgpu_ai_mailbox_peek_msg(adev);
  240. switch (event) {
  241. case IDH_FLR_NOTIFICATION:
  242. if (amdgpu_sriov_runtime(adev))
  243. schedule_work(&adev->virt.flr_work);
  244. break;
  245. /* READY_TO_ACCESS_GPU is fetched by kernel polling, IRQ can ignore
  246. * it byfar since that polling thread will handle it,
  247. * other msg like flr complete is not handled here.
  248. */
  249. case IDH_CLR_MSG_BUF:
  250. case IDH_FLR_NOTIFICATION_CMPL:
  251. case IDH_READY_TO_ACCESS_GPU:
  252. default:
  253. break;
  254. }
  255. return 0;
  256. }
  257. static const struct amdgpu_irq_src_funcs xgpu_ai_mailbox_ack_irq_funcs = {
  258. .set = xgpu_ai_set_mailbox_ack_irq,
  259. .process = xgpu_ai_mailbox_ack_irq,
  260. };
  261. static const struct amdgpu_irq_src_funcs xgpu_ai_mailbox_rcv_irq_funcs = {
  262. .set = xgpu_ai_set_mailbox_rcv_irq,
  263. .process = xgpu_ai_mailbox_rcv_irq,
  264. };
  265. void xgpu_ai_mailbox_set_irq_funcs(struct amdgpu_device *adev)
  266. {
  267. adev->virt.ack_irq.num_types = 1;
  268. adev->virt.ack_irq.funcs = &xgpu_ai_mailbox_ack_irq_funcs;
  269. adev->virt.rcv_irq.num_types = 1;
  270. adev->virt.rcv_irq.funcs = &xgpu_ai_mailbox_rcv_irq_funcs;
  271. }
  272. int xgpu_ai_mailbox_add_irq_id(struct amdgpu_device *adev)
  273. {
  274. int r;
  275. r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_BIF, 135, &adev->virt.rcv_irq);
  276. if (r)
  277. return r;
  278. r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_BIF, 138, &adev->virt.ack_irq);
  279. if (r) {
  280. amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
  281. return r;
  282. }
  283. return 0;
  284. }
  285. int xgpu_ai_mailbox_get_irq(struct amdgpu_device *adev)
  286. {
  287. int r;
  288. r = amdgpu_irq_get(adev, &adev->virt.rcv_irq, 0);
  289. if (r)
  290. return r;
  291. r = amdgpu_irq_get(adev, &adev->virt.ack_irq, 0);
  292. if (r) {
  293. amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
  294. return r;
  295. }
  296. INIT_WORK(&adev->virt.flr_work, xgpu_ai_mailbox_flr_work);
  297. return 0;
  298. }
  299. void xgpu_ai_mailbox_put_irq(struct amdgpu_device *adev)
  300. {
  301. amdgpu_irq_put(adev, &adev->virt.ack_irq, 0);
  302. amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
  303. }
  304. const struct amdgpu_virt_ops xgpu_ai_virt_ops = {
  305. .req_full_gpu = xgpu_ai_request_full_gpu_access,
  306. .rel_full_gpu = xgpu_ai_release_full_gpu_access,
  307. .reset_gpu = xgpu_ai_request_reset,
  308. .wait_reset = NULL,
  309. .trans_msg = xgpu_ai_mailbox_trans_msg,
  310. };