syncpt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Tegra host1x Syncpoints
  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/module.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <trace/events/host1x.h>
  22. #include "syncpt.h"
  23. #include "dev.h"
  24. static struct host1x_syncpt *_host1x_syncpt_alloc(struct host1x *host,
  25. struct device *dev,
  26. int client_managed)
  27. {
  28. int i;
  29. struct host1x_syncpt *sp = host->syncpt;
  30. char *name;
  31. for (i = 0; i < host->info->nb_pts && sp->name; i++, sp++)
  32. ;
  33. if (sp->dev)
  34. return NULL;
  35. name = kasprintf(GFP_KERNEL, "%02d-%s", sp->id,
  36. dev ? dev_name(dev) : NULL);
  37. if (!name)
  38. return NULL;
  39. sp->dev = dev;
  40. sp->name = name;
  41. sp->client_managed = client_managed;
  42. return sp;
  43. }
  44. u32 host1x_syncpt_id(struct host1x_syncpt *sp)
  45. {
  46. return sp->id;
  47. }
  48. /*
  49. * Updates the value sent to hardware.
  50. */
  51. u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs)
  52. {
  53. return (u32)atomic_add_return(incrs, &sp->max_val);
  54. }
  55. /*
  56. * Write cached syncpoint and waitbase values to hardware.
  57. */
  58. void host1x_syncpt_restore(struct host1x *host)
  59. {
  60. struct host1x_syncpt *sp_base = host->syncpt;
  61. u32 i;
  62. for (i = 0; i < host1x_syncpt_nb_pts(host); i++)
  63. host1x_hw_syncpt_restore(host, sp_base + i);
  64. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  65. host1x_hw_syncpt_restore_wait_base(host, sp_base + i);
  66. wmb();
  67. }
  68. /*
  69. * Update the cached syncpoint and waitbase values by reading them
  70. * from the registers.
  71. */
  72. void host1x_syncpt_save(struct host1x *host)
  73. {
  74. struct host1x_syncpt *sp_base = host->syncpt;
  75. u32 i;
  76. for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
  77. if (host1x_syncpt_client_managed(sp_base + i))
  78. host1x_hw_syncpt_load(host, sp_base + i);
  79. else
  80. WARN_ON(!host1x_syncpt_idle(sp_base + i));
  81. }
  82. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  83. host1x_hw_syncpt_load_wait_base(host, sp_base + i);
  84. }
  85. /*
  86. * Updates the cached syncpoint value by reading a new value from the hardware
  87. * register
  88. */
  89. u32 host1x_syncpt_load(struct host1x_syncpt *sp)
  90. {
  91. u32 val;
  92. val = host1x_hw_syncpt_load(sp->host, sp);
  93. trace_host1x_syncpt_load_min(sp->id, val);
  94. return val;
  95. }
  96. /*
  97. * Get the current syncpoint base
  98. */
  99. u32 host1x_syncpt_load_wait_base(struct host1x_syncpt *sp)
  100. {
  101. u32 val;
  102. host1x_hw_syncpt_load_wait_base(sp->host, sp);
  103. val = sp->base_val;
  104. return val;
  105. }
  106. /*
  107. * Write a cpu syncpoint increment to the hardware, without touching
  108. * the cache. Caller is responsible for host being powered.
  109. */
  110. void host1x_syncpt_cpu_incr(struct host1x_syncpt *sp)
  111. {
  112. host1x_hw_syncpt_cpu_incr(sp->host, sp);
  113. }
  114. /*
  115. * Increment syncpoint value from cpu, updating cache
  116. */
  117. void host1x_syncpt_incr(struct host1x_syncpt *sp)
  118. {
  119. if (host1x_syncpt_client_managed(sp))
  120. host1x_syncpt_incr_max(sp, 1);
  121. host1x_syncpt_cpu_incr(sp);
  122. }
  123. int host1x_syncpt_init(struct host1x *host)
  124. {
  125. struct host1x_syncpt *syncpt;
  126. int i;
  127. syncpt = devm_kzalloc(host->dev, sizeof(*syncpt) * host->info->nb_pts,
  128. GFP_KERNEL);
  129. if (!syncpt)
  130. return -ENOMEM;
  131. for (i = 0; i < host->info->nb_pts; ++i) {
  132. syncpt[i].id = i;
  133. syncpt[i].host = host;
  134. }
  135. host->syncpt = syncpt;
  136. host1x_syncpt_restore(host);
  137. return 0;
  138. }
  139. struct host1x_syncpt *host1x_syncpt_request(struct device *dev,
  140. int client_managed)
  141. {
  142. struct host1x *host = dev_get_drvdata(dev->parent);
  143. return _host1x_syncpt_alloc(host, dev, client_managed);
  144. }
  145. void host1x_syncpt_free(struct host1x_syncpt *sp)
  146. {
  147. if (!sp)
  148. return;
  149. kfree(sp->name);
  150. sp->dev = NULL;
  151. sp->name = NULL;
  152. sp->client_managed = 0;
  153. }
  154. void host1x_syncpt_deinit(struct host1x *host)
  155. {
  156. int i;
  157. struct host1x_syncpt *sp = host->syncpt;
  158. for (i = 0; i < host->info->nb_pts; i++, sp++)
  159. kfree(sp->name);
  160. }
  161. int host1x_syncpt_nb_pts(struct host1x *host)
  162. {
  163. return host->info->nb_pts;
  164. }
  165. int host1x_syncpt_nb_bases(struct host1x *host)
  166. {
  167. return host->info->nb_bases;
  168. }
  169. int host1x_syncpt_nb_mlocks(struct host1x *host)
  170. {
  171. return host->info->nb_mlocks;
  172. }
  173. struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id)
  174. {
  175. if (host->info->nb_pts < id)
  176. return NULL;
  177. return host->syncpt + id;
  178. }