syncpt.c 10 KB

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