cdma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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);
  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. host1x_hw_cdma_flush(host1x, cdma);
  204. /* If somebody has managed to already start waiting, yield */
  205. if (cdma->event != CDMA_EVENT_NONE) {
  206. mutex_unlock(&cdma->lock);
  207. schedule();
  208. mutex_lock(&cdma->lock);
  209. continue;
  210. }
  211. cdma->event = CDMA_EVENT_PUSH_BUFFER_SPACE;
  212. mutex_unlock(&cdma->lock);
  213. wait_for_completion(&cdma->complete);
  214. mutex_lock(&cdma->lock);
  215. }
  216. return 0;
  217. }
  218. /*
  219. * Start timer that tracks the time spent by the job.
  220. * Must be called with the cdma lock held.
  221. */
  222. static void cdma_start_timer_locked(struct host1x_cdma *cdma,
  223. struct host1x_job *job)
  224. {
  225. if (cdma->timeout.client) {
  226. /* timer already started */
  227. return;
  228. }
  229. cdma->timeout.client = job->client;
  230. cdma->timeout.syncpt = job->syncpt;
  231. cdma->timeout.syncpt_val = job->syncpt_end;
  232. cdma->timeout.start_ktime = ktime_get();
  233. schedule_delayed_work(&cdma->timeout.wq,
  234. msecs_to_jiffies(job->timeout));
  235. }
  236. /*
  237. * Stop timer when a buffer submission completes.
  238. * Must be called with the cdma lock held.
  239. */
  240. static void stop_cdma_timer_locked(struct host1x_cdma *cdma)
  241. {
  242. cancel_delayed_work(&cdma->timeout.wq);
  243. cdma->timeout.client = NULL;
  244. }
  245. /*
  246. * For all sync queue entries that have already finished according to the
  247. * current sync point registers:
  248. * - unpin & unref their mems
  249. * - pop their push buffer slots
  250. * - remove them from the sync queue
  251. * This is normally called from the host code's worker thread, but can be
  252. * called manually if necessary.
  253. * Must be called with the cdma lock held.
  254. */
  255. static void update_cdma_locked(struct host1x_cdma *cdma)
  256. {
  257. bool signal = false;
  258. struct host1x_job *job, *n;
  259. /*
  260. * Walk the sync queue, reading the sync point registers as necessary,
  261. * to consume as many sync queue entries as possible without blocking
  262. */
  263. list_for_each_entry_safe(job, n, &cdma->sync_queue, list) {
  264. struct host1x_syncpt *sp = job->syncpt;
  265. /* Check whether this syncpt has completed, and bail if not */
  266. if (!host1x_syncpt_is_expired(sp, job->syncpt_end) &&
  267. !job->cancelled) {
  268. /* Start timer on next pending syncpt */
  269. if (job->timeout)
  270. cdma_start_timer_locked(cdma, job);
  271. break;
  272. }
  273. /* Cancel timeout, when a buffer completes */
  274. if (cdma->timeout.client)
  275. stop_cdma_timer_locked(cdma);
  276. /* Unpin the memory */
  277. host1x_job_unpin(job);
  278. /* Pop push buffer slots */
  279. if (job->num_slots) {
  280. struct push_buffer *pb = &cdma->push_buffer;
  281. host1x_pushbuffer_pop(pb, job->num_slots);
  282. if (cdma->event == CDMA_EVENT_PUSH_BUFFER_SPACE)
  283. signal = true;
  284. }
  285. list_del(&job->list);
  286. host1x_job_put(job);
  287. }
  288. if (cdma->event == CDMA_EVENT_SYNC_QUEUE_EMPTY &&
  289. list_empty(&cdma->sync_queue))
  290. signal = true;
  291. if (signal) {
  292. cdma->event = CDMA_EVENT_NONE;
  293. complete(&cdma->complete);
  294. }
  295. }
  296. void host1x_cdma_update_sync_queue(struct host1x_cdma *cdma,
  297. struct device *dev)
  298. {
  299. struct host1x *host1x = cdma_to_host1x(cdma);
  300. u32 restart_addr, syncpt_incrs, syncpt_val;
  301. struct host1x_job *job, *next_job = NULL;
  302. syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
  303. dev_dbg(dev, "%s: starting cleanup (thresh %d)\n",
  304. __func__, syncpt_val);
  305. /*
  306. * Move the sync_queue read pointer to the first entry that hasn't
  307. * completed based on the current HW syncpt value. It's likely there
  308. * won't be any (i.e. we're still at the head), but covers the case
  309. * where a syncpt incr happens just prior/during the teardown.
  310. */
  311. dev_dbg(dev, "%s: skip completed buffers still in sync_queue\n",
  312. __func__);
  313. list_for_each_entry(job, &cdma->sync_queue, list) {
  314. if (syncpt_val < job->syncpt_end) {
  315. if (!list_is_last(&job->list, &cdma->sync_queue))
  316. next_job = list_next_entry(job, list);
  317. goto syncpt_incr;
  318. }
  319. host1x_job_dump(dev, job);
  320. }
  321. /* all jobs have been completed */
  322. job = NULL;
  323. syncpt_incr:
  324. /*
  325. * Increment with CPU the remaining syncpts of a partially executed job.
  326. *
  327. * CDMA will continue execution starting with the next job or will get
  328. * into idle state.
  329. */
  330. if (next_job)
  331. restart_addr = next_job->first_get;
  332. else
  333. restart_addr = cdma->last_pos;
  334. if (!job)
  335. goto resume;
  336. /* do CPU increments for the remaining syncpts */
  337. if (job->syncpt_recovery) {
  338. dev_dbg(dev, "%s: perform CPU incr on pending buffers\n",
  339. __func__);
  340. /* won't need a timeout when replayed */
  341. job->timeout = 0;
  342. syncpt_incrs = job->syncpt_end - syncpt_val;
  343. dev_dbg(dev, "%s: CPU incr (%d)\n", __func__, syncpt_incrs);
  344. host1x_job_dump(dev, job);
  345. /* safe to use CPU to incr syncpts */
  346. host1x_hw_cdma_timeout_cpu_incr(host1x, cdma, job->first_get,
  347. syncpt_incrs, job->syncpt_end,
  348. job->num_slots);
  349. dev_dbg(dev, "%s: finished sync_queue modification\n",
  350. __func__);
  351. } else {
  352. struct host1x_job *failed_job = job;
  353. host1x_job_dump(dev, job);
  354. host1x_syncpt_set_locked(job->syncpt);
  355. failed_job->cancelled = true;
  356. list_for_each_entry_continue(job, &cdma->sync_queue, list) {
  357. unsigned int i;
  358. if (job->syncpt != failed_job->syncpt)
  359. continue;
  360. for (i = 0; i < job->num_slots; i++) {
  361. unsigned int slot = (job->first_get/8 + i) %
  362. HOST1X_PUSHBUFFER_SLOTS;
  363. u32 *mapped = cdma->push_buffer.mapped;
  364. /*
  365. * Overwrite opcodes with 0 word writes
  366. * to offset 0xbad. This does nothing but
  367. * has a easily detected signature in debug
  368. * traces.
  369. *
  370. * On systems with MLOCK enforcement enabled,
  371. * the above 0 word writes would fall foul of
  372. * the enforcement. As such, in the first slot
  373. * put a RESTART_W opcode to the beginning
  374. * of the next job. We don't use this for older
  375. * chips since those only support the RESTART
  376. * opcode with inconvenient alignment requirements.
  377. */
  378. if (i == 0 && host1x->info->has_wide_gather) {
  379. unsigned int next_job = (job->first_get/8 + job->num_slots)
  380. % HOST1X_PUSHBUFFER_SLOTS;
  381. mapped[2*slot+0] = (0xd << 28) | (next_job * 2);
  382. mapped[2*slot+1] = 0x0;
  383. } else {
  384. mapped[2*slot+0] = 0x1bad0000;
  385. mapped[2*slot+1] = 0x1bad0000;
  386. }
  387. }
  388. job->cancelled = true;
  389. }
  390. wmb();
  391. update_cdma_locked(cdma);
  392. }
  393. resume:
  394. /* roll back DMAGET and start up channel again */
  395. host1x_hw_cdma_resume(host1x, cdma, restart_addr);
  396. }
  397. /*
  398. * Create a cdma
  399. */
  400. int host1x_cdma_init(struct host1x_cdma *cdma)
  401. {
  402. int err;
  403. mutex_init(&cdma->lock);
  404. init_completion(&cdma->complete);
  405. INIT_LIST_HEAD(&cdma->sync_queue);
  406. cdma->event = CDMA_EVENT_NONE;
  407. cdma->running = false;
  408. cdma->torndown = false;
  409. err = host1x_pushbuffer_init(&cdma->push_buffer);
  410. if (err)
  411. return err;
  412. return 0;
  413. }
  414. /*
  415. * Destroy a cdma
  416. */
  417. int host1x_cdma_deinit(struct host1x_cdma *cdma)
  418. {
  419. struct push_buffer *pb = &cdma->push_buffer;
  420. struct host1x *host1x = cdma_to_host1x(cdma);
  421. if (cdma->running) {
  422. pr_warn("%s: CDMA still running\n", __func__);
  423. return -EBUSY;
  424. }
  425. host1x_pushbuffer_destroy(pb);
  426. host1x_hw_cdma_timeout_destroy(host1x, cdma);
  427. return 0;
  428. }
  429. /*
  430. * Begin a cdma submit
  431. */
  432. int host1x_cdma_begin(struct host1x_cdma *cdma, struct host1x_job *job)
  433. {
  434. struct host1x *host1x = cdma_to_host1x(cdma);
  435. mutex_lock(&cdma->lock);
  436. /*
  437. * Check if syncpoint was locked due to previous job timeout.
  438. * This needs to be done within the cdma lock to avoid a race
  439. * with the timeout handler.
  440. */
  441. if (job->syncpt->locked) {
  442. mutex_unlock(&cdma->lock);
  443. return -EPERM;
  444. }
  445. if (job->timeout) {
  446. /* init state on first submit with timeout value */
  447. if (!cdma->timeout.initialized) {
  448. int err;
  449. err = host1x_hw_cdma_timeout_init(host1x, cdma);
  450. if (err) {
  451. mutex_unlock(&cdma->lock);
  452. return err;
  453. }
  454. }
  455. }
  456. if (!cdma->running)
  457. host1x_hw_cdma_start(host1x, cdma);
  458. cdma->slots_free = 0;
  459. cdma->slots_used = 0;
  460. cdma->first_get = cdma->push_buffer.pos;
  461. trace_host1x_cdma_begin(dev_name(job->channel->dev));
  462. return 0;
  463. }
  464. /*
  465. * Push two words into a push buffer slot
  466. * Blocks as necessary if the push buffer is full.
  467. */
  468. void host1x_cdma_push(struct host1x_cdma *cdma, u32 op1, u32 op2)
  469. {
  470. struct host1x *host1x = cdma_to_host1x(cdma);
  471. struct push_buffer *pb = &cdma->push_buffer;
  472. u32 slots_free = cdma->slots_free;
  473. if (host1x_debug_trace_cmdbuf)
  474. trace_host1x_cdma_push(dev_name(cdma_to_channel(cdma)->dev),
  475. op1, op2);
  476. if (slots_free == 0) {
  477. host1x_hw_cdma_flush(host1x, cdma);
  478. slots_free = host1x_cdma_wait_locked(cdma,
  479. CDMA_EVENT_PUSH_BUFFER_SPACE);
  480. }
  481. cdma->slots_free = slots_free - 1;
  482. cdma->slots_used++;
  483. host1x_pushbuffer_push(pb, op1, op2);
  484. }
  485. /*
  486. * Push four words into two consecutive push buffer slots. Note that extra
  487. * care needs to be taken not to split the two slots across the end of the
  488. * push buffer. Otherwise the RESTART opcode at the end of the push buffer
  489. * that ensures processing will restart at the beginning will break up the
  490. * four words.
  491. *
  492. * Blocks as necessary if the push buffer is full.
  493. */
  494. void host1x_cdma_push_wide(struct host1x_cdma *cdma, u32 op1, u32 op2,
  495. u32 op3, u32 op4)
  496. {
  497. struct host1x_channel *channel = cdma_to_channel(cdma);
  498. struct host1x *host1x = cdma_to_host1x(cdma);
  499. struct push_buffer *pb = &cdma->push_buffer;
  500. unsigned int space = cdma->slots_free;
  501. unsigned int needed = 2, extra = 0;
  502. if (host1x_debug_trace_cmdbuf)
  503. trace_host1x_cdma_push_wide(dev_name(channel->dev), op1, op2,
  504. op3, op4);
  505. /* compute number of extra slots needed for padding */
  506. if (pb->pos + 16 > pb->size) {
  507. extra = (pb->size - pb->pos) / 8;
  508. needed += extra;
  509. }
  510. host1x_cdma_wait_pushbuffer_space(host1x, cdma, needed);
  511. space = host1x_pushbuffer_space(pb);
  512. cdma->slots_free = space - needed;
  513. cdma->slots_used += needed;
  514. if (extra > 0) {
  515. /*
  516. * If there isn't enough space at the tail of the pushbuffer,
  517. * insert a RESTART(0) here to go back to the beginning.
  518. * The code above adjusted the indexes appropriately.
  519. */
  520. host1x_pushbuffer_push(pb, (0x5 << 28), 0xdead0000);
  521. }
  522. host1x_pushbuffer_push(pb, op1, op2);
  523. host1x_pushbuffer_push(pb, op3, op4);
  524. }
  525. /*
  526. * End a cdma submit
  527. * Kick off DMA, add job to the sync queue, and a number of slots to be freed
  528. * from the pushbuffer. The handles for a submit must all be pinned at the same
  529. * time, but they can be unpinned in smaller chunks.
  530. */
  531. void host1x_cdma_end(struct host1x_cdma *cdma,
  532. struct host1x_job *job)
  533. {
  534. struct host1x *host1x = cdma_to_host1x(cdma);
  535. bool idle = list_empty(&cdma->sync_queue);
  536. host1x_hw_cdma_flush(host1x, cdma);
  537. job->first_get = cdma->first_get;
  538. job->num_slots = cdma->slots_used;
  539. host1x_job_get(job);
  540. list_add_tail(&job->list, &cdma->sync_queue);
  541. /* start timer on idle -> active transitions */
  542. if (job->timeout && idle)
  543. cdma_start_timer_locked(cdma, job);
  544. trace_host1x_cdma_end(dev_name(job->channel->dev));
  545. mutex_unlock(&cdma->lock);
  546. }
  547. /*
  548. * Update cdma state according to current sync point values
  549. */
  550. void host1x_cdma_update(struct host1x_cdma *cdma)
  551. {
  552. mutex_lock(&cdma->lock);
  553. update_cdma_locked(cdma);
  554. mutex_unlock(&cdma->lock);
  555. }