channel_hw.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Tegra host1x Channel
  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 <linux/host1x.h>
  19. #include <linux/slab.h>
  20. #include <trace/events/host1x.h>
  21. #include "../channel.h"
  22. #include "../dev.h"
  23. #include "../intr.h"
  24. #include "../job.h"
  25. #define HOST1X_CHANNEL_SIZE 16384
  26. #define TRACE_MAX_LENGTH 128U
  27. static void trace_write_gather(struct host1x_cdma *cdma, struct host1x_bo *bo,
  28. u32 offset, u32 words)
  29. {
  30. void *mem = NULL;
  31. if (host1x_debug_trace_cmdbuf)
  32. mem = host1x_bo_mmap(bo);
  33. if (mem) {
  34. u32 i;
  35. /*
  36. * Write in batches of 128 as there seems to be a limit
  37. * of how much you can output to ftrace at once.
  38. */
  39. for (i = 0; i < words; i += TRACE_MAX_LENGTH) {
  40. trace_host1x_cdma_push_gather(
  41. dev_name(cdma_to_channel(cdma)->dev),
  42. (u32)bo, min(words - i, TRACE_MAX_LENGTH),
  43. offset + i * sizeof(u32), mem);
  44. }
  45. host1x_bo_munmap(bo, mem);
  46. }
  47. }
  48. static void submit_gathers(struct host1x_job *job)
  49. {
  50. struct host1x_cdma *cdma = &job->channel->cdma;
  51. unsigned int i;
  52. for (i = 0; i < job->num_gathers; i++) {
  53. struct host1x_job_gather *g = &job->gathers[i];
  54. u32 op1 = host1x_opcode_gather(g->words);
  55. u32 op2 = g->base + g->offset;
  56. trace_write_gather(cdma, g->bo, g->offset, op1 & 0xffff);
  57. host1x_cdma_push(cdma, op1, op2);
  58. }
  59. }
  60. static inline void synchronize_syncpt_base(struct host1x_job *job)
  61. {
  62. struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
  63. struct host1x_syncpt *sp = host->syncpt + job->syncpt_id;
  64. u32 id, value;
  65. value = host1x_syncpt_read_max(sp);
  66. id = sp->base->id;
  67. host1x_cdma_push(&job->channel->cdma,
  68. host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
  69. HOST1X_UCLASS_LOAD_SYNCPT_BASE, 1),
  70. HOST1X_UCLASS_LOAD_SYNCPT_BASE_BASE_INDX_F(id) |
  71. HOST1X_UCLASS_LOAD_SYNCPT_BASE_VALUE_F(value));
  72. }
  73. static int channel_submit(struct host1x_job *job)
  74. {
  75. struct host1x_channel *ch = job->channel;
  76. struct host1x_syncpt *sp;
  77. u32 user_syncpt_incrs = job->syncpt_incrs;
  78. u32 prev_max = 0;
  79. u32 syncval;
  80. int err;
  81. struct host1x_waitlist *completed_waiter = NULL;
  82. struct host1x *host = dev_get_drvdata(ch->dev->parent);
  83. sp = host->syncpt + job->syncpt_id;
  84. trace_host1x_channel_submit(dev_name(ch->dev),
  85. job->num_gathers, job->num_relocs,
  86. job->num_waitchk, job->syncpt_id,
  87. job->syncpt_incrs);
  88. /* before error checks, return current max */
  89. prev_max = job->syncpt_end = host1x_syncpt_read_max(sp);
  90. /* get submit lock */
  91. err = mutex_lock_interruptible(&ch->submitlock);
  92. if (err)
  93. goto error;
  94. completed_waiter = kzalloc(sizeof(*completed_waiter), GFP_KERNEL);
  95. if (!completed_waiter) {
  96. mutex_unlock(&ch->submitlock);
  97. err = -ENOMEM;
  98. goto error;
  99. }
  100. /* begin a CDMA submit */
  101. err = host1x_cdma_begin(&ch->cdma, job);
  102. if (err) {
  103. mutex_unlock(&ch->submitlock);
  104. goto error;
  105. }
  106. if (job->serialize) {
  107. /*
  108. * Force serialization by inserting a host wait for the
  109. * previous job to finish before this one can commence.
  110. */
  111. host1x_cdma_push(&ch->cdma,
  112. host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
  113. host1x_uclass_wait_syncpt_r(), 1),
  114. host1x_class_host_wait_syncpt(job->syncpt_id,
  115. host1x_syncpt_read_max(sp)));
  116. }
  117. /* Synchronize base register to allow using it for relative waiting */
  118. if (sp->base)
  119. synchronize_syncpt_base(job);
  120. syncval = host1x_syncpt_incr_max(sp, user_syncpt_incrs);
  121. job->syncpt_end = syncval;
  122. /* add a setclass for modules that require it */
  123. if (job->class)
  124. host1x_cdma_push(&ch->cdma,
  125. host1x_opcode_setclass(job->class, 0, 0),
  126. HOST1X_OPCODE_NOP);
  127. submit_gathers(job);
  128. /* end CDMA submit & stash pinned hMems into sync queue */
  129. host1x_cdma_end(&ch->cdma, job);
  130. trace_host1x_channel_submitted(dev_name(ch->dev), prev_max, syncval);
  131. /* schedule a submit complete interrupt */
  132. err = host1x_intr_add_action(host, job->syncpt_id, syncval,
  133. HOST1X_INTR_ACTION_SUBMIT_COMPLETE, ch,
  134. completed_waiter, NULL);
  135. completed_waiter = NULL;
  136. WARN(err, "Failed to set submit complete interrupt");
  137. mutex_unlock(&ch->submitlock);
  138. return 0;
  139. error:
  140. kfree(completed_waiter);
  141. return err;
  142. }
  143. static int host1x_channel_init(struct host1x_channel *ch, struct host1x *dev,
  144. unsigned int index)
  145. {
  146. ch->id = index;
  147. mutex_init(&ch->reflock);
  148. mutex_init(&ch->submitlock);
  149. ch->regs = dev->regs + index * HOST1X_CHANNEL_SIZE;
  150. return 0;
  151. }
  152. static const struct host1x_channel_ops host1x_channel_ops = {
  153. .init = host1x_channel_init,
  154. .submit = channel_submit,
  155. };