syncpt.c 11 KB

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