cdma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x Command DMA
  4. *
  5. * Copyright (c) 2010-2013, NVIDIA Corporation.
  6. */
  7. #include <asm/cacheflush.h>
  8. #include <linux/device.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/host1x.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kfifo.h>
  14. #include <linux/slab.h>
  15. #include <trace/events/host1x.h>
  16. #include "cdma.h"
  17. #include "channel.h"
  18. #include "dev.h"
  19. #include "debug.h"
  20. #include "job.h"
  21. /*
  22. * push_buffer
  23. *
  24. * The push buffer is a circular array of words to be fetched by command DMA.
  25. * Note that it works slightly differently to the sync queue; fence == pos
  26. * means that the push buffer is full, not empty.
  27. */
  28. /*
  29. * Typically the commands written into the push buffer are a pair of words. We
  30. * use slots to represent each of these pairs and to simplify things. Note the
  31. * strange number of slots allocated here. 512 slots will fit exactly within a
  32. * single memory page. We also need one additional word at the end of the push
  33. * buffer for the RESTART opcode that will instruct the CDMA to jump back to
  34. * the beginning of the push buffer. With 512 slots, this means that we'll use
  35. * 2 memory pages and waste 4092 bytes of the second page that will never be
  36. * used.
  37. */
  38. #define HOST1X_PUSHBUFFER_SLOTS 511
  39. /*
  40. * Clean up push buffer resources
  41. */
  42. static void host1x_pushbuffer_destroy(struct push_buffer *pb)
  43. {
  44. struct host1x_cdma *cdma = pb_to_cdma(pb);
  45. struct host1x *host1x = cdma_to_host1x(cdma);
  46. if (!pb->mapped)
  47. return;
  48. if (host1x->domain) {
  49. iommu_unmap(host1x->domain, pb->dma, pb->alloc_size);
  50. free_iova(&host1x->iova, iova_pfn(&host1x->iova, pb->dma));
  51. }
  52. dma_free_wc(host1x->dev, pb->alloc_size, pb->mapped, pb->phys);
  53. pb->mapped = NULL;
  54. pb->phys = 0;
  55. }
  56. /*
  57. * Init push buffer resources
  58. */
  59. static int host1x_pushbuffer_init(struct push_buffer *pb)
  60. {
  61. struct host1x_cdma *cdma = pb_to_cdma(pb);
  62. struct host1x *host1x = cdma_to_host1x(cdma);
  63. struct iova *alloc;
  64. u32 size;
  65. int err;
  66. pb->mapped = NULL;
  67. pb->phys = 0;
  68. pb->size = HOST1X_PUSHBUFFER_SLOTS * 8;
  69. size = pb->size + 4;
  70. /* initialize buffer pointers */
  71. pb->fence = pb->size - 8;
  72. pb->pos = 0;
  73. if (host1x->domain) {
  74. unsigned long shift;
  75. size = iova_align(&host1x->iova, size);
  76. pb->mapped = dma_alloc_wc(host1x->dev, size, &pb->phys,
  77. GFP_KERNEL);
  78. if (!pb->mapped)
  79. return -ENOMEM;
  80. shift = iova_shift(&host1x->iova);
  81. alloc = alloc_iova(&host1x->iova, size >> shift,
  82. host1x->iova_end >> shift, true);
  83. if (!alloc) {
  84. err = -ENOMEM;
  85. goto iommu_free_mem;
  86. }
  87. pb->dma = iova_dma_addr(&host1x->iova, alloc);
  88. err = iommu_map(host1x->domain, pb->dma, pb->phys, size,
  89. IOMMU_READ, GFP_KERNEL);
  90. if (err)
  91. goto iommu_free_iova;
  92. } else {
  93. pb->mapped = dma_alloc_wc(host1x->dev, size, &pb->phys,
  94. GFP_KERNEL);
  95. if (!pb->mapped)
  96. return -ENOMEM;
  97. pb->dma = pb->phys;
  98. }
  99. pb->alloc_size = size;
  100. host1x_hw_pushbuffer_init(host1x, pb);
  101. return 0;
  102. iommu_free_iova:
  103. __free_iova(&host1x->iova, alloc);
  104. iommu_free_mem:
  105. dma_free_wc(host1x->dev, size, pb->mapped, pb->phys);
  106. return err;
  107. }
  108. /*
  109. * Push two words to the push buffer
  110. * Caller must ensure push buffer is not full
  111. */
  112. static void host1x_pushbuffer_push(struct push_buffer *pb, u32 op1, u32 op2)
  113. {
  114. u32 *p = (u32 *)((void *)pb->mapped + pb->pos);
  115. WARN_ON(pb->pos == pb->fence);
  116. *(p++) = op1;
  117. *(p++) = op2;
  118. pb->pos += 8;
  119. if (pb->pos >= pb->size)
  120. pb->pos -= pb->size;
  121. }
  122. /*
  123. * Pop a number of two word slots from the push buffer
  124. * Caller must ensure push buffer is not empty
  125. */
  126. static void host1x_pushbuffer_pop(struct push_buffer *pb, unsigned int slots)
  127. {
  128. /* Advance the next write position */
  129. pb->fence += slots * 8;
  130. if (pb->fence >= pb->size)
  131. pb->fence -= pb->size;
  132. }
  133. /*
  134. * Return the number of two word slots free in the push buffer
  135. */
  136. static u32 host1x_pushbuffer_space(struct push_buffer *pb)
  137. {
  138. unsigned int fence = pb->fence;
  139. if (pb->fence < pb->pos)
  140. fence += pb->size;
  141. return (fence - pb->pos) / 8;
  142. }
  143. /*
  144. * Sleep (if necessary) until the requested event happens
  145. * - CDMA_EVENT_SYNC_QUEUE_EMPTY : sync queue is completely empty.
  146. * - Returns 1
  147. * - CDMA_EVENT_PUSH_BUFFER_SPACE : there is space in the push buffer
  148. * - Return the amount of space (> 0)
  149. * Must be called with the cdma lock held.
  150. */
  151. unsigned int host1x_cdma_wait_locked(struct host1x_cdma *cdma,
  152. enum cdma_event event)
  153. {
  154. for (;;) {
  155. struct push_buffer *pb = &cdma->push_buffer;
  156. unsigned int space;
  157. switch (event) {
  158. case CDMA_EVENT_SYNC_QUEUE_EMPTY:
  159. space = list_empty(&cdma->sync_queue) ? 1 : 0;
  160. break;
  161. case CDMA_EVENT_PUSH_BUFFER_SPACE:
  162. space = host1x_pushbuffer_space(pb);
  163. break;
  164. default:
  165. WARN_ON(1);
  166. return -EINVAL;
  167. }
  168. if (space)
  169. return space;
  170. trace_host1x_wait_cdma(dev_name(cdma_to_channel(cdma)->dev),
  171. event);
  172. /* If somebody has managed to already start waiting, yield */
  173. if (cdma->event != CDMA_EVENT_NONE) {
  174. mutex_unlock(&cdma->lock);
  175. schedule();
  176. mutex_lock(&cdma->lock);
  177. continue;
  178. }
  179. cdma->event = event;
  180. mutex_unlock(&cdma->lock);
  181. wait_for_completion(&cdma->complete);
  182. mutex_lock(&cdma->lock);
  183. }
  184. return 0;
  185. }
  186. /*
  187. * Sleep (if necessary) until the push buffer has enough free space.
  188. *
  189. * Must be called with the cdma lock held.
  190. */
  191. static int host1x_cdma_wait_pushbuffer_space(struct host1x *host1x,
  192. struct host1x_cdma *cdma,
  193. unsigned int needed)
  194. {
  195. while (true) {
  196. struct push_buffer *pb = &cdma->push_buffer;
  197. unsigned int space;
  198. space = host1x_pushbuffer_space(pb);
  199. if (space >= needed)
  200. break;
  201. trace_host1x_wait_cdma(dev_name(cdma_to_channel(cdma)->dev),
  202. CDMA_EVENT_PUSH_BUFFER_SPACE);
  203. /* If somebody has managed to already start waiting, yield */
  204. if (cdma->event != CDMA_EVENT_NONE) {
  205. mutex_unlock(&cdma->lock);
  206. schedule();
  207. mutex_lock(&cdma->lock);
  208. continue;
  209. }
  210. cdma->event = CDMA_EVENT_PUSH_BUFFER_SPACE;
  211. mutex_unlock(&cdma->lock);
  212. wait_for_completion(&cdma->complete);
  213. mutex_lock(&cdma->lock);
  214. }
  215. return 0;
  216. }
  217. /*
  218. * Start timer that tracks the time spent by the job.
  219. * Must be called with the cdma lock held.
  220. */
  221. static void cdma_start_timer_locked(struct host1x_cdma *cdma,
  222. struct host1x_job *job)
  223. {
  224. if (cdma->timeout.client) {
  225. /* timer already started */
  226. return;
  227. }
  228. cdma->timeout.client = job->client;
  229. cdma->timeout.syncpt = job->syncpt;
  230. cdma->timeout.syncpt_val = job->syncpt_end;
  231. cdma->timeout.start_ktime = ktime_get();
  232. schedule_delayed_work(&cdma->timeout.wq,
  233. msecs_to_jiffies(job->timeout));
  234. }
  235. /*
  236. * Stop timer when a buffer submission completes.
  237. * Must be called with the cdma lock held.
  238. */
  239. static void stop_cdma_timer_locked(struct host1x_cdma *cdma)
  240. {
  241. cancel_delayed_work(&cdma->timeout.wq);
  242. cdma->timeout.client = NULL;
  243. }
  244. /*
  245. * For all sync queue entries that have already finished according to the
  246. * current sync point registers:
  247. * - unpin & unref their mems
  248. * - pop their push buffer slots
  249. * - remove them from the sync queue
  250. * This is normally called from the host code's worker thread, but can be
  251. * called manually if necessary.
  252. * Must be called with the cdma lock held.
  253. */
  254. static void update_cdma_locked(struct host1x_cdma *cdma)
  255. {
  256. bool signal = false;
  257. struct host1x_job *job, *n;
  258. /*
  259. * Walk the sync queue, reading the sync point registers as necessary,
  260. * to consume as many sync queue entries as possible without blocking
  261. */
  262. list_for_each_entry_safe(job, n, &cdma->sync_queue, list) {
  263. struct host1x_syncpt *sp = job->syncpt;
  264. /* Check whether this syncpt has completed, and bail if not */
  265. if (!host1x_syncpt_is_expired(sp, job->syncpt_end) &&
  266. !job->cancelled) {
  267. /* Start timer on next pending syncpt */
  268. if (job->timeout)
  269. cdma_start_timer_locked(cdma, job);
  270. break;
  271. }
  272. /* Cancel timeout, when a buffer completes */
  273. if (cdma->timeout.client)
  274. stop_cdma_timer_locked(cdma);
  275. /* Unpin the memory */
  276. host1x_job_unpin(job);
  277. /* Pop push buffer slots */
  278. if (job->num_slots) {
  279. struct push_buffer *pb = &cdma->push_buffer;
  280. host1x_pushbuffer_pop(pb, job->num_slots);
  281. if (cdma->event == CDMA_EVENT_PUSH_BUFFER_SPACE)
  282. signal = true;
  283. }
  284. list_del(&job->list);
  285. host1x_job_put(job);
  286. }
  287. if (cdma->event == CDMA_EVENT_SYNC_QUEUE_EMPTY &&
  288. list_empty(&cdma->sync_queue))
  289. signal = true;
  290. if (signal) {
  291. cdma->event = CDMA_EVENT_NONE;
  292. complete(&cdma->complete);
  293. }
  294. }
  295. void host1x_cdma_update_sync_queue(struct host1x_cdma *cdma,
  296. struct device *dev)
  297. {
  298. struct host1x *host1x = cdma_to_host1x(cdma);
  299. u32 restart_addr, syncpt_incrs, syncpt_val;
  300. struct host1x_job *job, *next_job = NULL;
  301. syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
  302. dev_dbg(dev, "%s: starting cleanup (thresh %d)\n",
  303. __func__, syncpt_val);
  304. /*
  305. * Move the sync_queue read pointer to the first entry that hasn't
  306. * completed based on the current HW syncpt value. It's likely there
  307. * won't be any (i.e. we're still at the head), but covers the case
  308. * where a syncpt incr happens just prior/during the teardown.
  309. */
  310. dev_dbg(dev, "%s: skip completed buffers still in sync_queue\n",
  311. __func__);
  312. list_for_each_entry(job, &cdma->sync_queue, list) {
  313. if (syncpt_val < job->syncpt_end) {
  314. if (!list_is_last(&job->list, &cdma->sync_queue))
  315. next_job = list_next_entry(job, list);
  316. goto syncpt_incr;
  317. }
  318. host1x_job_dump(dev, job);
  319. }
  320. /* all jobs have been completed */
  321. job = NULL;
  322. syncpt_incr:
  323. /*
  324. * Increment with CPU the remaining syncpts of a partially executed job.
  325. *
  326. * CDMA will continue execution starting with the next job or will get
  327. * into idle state.
  328. */
  329. if (next_job)
  330. restart_addr = next_job->first_get;
  331. else
  332. restart_addr = cdma->last_pos;
  333. if (!job)
  334. goto resume;
  335. /* do CPU increments for the remaining syncpts */
  336. if (job->syncpt_recovery) {
  337. dev_dbg(dev, "%s: perform CPU incr on pending buffers\n",
  338. __func__);
  339. /* won't need a timeout when replayed */
  340. job->timeout = 0;
  341. syncpt_incrs = job->syncpt_end - syncpt_val;
  342. dev_dbg(dev, "%s: CPU incr (%d)\n", __func__, syncpt_incrs);
  343. host1x_job_dump(dev, job);
  344. /* safe to use CPU to incr syncpts */
  345. host1x_hw_cdma_timeout_cpu_incr(host1x, cdma, job->first_get,
  346. syncpt_incrs, job->syncpt_end,
  347. job->num_slots);
  348. dev_dbg(dev, "%s: finished sync_queue modification\n",
  349. __func__);
  350. } else {
  351. struct host1x_job *failed_job = job;
  352. host1x_job_dump(dev, job);
  353. host1x_syncpt_set_locked(job->syncpt);
  354. failed_job->cancelled = true;
  355. list_for_each_entry_continue(job, &cdma->sync_queue, list) {
  356. unsigned int i;
  357. if (job->syncpt != failed_job->syncpt)
  358. continue;
  359. for (i = 0; i < job->num_slots; i++) {
  360. unsigned int slot = (job->first_get/8 + i) %
  361. HOST1X_PUSHBUFFER_SLOTS;
  362. u32 *mapped = cdma->push_buffer.mapped;
  363. /*
  364. * Overwrite opcodes with 0 word writes
  365. * to offset 0xbad. This does nothing but
  366. * has a easily detected signature in debug
  367. * traces.
  368. *
  369. * On systems with MLOCK enforcement enabled,
  370. * the above 0 word writes would fall foul of
  371. * the enforcement. As such, in the first slot
  372. * put a RESTART_W opcode to the beginning
  373. * of the next job. We don't use this for older
  374. * chips since those only support the RESTART
  375. * opcode with inconvenient alignment requirements.
  376. */
  377. if (i == 0 && host1x->info->has_wide_gather) {
  378. unsigned int next_job = (job->first_get/8 + job->num_slots)
  379. % HOST1X_PUSHBUFFER_SLOTS;
  380. mapped[2*slot+0] = (0xd << 28) | (next_job * 2);
  381. mapped[2*slot+1] = 0x0;
  382. } else {
  383. mapped[2*slot+0] = 0x1bad0000;
  384. mapped[2*slot+1] = 0x1bad0000;
  385. }
  386. }
  387. job->cancelled = true;
  388. }
  389. wmb();
  390. update_cdma_locked(cdma);
  391. }
  392. resume:
  393. /* roll back DMAGET and start up channel again */
  394. host1x_hw_cdma_resume(host1x, cdma, restart_addr);
  395. }
  396. static void cdma_update_work(struct work_struct *work)
  397. {
  398. struct host1x_cdma *cdma = container_of(work, struct host1x_cdma, update_work);
  399. mutex_lock(&cdma->lock);
  400. update_cdma_locked(cdma);
  401. mutex_unlock(&cdma->lock);
  402. }
  403. /*
  404. * Create a cdma
  405. */
  406. int host1x_cdma_init(struct host1x_cdma *cdma)
  407. {
  408. int err;
  409. mutex_init(&cdma->lock);
  410. init_completion(&cdma->complete);
  411. INIT_WORK(&cdma->update_work, cdma_update_work);
  412. INIT_LIST_HEAD(&cdma->sync_queue);
  413. cdma->event = CDMA_EVENT_NONE;
  414. cdma->running = false;
  415. cdma->torndown = false;
  416. err = host1x_pushbuffer_init(&cdma->push_buffer);
  417. if (err)
  418. return err;
  419. return 0;
  420. }
  421. /*
  422. * Destroy a cdma
  423. */
  424. int host1x_cdma_deinit(struct host1x_cdma *cdma)
  425. {
  426. struct push_buffer *pb = &cdma->push_buffer;
  427. struct host1x *host1x = cdma_to_host1x(cdma);
  428. if (cdma->running) {
  429. pr_warn("%s: CDMA still running\n", __func__);
  430. return -EBUSY;
  431. }
  432. host1x_pushbuffer_destroy(pb);
  433. host1x_hw_cdma_timeout_destroy(host1x, cdma);
  434. return 0;
  435. }
  436. /*
  437. * Begin a cdma submit
  438. */
  439. int host1x_cdma_begin(struct host1x_cdma *cdma, struct host1x_job *job)
  440. {
  441. struct host1x *host1x = cdma_to_host1x(cdma);
  442. mutex_lock(&cdma->lock);
  443. /*
  444. * Check if syncpoint was locked due to previous job timeout.
  445. * This needs to be done within the cdma lock to avoid a race
  446. * with the timeout handler.
  447. */
  448. if (job->syncpt->locked) {
  449. mutex_unlock(&cdma->lock);
  450. return -EPERM;
  451. }
  452. if (job->timeout) {
  453. /* init state on first submit with timeout value */
  454. if (!cdma->timeout.initialized) {
  455. int err;
  456. err = host1x_hw_cdma_timeout_init(host1x, cdma);
  457. if (err) {
  458. mutex_unlock(&cdma->lock);
  459. return err;
  460. }
  461. }
  462. }
  463. if (!cdma->running)
  464. host1x_hw_cdma_start(host1x, cdma);
  465. cdma->slots_free = 0;
  466. cdma->slots_used = 0;
  467. cdma->first_get = cdma->push_buffer.pos;
  468. trace_host1x_cdma_begin(dev_name(job->channel->dev));
  469. return 0;
  470. }
  471. /*
  472. * Push two words into a push buffer slot
  473. * Blocks as necessary if the push buffer is full.
  474. */
  475. void host1x_cdma_push(struct host1x_cdma *cdma, u32 op1, u32 op2)
  476. {
  477. struct push_buffer *pb = &cdma->push_buffer;
  478. u32 slots_free = cdma->slots_free;
  479. if (host1x_debug_trace_cmdbuf)
  480. trace_host1x_cdma_push(dev_name(cdma_to_channel(cdma)->dev),
  481. op1, op2);
  482. if (slots_free == 0)
  483. slots_free = host1x_cdma_wait_locked(cdma,
  484. CDMA_EVENT_PUSH_BUFFER_SPACE);
  485. cdma->slots_free = slots_free - 1;
  486. cdma->slots_used++;
  487. host1x_pushbuffer_push(pb, op1, op2);
  488. }
  489. /*
  490. * Push four words into two consecutive push buffer slots. Note that extra
  491. * care needs to be taken not to split the two slots across the end of the
  492. * push buffer. Otherwise the RESTART opcode at the end of the push buffer
  493. * that ensures processing will restart at the beginning will break up the
  494. * four words.
  495. *
  496. * Blocks as necessary if the push buffer is full.
  497. */
  498. void host1x_cdma_push_wide(struct host1x_cdma *cdma, u32 op1, u32 op2,
  499. u32 op3, u32 op4)
  500. {
  501. struct host1x_channel *channel = cdma_to_channel(cdma);
  502. struct host1x *host1x = cdma_to_host1x(cdma);
  503. struct push_buffer *pb = &cdma->push_buffer;
  504. unsigned int space, needed = 2, extra = 0;
  505. if (host1x_debug_trace_cmdbuf)
  506. trace_host1x_cdma_push_wide(dev_name(channel->dev), op1, op2,
  507. op3, op4);
  508. /* compute number of extra slots needed for padding */
  509. if (pb->pos + 16 > pb->size) {
  510. extra = (pb->size - pb->pos) / 8;
  511. needed += extra;
  512. }
  513. host1x_cdma_wait_pushbuffer_space(host1x, cdma, needed);
  514. space = host1x_pushbuffer_space(pb);
  515. cdma->slots_free = space - needed;
  516. cdma->slots_used += needed;
  517. if (extra > 0) {
  518. /*
  519. * If there isn't enough space at the tail of the pushbuffer,
  520. * insert a RESTART(0) here to go back to the beginning.
  521. * The code above adjusted the indexes appropriately.
  522. */
  523. host1x_pushbuffer_push(pb, (0x5 << 28), 0xdead0000);
  524. }
  525. host1x_pushbuffer_push(pb, op1, op2);
  526. host1x_pushbuffer_push(pb, op3, op4);
  527. }
  528. /*
  529. * End a cdma submit
  530. * Kick off DMA, add job to the sync queue, and a number of slots to be freed
  531. * from the pushbuffer. The handles for a submit must all be pinned at the same
  532. * time, but they can be unpinned in smaller chunks.
  533. */
  534. void host1x_cdma_end(struct host1x_cdma *cdma,
  535. struct host1x_job *job)
  536. {
  537. struct host1x *host1x = cdma_to_host1x(cdma);
  538. bool idle = list_empty(&cdma->sync_queue);
  539. host1x_hw_cdma_flush(host1x, cdma);
  540. job->first_get = cdma->first_get;
  541. job->num_slots = cdma->slots_used;
  542. host1x_job_get(job);
  543. list_add_tail(&job->list, &cdma->sync_queue);
  544. /* start timer on idle -> active transitions */
  545. if (job->timeout && idle)
  546. cdma_start_timer_locked(cdma, job);
  547. trace_host1x_cdma_end(dev_name(job->channel->dev));
  548. mutex_unlock(&cdma->lock);
  549. }
  550. /*
  551. * Update cdma state according to current sync point values
  552. */
  553. void host1x_cdma_update(struct host1x_cdma *cdma)
  554. {
  555. schedule_work(&cdma->update_work);
  556. }