syncpt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #include "intr.h"
  25. #include "debug.h"
  26. #define SYNCPT_CHECK_PERIOD (2 * HZ)
  27. #define MAX_STUCK_CHECK_COUNT 15
  28. static struct host1x_syncpt *_host1x_syncpt_alloc(struct host1x *host,
  29. struct device *dev,
  30. bool client_managed)
  31. {
  32. int i;
  33. struct host1x_syncpt *sp = host->syncpt;
  34. char *name;
  35. for (i = 0; i < host->info->nb_pts && sp->name; i++, sp++)
  36. ;
  37. if (i >= host->info->nb_pts)
  38. return NULL;
  39. name = kasprintf(GFP_KERNEL, "%02d-%s", sp->id,
  40. dev ? dev_name(dev) : NULL);
  41. if (!name)
  42. return NULL;
  43. sp->dev = dev;
  44. sp->name = name;
  45. sp->client_managed = client_managed;
  46. return sp;
  47. }
  48. u32 host1x_syncpt_id(struct host1x_syncpt *sp)
  49. {
  50. return sp->id;
  51. }
  52. /*
  53. * Updates the value sent to hardware.
  54. */
  55. u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs)
  56. {
  57. return (u32)atomic_add_return(incrs, &sp->max_val);
  58. }
  59. /*
  60. * Write cached syncpoint and waitbase values to hardware.
  61. */
  62. void host1x_syncpt_restore(struct host1x *host)
  63. {
  64. struct host1x_syncpt *sp_base = host->syncpt;
  65. u32 i;
  66. for (i = 0; i < host1x_syncpt_nb_pts(host); i++)
  67. host1x_hw_syncpt_restore(host, sp_base + i);
  68. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  69. host1x_hw_syncpt_restore_wait_base(host, sp_base + i);
  70. wmb();
  71. }
  72. /*
  73. * Update the cached syncpoint and waitbase values by reading them
  74. * from the registers.
  75. */
  76. void host1x_syncpt_save(struct host1x *host)
  77. {
  78. struct host1x_syncpt *sp_base = host->syncpt;
  79. u32 i;
  80. for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
  81. if (host1x_syncpt_client_managed(sp_base + i))
  82. host1x_hw_syncpt_load(host, sp_base + i);
  83. else
  84. WARN_ON(!host1x_syncpt_idle(sp_base + i));
  85. }
  86. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  87. host1x_hw_syncpt_load_wait_base(host, sp_base + i);
  88. }
  89. /*
  90. * Updates the cached syncpoint value by reading a new value from the hardware
  91. * register
  92. */
  93. u32 host1x_syncpt_load(struct host1x_syncpt *sp)
  94. {
  95. u32 val;
  96. val = host1x_hw_syncpt_load(sp->host, sp);
  97. trace_host1x_syncpt_load_min(sp->id, val);
  98. return val;
  99. }
  100. /*
  101. * Get the current syncpoint base
  102. */
  103. u32 host1x_syncpt_load_wait_base(struct host1x_syncpt *sp)
  104. {
  105. u32 val;
  106. host1x_hw_syncpt_load_wait_base(sp->host, sp);
  107. val = sp->base_val;
  108. return val;
  109. }
  110. /*
  111. * Increment syncpoint value from cpu, updating cache
  112. */
  113. int host1x_syncpt_incr(struct host1x_syncpt *sp)
  114. {
  115. return host1x_hw_syncpt_cpu_incr(sp->host, sp);
  116. }
  117. /*
  118. * Updated sync point form hardware, and returns true if syncpoint is expired,
  119. * false if we may need to wait
  120. */
  121. static bool syncpt_load_min_is_expired(struct host1x_syncpt *sp, u32 thresh)
  122. {
  123. host1x_hw_syncpt_load(sp->host, sp);
  124. return host1x_syncpt_is_expired(sp, thresh);
  125. }
  126. /*
  127. * Main entrypoint for syncpoint value waits.
  128. */
  129. int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
  130. u32 *value)
  131. {
  132. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  133. void *ref;
  134. struct host1x_waitlist *waiter;
  135. int err = 0, check_count = 0;
  136. u32 val;
  137. if (value)
  138. *value = 0;
  139. /* first check cache */
  140. if (host1x_syncpt_is_expired(sp, thresh)) {
  141. if (value)
  142. *value = host1x_syncpt_load(sp);
  143. return 0;
  144. }
  145. /* try to read from register */
  146. val = host1x_hw_syncpt_load(sp->host, sp);
  147. if (host1x_syncpt_is_expired(sp, thresh)) {
  148. if (value)
  149. *value = val;
  150. goto done;
  151. }
  152. if (!timeout) {
  153. err = -EAGAIN;
  154. goto done;
  155. }
  156. /* allocate a waiter */
  157. waiter = kzalloc(sizeof(*waiter), GFP_KERNEL);
  158. if (!waiter) {
  159. err = -ENOMEM;
  160. goto done;
  161. }
  162. /* schedule a wakeup when the syncpoint value is reached */
  163. err = host1x_intr_add_action(sp->host, sp->id, thresh,
  164. HOST1X_INTR_ACTION_WAKEUP_INTERRUPTIBLE,
  165. &wq, waiter, &ref);
  166. if (err)
  167. goto done;
  168. err = -EAGAIN;
  169. /* Caller-specified timeout may be impractically low */
  170. if (timeout < 0)
  171. timeout = LONG_MAX;
  172. /* wait for the syncpoint, or timeout, or signal */
  173. while (timeout) {
  174. long check = min_t(long, SYNCPT_CHECK_PERIOD, timeout);
  175. int remain = wait_event_interruptible_timeout(wq,
  176. syncpt_load_min_is_expired(sp, thresh),
  177. check);
  178. if (remain > 0 || host1x_syncpt_is_expired(sp, thresh)) {
  179. if (value)
  180. *value = host1x_syncpt_load(sp);
  181. err = 0;
  182. break;
  183. }
  184. if (remain < 0) {
  185. err = remain;
  186. break;
  187. }
  188. timeout -= check;
  189. if (timeout && check_count <= MAX_STUCK_CHECK_COUNT) {
  190. dev_warn(sp->host->dev,
  191. "%s: syncpoint id %d (%s) stuck waiting %d, timeout=%ld\n",
  192. current->comm, sp->id, sp->name,
  193. thresh, timeout);
  194. host1x_debug_dump_syncpts(sp->host);
  195. if (check_count == MAX_STUCK_CHECK_COUNT)
  196. host1x_debug_dump(sp->host);
  197. check_count++;
  198. }
  199. }
  200. host1x_intr_put_ref(sp->host, sp->id, ref);
  201. done:
  202. return err;
  203. }
  204. EXPORT_SYMBOL(host1x_syncpt_wait);
  205. /*
  206. * Returns true if syncpoint is expired, false if we may need to wait
  207. */
  208. bool host1x_syncpt_is_expired(struct host1x_syncpt *sp, u32 thresh)
  209. {
  210. u32 current_val;
  211. u32 future_val;
  212. smp_rmb();
  213. current_val = (u32)atomic_read(&sp->min_val);
  214. future_val = (u32)atomic_read(&sp->max_val);
  215. /* Note the use of unsigned arithmetic here (mod 1<<32).
  216. *
  217. * c = current_val = min_val = the current value of the syncpoint.
  218. * t = thresh = the value we are checking
  219. * f = future_val = max_val = the value c will reach when all
  220. * outstanding increments have completed.
  221. *
  222. * Note that c always chases f until it reaches f.
  223. *
  224. * Dtf = (f - t)
  225. * Dtc = (c - t)
  226. *
  227. * Consider all cases:
  228. *
  229. * A) .....c..t..f..... Dtf < Dtc need to wait
  230. * B) .....c.....f..t.. Dtf > Dtc expired
  231. * C) ..t..c.....f..... Dtf > Dtc expired (Dct very large)
  232. *
  233. * Any case where f==c: always expired (for any t). Dtf == Dcf
  234. * Any case where t==c: always expired (for any f). Dtf >= Dtc (because Dtc==0)
  235. * Any case where t==f!=c: always wait. Dtf < Dtc (because Dtf==0,
  236. * Dtc!=0)
  237. *
  238. * Other cases:
  239. *
  240. * A) .....t..f..c..... Dtf < Dtc need to wait
  241. * A) .....f..c..t..... Dtf < Dtc need to wait
  242. * A) .....f..t..c..... Dtf > Dtc expired
  243. *
  244. * So:
  245. * Dtf >= Dtc implies EXPIRED (return true)
  246. * Dtf < Dtc implies WAIT (return false)
  247. *
  248. * Note: If t is expired then we *cannot* wait on it. We would wait
  249. * forever (hang the system).
  250. *
  251. * Note: do NOT get clever and remove the -thresh from both sides. It
  252. * is NOT the same.
  253. *
  254. * If future valueis zero, we have a client managed sync point. In that
  255. * case we do a direct comparison.
  256. */
  257. if (!host1x_syncpt_client_managed(sp))
  258. return future_val - thresh >= current_val - thresh;
  259. else
  260. return (s32)(current_val - thresh) >= 0;
  261. }
  262. /* remove a wait pointed to by patch_addr */
  263. int host1x_syncpt_patch_wait(struct host1x_syncpt *sp, void *patch_addr)
  264. {
  265. return host1x_hw_syncpt_patch_wait(sp->host, sp, patch_addr);
  266. }
  267. int host1x_syncpt_init(struct host1x *host)
  268. {
  269. struct host1x_syncpt *syncpt;
  270. int i;
  271. syncpt = devm_kzalloc(host->dev, sizeof(*syncpt) * host->info->nb_pts,
  272. GFP_KERNEL);
  273. if (!syncpt)
  274. return -ENOMEM;
  275. for (i = 0; i < host->info->nb_pts; ++i) {
  276. syncpt[i].id = i;
  277. syncpt[i].host = host;
  278. }
  279. host->syncpt = syncpt;
  280. host1x_syncpt_restore(host);
  281. /* Allocate sync point to use for clearing waits for expired fences */
  282. host->nop_sp = _host1x_syncpt_alloc(host, NULL, false);
  283. if (!host->nop_sp)
  284. return -ENOMEM;
  285. return 0;
  286. }
  287. struct host1x_syncpt *host1x_syncpt_request(struct device *dev,
  288. bool client_managed)
  289. {
  290. struct host1x *host = dev_get_drvdata(dev->parent);
  291. return _host1x_syncpt_alloc(host, dev, client_managed);
  292. }
  293. void host1x_syncpt_free(struct host1x_syncpt *sp)
  294. {
  295. if (!sp)
  296. return;
  297. kfree(sp->name);
  298. sp->dev = NULL;
  299. sp->name = NULL;
  300. sp->client_managed = false;
  301. }
  302. void host1x_syncpt_deinit(struct host1x *host)
  303. {
  304. int i;
  305. struct host1x_syncpt *sp = host->syncpt;
  306. for (i = 0; i < host->info->nb_pts; i++, sp++)
  307. kfree(sp->name);
  308. }
  309. int host1x_syncpt_nb_pts(struct host1x *host)
  310. {
  311. return host->info->nb_pts;
  312. }
  313. int host1x_syncpt_nb_bases(struct host1x *host)
  314. {
  315. return host->info->nb_bases;
  316. }
  317. int host1x_syncpt_nb_mlocks(struct host1x *host)
  318. {
  319. return host->info->nb_mlocks;
  320. }
  321. struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id)
  322. {
  323. if (host->info->nb_pts < id)
  324. return NULL;
  325. return host->syncpt + id;
  326. }