syncpt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. 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. /*
  87. * Write cached syncpoint and waitbase values to hardware.
  88. */
  89. void host1x_syncpt_restore(struct host1x *host)
  90. {
  91. struct host1x_syncpt *sp_base = host->syncpt;
  92. u32 i;
  93. for (i = 0; i < host1x_syncpt_nb_pts(host); i++)
  94. host1x_hw_syncpt_restore(host, sp_base + i);
  95. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  96. host1x_hw_syncpt_restore_wait_base(host, sp_base + i);
  97. wmb();
  98. }
  99. /*
  100. * Update the cached syncpoint and waitbase values by reading them
  101. * from the registers.
  102. */
  103. void host1x_syncpt_save(struct host1x *host)
  104. {
  105. struct host1x_syncpt *sp_base = host->syncpt;
  106. u32 i;
  107. for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
  108. if (host1x_syncpt_client_managed(sp_base + i))
  109. host1x_hw_syncpt_load(host, sp_base + i);
  110. else
  111. WARN_ON(!host1x_syncpt_idle(sp_base + i));
  112. }
  113. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  114. host1x_hw_syncpt_load_wait_base(host, sp_base + i);
  115. }
  116. /*
  117. * Updates the cached syncpoint value by reading a new value from the hardware
  118. * register
  119. */
  120. u32 host1x_syncpt_load(struct host1x_syncpt *sp)
  121. {
  122. u32 val;
  123. val = host1x_hw_syncpt_load(sp->host, sp);
  124. trace_host1x_syncpt_load_min(sp->id, val);
  125. return val;
  126. }
  127. /*
  128. * Get the current syncpoint base
  129. */
  130. u32 host1x_syncpt_load_wait_base(struct host1x_syncpt *sp)
  131. {
  132. u32 val;
  133. host1x_hw_syncpt_load_wait_base(sp->host, sp);
  134. val = sp->base_val;
  135. return val;
  136. }
  137. /*
  138. * Increment syncpoint value from cpu, updating cache
  139. */
  140. int host1x_syncpt_incr(struct host1x_syncpt *sp)
  141. {
  142. return host1x_hw_syncpt_cpu_incr(sp->host, sp);
  143. }
  144. EXPORT_SYMBOL(host1x_syncpt_incr);
  145. /*
  146. * Updated sync point form hardware, and returns true if syncpoint is expired,
  147. * false if we may need to wait
  148. */
  149. static bool syncpt_load_min_is_expired(struct host1x_syncpt *sp, u32 thresh)
  150. {
  151. host1x_hw_syncpt_load(sp->host, sp);
  152. return host1x_syncpt_is_expired(sp, thresh);
  153. }
  154. /*
  155. * Main entrypoint for syncpoint value waits.
  156. */
  157. int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
  158. u32 *value)
  159. {
  160. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  161. void *ref;
  162. struct host1x_waitlist *waiter;
  163. int err = 0, check_count = 0;
  164. u32 val;
  165. if (value)
  166. *value = 0;
  167. /* first check cache */
  168. if (host1x_syncpt_is_expired(sp, thresh)) {
  169. if (value)
  170. *value = host1x_syncpt_load(sp);
  171. return 0;
  172. }
  173. /* try to read from register */
  174. val = host1x_hw_syncpt_load(sp->host, sp);
  175. if (host1x_syncpt_is_expired(sp, thresh)) {
  176. if (value)
  177. *value = val;
  178. goto done;
  179. }
  180. if (!timeout) {
  181. err = -EAGAIN;
  182. goto done;
  183. }
  184. /* allocate a waiter */
  185. waiter = kzalloc(sizeof(*waiter), GFP_KERNEL);
  186. if (!waiter) {
  187. err = -ENOMEM;
  188. goto done;
  189. }
  190. /* schedule a wakeup when the syncpoint value is reached */
  191. err = host1x_intr_add_action(sp->host, sp->id, thresh,
  192. HOST1X_INTR_ACTION_WAKEUP_INTERRUPTIBLE,
  193. &wq, waiter, &ref);
  194. if (err)
  195. goto done;
  196. err = -EAGAIN;
  197. /* Caller-specified timeout may be impractically low */
  198. if (timeout < 0)
  199. timeout = LONG_MAX;
  200. /* wait for the syncpoint, or timeout, or signal */
  201. while (timeout) {
  202. long check = min_t(long, SYNCPT_CHECK_PERIOD, timeout);
  203. int 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 %d (%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. int i;
  300. syncpt = devm_kzalloc(host->dev, sizeof(*syncpt) * host->info->nb_pts,
  301. GFP_KERNEL);
  302. if (!syncpt)
  303. return -ENOMEM;
  304. bases = devm_kzalloc(host->dev, sizeof(*bases) * host->info->nb_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. int i;
  345. struct host1x_syncpt *sp = host->syncpt;
  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. int host1x_syncpt_nb_pts(struct host1x *host)
  369. {
  370. return host->info->nb_pts;
  371. }
  372. int host1x_syncpt_nb_bases(struct host1x *host)
  373. {
  374. return host->info->nb_bases;
  375. }
  376. int host1x_syncpt_nb_mlocks(struct host1x *host)
  377. {
  378. return host->info->nb_mlocks;
  379. }
  380. struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id)
  381. {
  382. if (host->info->nb_pts < id)
  383. return NULL;
  384. return host->syncpt + id;
  385. }
  386. EXPORT_SYMBOL(host1x_syncpt_get);
  387. struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp)
  388. {
  389. return sp ? sp->base : NULL;
  390. }
  391. EXPORT_SYMBOL(host1x_syncpt_get_base);
  392. u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base)
  393. {
  394. return base->id;
  395. }
  396. EXPORT_SYMBOL(host1x_syncpt_base_id);