hw_queue.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016-2019 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. */
  6. #include "habanalabs.h"
  7. #include <linux/slab.h>
  8. /*
  9. * hl_queue_add_ptr - add to pi or ci and checks if it wraps around
  10. *
  11. * @ptr: the current pi/ci value
  12. * @val: the amount to add
  13. *
  14. * Add val to ptr. It can go until twice the queue length.
  15. */
  16. inline u32 hl_hw_queue_add_ptr(u32 ptr, u16 val)
  17. {
  18. ptr += val;
  19. ptr &= ((HL_QUEUE_LENGTH << 1) - 1);
  20. return ptr;
  21. }
  22. static inline int queue_ci_get(atomic_t *ci, u32 queue_len)
  23. {
  24. return atomic_read(ci) & ((queue_len << 1) - 1);
  25. }
  26. static inline int queue_free_slots(struct hl_hw_queue *q, u32 queue_len)
  27. {
  28. int delta = (q->pi - queue_ci_get(&q->ci, queue_len));
  29. if (delta >= 0)
  30. return (queue_len - delta);
  31. else
  32. return (abs(delta) - queue_len);
  33. }
  34. void hl_hw_queue_update_ci(struct hl_cs *cs)
  35. {
  36. struct hl_device *hdev = cs->ctx->hdev;
  37. struct hl_hw_queue *q;
  38. int i;
  39. if (hdev->disabled)
  40. return;
  41. q = &hdev->kernel_queues[0];
  42. /* There are no internal queues if H/W queues are being used */
  43. if (!hdev->asic_prop.max_queues || q->queue_type == QUEUE_TYPE_HW)
  44. return;
  45. /* We must increment CI for every queue that will never get a
  46. * completion, there are 2 scenarios this can happen:
  47. * 1. All queues of a non completion CS will never get a completion.
  48. * 2. Internal queues never gets completion.
  49. */
  50. for (i = 0 ; i < hdev->asic_prop.max_queues ; i++, q++) {
  51. if (!cs_needs_completion(cs) || q->queue_type == QUEUE_TYPE_INT)
  52. atomic_add(cs->jobs_in_queue_cnt[i], &q->ci);
  53. }
  54. }
  55. /*
  56. * hl_hw_queue_submit_bd() - Submit a buffer descriptor to an external or a
  57. * H/W queue.
  58. * @hdev: pointer to habanalabs device structure
  59. * @q: pointer to habanalabs queue structure
  60. * @ctl: BD's control word
  61. * @len: BD's length
  62. * @ptr: BD's pointer
  63. *
  64. * This function assumes there is enough space on the queue to submit a new
  65. * BD to it. It initializes the next BD and calls the device specific
  66. * function to set the pi (and doorbell)
  67. *
  68. * This function must be called when the scheduler mutex is taken
  69. *
  70. */
  71. void hl_hw_queue_submit_bd(struct hl_device *hdev, struct hl_hw_queue *q,
  72. u32 ctl, u32 len, u64 ptr)
  73. {
  74. struct hl_bd *bd;
  75. bd = q->kernel_address;
  76. bd += hl_pi_2_offset(q->pi);
  77. bd->ctl = cpu_to_le32(ctl);
  78. bd->len = cpu_to_le32(len);
  79. bd->ptr = cpu_to_le64(ptr);
  80. q->pi = hl_queue_inc_ptr(q->pi);
  81. hdev->asic_funcs->ring_doorbell(hdev, q->hw_queue_id, q->pi);
  82. }
  83. /*
  84. * ext_queue_sanity_checks - perform some sanity checks on external queue
  85. *
  86. * @hdev : pointer to hl_device structure
  87. * @q : pointer to hl_hw_queue structure
  88. * @num_of_entries : how many entries to check for space
  89. * @reserve_cq_entry : whether to reserve an entry in the cq
  90. *
  91. * H/W queues spinlock should be taken before calling this function
  92. *
  93. * Perform the following:
  94. * - Make sure we have enough space in the h/w queue
  95. * - Make sure we have enough space in the completion queue
  96. * - Reserve space in the completion queue (needs to be reversed if there
  97. * is a failure down the road before the actual submission of work). Only
  98. * do this action if reserve_cq_entry is true
  99. *
  100. */
  101. static int ext_queue_sanity_checks(struct hl_device *hdev,
  102. struct hl_hw_queue *q, int num_of_entries,
  103. bool reserve_cq_entry)
  104. {
  105. atomic_t *free_slots =
  106. &hdev->completion_queue[q->cq_id].free_slots_cnt;
  107. int free_slots_cnt;
  108. /* Check we have enough space in the queue */
  109. free_slots_cnt = queue_free_slots(q, HL_QUEUE_LENGTH);
  110. if (free_slots_cnt < num_of_entries) {
  111. dev_dbg(hdev->dev, "Queue %d doesn't have room for %d CBs\n",
  112. q->hw_queue_id, num_of_entries);
  113. return -EAGAIN;
  114. }
  115. if (reserve_cq_entry) {
  116. /*
  117. * Check we have enough space in the completion queue
  118. * Add -1 to counter (decrement) unless counter was already 0
  119. * In that case, CQ is full so we can't submit a new CB because
  120. * we won't get ack on its completion
  121. * atomic_add_unless will return 0 if counter was already 0
  122. */
  123. if (atomic_add_negative(num_of_entries * -1, free_slots)) {
  124. dev_dbg(hdev->dev, "No space for %d on CQ %d\n",
  125. num_of_entries, q->hw_queue_id);
  126. atomic_add(num_of_entries, free_slots);
  127. return -EAGAIN;
  128. }
  129. }
  130. return 0;
  131. }
  132. /*
  133. * int_queue_sanity_checks - perform some sanity checks on internal queue
  134. *
  135. * @hdev : pointer to hl_device structure
  136. * @q : pointer to hl_hw_queue structure
  137. * @num_of_entries : how many entries to check for space
  138. *
  139. * H/W queues spinlock should be taken before calling this function
  140. *
  141. * Perform the following:
  142. * - Make sure we have enough space in the h/w queue
  143. *
  144. */
  145. static int int_queue_sanity_checks(struct hl_device *hdev,
  146. struct hl_hw_queue *q,
  147. int num_of_entries)
  148. {
  149. int free_slots_cnt;
  150. if (num_of_entries > q->int_queue_len) {
  151. dev_err(hdev->dev,
  152. "Cannot populate queue %u with %u jobs\n",
  153. q->hw_queue_id, num_of_entries);
  154. return -ENOMEM;
  155. }
  156. /* Check we have enough space in the queue */
  157. free_slots_cnt = queue_free_slots(q, q->int_queue_len);
  158. if (free_slots_cnt < num_of_entries) {
  159. dev_dbg(hdev->dev, "Queue %d doesn't have room for %d CBs\n",
  160. q->hw_queue_id, num_of_entries);
  161. return -EAGAIN;
  162. }
  163. return 0;
  164. }
  165. /*
  166. * hw_queue_sanity_checks() - Make sure we have enough space in the h/w queue
  167. * @hdev: Pointer to hl_device structure.
  168. * @q: Pointer to hl_hw_queue structure.
  169. * @num_of_entries: How many entries to check for space.
  170. *
  171. * Notice: We do not reserve queue entries so this function mustn't be called
  172. * more than once per CS for the same queue
  173. *
  174. */
  175. static int hw_queue_sanity_checks(struct hl_device *hdev, struct hl_hw_queue *q,
  176. int num_of_entries)
  177. {
  178. int free_slots_cnt;
  179. /* Check we have enough space in the queue */
  180. free_slots_cnt = queue_free_slots(q, HL_QUEUE_LENGTH);
  181. if (free_slots_cnt < num_of_entries) {
  182. dev_dbg(hdev->dev, "Queue %d doesn't have room for %d CBs\n",
  183. q->hw_queue_id, num_of_entries);
  184. return -EAGAIN;
  185. }
  186. return 0;
  187. }
  188. /*
  189. * hl_hw_queue_send_cb_no_cmpl - send a single CB (not a JOB) without completion
  190. *
  191. * @hdev: pointer to hl_device structure
  192. * @hw_queue_id: Queue's type
  193. * @cb_size: size of CB
  194. * @cb_ptr: pointer to CB location
  195. *
  196. * This function sends a single CB, that must NOT generate a completion entry.
  197. * Sending CPU messages can be done instead via 'hl_hw_queue_submit_bd()'
  198. */
  199. int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
  200. u32 cb_size, u64 cb_ptr)
  201. {
  202. struct hl_hw_queue *q = &hdev->kernel_queues[hw_queue_id];
  203. int rc = 0;
  204. hdev->asic_funcs->hw_queues_lock(hdev);
  205. if (hdev->disabled) {
  206. rc = -EPERM;
  207. goto out;
  208. }
  209. /*
  210. * hl_hw_queue_send_cb_no_cmpl() is called for queues of a H/W queue
  211. * type only on init phase, when the queues are empty and being tested,
  212. * so there is no need for sanity checks.
  213. */
  214. if (q->queue_type != QUEUE_TYPE_HW) {
  215. rc = ext_queue_sanity_checks(hdev, q, 1, false);
  216. if (rc)
  217. goto out;
  218. }
  219. hl_hw_queue_submit_bd(hdev, q, 0, cb_size, cb_ptr);
  220. out:
  221. hdev->asic_funcs->hw_queues_unlock(hdev);
  222. return rc;
  223. }
  224. /*
  225. * ext_queue_schedule_job - submit a JOB to an external queue
  226. *
  227. * @job: pointer to the job that needs to be submitted to the queue
  228. *
  229. * This function must be called when the scheduler mutex is taken
  230. *
  231. */
  232. static void ext_queue_schedule_job(struct hl_cs_job *job)
  233. {
  234. struct hl_device *hdev = job->cs->ctx->hdev;
  235. struct hl_hw_queue *q = &hdev->kernel_queues[job->hw_queue_id];
  236. struct hl_cq_entry cq_pkt;
  237. struct hl_cq *cq;
  238. u64 cq_addr;
  239. struct hl_cb *cb;
  240. u32 ctl;
  241. u32 len;
  242. u64 ptr;
  243. /*
  244. * Update the JOB ID inside the BD CTL so the device would know what
  245. * to write in the completion queue
  246. */
  247. ctl = ((q->pi << BD_CTL_SHADOW_INDEX_SHIFT) & BD_CTL_SHADOW_INDEX_MASK);
  248. cb = job->patched_cb;
  249. len = job->job_cb_size;
  250. ptr = cb->bus_address;
  251. /* Skip completion flow in case this is a non completion CS */
  252. if (!cs_needs_completion(job->cs))
  253. goto submit_bd;
  254. cq_pkt.data = cpu_to_le32(
  255. ((q->pi << CQ_ENTRY_SHADOW_INDEX_SHIFT)
  256. & CQ_ENTRY_SHADOW_INDEX_MASK) |
  257. FIELD_PREP(CQ_ENTRY_SHADOW_INDEX_VALID_MASK, 1) |
  258. FIELD_PREP(CQ_ENTRY_READY_MASK, 1));
  259. /*
  260. * No need to protect pi_offset because scheduling to the
  261. * H/W queues is done under the scheduler mutex
  262. *
  263. * No need to check if CQ is full because it was already
  264. * checked in ext_queue_sanity_checks
  265. */
  266. cq = &hdev->completion_queue[q->cq_id];
  267. cq_addr = cq->bus_address + cq->pi * sizeof(struct hl_cq_entry);
  268. hdev->asic_funcs->add_end_of_cb_packets(hdev, cb->kernel_address, len,
  269. job->user_cb_size,
  270. cq_addr,
  271. le32_to_cpu(cq_pkt.data),
  272. q->msi_vec,
  273. job->contains_dma_pkt);
  274. q->shadow_queue[hl_pi_2_offset(q->pi)] = job;
  275. cq->pi = hl_cq_inc_ptr(cq->pi);
  276. submit_bd:
  277. hl_hw_queue_submit_bd(hdev, q, ctl, len, ptr);
  278. }
  279. /*
  280. * int_queue_schedule_job - submit a JOB to an internal queue
  281. *
  282. * @job: pointer to the job that needs to be submitted to the queue
  283. *
  284. * This function must be called when the scheduler mutex is taken
  285. *
  286. */
  287. static void int_queue_schedule_job(struct hl_cs_job *job)
  288. {
  289. struct hl_device *hdev = job->cs->ctx->hdev;
  290. struct hl_hw_queue *q = &hdev->kernel_queues[job->hw_queue_id];
  291. struct hl_bd bd;
  292. __le64 *pi;
  293. bd.ctl = 0;
  294. bd.len = cpu_to_le32(job->job_cb_size);
  295. if (job->is_kernel_allocated_cb)
  296. /* bus_address is actually a mmu mapped address
  297. * allocated from an internal pool
  298. */
  299. bd.ptr = cpu_to_le64(job->user_cb->bus_address);
  300. else
  301. bd.ptr = cpu_to_le64((u64) (uintptr_t) job->user_cb);
  302. pi = q->kernel_address + (q->pi & (q->int_queue_len - 1)) * sizeof(bd);
  303. q->pi++;
  304. q->pi &= ((q->int_queue_len << 1) - 1);
  305. hdev->asic_funcs->pqe_write(hdev, pi, &bd);
  306. hdev->asic_funcs->ring_doorbell(hdev, q->hw_queue_id, q->pi);
  307. }
  308. /*
  309. * hw_queue_schedule_job - submit a JOB to a H/W queue
  310. *
  311. * @job: pointer to the job that needs to be submitted to the queue
  312. *
  313. * This function must be called when the scheduler mutex is taken
  314. *
  315. */
  316. static void hw_queue_schedule_job(struct hl_cs_job *job)
  317. {
  318. struct hl_device *hdev = job->cs->ctx->hdev;
  319. struct hl_hw_queue *q = &hdev->kernel_queues[job->hw_queue_id];
  320. u64 ptr;
  321. u32 offset, ctl, len;
  322. /*
  323. * Upon PQE completion, COMP_DATA is used as the write data to the
  324. * completion queue (QMAN HBW message), and COMP_OFFSET is used as the
  325. * write address offset in the SM block (QMAN LBW message).
  326. * The write address offset is calculated as "COMP_OFFSET << 2".
  327. */
  328. offset = job->cs->sequence & (hdev->asic_prop.max_pending_cs - 1);
  329. ctl = ((offset << BD_CTL_COMP_OFFSET_SHIFT) & BD_CTL_COMP_OFFSET_MASK) |
  330. ((q->pi << BD_CTL_COMP_DATA_SHIFT) & BD_CTL_COMP_DATA_MASK);
  331. len = job->job_cb_size;
  332. /*
  333. * A patched CB is created only if a user CB was allocated by driver and
  334. * MMU is disabled. If MMU is enabled, the user CB should be used
  335. * instead. If the user CB wasn't allocated by driver, assume that it
  336. * holds an address.
  337. */
  338. if (job->patched_cb)
  339. ptr = job->patched_cb->bus_address;
  340. else if (job->is_kernel_allocated_cb)
  341. ptr = job->user_cb->bus_address;
  342. else
  343. ptr = (u64) (uintptr_t) job->user_cb;
  344. hl_hw_queue_submit_bd(hdev, q, ctl, len, ptr);
  345. }
  346. static int init_signal_cs(struct hl_device *hdev,
  347. struct hl_cs_job *job, struct hl_cs_compl *cs_cmpl)
  348. {
  349. struct hl_sync_stream_properties *prop;
  350. struct hl_hw_sob *hw_sob;
  351. u32 q_idx;
  352. int rc = 0;
  353. q_idx = job->hw_queue_id;
  354. prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
  355. hw_sob = &prop->hw_sob[prop->curr_sob_offset];
  356. cs_cmpl->hw_sob = hw_sob;
  357. cs_cmpl->sob_val = prop->next_sob_val;
  358. dev_dbg(hdev->dev,
  359. "generate signal CB, sob_id: %d, sob val: %u, q_idx: %d, seq: %llu\n",
  360. cs_cmpl->hw_sob->sob_id, cs_cmpl->sob_val, q_idx,
  361. cs_cmpl->cs_seq);
  362. /* we set an EB since we must make sure all oeprations are done
  363. * when sending the signal
  364. */
  365. hdev->asic_funcs->gen_signal_cb(hdev, job->patched_cb,
  366. cs_cmpl->hw_sob->sob_id, 0, true);
  367. rc = hl_cs_signal_sob_wraparound_handler(hdev, q_idx, &hw_sob, 1,
  368. false);
  369. job->cs->sob_addr_offset = hw_sob->sob_addr;
  370. job->cs->initial_sob_count = prop->next_sob_val - 1;
  371. return rc;
  372. }
  373. void hl_hw_queue_encaps_sig_set_sob_info(struct hl_device *hdev,
  374. struct hl_cs *cs, struct hl_cs_job *job,
  375. struct hl_cs_compl *cs_cmpl)
  376. {
  377. struct hl_cs_encaps_sig_handle *handle = cs->encaps_sig_hdl;
  378. u32 offset = 0;
  379. cs_cmpl->hw_sob = handle->hw_sob;
  380. /* Note that encaps_sig_wait_offset was validated earlier in the flow
  381. * for offset value which exceeds the max reserved signal count.
  382. * always decrement 1 of the offset since when the user
  383. * set offset 1 for example he mean to wait only for the first
  384. * signal only, which will be pre_sob_val, and if he set offset 2
  385. * then the value required is (pre_sob_val + 1) and so on...
  386. * if user set wait offset to 0, then treat it as legacy wait cs,
  387. * wait for the next signal.
  388. */
  389. if (job->encaps_sig_wait_offset)
  390. offset = job->encaps_sig_wait_offset - 1;
  391. cs_cmpl->sob_val = handle->pre_sob_val + offset;
  392. }
  393. static int init_wait_cs(struct hl_device *hdev, struct hl_cs *cs,
  394. struct hl_cs_job *job, struct hl_cs_compl *cs_cmpl)
  395. {
  396. struct hl_gen_wait_properties wait_prop;
  397. struct hl_sync_stream_properties *prop;
  398. struct hl_cs_compl *signal_cs_cmpl;
  399. u32 q_idx;
  400. q_idx = job->hw_queue_id;
  401. prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
  402. signal_cs_cmpl = container_of(cs->signal_fence,
  403. struct hl_cs_compl,
  404. base_fence);
  405. if (cs->encaps_signals) {
  406. /* use the encaps signal handle stored earlier in the flow
  407. * and set the SOB information from the encaps
  408. * signals handle
  409. */
  410. hl_hw_queue_encaps_sig_set_sob_info(hdev, cs, job, cs_cmpl);
  411. dev_dbg(hdev->dev, "Wait for encaps signals handle, qidx(%u), CS sequence(%llu), sob val: 0x%x, offset: %u\n",
  412. cs->encaps_sig_hdl->q_idx,
  413. cs->encaps_sig_hdl->cs_seq,
  414. cs_cmpl->sob_val,
  415. job->encaps_sig_wait_offset);
  416. } else {
  417. /* Copy the SOB id and value of the signal CS */
  418. cs_cmpl->hw_sob = signal_cs_cmpl->hw_sob;
  419. cs_cmpl->sob_val = signal_cs_cmpl->sob_val;
  420. }
  421. /* check again if the signal cs already completed.
  422. * if yes then don't send any wait cs since the hw_sob
  423. * could be in reset already. if signal is not completed
  424. * then get refcount to hw_sob to prevent resetting the sob
  425. * while wait cs is not submitted.
  426. * note that this check is protected by two locks,
  427. * hw queue lock and completion object lock,
  428. * and the same completion object lock also protects
  429. * the hw_sob reset handler function.
  430. * The hw_queue lock prevent out of sync of hw_sob
  431. * refcount value, changed by signal/wait flows.
  432. */
  433. spin_lock(&signal_cs_cmpl->lock);
  434. if (completion_done(&cs->signal_fence->completion)) {
  435. spin_unlock(&signal_cs_cmpl->lock);
  436. return -EINVAL;
  437. }
  438. kref_get(&cs_cmpl->hw_sob->kref);
  439. spin_unlock(&signal_cs_cmpl->lock);
  440. dev_dbg(hdev->dev,
  441. "generate wait CB, sob_id: %d, sob_val: 0x%x, mon_id: %d, q_idx: %d, seq: %llu\n",
  442. cs_cmpl->hw_sob->sob_id, cs_cmpl->sob_val,
  443. prop->base_mon_id, q_idx, cs->sequence);
  444. wait_prop.data = (void *) job->patched_cb;
  445. wait_prop.sob_base = cs_cmpl->hw_sob->sob_id;
  446. wait_prop.sob_mask = 0x1;
  447. wait_prop.sob_val = cs_cmpl->sob_val;
  448. wait_prop.mon_id = prop->base_mon_id;
  449. wait_prop.q_idx = q_idx;
  450. wait_prop.size = 0;
  451. hdev->asic_funcs->gen_wait_cb(hdev, &wait_prop);
  452. mb();
  453. hl_fence_put(cs->signal_fence);
  454. cs->signal_fence = NULL;
  455. return 0;
  456. }
  457. /*
  458. * init_signal_wait_cs - initialize a signal/wait CS
  459. * @cs: pointer to the signal/wait CS
  460. *
  461. * H/W queues spinlock should be taken before calling this function
  462. */
  463. static int init_signal_wait_cs(struct hl_cs *cs)
  464. {
  465. struct hl_ctx *ctx = cs->ctx;
  466. struct hl_device *hdev = ctx->hdev;
  467. struct hl_cs_job *job;
  468. struct hl_cs_compl *cs_cmpl =
  469. container_of(cs->fence, struct hl_cs_compl, base_fence);
  470. int rc = 0;
  471. /* There is only one job in a signal/wait CS */
  472. job = list_first_entry(&cs->job_list, struct hl_cs_job,
  473. cs_node);
  474. if (cs->type & CS_TYPE_SIGNAL)
  475. rc = init_signal_cs(hdev, job, cs_cmpl);
  476. else if (cs->type & CS_TYPE_WAIT)
  477. rc = init_wait_cs(hdev, cs, job, cs_cmpl);
  478. return rc;
  479. }
  480. static int encaps_sig_first_staged_cs_handler
  481. (struct hl_device *hdev, struct hl_cs *cs)
  482. {
  483. struct hl_cs_compl *cs_cmpl =
  484. container_of(cs->fence,
  485. struct hl_cs_compl, base_fence);
  486. struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
  487. struct hl_encaps_signals_mgr *mgr;
  488. int rc = 0;
  489. mgr = &cs->ctx->sig_mgr;
  490. spin_lock(&mgr->lock);
  491. encaps_sig_hdl = idr_find(&mgr->handles, cs->encaps_sig_hdl_id);
  492. if (encaps_sig_hdl) {
  493. /*
  494. * Set handler CS sequence,
  495. * the CS which contains the encapsulated signals.
  496. */
  497. encaps_sig_hdl->cs_seq = cs->sequence;
  498. /* store the handle and set encaps signal indication,
  499. * to be used later in cs_do_release to put the last
  500. * reference to encaps signals handlers.
  501. */
  502. cs_cmpl->encaps_signals = true;
  503. cs_cmpl->encaps_sig_hdl = encaps_sig_hdl;
  504. /* set hw_sob pointer in completion object
  505. * since it's used in cs_do_release flow to put
  506. * refcount to sob
  507. */
  508. cs_cmpl->hw_sob = encaps_sig_hdl->hw_sob;
  509. cs_cmpl->sob_val = encaps_sig_hdl->pre_sob_val +
  510. encaps_sig_hdl->count;
  511. dev_dbg(hdev->dev, "CS seq (%llu) added to encaps signal handler id (%u), count(%u), qidx(%u), sob(%u), val(%u)\n",
  512. cs->sequence, encaps_sig_hdl->id,
  513. encaps_sig_hdl->count,
  514. encaps_sig_hdl->q_idx,
  515. cs_cmpl->hw_sob->sob_id,
  516. cs_cmpl->sob_val);
  517. } else {
  518. dev_err(hdev->dev, "encaps handle id(%u) wasn't found!\n",
  519. cs->encaps_sig_hdl_id);
  520. rc = -EINVAL;
  521. }
  522. spin_unlock(&mgr->lock);
  523. return rc;
  524. }
  525. /*
  526. * hl_hw_queue_schedule_cs - schedule a command submission
  527. * @cs: pointer to the CS
  528. */
  529. int hl_hw_queue_schedule_cs(struct hl_cs *cs)
  530. {
  531. enum hl_device_status status;
  532. struct hl_cs_counters_atomic *cntr;
  533. struct hl_ctx *ctx = cs->ctx;
  534. struct hl_device *hdev = ctx->hdev;
  535. struct hl_cs_job *job, *tmp;
  536. struct hl_hw_queue *q;
  537. int rc = 0, i, cq_cnt;
  538. bool first_entry;
  539. u32 max_queues;
  540. cntr = &hdev->aggregated_cs_counters;
  541. hdev->asic_funcs->hw_queues_lock(hdev);
  542. if (!hl_device_operational(hdev, &status)) {
  543. atomic64_inc(&cntr->device_in_reset_drop_cnt);
  544. atomic64_inc(&ctx->cs_counters.device_in_reset_drop_cnt);
  545. dev_err(hdev->dev,
  546. "device is %s, CS rejected!\n", hdev->status[status]);
  547. rc = -EPERM;
  548. goto out;
  549. }
  550. max_queues = hdev->asic_prop.max_queues;
  551. q = &hdev->kernel_queues[0];
  552. for (i = 0, cq_cnt = 0 ; i < max_queues ; i++, q++) {
  553. if (cs->jobs_in_queue_cnt[i]) {
  554. switch (q->queue_type) {
  555. case QUEUE_TYPE_EXT:
  556. rc = ext_queue_sanity_checks(hdev, q,
  557. cs->jobs_in_queue_cnt[i],
  558. cs_needs_completion(cs) ?
  559. true : false);
  560. break;
  561. case QUEUE_TYPE_INT:
  562. rc = int_queue_sanity_checks(hdev, q,
  563. cs->jobs_in_queue_cnt[i]);
  564. break;
  565. case QUEUE_TYPE_HW:
  566. rc = hw_queue_sanity_checks(hdev, q,
  567. cs->jobs_in_queue_cnt[i]);
  568. break;
  569. default:
  570. dev_err(hdev->dev, "Queue type %d is invalid\n",
  571. q->queue_type);
  572. rc = -EINVAL;
  573. break;
  574. }
  575. if (rc) {
  576. atomic64_inc(
  577. &ctx->cs_counters.queue_full_drop_cnt);
  578. atomic64_inc(&cntr->queue_full_drop_cnt);
  579. goto unroll_cq_resv;
  580. }
  581. if (q->queue_type == QUEUE_TYPE_EXT)
  582. cq_cnt++;
  583. }
  584. }
  585. if ((cs->type == CS_TYPE_SIGNAL) || (cs->type == CS_TYPE_WAIT)) {
  586. rc = init_signal_wait_cs(cs);
  587. if (rc)
  588. goto unroll_cq_resv;
  589. } else if (cs->type == CS_TYPE_COLLECTIVE_WAIT) {
  590. rc = hdev->asic_funcs->collective_wait_init_cs(cs);
  591. if (rc)
  592. goto unroll_cq_resv;
  593. }
  594. rc = hdev->asic_funcs->pre_schedule_cs(cs);
  595. if (rc) {
  596. dev_err(hdev->dev,
  597. "Failed in pre-submission operations of CS %d.%llu\n",
  598. ctx->asid, cs->sequence);
  599. goto unroll_cq_resv;
  600. }
  601. hdev->shadow_cs_queue[cs->sequence &
  602. (hdev->asic_prop.max_pending_cs - 1)] = cs;
  603. if (cs->encaps_signals && cs->staged_first) {
  604. rc = encaps_sig_first_staged_cs_handler(hdev, cs);
  605. if (rc)
  606. goto unroll_cq_resv;
  607. }
  608. spin_lock(&hdev->cs_mirror_lock);
  609. /* Verify staged CS exists and add to the staged list */
  610. if (cs->staged_cs && !cs->staged_first) {
  611. struct hl_cs *staged_cs;
  612. staged_cs = hl_staged_cs_find_first(hdev, cs->staged_sequence);
  613. if (!staged_cs) {
  614. dev_err(hdev->dev,
  615. "Cannot find staged submission sequence %llu",
  616. cs->staged_sequence);
  617. rc = -EINVAL;
  618. goto unlock_cs_mirror;
  619. }
  620. if (is_staged_cs_last_exists(hdev, staged_cs)) {
  621. dev_err(hdev->dev,
  622. "Staged submission sequence %llu already submitted",
  623. cs->staged_sequence);
  624. rc = -EINVAL;
  625. goto unlock_cs_mirror;
  626. }
  627. list_add_tail(&cs->staged_cs_node, &staged_cs->staged_cs_node);
  628. /* update stream map of the first CS */
  629. if (hdev->supports_wait_for_multi_cs)
  630. staged_cs->fence->stream_master_qid_map |=
  631. cs->fence->stream_master_qid_map;
  632. }
  633. list_add_tail(&cs->mirror_node, &hdev->cs_mirror_list);
  634. /* Queue TDR if the CS is the first entry and if timeout is wanted */
  635. first_entry = list_first_entry(&hdev->cs_mirror_list,
  636. struct hl_cs, mirror_node) == cs;
  637. if ((hdev->timeout_jiffies != MAX_SCHEDULE_TIMEOUT) &&
  638. first_entry && cs_needs_timeout(cs)) {
  639. cs->tdr_active = true;
  640. schedule_delayed_work(&cs->work_tdr, cs->timeout_jiffies);
  641. }
  642. spin_unlock(&hdev->cs_mirror_lock);
  643. list_for_each_entry_safe(job, tmp, &cs->job_list, cs_node)
  644. switch (job->queue_type) {
  645. case QUEUE_TYPE_EXT:
  646. ext_queue_schedule_job(job);
  647. break;
  648. case QUEUE_TYPE_INT:
  649. int_queue_schedule_job(job);
  650. break;
  651. case QUEUE_TYPE_HW:
  652. hw_queue_schedule_job(job);
  653. break;
  654. default:
  655. break;
  656. }
  657. cs->submitted = true;
  658. goto out;
  659. unlock_cs_mirror:
  660. spin_unlock(&hdev->cs_mirror_lock);
  661. unroll_cq_resv:
  662. q = &hdev->kernel_queues[0];
  663. for (i = 0 ; (i < max_queues) && (cq_cnt > 0) ; i++, q++) {
  664. if ((q->queue_type == QUEUE_TYPE_EXT) &&
  665. (cs->jobs_in_queue_cnt[i])) {
  666. atomic_t *free_slots =
  667. &hdev->completion_queue[i].free_slots_cnt;
  668. atomic_add(cs->jobs_in_queue_cnt[i], free_slots);
  669. cq_cnt--;
  670. }
  671. }
  672. out:
  673. hdev->asic_funcs->hw_queues_unlock(hdev);
  674. return rc;
  675. }
  676. /*
  677. * hl_hw_queue_inc_ci_kernel - increment ci for kernel's queue
  678. *
  679. * @hdev: pointer to hl_device structure
  680. * @hw_queue_id: which queue to increment its ci
  681. */
  682. void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id)
  683. {
  684. struct hl_hw_queue *q = &hdev->kernel_queues[hw_queue_id];
  685. atomic_inc(&q->ci);
  686. }
  687. static int ext_and_cpu_queue_init(struct hl_device *hdev, struct hl_hw_queue *q,
  688. bool is_cpu_queue)
  689. {
  690. void *p;
  691. int rc;
  692. if (is_cpu_queue)
  693. p = hl_cpu_accessible_dma_pool_alloc(hdev, HL_QUEUE_SIZE_IN_BYTES, &q->bus_address);
  694. else
  695. p = hl_asic_dma_alloc_coherent(hdev, HL_QUEUE_SIZE_IN_BYTES, &q->bus_address,
  696. GFP_KERNEL | __GFP_ZERO);
  697. if (!p)
  698. return -ENOMEM;
  699. q->kernel_address = p;
  700. q->shadow_queue = kmalloc_array(HL_QUEUE_LENGTH, sizeof(struct hl_cs_job *), GFP_KERNEL);
  701. if (!q->shadow_queue) {
  702. dev_err(hdev->dev,
  703. "Failed to allocate shadow queue for H/W queue %d\n",
  704. q->hw_queue_id);
  705. rc = -ENOMEM;
  706. goto free_queue;
  707. }
  708. /* Make sure read/write pointers are initialized to start of queue */
  709. atomic_set(&q->ci, 0);
  710. q->pi = 0;
  711. return 0;
  712. free_queue:
  713. if (is_cpu_queue)
  714. hl_cpu_accessible_dma_pool_free(hdev, HL_QUEUE_SIZE_IN_BYTES, q->kernel_address);
  715. else
  716. hl_asic_dma_free_coherent(hdev, HL_QUEUE_SIZE_IN_BYTES, q->kernel_address,
  717. q->bus_address);
  718. return rc;
  719. }
  720. static int int_queue_init(struct hl_device *hdev, struct hl_hw_queue *q)
  721. {
  722. void *p;
  723. p = hdev->asic_funcs->get_int_queue_base(hdev, q->hw_queue_id,
  724. &q->bus_address, &q->int_queue_len);
  725. if (!p) {
  726. dev_err(hdev->dev,
  727. "Failed to get base address for internal queue %d\n",
  728. q->hw_queue_id);
  729. return -EFAULT;
  730. }
  731. q->kernel_address = p;
  732. q->pi = 0;
  733. atomic_set(&q->ci, 0);
  734. return 0;
  735. }
  736. static int cpu_queue_init(struct hl_device *hdev, struct hl_hw_queue *q)
  737. {
  738. return ext_and_cpu_queue_init(hdev, q, true);
  739. }
  740. static int ext_queue_init(struct hl_device *hdev, struct hl_hw_queue *q)
  741. {
  742. return ext_and_cpu_queue_init(hdev, q, false);
  743. }
  744. static int hw_queue_init(struct hl_device *hdev, struct hl_hw_queue *q)
  745. {
  746. void *p;
  747. p = hl_asic_dma_alloc_coherent(hdev, HL_QUEUE_SIZE_IN_BYTES, &q->bus_address,
  748. GFP_KERNEL | __GFP_ZERO);
  749. if (!p)
  750. return -ENOMEM;
  751. q->kernel_address = p;
  752. /* Make sure read/write pointers are initialized to start of queue */
  753. atomic_set(&q->ci, 0);
  754. q->pi = 0;
  755. return 0;
  756. }
  757. static void sync_stream_queue_init(struct hl_device *hdev, u32 q_idx)
  758. {
  759. struct hl_sync_stream_properties *sync_stream_prop;
  760. struct asic_fixed_properties *prop = &hdev->asic_prop;
  761. struct hl_hw_sob *hw_sob;
  762. int sob, reserved_mon_idx, queue_idx;
  763. sync_stream_prop = &hdev->kernel_queues[q_idx].sync_stream_prop;
  764. /* We use 'collective_mon_idx' as a running index in order to reserve
  765. * monitors for collective master/slave queues.
  766. * collective master queue gets 2 reserved monitors
  767. * collective slave queue gets 1 reserved monitor
  768. */
  769. if (hdev->kernel_queues[q_idx].collective_mode ==
  770. HL_COLLECTIVE_MASTER) {
  771. reserved_mon_idx = hdev->collective_mon_idx;
  772. /* reserve the first monitor for collective master queue */
  773. sync_stream_prop->collective_mstr_mon_id[0] =
  774. prop->collective_first_mon + reserved_mon_idx;
  775. /* reserve the second monitor for collective master queue */
  776. sync_stream_prop->collective_mstr_mon_id[1] =
  777. prop->collective_first_mon + reserved_mon_idx + 1;
  778. hdev->collective_mon_idx += HL_COLLECTIVE_RSVD_MSTR_MONS;
  779. } else if (hdev->kernel_queues[q_idx].collective_mode ==
  780. HL_COLLECTIVE_SLAVE) {
  781. reserved_mon_idx = hdev->collective_mon_idx++;
  782. /* reserve a monitor for collective slave queue */
  783. sync_stream_prop->collective_slave_mon_id =
  784. prop->collective_first_mon + reserved_mon_idx;
  785. }
  786. if (!hdev->kernel_queues[q_idx].supports_sync_stream)
  787. return;
  788. queue_idx = hdev->sync_stream_queue_idx++;
  789. sync_stream_prop->base_sob_id = prop->sync_stream_first_sob +
  790. (queue_idx * HL_RSVD_SOBS);
  791. sync_stream_prop->base_mon_id = prop->sync_stream_first_mon +
  792. (queue_idx * HL_RSVD_MONS);
  793. sync_stream_prop->next_sob_val = 1;
  794. sync_stream_prop->curr_sob_offset = 0;
  795. for (sob = 0 ; sob < HL_RSVD_SOBS ; sob++) {
  796. hw_sob = &sync_stream_prop->hw_sob[sob];
  797. hw_sob->hdev = hdev;
  798. hw_sob->sob_id = sync_stream_prop->base_sob_id + sob;
  799. hw_sob->sob_addr =
  800. hdev->asic_funcs->get_sob_addr(hdev, hw_sob->sob_id);
  801. hw_sob->q_idx = q_idx;
  802. kref_init(&hw_sob->kref);
  803. }
  804. }
  805. static void sync_stream_queue_reset(struct hl_device *hdev, u32 q_idx)
  806. {
  807. struct hl_sync_stream_properties *prop =
  808. &hdev->kernel_queues[q_idx].sync_stream_prop;
  809. /*
  810. * In case we got here due to a stuck CS, the refcnt might be bigger
  811. * than 1 and therefore we reset it.
  812. */
  813. kref_init(&prop->hw_sob[prop->curr_sob_offset].kref);
  814. prop->curr_sob_offset = 0;
  815. prop->next_sob_val = 1;
  816. }
  817. /*
  818. * queue_init - main initialization function for H/W queue object
  819. *
  820. * @hdev: pointer to hl_device device structure
  821. * @q: pointer to hl_hw_queue queue structure
  822. * @hw_queue_id: The id of the H/W queue
  823. *
  824. * Allocate dma-able memory for the queue and initialize fields
  825. * Returns 0 on success
  826. */
  827. static int queue_init(struct hl_device *hdev, struct hl_hw_queue *q,
  828. u32 hw_queue_id)
  829. {
  830. int rc;
  831. q->hw_queue_id = hw_queue_id;
  832. switch (q->queue_type) {
  833. case QUEUE_TYPE_EXT:
  834. rc = ext_queue_init(hdev, q);
  835. break;
  836. case QUEUE_TYPE_INT:
  837. rc = int_queue_init(hdev, q);
  838. break;
  839. case QUEUE_TYPE_CPU:
  840. rc = cpu_queue_init(hdev, q);
  841. break;
  842. case QUEUE_TYPE_HW:
  843. rc = hw_queue_init(hdev, q);
  844. break;
  845. case QUEUE_TYPE_NA:
  846. q->valid = 0;
  847. return 0;
  848. default:
  849. dev_crit(hdev->dev, "wrong queue type %d during init\n",
  850. q->queue_type);
  851. rc = -EINVAL;
  852. break;
  853. }
  854. sync_stream_queue_init(hdev, q->hw_queue_id);
  855. if (rc)
  856. return rc;
  857. q->valid = 1;
  858. return 0;
  859. }
  860. /*
  861. * hw_queue_fini - destroy queue
  862. *
  863. * @hdev: pointer to hl_device device structure
  864. * @q: pointer to hl_hw_queue queue structure
  865. *
  866. * Free the queue memory
  867. */
  868. static void queue_fini(struct hl_device *hdev, struct hl_hw_queue *q)
  869. {
  870. if (!q->valid)
  871. return;
  872. /*
  873. * If we arrived here, there are no jobs waiting on this queue
  874. * so we can safely remove it.
  875. * This is because this function can only called when:
  876. * 1. Either a context is deleted, which only can occur if all its
  877. * jobs were finished
  878. * 2. A context wasn't able to be created due to failure or timeout,
  879. * which means there are no jobs on the queue yet
  880. *
  881. * The only exception are the queues of the kernel context, but
  882. * if they are being destroyed, it means that the entire module is
  883. * being removed. If the module is removed, it means there is no open
  884. * user context. It also means that if a job was submitted by
  885. * the kernel driver (e.g. context creation), the job itself was
  886. * released by the kernel driver when a timeout occurred on its
  887. * Completion. Thus, we don't need to release it again.
  888. */
  889. if (q->queue_type == QUEUE_TYPE_INT)
  890. return;
  891. kfree(q->shadow_queue);
  892. if (q->queue_type == QUEUE_TYPE_CPU)
  893. hl_cpu_accessible_dma_pool_free(hdev, HL_QUEUE_SIZE_IN_BYTES, q->kernel_address);
  894. else
  895. hl_asic_dma_free_coherent(hdev, HL_QUEUE_SIZE_IN_BYTES, q->kernel_address,
  896. q->bus_address);
  897. }
  898. int hl_hw_queues_create(struct hl_device *hdev)
  899. {
  900. struct asic_fixed_properties *asic = &hdev->asic_prop;
  901. struct hl_hw_queue *q;
  902. int i, rc, q_ready_cnt;
  903. hdev->kernel_queues = kcalloc(asic->max_queues,
  904. sizeof(*hdev->kernel_queues), GFP_KERNEL);
  905. if (!hdev->kernel_queues) {
  906. dev_err(hdev->dev, "Not enough memory for H/W queues\n");
  907. return -ENOMEM;
  908. }
  909. /* Initialize the H/W queues */
  910. for (i = 0, q_ready_cnt = 0, q = hdev->kernel_queues;
  911. i < asic->max_queues ; i++, q_ready_cnt++, q++) {
  912. q->queue_type = asic->hw_queues_props[i].type;
  913. q->supports_sync_stream =
  914. asic->hw_queues_props[i].supports_sync_stream;
  915. q->collective_mode = asic->hw_queues_props[i].collective_mode;
  916. rc = queue_init(hdev, q, i);
  917. if (rc) {
  918. dev_err(hdev->dev,
  919. "failed to initialize queue %d\n", i);
  920. goto release_queues;
  921. }
  922. }
  923. return 0;
  924. release_queues:
  925. for (i = 0, q = hdev->kernel_queues ; i < q_ready_cnt ; i++, q++)
  926. queue_fini(hdev, q);
  927. kfree(hdev->kernel_queues);
  928. return rc;
  929. }
  930. void hl_hw_queues_destroy(struct hl_device *hdev)
  931. {
  932. struct hl_hw_queue *q;
  933. u32 max_queues = hdev->asic_prop.max_queues;
  934. int i;
  935. for (i = 0, q = hdev->kernel_queues ; i < max_queues ; i++, q++)
  936. queue_fini(hdev, q);
  937. kfree(hdev->kernel_queues);
  938. }
  939. void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset)
  940. {
  941. struct hl_hw_queue *q;
  942. u32 max_queues = hdev->asic_prop.max_queues;
  943. int i;
  944. for (i = 0, q = hdev->kernel_queues ; i < max_queues ; i++, q++) {
  945. if ((!q->valid) ||
  946. ((!hard_reset) && (q->queue_type == QUEUE_TYPE_CPU)))
  947. continue;
  948. q->pi = 0;
  949. atomic_set(&q->ci, 0);
  950. if (q->supports_sync_stream)
  951. sync_stream_queue_reset(hdev, q->hw_queue_id);
  952. }
  953. }