irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016-2022 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. */
  6. #include "habanalabs.h"
  7. #include <linux/slab.h>
  8. /**
  9. * struct hl_eqe_work - This structure is used to schedule work of EQ
  10. * entry and cpucp_reset event
  11. *
  12. * @eq_work: workqueue object to run when EQ entry is received
  13. * @hdev: pointer to device structure
  14. * @eq_entry: copy of the EQ entry
  15. */
  16. struct hl_eqe_work {
  17. struct work_struct eq_work;
  18. struct hl_device *hdev;
  19. struct hl_eq_entry eq_entry;
  20. };
  21. /**
  22. * hl_cq_inc_ptr - increment ci or pi of cq
  23. *
  24. * @ptr: the current ci or pi value of the completion queue
  25. *
  26. * Increment ptr by 1. If it reaches the number of completion queue
  27. * entries, set it to 0
  28. */
  29. inline u32 hl_cq_inc_ptr(u32 ptr)
  30. {
  31. ptr++;
  32. if (unlikely(ptr == HL_CQ_LENGTH))
  33. ptr = 0;
  34. return ptr;
  35. }
  36. /**
  37. * hl_eq_inc_ptr - increment ci of eq
  38. *
  39. * @ptr: the current ci value of the event queue
  40. *
  41. * Increment ptr by 1. If it reaches the number of event queue
  42. * entries, set it to 0
  43. */
  44. static inline u32 hl_eq_inc_ptr(u32 ptr)
  45. {
  46. ptr++;
  47. if (unlikely(ptr == HL_EQ_LENGTH))
  48. ptr = 0;
  49. return ptr;
  50. }
  51. static void irq_handle_eqe(struct work_struct *work)
  52. {
  53. struct hl_eqe_work *eqe_work = container_of(work, struct hl_eqe_work,
  54. eq_work);
  55. struct hl_device *hdev = eqe_work->hdev;
  56. hdev->asic_funcs->handle_eqe(hdev, &eqe_work->eq_entry);
  57. kfree(eqe_work);
  58. }
  59. /**
  60. * job_finish - queue job finish work
  61. *
  62. * @hdev: pointer to device structure
  63. * @cs_seq: command submission sequence
  64. * @cq: completion queue
  65. *
  66. */
  67. static void job_finish(struct hl_device *hdev, u32 cs_seq, struct hl_cq *cq)
  68. {
  69. struct hl_hw_queue *queue;
  70. struct hl_cs_job *job;
  71. queue = &hdev->kernel_queues[cq->hw_queue_id];
  72. job = queue->shadow_queue[hl_pi_2_offset(cs_seq)];
  73. queue_work(hdev->cq_wq[cq->cq_idx], &job->finish_work);
  74. atomic_inc(&queue->ci);
  75. }
  76. /**
  77. * cs_finish - queue all cs jobs finish work
  78. *
  79. * @hdev: pointer to device structure
  80. * @cs_seq: command submission sequence
  81. *
  82. */
  83. static void cs_finish(struct hl_device *hdev, u16 cs_seq)
  84. {
  85. struct asic_fixed_properties *prop = &hdev->asic_prop;
  86. struct hl_hw_queue *queue;
  87. struct hl_cs *cs;
  88. struct hl_cs_job *job;
  89. cs = hdev->shadow_cs_queue[cs_seq & (prop->max_pending_cs - 1)];
  90. if (!cs) {
  91. dev_warn(hdev->dev,
  92. "No pointer to CS in shadow array at index %d\n",
  93. cs_seq);
  94. return;
  95. }
  96. list_for_each_entry(job, &cs->job_list, cs_node) {
  97. queue = &hdev->kernel_queues[job->hw_queue_id];
  98. atomic_inc(&queue->ci);
  99. }
  100. queue_work(hdev->cs_cmplt_wq, &cs->finish_work);
  101. }
  102. /**
  103. * hl_irq_handler_cq - irq handler for completion queue
  104. *
  105. * @irq: irq number
  106. * @arg: pointer to completion queue structure
  107. *
  108. */
  109. irqreturn_t hl_irq_handler_cq(int irq, void *arg)
  110. {
  111. struct hl_cq *cq = arg;
  112. struct hl_device *hdev = cq->hdev;
  113. bool shadow_index_valid, entry_ready;
  114. u16 shadow_index;
  115. struct hl_cq_entry *cq_entry, *cq_base;
  116. if (hdev->disabled) {
  117. dev_dbg(hdev->dev,
  118. "Device disabled but received IRQ %d for CQ %d\n",
  119. irq, cq->hw_queue_id);
  120. return IRQ_HANDLED;
  121. }
  122. cq_base = cq->kernel_address;
  123. while (1) {
  124. cq_entry = (struct hl_cq_entry *) &cq_base[cq->ci];
  125. entry_ready = !!FIELD_GET(CQ_ENTRY_READY_MASK,
  126. le32_to_cpu(cq_entry->data));
  127. if (!entry_ready)
  128. break;
  129. /* Make sure we read CQ entry contents after we've
  130. * checked the ownership bit.
  131. */
  132. dma_rmb();
  133. shadow_index_valid =
  134. !!FIELD_GET(CQ_ENTRY_SHADOW_INDEX_VALID_MASK,
  135. le32_to_cpu(cq_entry->data));
  136. shadow_index = FIELD_GET(CQ_ENTRY_SHADOW_INDEX_MASK,
  137. le32_to_cpu(cq_entry->data));
  138. /*
  139. * CQ interrupt handler has 2 modes of operation:
  140. * 1. Interrupt per CS completion: (Single CQ for all queues)
  141. * CQ entry represents a completed CS
  142. *
  143. * 2. Interrupt per CS job completion in queue: (CQ per queue)
  144. * CQ entry represents a completed job in a certain queue
  145. */
  146. if (shadow_index_valid && !hdev->disabled) {
  147. if (hdev->asic_prop.completion_mode ==
  148. HL_COMPLETION_MODE_CS)
  149. cs_finish(hdev, shadow_index);
  150. else
  151. job_finish(hdev, shadow_index, cq);
  152. }
  153. /* Clear CQ entry ready bit */
  154. cq_entry->data = cpu_to_le32(le32_to_cpu(cq_entry->data) &
  155. ~CQ_ENTRY_READY_MASK);
  156. cq->ci = hl_cq_inc_ptr(cq->ci);
  157. /* Increment free slots */
  158. atomic_inc(&cq->free_slots_cnt);
  159. }
  160. return IRQ_HANDLED;
  161. }
  162. /*
  163. * hl_ts_free_objects - handler of the free objects workqueue.
  164. * This function should put refcount to objects that the registration node
  165. * took refcount to them.
  166. * @work: workqueue object pointer
  167. */
  168. static void hl_ts_free_objects(struct work_struct *work)
  169. {
  170. struct timestamp_reg_work_obj *job =
  171. container_of(work, struct timestamp_reg_work_obj, free_obj);
  172. struct timestamp_reg_free_node *free_obj, *temp_free_obj;
  173. struct list_head *free_list_head = job->free_obj_head;
  174. struct hl_device *hdev = job->hdev;
  175. list_for_each_entry_safe(free_obj, temp_free_obj, free_list_head, free_objects_node) {
  176. dev_dbg(hdev->dev, "About to put refcount to buf (%p) cq_cb(%p)\n",
  177. free_obj->buf,
  178. free_obj->cq_cb);
  179. hl_mmap_mem_buf_put(free_obj->buf);
  180. hl_cb_put(free_obj->cq_cb);
  181. kfree(free_obj);
  182. }
  183. kfree(free_list_head);
  184. kfree(job);
  185. }
  186. /*
  187. * This function called with spin_lock of wait_list_lock taken
  188. * This function will set timestamp and delete the registration node from the
  189. * wait_list_lock.
  190. * and since we're protected with spin_lock here, so we cannot just put the refcount
  191. * for the objects here, since the release function may be called and it's also a long
  192. * logic (which might sleep also) that cannot be handled in irq context.
  193. * so here we'll be filling a list with nodes of "put" jobs and then will send this
  194. * list to a dedicated workqueue to do the actual put.
  195. */
  196. static int handle_registration_node(struct hl_device *hdev, struct hl_user_pending_interrupt *pend,
  197. struct list_head **free_list)
  198. {
  199. struct timestamp_reg_free_node *free_node;
  200. u64 timestamp;
  201. if (!(*free_list)) {
  202. /* Alloc/Init the timestamp registration free objects list */
  203. *free_list = kmalloc(sizeof(struct list_head), GFP_ATOMIC);
  204. if (!(*free_list))
  205. return -ENOMEM;
  206. INIT_LIST_HEAD(*free_list);
  207. }
  208. free_node = kmalloc(sizeof(*free_node), GFP_ATOMIC);
  209. if (!free_node)
  210. return -ENOMEM;
  211. timestamp = ktime_get_ns();
  212. *pend->ts_reg_info.timestamp_kernel_addr = timestamp;
  213. dev_dbg(hdev->dev, "Timestamp is set to ts cb address (%p), ts: 0x%llx\n",
  214. pend->ts_reg_info.timestamp_kernel_addr,
  215. *(u64 *)pend->ts_reg_info.timestamp_kernel_addr);
  216. list_del(&pend->wait_list_node);
  217. /* Mark kernel CB node as free */
  218. pend->ts_reg_info.in_use = 0;
  219. /* Putting the refcount for ts_buff and cq_cb objects will be handled
  220. * in workqueue context, just add job to free_list.
  221. */
  222. free_node->buf = pend->ts_reg_info.buf;
  223. free_node->cq_cb = pend->ts_reg_info.cq_cb;
  224. list_add(&free_node->free_objects_node, *free_list);
  225. return 0;
  226. }
  227. static void handle_user_interrupt(struct hl_device *hdev, struct hl_user_interrupt *intr)
  228. {
  229. struct hl_user_pending_interrupt *pend, *temp_pend;
  230. struct list_head *ts_reg_free_list_head = NULL;
  231. struct timestamp_reg_work_obj *job;
  232. bool reg_node_handle_fail = false;
  233. ktime_t now = ktime_get();
  234. int rc;
  235. /* For registration nodes:
  236. * As part of handling the registration nodes, we should put refcount to
  237. * some objects. the problem is that we cannot do that under spinlock
  238. * or in irq handler context at all (since release functions are long and
  239. * might sleep), so we will need to handle that part in workqueue context.
  240. * To avoid handling kmalloc failure which compels us rolling back actions
  241. * and move nodes hanged on the free list back to the interrupt wait list
  242. * we always alloc the job of the WQ at the beginning.
  243. */
  244. job = kmalloc(sizeof(*job), GFP_ATOMIC);
  245. if (!job)
  246. return;
  247. spin_lock(&intr->wait_list_lock);
  248. list_for_each_entry_safe(pend, temp_pend, &intr->wait_list_head, wait_list_node) {
  249. if ((pend->cq_kernel_addr && *(pend->cq_kernel_addr) >= pend->cq_target_value) ||
  250. !pend->cq_kernel_addr) {
  251. if (pend->ts_reg_info.buf) {
  252. if (!reg_node_handle_fail) {
  253. rc = handle_registration_node(hdev, pend,
  254. &ts_reg_free_list_head);
  255. if (rc)
  256. reg_node_handle_fail = true;
  257. }
  258. } else {
  259. /* Handle wait target value node */
  260. pend->fence.timestamp = now;
  261. complete_all(&pend->fence.completion);
  262. }
  263. }
  264. }
  265. spin_unlock(&intr->wait_list_lock);
  266. if (ts_reg_free_list_head) {
  267. INIT_WORK(&job->free_obj, hl_ts_free_objects);
  268. job->free_obj_head = ts_reg_free_list_head;
  269. job->hdev = hdev;
  270. queue_work(hdev->ts_free_obj_wq, &job->free_obj);
  271. } else {
  272. kfree(job);
  273. }
  274. }
  275. /**
  276. * hl_irq_handler_user_interrupt - irq handler for user interrupts
  277. *
  278. * @irq: irq number
  279. * @arg: pointer to user interrupt structure
  280. *
  281. */
  282. irqreturn_t hl_irq_handler_user_interrupt(int irq, void *arg)
  283. {
  284. struct hl_user_interrupt *user_int = arg;
  285. struct hl_device *hdev = user_int->hdev;
  286. if (user_int->is_decoder)
  287. handle_user_interrupt(hdev, &hdev->common_decoder_interrupt);
  288. else
  289. handle_user_interrupt(hdev, &hdev->common_user_cq_interrupt);
  290. /* Handle user cq or decoder interrupts registered on this specific irq */
  291. handle_user_interrupt(hdev, user_int);
  292. return IRQ_HANDLED;
  293. }
  294. /**
  295. * hl_irq_handler_default - default irq handler
  296. *
  297. * @irq: irq number
  298. * @arg: pointer to user interrupt structure
  299. *
  300. */
  301. irqreturn_t hl_irq_handler_default(int irq, void *arg)
  302. {
  303. struct hl_user_interrupt *user_interrupt = arg;
  304. struct hl_device *hdev = user_interrupt->hdev;
  305. u32 interrupt_id = user_interrupt->interrupt_id;
  306. dev_err(hdev->dev, "got invalid user interrupt %u", interrupt_id);
  307. return IRQ_HANDLED;
  308. }
  309. /**
  310. * hl_irq_handler_eq - irq handler for event queue
  311. *
  312. * @irq: irq number
  313. * @arg: pointer to event queue structure
  314. *
  315. */
  316. irqreturn_t hl_irq_handler_eq(int irq, void *arg)
  317. {
  318. struct hl_eq *eq = arg;
  319. struct hl_device *hdev = eq->hdev;
  320. struct hl_eq_entry *eq_entry;
  321. struct hl_eq_entry *eq_base;
  322. struct hl_eqe_work *handle_eqe_work;
  323. bool entry_ready;
  324. u32 cur_eqe;
  325. u16 cur_eqe_index;
  326. eq_base = eq->kernel_address;
  327. while (1) {
  328. cur_eqe = le32_to_cpu(eq_base[eq->ci].hdr.ctl);
  329. entry_ready = !!FIELD_GET(EQ_CTL_READY_MASK, cur_eqe);
  330. if (!entry_ready)
  331. break;
  332. cur_eqe_index = FIELD_GET(EQ_CTL_INDEX_MASK, cur_eqe);
  333. if ((hdev->event_queue.check_eqe_index) &&
  334. (((eq->prev_eqe_index + 1) & EQ_CTL_INDEX_MASK)
  335. != cur_eqe_index)) {
  336. dev_dbg(hdev->dev,
  337. "EQE 0x%x in queue is ready but index does not match %d!=%d",
  338. eq_base[eq->ci].hdr.ctl,
  339. ((eq->prev_eqe_index + 1) & EQ_CTL_INDEX_MASK),
  340. cur_eqe_index);
  341. break;
  342. }
  343. eq->prev_eqe_index++;
  344. eq_entry = &eq_base[eq->ci];
  345. /*
  346. * Make sure we read EQ entry contents after we've
  347. * checked the ownership bit.
  348. */
  349. dma_rmb();
  350. if (hdev->disabled && !hdev->reset_info.in_compute_reset) {
  351. dev_warn(hdev->dev, "Device disabled but received an EQ event\n");
  352. goto skip_irq;
  353. }
  354. handle_eqe_work = kmalloc(sizeof(*handle_eqe_work), GFP_ATOMIC);
  355. if (handle_eqe_work) {
  356. INIT_WORK(&handle_eqe_work->eq_work, irq_handle_eqe);
  357. handle_eqe_work->hdev = hdev;
  358. memcpy(&handle_eqe_work->eq_entry, eq_entry,
  359. sizeof(*eq_entry));
  360. queue_work(hdev->eq_wq, &handle_eqe_work->eq_work);
  361. }
  362. skip_irq:
  363. /* Clear EQ entry ready bit */
  364. eq_entry->hdr.ctl =
  365. cpu_to_le32(le32_to_cpu(eq_entry->hdr.ctl) &
  366. ~EQ_CTL_READY_MASK);
  367. eq->ci = hl_eq_inc_ptr(eq->ci);
  368. hdev->asic_funcs->update_eq_ci(hdev, eq->ci);
  369. }
  370. return IRQ_HANDLED;
  371. }
  372. /**
  373. * hl_irq_handler_dec_abnrm - Decoder error interrupt handler
  374. * @irq: IRQ number
  375. * @arg: pointer to decoder structure.
  376. */
  377. irqreturn_t hl_irq_handler_dec_abnrm(int irq, void *arg)
  378. {
  379. struct hl_dec *dec = arg;
  380. schedule_work(&dec->completion_abnrm_work);
  381. return IRQ_HANDLED;
  382. }
  383. /**
  384. * hl_cq_init - main initialization function for an cq object
  385. *
  386. * @hdev: pointer to device structure
  387. * @q: pointer to cq structure
  388. * @hw_queue_id: The H/W queue ID this completion queue belongs to
  389. * HL_INVALID_QUEUE if cq is not attached to any specific queue
  390. *
  391. * Allocate dma-able memory for the completion queue and initialize fields
  392. * Returns 0 on success
  393. */
  394. int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id)
  395. {
  396. void *p;
  397. p = hl_asic_dma_alloc_coherent(hdev, HL_CQ_SIZE_IN_BYTES, &q->bus_address,
  398. GFP_KERNEL | __GFP_ZERO);
  399. if (!p)
  400. return -ENOMEM;
  401. q->hdev = hdev;
  402. q->kernel_address = p;
  403. q->hw_queue_id = hw_queue_id;
  404. q->ci = 0;
  405. q->pi = 0;
  406. atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH);
  407. return 0;
  408. }
  409. /**
  410. * hl_cq_fini - destroy completion queue
  411. *
  412. * @hdev: pointer to device structure
  413. * @q: pointer to cq structure
  414. *
  415. * Free the completion queue memory
  416. */
  417. void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q)
  418. {
  419. hl_asic_dma_free_coherent(hdev, HL_CQ_SIZE_IN_BYTES, q->kernel_address, q->bus_address);
  420. }
  421. void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q)
  422. {
  423. q->ci = 0;
  424. q->pi = 0;
  425. atomic_set(&q->free_slots_cnt, HL_CQ_LENGTH);
  426. /*
  427. * It's not enough to just reset the PI/CI because the H/W may have
  428. * written valid completion entries before it was halted and therefore
  429. * we need to clean the actual queues so we won't process old entries
  430. * when the device is operational again
  431. */
  432. memset(q->kernel_address, 0, HL_CQ_SIZE_IN_BYTES);
  433. }
  434. /**
  435. * hl_eq_init - main initialization function for an event queue object
  436. *
  437. * @hdev: pointer to device structure
  438. * @q: pointer to eq structure
  439. *
  440. * Allocate dma-able memory for the event queue and initialize fields
  441. * Returns 0 on success
  442. */
  443. int hl_eq_init(struct hl_device *hdev, struct hl_eq *q)
  444. {
  445. void *p;
  446. p = hl_cpu_accessible_dma_pool_alloc(hdev, HL_EQ_SIZE_IN_BYTES, &q->bus_address);
  447. if (!p)
  448. return -ENOMEM;
  449. q->hdev = hdev;
  450. q->kernel_address = p;
  451. q->ci = 0;
  452. q->prev_eqe_index = 0;
  453. return 0;
  454. }
  455. /**
  456. * hl_eq_fini - destroy event queue
  457. *
  458. * @hdev: pointer to device structure
  459. * @q: pointer to eq structure
  460. *
  461. * Free the event queue memory
  462. */
  463. void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q)
  464. {
  465. flush_workqueue(hdev->eq_wq);
  466. hl_cpu_accessible_dma_pool_free(hdev, HL_EQ_SIZE_IN_BYTES, q->kernel_address);
  467. }
  468. void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q)
  469. {
  470. q->ci = 0;
  471. q->prev_eqe_index = 0;
  472. /*
  473. * It's not enough to just reset the PI/CI because the H/W may have
  474. * written valid completion entries before it was halted and therefore
  475. * we need to clean the actual queues so we won't process old entries
  476. * when the device is operational again
  477. */
  478. memset(q->kernel_address, 0, HL_EQ_SIZE_IN_BYTES);
  479. }