cdma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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_wc(host1x->dev, pb->size_bytes + 4, pb->mapped,
  49. 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_wc(host1x->dev, pb->size_bytes + 4, &pb->phys,
  68. 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 *p = (u32 *)((void *)pb->mapped + pb->pos);
  84. WARN_ON(pb->pos == pb->fence);
  85. *(p++) = op1;
  86. *(p++) = op2;
  87. pb->pos = (pb->pos + 8) & (pb->size_bytes - 1);
  88. }
  89. /*
  90. * Pop a number of two word slots from the push buffer
  91. * Caller must ensure push buffer is not empty
  92. */
  93. static void host1x_pushbuffer_pop(struct push_buffer *pb, unsigned int slots)
  94. {
  95. /* Advance the next write position */
  96. pb->fence = (pb->fence + slots * 8) & (pb->size_bytes - 1);
  97. }
  98. /*
  99. * Return the number of two word slots free in the push buffer
  100. */
  101. static u32 host1x_pushbuffer_space(struct push_buffer *pb)
  102. {
  103. return ((pb->fence - pb->pos) & (pb->size_bytes - 1)) / 8;
  104. }
  105. /*
  106. * Sleep (if necessary) until the requested event happens
  107. * - CDMA_EVENT_SYNC_QUEUE_EMPTY : sync queue is completely empty.
  108. * - Returns 1
  109. * - CDMA_EVENT_PUSH_BUFFER_SPACE : there is space in the push buffer
  110. * - Return the amount of space (> 0)
  111. * Must be called with the cdma lock held.
  112. */
  113. unsigned int host1x_cdma_wait_locked(struct host1x_cdma *cdma,
  114. enum cdma_event event)
  115. {
  116. for (;;) {
  117. struct push_buffer *pb = &cdma->push_buffer;
  118. unsigned int space;
  119. switch (event) {
  120. case CDMA_EVENT_SYNC_QUEUE_EMPTY:
  121. space = list_empty(&cdma->sync_queue) ? 1 : 0;
  122. break;
  123. case CDMA_EVENT_PUSH_BUFFER_SPACE:
  124. space = host1x_pushbuffer_space(pb);
  125. break;
  126. default:
  127. WARN_ON(1);
  128. return -EINVAL;
  129. }
  130. if (space)
  131. return space;
  132. trace_host1x_wait_cdma(dev_name(cdma_to_channel(cdma)->dev),
  133. event);
  134. /* If somebody has managed to already start waiting, yield */
  135. if (cdma->event != CDMA_EVENT_NONE) {
  136. mutex_unlock(&cdma->lock);
  137. schedule();
  138. mutex_lock(&cdma->lock);
  139. continue;
  140. }
  141. cdma->event = event;
  142. mutex_unlock(&cdma->lock);
  143. down(&cdma->sem);
  144. mutex_lock(&cdma->lock);
  145. }
  146. return 0;
  147. }
  148. /*
  149. * Start timer that tracks the time spent by the job.
  150. * Must be called with the cdma lock held.
  151. */
  152. static void cdma_start_timer_locked(struct host1x_cdma *cdma,
  153. struct host1x_job *job)
  154. {
  155. struct host1x *host = cdma_to_host1x(cdma);
  156. if (cdma->timeout.client) {
  157. /* timer already started */
  158. return;
  159. }
  160. cdma->timeout.client = job->client;
  161. cdma->timeout.syncpt = host1x_syncpt_get(host, job->syncpt_id);
  162. cdma->timeout.syncpt_val = job->syncpt_end;
  163. cdma->timeout.start_ktime = ktime_get();
  164. schedule_delayed_work(&cdma->timeout.wq,
  165. msecs_to_jiffies(job->timeout));
  166. }
  167. /*
  168. * Stop timer when a buffer submission completes.
  169. * Must be called with the cdma lock held.
  170. */
  171. static void stop_cdma_timer_locked(struct host1x_cdma *cdma)
  172. {
  173. cancel_delayed_work(&cdma->timeout.wq);
  174. cdma->timeout.client = 0;
  175. }
  176. /*
  177. * For all sync queue entries that have already finished according to the
  178. * current sync point registers:
  179. * - unpin & unref their mems
  180. * - pop their push buffer slots
  181. * - remove them from the sync queue
  182. * This is normally called from the host code's worker thread, but can be
  183. * called manually if necessary.
  184. * Must be called with the cdma lock held.
  185. */
  186. static void update_cdma_locked(struct host1x_cdma *cdma)
  187. {
  188. bool signal = false;
  189. struct host1x *host1x = cdma_to_host1x(cdma);
  190. struct host1x_job *job, *n;
  191. /* If CDMA is stopped, queue is cleared and we can return */
  192. if (!cdma->running)
  193. return;
  194. /*
  195. * Walk the sync queue, reading the sync point registers as necessary,
  196. * to consume as many sync queue entries as possible without blocking
  197. */
  198. list_for_each_entry_safe(job, n, &cdma->sync_queue, list) {
  199. struct host1x_syncpt *sp =
  200. host1x_syncpt_get(host1x, job->syncpt_id);
  201. /* Check whether this syncpt has completed, and bail if not */
  202. if (!host1x_syncpt_is_expired(sp, job->syncpt_end)) {
  203. /* Start timer on next pending syncpt */
  204. if (job->timeout)
  205. cdma_start_timer_locked(cdma, job);
  206. break;
  207. }
  208. /* Cancel timeout, when a buffer completes */
  209. if (cdma->timeout.client)
  210. stop_cdma_timer_locked(cdma);
  211. /* Unpin the memory */
  212. host1x_job_unpin(job);
  213. /* Pop push buffer slots */
  214. if (job->num_slots) {
  215. struct push_buffer *pb = &cdma->push_buffer;
  216. host1x_pushbuffer_pop(pb, job->num_slots);
  217. if (cdma->event == CDMA_EVENT_PUSH_BUFFER_SPACE)
  218. signal = true;
  219. }
  220. list_del(&job->list);
  221. host1x_job_put(job);
  222. }
  223. if (cdma->event == CDMA_EVENT_SYNC_QUEUE_EMPTY &&
  224. list_empty(&cdma->sync_queue))
  225. signal = true;
  226. if (signal) {
  227. cdma->event = CDMA_EVENT_NONE;
  228. up(&cdma->sem);
  229. }
  230. }
  231. void host1x_cdma_update_sync_queue(struct host1x_cdma *cdma,
  232. struct device *dev)
  233. {
  234. struct host1x *host1x = cdma_to_host1x(cdma);
  235. u32 restart_addr, syncpt_incrs, syncpt_val;
  236. struct host1x_job *job = NULL;
  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. /*
  290. * The following sumbits from the same client may be dependent on the
  291. * failed submit and therefore they may fail. Force a small timeout
  292. * to make the queue cleanup faster.
  293. */
  294. list_for_each_entry_from(job, &cdma->sync_queue, list)
  295. if (job->client == cdma->timeout.client)
  296. job->timeout = min_t(unsigned int, job->timeout, 500);
  297. dev_dbg(dev, "%s: finished sync_queue modification\n", __func__);
  298. /* roll back DMAGET and start up channel again */
  299. host1x_hw_cdma_resume(host1x, cdma, restart_addr);
  300. }
  301. /*
  302. * Create a cdma
  303. */
  304. int host1x_cdma_init(struct host1x_cdma *cdma)
  305. {
  306. int err;
  307. mutex_init(&cdma->lock);
  308. sema_init(&cdma->sem, 0);
  309. INIT_LIST_HEAD(&cdma->sync_queue);
  310. cdma->event = CDMA_EVENT_NONE;
  311. cdma->running = false;
  312. cdma->torndown = false;
  313. err = host1x_pushbuffer_init(&cdma->push_buffer);
  314. if (err)
  315. return err;
  316. return 0;
  317. }
  318. /*
  319. * Destroy a cdma
  320. */
  321. int host1x_cdma_deinit(struct host1x_cdma *cdma)
  322. {
  323. struct push_buffer *pb = &cdma->push_buffer;
  324. struct host1x *host1x = cdma_to_host1x(cdma);
  325. if (cdma->running) {
  326. pr_warn("%s: CDMA still running\n", __func__);
  327. return -EBUSY;
  328. }
  329. host1x_pushbuffer_destroy(pb);
  330. host1x_hw_cdma_timeout_destroy(host1x, cdma);
  331. return 0;
  332. }
  333. /*
  334. * Begin a cdma submit
  335. */
  336. int host1x_cdma_begin(struct host1x_cdma *cdma, struct host1x_job *job)
  337. {
  338. struct host1x *host1x = cdma_to_host1x(cdma);
  339. mutex_lock(&cdma->lock);
  340. if (job->timeout) {
  341. /* init state on first submit with timeout value */
  342. if (!cdma->timeout.initialized) {
  343. int err;
  344. err = host1x_hw_cdma_timeout_init(host1x, cdma,
  345. job->syncpt_id);
  346. if (err) {
  347. mutex_unlock(&cdma->lock);
  348. return err;
  349. }
  350. }
  351. }
  352. if (!cdma->running)
  353. host1x_hw_cdma_start(host1x, cdma);
  354. cdma->slots_free = 0;
  355. cdma->slots_used = 0;
  356. cdma->first_get = cdma->push_buffer.pos;
  357. trace_host1x_cdma_begin(dev_name(job->channel->dev));
  358. return 0;
  359. }
  360. /*
  361. * Push two words into a push buffer slot
  362. * Blocks as necessary if the push buffer is full.
  363. */
  364. void host1x_cdma_push(struct host1x_cdma *cdma, u32 op1, u32 op2)
  365. {
  366. struct host1x *host1x = cdma_to_host1x(cdma);
  367. struct push_buffer *pb = &cdma->push_buffer;
  368. u32 slots_free = cdma->slots_free;
  369. if (host1x_debug_trace_cmdbuf)
  370. trace_host1x_cdma_push(dev_name(cdma_to_channel(cdma)->dev),
  371. op1, op2);
  372. if (slots_free == 0) {
  373. host1x_hw_cdma_flush(host1x, cdma);
  374. slots_free = host1x_cdma_wait_locked(cdma,
  375. CDMA_EVENT_PUSH_BUFFER_SPACE);
  376. }
  377. cdma->slots_free = slots_free - 1;
  378. cdma->slots_used++;
  379. host1x_pushbuffer_push(pb, op1, op2);
  380. }
  381. /*
  382. * End a cdma submit
  383. * Kick off DMA, add job to the sync queue, and a number of slots to be freed
  384. * from the pushbuffer. The handles for a submit must all be pinned at the same
  385. * time, but they can be unpinned in smaller chunks.
  386. */
  387. void host1x_cdma_end(struct host1x_cdma *cdma,
  388. struct host1x_job *job)
  389. {
  390. struct host1x *host1x = cdma_to_host1x(cdma);
  391. bool idle = list_empty(&cdma->sync_queue);
  392. host1x_hw_cdma_flush(host1x, cdma);
  393. job->first_get = cdma->first_get;
  394. job->num_slots = cdma->slots_used;
  395. host1x_job_get(job);
  396. list_add_tail(&job->list, &cdma->sync_queue);
  397. /* start timer on idle -> active transitions */
  398. if (job->timeout && idle)
  399. cdma_start_timer_locked(cdma, job);
  400. trace_host1x_cdma_end(dev_name(job->channel->dev));
  401. mutex_unlock(&cdma->lock);
  402. }
  403. /*
  404. * Update cdma state according to current sync point values
  405. */
  406. void host1x_cdma_update(struct host1x_cdma *cdma)
  407. {
  408. mutex_lock(&cdma->lock);
  409. update_cdma_locked(cdma);
  410. mutex_unlock(&cdma->lock);
  411. }