syncpt.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. int 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 (sp->dev)
  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. * Write a cpu syncpoint increment to the hardware, without touching
  112. * the cache. Caller is responsible for host being powered.
  113. */
  114. void host1x_syncpt_cpu_incr(struct host1x_syncpt *sp)
  115. {
  116. host1x_hw_syncpt_cpu_incr(sp->host, sp);
  117. }
  118. /*
  119. * Increment syncpoint value from cpu, updating cache
  120. */
  121. void host1x_syncpt_incr(struct host1x_syncpt *sp)
  122. {
  123. if (host1x_syncpt_client_managed(sp))
  124. host1x_syncpt_incr_max(sp, 1);
  125. host1x_syncpt_cpu_incr(sp);
  126. }
  127. /*
  128. * Updated sync point form hardware, and returns true if syncpoint is expired,
  129. * false if we may need to wait
  130. */
  131. static bool syncpt_load_min_is_expired(struct host1x_syncpt *sp, u32 thresh)
  132. {
  133. host1x_hw_syncpt_load(sp->host, sp);
  134. return host1x_syncpt_is_expired(sp, thresh);
  135. }
  136. /*
  137. * Main entrypoint for syncpoint value waits.
  138. */
  139. int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
  140. u32 *value)
  141. {
  142. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  143. void *ref;
  144. struct host1x_waitlist *waiter;
  145. int err = 0, check_count = 0;
  146. u32 val;
  147. if (value)
  148. *value = 0;
  149. /* first check cache */
  150. if (host1x_syncpt_is_expired(sp, thresh)) {
  151. if (value)
  152. *value = host1x_syncpt_load(sp);
  153. return 0;
  154. }
  155. /* try to read from register */
  156. val = host1x_hw_syncpt_load(sp->host, sp);
  157. if (host1x_syncpt_is_expired(sp, thresh)) {
  158. if (value)
  159. *value = val;
  160. goto done;
  161. }
  162. if (!timeout) {
  163. err = -EAGAIN;
  164. goto done;
  165. }
  166. /* allocate a waiter */
  167. waiter = kzalloc(sizeof(*waiter), GFP_KERNEL);
  168. if (!waiter) {
  169. err = -ENOMEM;
  170. goto done;
  171. }
  172. /* schedule a wakeup when the syncpoint value is reached */
  173. err = host1x_intr_add_action(sp->host, sp->id, thresh,
  174. HOST1X_INTR_ACTION_WAKEUP_INTERRUPTIBLE,
  175. &wq, waiter, &ref);
  176. if (err)
  177. goto done;
  178. err = -EAGAIN;
  179. /* Caller-specified timeout may be impractically low */
  180. if (timeout < 0)
  181. timeout = LONG_MAX;
  182. /* wait for the syncpoint, or timeout, or signal */
  183. while (timeout) {
  184. long check = min_t(long, SYNCPT_CHECK_PERIOD, timeout);
  185. int remain = wait_event_interruptible_timeout(wq,
  186. syncpt_load_min_is_expired(sp, thresh),
  187. check);
  188. if (remain > 0 || host1x_syncpt_is_expired(sp, thresh)) {
  189. if (value)
  190. *value = host1x_syncpt_load(sp);
  191. err = 0;
  192. break;
  193. }
  194. if (remain < 0) {
  195. err = remain;
  196. break;
  197. }
  198. timeout -= check;
  199. if (timeout && check_count <= MAX_STUCK_CHECK_COUNT) {
  200. dev_warn(sp->host->dev,
  201. "%s: syncpoint id %d (%s) stuck waiting %d, timeout=%ld\n",
  202. current->comm, sp->id, sp->name,
  203. thresh, timeout);
  204. host1x_debug_dump_syncpts(sp->host);
  205. if (check_count == MAX_STUCK_CHECK_COUNT)
  206. host1x_debug_dump(sp->host);
  207. check_count++;
  208. }
  209. }
  210. host1x_intr_put_ref(sp->host, sp->id, ref);
  211. done:
  212. return err;
  213. }
  214. EXPORT_SYMBOL(host1x_syncpt_wait);
  215. /*
  216. * Returns true if syncpoint is expired, false if we may need to wait
  217. */
  218. bool host1x_syncpt_is_expired(struct host1x_syncpt *sp, u32 thresh)
  219. {
  220. u32 current_val;
  221. u32 future_val;
  222. smp_rmb();
  223. current_val = (u32)atomic_read(&sp->min_val);
  224. future_val = (u32)atomic_read(&sp->max_val);
  225. /* Note the use of unsigned arithmetic here (mod 1<<32).
  226. *
  227. * c = current_val = min_val = the current value of the syncpoint.
  228. * t = thresh = the value we are checking
  229. * f = future_val = max_val = the value c will reach when all
  230. * outstanding increments have completed.
  231. *
  232. * Note that c always chases f until it reaches f.
  233. *
  234. * Dtf = (f - t)
  235. * Dtc = (c - t)
  236. *
  237. * Consider all cases:
  238. *
  239. * A) .....c..t..f..... Dtf < Dtc need to wait
  240. * B) .....c.....f..t.. Dtf > Dtc expired
  241. * C) ..t..c.....f..... Dtf > Dtc expired (Dct very large)
  242. *
  243. * Any case where f==c: always expired (for any t). Dtf == Dcf
  244. * Any case where t==c: always expired (for any f). Dtf >= Dtc (because Dtc==0)
  245. * Any case where t==f!=c: always wait. Dtf < Dtc (because Dtf==0,
  246. * Dtc!=0)
  247. *
  248. * Other cases:
  249. *
  250. * A) .....t..f..c..... Dtf < Dtc need to wait
  251. * A) .....f..c..t..... Dtf < Dtc need to wait
  252. * A) .....f..t..c..... Dtf > Dtc expired
  253. *
  254. * So:
  255. * Dtf >= Dtc implies EXPIRED (return true)
  256. * Dtf < Dtc implies WAIT (return false)
  257. *
  258. * Note: If t is expired then we *cannot* wait on it. We would wait
  259. * forever (hang the system).
  260. *
  261. * Note: do NOT get clever and remove the -thresh from both sides. It
  262. * is NOT the same.
  263. *
  264. * If future valueis zero, we have a client managed sync point. In that
  265. * case we do a direct comparison.
  266. */
  267. if (!host1x_syncpt_client_managed(sp))
  268. return future_val - thresh >= current_val - thresh;
  269. else
  270. return (s32)(current_val - thresh) >= 0;
  271. }
  272. /* remove a wait pointed to by patch_addr */
  273. int host1x_syncpt_patch_wait(struct host1x_syncpt *sp, void *patch_addr)
  274. {
  275. return host1x_hw_syncpt_patch_wait(sp->host, sp, patch_addr);
  276. }
  277. int host1x_syncpt_init(struct host1x *host)
  278. {
  279. struct host1x_syncpt *syncpt;
  280. int i;
  281. syncpt = devm_kzalloc(host->dev, sizeof(*syncpt) * host->info->nb_pts,
  282. GFP_KERNEL);
  283. if (!syncpt)
  284. return -ENOMEM;
  285. for (i = 0; i < host->info->nb_pts; ++i) {
  286. syncpt[i].id = i;
  287. syncpt[i].host = host;
  288. }
  289. host->syncpt = syncpt;
  290. host1x_syncpt_restore(host);
  291. /* Allocate sync point to use for clearing waits for expired fences */
  292. host->nop_sp = _host1x_syncpt_alloc(host, NULL, 0);
  293. if (!host->nop_sp)
  294. return -ENOMEM;
  295. return 0;
  296. }
  297. struct host1x_syncpt *host1x_syncpt_request(struct device *dev,
  298. int client_managed)
  299. {
  300. struct host1x *host = dev_get_drvdata(dev->parent);
  301. return _host1x_syncpt_alloc(host, dev, client_managed);
  302. }
  303. void host1x_syncpt_free(struct host1x_syncpt *sp)
  304. {
  305. if (!sp)
  306. return;
  307. kfree(sp->name);
  308. sp->dev = NULL;
  309. sp->name = NULL;
  310. sp->client_managed = 0;
  311. }
  312. void host1x_syncpt_deinit(struct host1x *host)
  313. {
  314. int i;
  315. struct host1x_syncpt *sp = host->syncpt;
  316. for (i = 0; i < host->info->nb_pts; i++, sp++)
  317. kfree(sp->name);
  318. }
  319. int host1x_syncpt_nb_pts(struct host1x *host)
  320. {
  321. return host->info->nb_pts;
  322. }
  323. int host1x_syncpt_nb_bases(struct host1x *host)
  324. {
  325. return host->info->nb_bases;
  326. }
  327. int host1x_syncpt_nb_mlocks(struct host1x *host)
  328. {
  329. return host->info->nb_mlocks;
  330. }
  331. struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id)
  332. {
  333. if (host->info->nb_pts < id)
  334. return NULL;
  335. return host->syncpt + id;
  336. }