cdma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Tegra host1x Command DMA
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <asm/cacheflush.h>
  19. #include <linux/device.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/host1x.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/kfifo.h>
  25. #include <linux/slab.h>
  26. #include <trace/events/host1x.h>
  27. #include "cdma.h"
  28. #include "channel.h"
  29. #include "dev.h"
  30. #include "debug.h"
  31. #include "job.h"
  32. /*
  33. * push_buffer
  34. *
  35. * The push buffer is a circular array of words to be fetched by command DMA.
  36. * Note that it works slightly differently to the sync queue; fence == pos
  37. * means that the push buffer is full, not empty.
  38. */
  39. #define HOST1X_PUSHBUFFER_SLOTS 512
  40. /*
  41. * Clean up push buffer resources
  42. */
  43. static void host1x_pushbuffer_destroy(struct push_buffer *pb)
  44. {
  45. struct host1x_cdma *cdma = pb_to_cdma(pb);
  46. struct host1x *host1x = cdma_to_host1x(cdma);
  47. if (pb->phys != 0)
  48. dma_free_writecombine(host1x->dev, pb->size_bytes + 4,
  49. pb->mapped, pb->phys);
  50. pb->mapped = NULL;
  51. pb->phys = 0;
  52. }
  53. /*
  54. * Init push buffer resources
  55. */
  56. static int host1x_pushbuffer_init(struct push_buffer *pb)
  57. {
  58. struct host1x_cdma *cdma = pb_to_cdma(pb);
  59. struct host1x *host1x = cdma_to_host1x(cdma);
  60. pb->mapped = NULL;
  61. pb->phys = 0;
  62. pb->size_bytes = HOST1X_PUSHBUFFER_SLOTS * 8;
  63. /* initialize buffer pointers */
  64. pb->fence = pb->size_bytes - 8;
  65. pb->pos = 0;
  66. /* allocate and map pushbuffer memory */
  67. pb->mapped = dma_alloc_writecombine(host1x->dev, pb->size_bytes + 4,
  68. &pb->phys, GFP_KERNEL);
  69. if (!pb->mapped)
  70. goto fail;
  71. host1x_hw_pushbuffer_init(host1x, pb);
  72. return 0;
  73. fail:
  74. host1x_pushbuffer_destroy(pb);
  75. return -ENOMEM;
  76. }
  77. /*
  78. * Push two words to the push buffer
  79. * Caller must ensure push buffer is not full
  80. */
  81. static void host1x_pushbuffer_push(struct push_buffer *pb, u32 op1, u32 op2)
  82. {
  83. u32 pos = pb->pos;
  84. u32 *p = (u32 *)((void *)pb->mapped + pos);
  85. WARN_ON(pos == pb->fence);
  86. *(p++) = op1;
  87. *(p++) = op2;
  88. pb->pos = (pos + 8) & (pb->size_bytes - 1);
  89. }
  90. /*
  91. * Pop a number of two word slots from the push buffer
  92. * Caller must ensure push buffer is not empty
  93. */
  94. static void host1x_pushbuffer_pop(struct push_buffer *pb, unsigned int slots)
  95. {
  96. /* Advance the next write position */
  97. pb->fence = (pb->fence + slots * 8) & (pb->size_bytes - 1);
  98. }
  99. /*
  100. * Return the number of two word slots free in the push buffer
  101. */
  102. static u32 host1x_pushbuffer_space(struct push_buffer *pb)
  103. {
  104. return ((pb->fence - pb->pos) & (pb->size_bytes - 1)) / 8;
  105. }
  106. /*
  107. * Sleep (if necessary) until the requested event happens
  108. * - CDMA_EVENT_SYNC_QUEUE_EMPTY : sync queue is completely empty.
  109. * - Returns 1
  110. * - CDMA_EVENT_PUSH_BUFFER_SPACE : there is space in the push buffer
  111. * - Return the amount of space (> 0)
  112. * Must be called with the cdma lock held.
  113. */
  114. unsigned int host1x_cdma_wait_locked(struct host1x_cdma *cdma,
  115. enum cdma_event event)
  116. {
  117. for (;;) {
  118. unsigned int space;
  119. if (event == CDMA_EVENT_SYNC_QUEUE_EMPTY)
  120. space = list_empty(&cdma->sync_queue) ? 1 : 0;
  121. else if (event == CDMA_EVENT_PUSH_BUFFER_SPACE) {
  122. struct push_buffer *pb = &cdma->push_buffer;
  123. space = host1x_pushbuffer_space(pb);
  124. } else {
  125. WARN_ON(1);
  126. return -EINVAL;
  127. }
  128. if (space)
  129. return space;
  130. trace_host1x_wait_cdma(dev_name(cdma_to_channel(cdma)->dev),
  131. event);
  132. /* If somebody has managed to already start waiting, yield */
  133. if (cdma->event != CDMA_EVENT_NONE) {
  134. mutex_unlock(&cdma->lock);
  135. schedule();
  136. mutex_lock(&cdma->lock);
  137. continue;
  138. }
  139. cdma->event = event;
  140. mutex_unlock(&cdma->lock);
  141. down(&cdma->sem);
  142. mutex_lock(&cdma->lock);
  143. }
  144. return 0;
  145. }
  146. /*
  147. * Start timer that tracks the time spent by the job.
  148. * Must be called with the cdma lock held.
  149. */
  150. static void cdma_start_timer_locked(struct host1x_cdma *cdma,
  151. struct host1x_job *job)
  152. {
  153. struct host1x *host = cdma_to_host1x(cdma);
  154. if (cdma->timeout.client) {
  155. /* timer already started */
  156. return;
  157. }
  158. cdma->timeout.client = job->client;
  159. cdma->timeout.syncpt = host1x_syncpt_get(host, job->syncpt_id);
  160. cdma->timeout.syncpt_val = job->syncpt_end;
  161. cdma->timeout.start_ktime = ktime_get();
  162. schedule_delayed_work(&cdma->timeout.wq,
  163. msecs_to_jiffies(job->timeout));
  164. }
  165. /*
  166. * Stop timer when a buffer submission completes.
  167. * Must be called with the cdma lock held.
  168. */
  169. static void stop_cdma_timer_locked(struct host1x_cdma *cdma)
  170. {
  171. cancel_delayed_work(&cdma->timeout.wq);
  172. cdma->timeout.client = 0;
  173. }
  174. /*
  175. * For all sync queue entries that have already finished according to the
  176. * current sync point registers:
  177. * - unpin & unref their mems
  178. * - pop their push buffer slots
  179. * - remove them from the sync queue
  180. * This is normally called from the host code's worker thread, but can be
  181. * called manually if necessary.
  182. * Must be called with the cdma lock held.
  183. */
  184. static void update_cdma_locked(struct host1x_cdma *cdma)
  185. {
  186. bool signal = false;
  187. struct host1x *host1x = cdma_to_host1x(cdma);
  188. struct host1x_job *job, *n;
  189. /* If CDMA is stopped, queue is cleared and we can return */
  190. if (!cdma->running)
  191. return;
  192. /*
  193. * Walk the sync queue, reading the sync point registers as necessary,
  194. * to consume as many sync queue entries as possible without blocking
  195. */
  196. list_for_each_entry_safe(job, n, &cdma->sync_queue, list) {
  197. struct host1x_syncpt *sp =
  198. host1x_syncpt_get(host1x, job->syncpt_id);
  199. /* Check whether this syncpt has completed, and bail if not */
  200. if (!host1x_syncpt_is_expired(sp, job->syncpt_end)) {
  201. /* Start timer on next pending syncpt */
  202. if (job->timeout)
  203. cdma_start_timer_locked(cdma, job);
  204. break;
  205. }
  206. /* Cancel timeout, when a buffer completes */
  207. if (cdma->timeout.client)
  208. stop_cdma_timer_locked(cdma);
  209. /* Unpin the memory */
  210. host1x_job_unpin(job);
  211. /* Pop push buffer slots */
  212. if (job->num_slots) {
  213. struct push_buffer *pb = &cdma->push_buffer;
  214. host1x_pushbuffer_pop(pb, job->num_slots);
  215. if (cdma->event == CDMA_EVENT_PUSH_BUFFER_SPACE)
  216. signal = true;
  217. }
  218. list_del(&job->list);
  219. host1x_job_put(job);
  220. }
  221. if (cdma->event == CDMA_EVENT_SYNC_QUEUE_EMPTY &&
  222. list_empty(&cdma->sync_queue))
  223. signal = true;
  224. if (signal) {
  225. cdma->event = CDMA_EVENT_NONE;
  226. up(&cdma->sem);
  227. }
  228. }
  229. void host1x_cdma_update_sync_queue(struct host1x_cdma *cdma,
  230. struct device *dev)
  231. {
  232. u32 restart_addr;
  233. u32 syncpt_incrs;
  234. struct host1x_job *job = NULL;
  235. u32 syncpt_val;
  236. struct host1x *host1x = cdma_to_host1x(cdma);
  237. syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
  238. dev_dbg(dev, "%s: starting cleanup (thresh %d)\n",
  239. __func__, syncpt_val);
  240. /*
  241. * Move the sync_queue read pointer to the first entry that hasn't
  242. * completed based on the current HW syncpt value. It's likely there
  243. * won't be any (i.e. we're still at the head), but covers the case
  244. * where a syncpt incr happens just prior/during the teardown.
  245. */
  246. dev_dbg(dev, "%s: skip completed buffers still in sync_queue\n",
  247. __func__);
  248. list_for_each_entry(job, &cdma->sync_queue, list) {
  249. if (syncpt_val < job->syncpt_end)
  250. break;
  251. host1x_job_dump(dev, job);
  252. }
  253. /*
  254. * Walk the sync_queue, first incrementing with the CPU syncpts that
  255. * are partially executed (the first buffer) or fully skipped while
  256. * still in the current context (slots are also NOP-ed).
  257. *
  258. * At the point contexts are interleaved, syncpt increments must be
  259. * done inline with the pushbuffer from a GATHER buffer to maintain
  260. * the order (slots are modified to be a GATHER of syncpt incrs).
  261. *
  262. * Note: save in restart_addr the location where the timed out buffer
  263. * started in the PB, so we can start the refetch from there (with the
  264. * modified NOP-ed PB slots). This lets things appear to have completed
  265. * properly for this buffer and resources are freed.
  266. */
  267. dev_dbg(dev, "%s: perform CPU incr on pending same ctx buffers\n",
  268. __func__);
  269. if (!list_empty(&cdma->sync_queue))
  270. restart_addr = job->first_get;
  271. else
  272. restart_addr = cdma->last_pos;
  273. /* do CPU increments as long as this context continues */
  274. list_for_each_entry_from(job, &cdma->sync_queue, list) {
  275. /* different context, gets us out of this loop */
  276. if (job->client != cdma->timeout.client)
  277. break;
  278. /* won't need a timeout when replayed */
  279. job->timeout = 0;
  280. syncpt_incrs = job->syncpt_end - syncpt_val;
  281. dev_dbg(dev, "%s: CPU incr (%d)\n", __func__, syncpt_incrs);
  282. host1x_job_dump(dev, job);
  283. /* safe to use CPU to incr syncpts */
  284. host1x_hw_cdma_timeout_cpu_incr(host1x, cdma, job->first_get,
  285. syncpt_incrs, job->syncpt_end,
  286. job->num_slots);
  287. syncpt_val += syncpt_incrs;
  288. }
  289. /* The following sumbits from the same client may be dependent on the
  290. * failed submit and therefore they may fail. Force a small timeout
  291. * to make the queue cleanup faster */
  292. list_for_each_entry_from(job, &cdma->sync_queue, list)
  293. if (job->client == cdma->timeout.client)
  294. job->timeout = min_t(unsigned int, job->timeout, 500);
  295. dev_dbg(dev, "%s: finished sync_queue modification\n", __func__);
  296. /* roll back DMAGET and start up channel again */
  297. host1x_hw_cdma_resume(host1x, cdma, restart_addr);
  298. }
  299. /*
  300. * Create a cdma
  301. */
  302. int host1x_cdma_init(struct host1x_cdma *cdma)
  303. {
  304. int err;
  305. mutex_init(&cdma->lock);
  306. sema_init(&cdma->sem, 0);
  307. INIT_LIST_HEAD(&cdma->sync_queue);
  308. cdma->event = CDMA_EVENT_NONE;
  309. cdma->running = false;
  310. cdma->torndown = false;
  311. err = host1x_pushbuffer_init(&cdma->push_buffer);
  312. if (err)
  313. return err;
  314. return 0;
  315. }
  316. /*
  317. * Destroy a cdma
  318. */
  319. int host1x_cdma_deinit(struct host1x_cdma *cdma)
  320. {
  321. struct push_buffer *pb = &cdma->push_buffer;
  322. struct host1x *host1x = cdma_to_host1x(cdma);
  323. if (cdma->running) {
  324. pr_warn("%s: CDMA still running\n", __func__);
  325. return -EBUSY;
  326. }
  327. host1x_pushbuffer_destroy(pb);
  328. host1x_hw_cdma_timeout_destroy(host1x, cdma);
  329. return 0;
  330. }
  331. /*
  332. * Begin a cdma submit
  333. */
  334. int host1x_cdma_begin(struct host1x_cdma *cdma, struct host1x_job *job)
  335. {
  336. struct host1x *host1x = cdma_to_host1x(cdma);
  337. mutex_lock(&cdma->lock);
  338. if (job->timeout) {
  339. /* init state on first submit with timeout value */
  340. if (!cdma->timeout.initialized) {
  341. int err;
  342. err = host1x_hw_cdma_timeout_init(host1x, cdma,
  343. job->syncpt_id);
  344. if (err) {
  345. mutex_unlock(&cdma->lock);
  346. return err;
  347. }
  348. }
  349. }
  350. if (!cdma->running)
  351. host1x_hw_cdma_start(host1x, cdma);
  352. cdma->slots_free = 0;
  353. cdma->slots_used = 0;
  354. cdma->first_get = cdma->push_buffer.pos;
  355. trace_host1x_cdma_begin(dev_name(job->channel->dev));
  356. return 0;
  357. }
  358. /*
  359. * Push two words into a push buffer slot
  360. * Blocks as necessary if the push buffer is full.
  361. */
  362. void host1x_cdma_push(struct host1x_cdma *cdma, u32 op1, u32 op2)
  363. {
  364. struct host1x *host1x = cdma_to_host1x(cdma);
  365. struct push_buffer *pb = &cdma->push_buffer;
  366. u32 slots_free = cdma->slots_free;
  367. if (host1x_debug_trace_cmdbuf)
  368. trace_host1x_cdma_push(dev_name(cdma_to_channel(cdma)->dev),
  369. op1, op2);
  370. if (slots_free == 0) {
  371. host1x_hw_cdma_flush(host1x, cdma);
  372. slots_free = host1x_cdma_wait_locked(cdma,
  373. CDMA_EVENT_PUSH_BUFFER_SPACE);
  374. }
  375. cdma->slots_free = slots_free - 1;
  376. cdma->slots_used++;
  377. host1x_pushbuffer_push(pb, op1, op2);
  378. }
  379. /*
  380. * End a cdma submit
  381. * Kick off DMA, add job to the sync queue, and a number of slots to be freed
  382. * from the pushbuffer. The handles for a submit must all be pinned at the same
  383. * time, but they can be unpinned in smaller chunks.
  384. */
  385. void host1x_cdma_end(struct host1x_cdma *cdma,
  386. struct host1x_job *job)
  387. {
  388. struct host1x *host1x = cdma_to_host1x(cdma);
  389. bool idle = list_empty(&cdma->sync_queue);
  390. host1x_hw_cdma_flush(host1x, cdma);
  391. job->first_get = cdma->first_get;
  392. job->num_slots = cdma->slots_used;
  393. host1x_job_get(job);
  394. list_add_tail(&job->list, &cdma->sync_queue);
  395. /* start timer on idle -> active transitions */
  396. if (job->timeout && idle)
  397. cdma_start_timer_locked(cdma, job);
  398. trace_host1x_cdma_end(dev_name(job->channel->dev));
  399. mutex_unlock(&cdma->lock);
  400. }
  401. /*
  402. * Update cdma state according to current sync point values
  403. */
  404. void host1x_cdma_update(struct host1x_cdma *cdma)
  405. {
  406. mutex_lock(&cdma->lock);
  407. update_cdma_locked(cdma);
  408. mutex_unlock(&cdma->lock);
  409. }