mxgpu_ai.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 assert! 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 || req == IDH_REQ_GPU_RESET_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. adev->in_gpu_reset = 0;
  220. mutex_unlock(&adev->lock_reset);
  221. }
  222. /* Trigger recovery for world switch failure if no TDR */
  223. if (amdgpu_lockup_timeout == 0)
  224. amdgpu_device_gpu_recover(adev, NULL, true);
  225. }
  226. static int xgpu_ai_set_mailbox_rcv_irq(struct amdgpu_device *adev,
  227. struct amdgpu_irq_src *src,
  228. unsigned type,
  229. enum amdgpu_interrupt_state state)
  230. {
  231. u32 tmp = RREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_INT_CNTL));
  232. tmp = REG_SET_FIELD(tmp, BIF_BX_PF0_MAILBOX_INT_CNTL, VALID_INT_EN,
  233. (state == AMDGPU_IRQ_STATE_ENABLE) ? 1 : 0);
  234. WREG32_NO_KIQ(SOC15_REG_OFFSET(NBIO, 0, mmBIF_BX_PF0_MAILBOX_INT_CNTL), tmp);
  235. return 0;
  236. }
  237. static int xgpu_ai_mailbox_rcv_irq(struct amdgpu_device *adev,
  238. struct amdgpu_irq_src *source,
  239. struct amdgpu_iv_entry *entry)
  240. {
  241. enum idh_event event = xgpu_ai_mailbox_peek_msg(adev);
  242. switch (event) {
  243. case IDH_FLR_NOTIFICATION:
  244. if (amdgpu_sriov_runtime(adev))
  245. schedule_work(&adev->virt.flr_work);
  246. break;
  247. /* READY_TO_ACCESS_GPU is fetched by kernel polling, IRQ can ignore
  248. * it byfar since that polling thread will handle it,
  249. * other msg like flr complete is not handled here.
  250. */
  251. case IDH_CLR_MSG_BUF:
  252. case IDH_FLR_NOTIFICATION_CMPL:
  253. case IDH_READY_TO_ACCESS_GPU:
  254. default:
  255. break;
  256. }
  257. return 0;
  258. }
  259. static const struct amdgpu_irq_src_funcs xgpu_ai_mailbox_ack_irq_funcs = {
  260. .set = xgpu_ai_set_mailbox_ack_irq,
  261. .process = xgpu_ai_mailbox_ack_irq,
  262. };
  263. static const struct amdgpu_irq_src_funcs xgpu_ai_mailbox_rcv_irq_funcs = {
  264. .set = xgpu_ai_set_mailbox_rcv_irq,
  265. .process = xgpu_ai_mailbox_rcv_irq,
  266. };
  267. void xgpu_ai_mailbox_set_irq_funcs(struct amdgpu_device *adev)
  268. {
  269. adev->virt.ack_irq.num_types = 1;
  270. adev->virt.ack_irq.funcs = &xgpu_ai_mailbox_ack_irq_funcs;
  271. adev->virt.rcv_irq.num_types = 1;
  272. adev->virt.rcv_irq.funcs = &xgpu_ai_mailbox_rcv_irq_funcs;
  273. }
  274. int xgpu_ai_mailbox_add_irq_id(struct amdgpu_device *adev)
  275. {
  276. int r;
  277. r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_BIF, 135, &adev->virt.rcv_irq);
  278. if (r)
  279. return r;
  280. r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_BIF, 138, &adev->virt.ack_irq);
  281. if (r) {
  282. amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
  283. return r;
  284. }
  285. return 0;
  286. }
  287. int xgpu_ai_mailbox_get_irq(struct amdgpu_device *adev)
  288. {
  289. int r;
  290. r = amdgpu_irq_get(adev, &adev->virt.rcv_irq, 0);
  291. if (r)
  292. return r;
  293. r = amdgpu_irq_get(adev, &adev->virt.ack_irq, 0);
  294. if (r) {
  295. amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
  296. return r;
  297. }
  298. INIT_WORK(&adev->virt.flr_work, xgpu_ai_mailbox_flr_work);
  299. return 0;
  300. }
  301. void xgpu_ai_mailbox_put_irq(struct amdgpu_device *adev)
  302. {
  303. amdgpu_irq_put(adev, &adev->virt.ack_irq, 0);
  304. amdgpu_irq_put(adev, &adev->virt.rcv_irq, 0);
  305. }
  306. const struct amdgpu_virt_ops xgpu_ai_virt_ops = {
  307. .req_full_gpu = xgpu_ai_request_full_gpu_access,
  308. .rel_full_gpu = xgpu_ai_release_full_gpu_access,
  309. .reset_gpu = xgpu_ai_request_reset,
  310. .wait_reset = NULL,
  311. .trans_msg = xgpu_ai_mailbox_trans_msg,
  312. };