syncpt.c 11 KB

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