syncpt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Tegra host1x Syncpoints
  3. *
  4. * Copyright (c) 2010-2015, 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. mutex_lock(&host->syncpt_mutex);
  54. for (i = 0; i < host->info->nb_pts && sp->name; i++, sp++)
  55. ;
  56. if (i >= host->info->nb_pts)
  57. goto unlock;
  58. if (flags & HOST1X_SYNCPT_HAS_BASE) {
  59. sp->base = host1x_syncpt_base_request(host);
  60. if (!sp->base)
  61. goto unlock;
  62. }
  63. name = kasprintf(GFP_KERNEL, "%02u-%s", sp->id,
  64. dev ? dev_name(dev) : NULL);
  65. if (!name)
  66. goto free_base;
  67. sp->dev = dev;
  68. sp->name = name;
  69. if (flags & HOST1X_SYNCPT_CLIENT_MANAGED)
  70. sp->client_managed = true;
  71. else
  72. sp->client_managed = false;
  73. mutex_unlock(&host->syncpt_mutex);
  74. return sp;
  75. free_base:
  76. host1x_syncpt_base_free(sp->base);
  77. sp->base = NULL;
  78. unlock:
  79. mutex_unlock(&host->syncpt_mutex);
  80. return NULL;
  81. }
  82. /**
  83. * host1x_syncpt_id() - retrieve syncpoint ID
  84. * @sp: host1x syncpoint
  85. *
  86. * Given a pointer to a struct host1x_syncpt, retrieves its ID. This ID is
  87. * often used as a value to program into registers that control how hardware
  88. * blocks interact with syncpoints.
  89. */
  90. u32 host1x_syncpt_id(struct host1x_syncpt *sp)
  91. {
  92. return sp->id;
  93. }
  94. EXPORT_SYMBOL(host1x_syncpt_id);
  95. /**
  96. * host1x_syncpt_incr_max() - update the value sent to hardware
  97. * @sp: host1x syncpoint
  98. * @incrs: number of increments
  99. */
  100. u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs)
  101. {
  102. return (u32)atomic_add_return(incrs, &sp->max_val);
  103. }
  104. EXPORT_SYMBOL(host1x_syncpt_incr_max);
  105. /*
  106. * Write cached syncpoint and waitbase values to hardware.
  107. */
  108. void host1x_syncpt_restore(struct host1x *host)
  109. {
  110. struct host1x_syncpt *sp_base = host->syncpt;
  111. unsigned int i;
  112. for (i = 0; i < host1x_syncpt_nb_pts(host); i++)
  113. host1x_hw_syncpt_restore(host, sp_base + i);
  114. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  115. host1x_hw_syncpt_restore_wait_base(host, sp_base + i);
  116. wmb();
  117. }
  118. /*
  119. * Update the cached syncpoint and waitbase values by reading them
  120. * from the registers.
  121. */
  122. void host1x_syncpt_save(struct host1x *host)
  123. {
  124. struct host1x_syncpt *sp_base = host->syncpt;
  125. unsigned int i;
  126. for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
  127. if (host1x_syncpt_client_managed(sp_base + i))
  128. host1x_hw_syncpt_load(host, sp_base + i);
  129. else
  130. WARN_ON(!host1x_syncpt_idle(sp_base + i));
  131. }
  132. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  133. host1x_hw_syncpt_load_wait_base(host, sp_base + i);
  134. }
  135. /*
  136. * Updates the cached syncpoint value by reading a new value from the hardware
  137. * register
  138. */
  139. u32 host1x_syncpt_load(struct host1x_syncpt *sp)
  140. {
  141. u32 val;
  142. val = host1x_hw_syncpt_load(sp->host, sp);
  143. trace_host1x_syncpt_load_min(sp->id, val);
  144. return val;
  145. }
  146. /*
  147. * Get the current syncpoint base
  148. */
  149. u32 host1x_syncpt_load_wait_base(struct host1x_syncpt *sp)
  150. {
  151. host1x_hw_syncpt_load_wait_base(sp->host, sp);
  152. return sp->base_val;
  153. }
  154. /**
  155. * host1x_syncpt_incr() - increment syncpoint value from CPU, updating cache
  156. * @sp: host1x syncpoint
  157. */
  158. int host1x_syncpt_incr(struct host1x_syncpt *sp)
  159. {
  160. return host1x_hw_syncpt_cpu_incr(sp->host, sp);
  161. }
  162. EXPORT_SYMBOL(host1x_syncpt_incr);
  163. /*
  164. * Updated sync point form hardware, and returns true if syncpoint is expired,
  165. * false if we may need to wait
  166. */
  167. static bool syncpt_load_min_is_expired(struct host1x_syncpt *sp, u32 thresh)
  168. {
  169. host1x_hw_syncpt_load(sp->host, sp);
  170. return host1x_syncpt_is_expired(sp, thresh);
  171. }
  172. /**
  173. * host1x_syncpt_wait() - wait for a syncpoint to reach a given value
  174. * @sp: host1x syncpoint
  175. * @thresh: threshold
  176. * @timeout: maximum time to wait for the syncpoint to reach the given value
  177. * @value: return location for the syncpoint value
  178. */
  179. int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
  180. u32 *value)
  181. {
  182. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  183. void *ref;
  184. struct host1x_waitlist *waiter;
  185. int err = 0, check_count = 0;
  186. u32 val;
  187. if (value)
  188. *value = 0;
  189. /* first check cache */
  190. if (host1x_syncpt_is_expired(sp, thresh)) {
  191. if (value)
  192. *value = host1x_syncpt_load(sp);
  193. return 0;
  194. }
  195. /* try to read from register */
  196. val = host1x_hw_syncpt_load(sp->host, sp);
  197. if (host1x_syncpt_is_expired(sp, thresh)) {
  198. if (value)
  199. *value = val;
  200. goto done;
  201. }
  202. if (!timeout) {
  203. err = -EAGAIN;
  204. goto done;
  205. }
  206. /* allocate a waiter */
  207. waiter = kzalloc(sizeof(*waiter), GFP_KERNEL);
  208. if (!waiter) {
  209. err = -ENOMEM;
  210. goto done;
  211. }
  212. /* schedule a wakeup when the syncpoint value is reached */
  213. err = host1x_intr_add_action(sp->host, sp->id, thresh,
  214. HOST1X_INTR_ACTION_WAKEUP_INTERRUPTIBLE,
  215. &wq, waiter, &ref);
  216. if (err)
  217. goto done;
  218. err = -EAGAIN;
  219. /* Caller-specified timeout may be impractically low */
  220. if (timeout < 0)
  221. timeout = LONG_MAX;
  222. /* wait for the syncpoint, or timeout, or signal */
  223. while (timeout) {
  224. long check = min_t(long, SYNCPT_CHECK_PERIOD, timeout);
  225. int remain;
  226. remain = wait_event_interruptible_timeout(wq,
  227. syncpt_load_min_is_expired(sp, thresh),
  228. check);
  229. if (remain > 0 || host1x_syncpt_is_expired(sp, thresh)) {
  230. if (value)
  231. *value = host1x_syncpt_load(sp);
  232. err = 0;
  233. break;
  234. }
  235. if (remain < 0) {
  236. err = remain;
  237. break;
  238. }
  239. timeout -= check;
  240. if (timeout && check_count <= MAX_STUCK_CHECK_COUNT) {
  241. dev_warn(sp->host->dev,
  242. "%s: syncpoint id %u (%s) stuck waiting %d, timeout=%ld\n",
  243. current->comm, sp->id, sp->name,
  244. thresh, timeout);
  245. host1x_debug_dump_syncpts(sp->host);
  246. if (check_count == MAX_STUCK_CHECK_COUNT)
  247. host1x_debug_dump(sp->host);
  248. check_count++;
  249. }
  250. }
  251. host1x_intr_put_ref(sp->host, sp->id, ref);
  252. done:
  253. return err;
  254. }
  255. EXPORT_SYMBOL(host1x_syncpt_wait);
  256. /*
  257. * Returns true if syncpoint is expired, false if we may need to wait
  258. */
  259. bool host1x_syncpt_is_expired(struct host1x_syncpt *sp, u32 thresh)
  260. {
  261. u32 current_val;
  262. u32 future_val;
  263. smp_rmb();
  264. current_val = (u32)atomic_read(&sp->min_val);
  265. future_val = (u32)atomic_read(&sp->max_val);
  266. /* Note the use of unsigned arithmetic here (mod 1<<32).
  267. *
  268. * c = current_val = min_val = the current value of the syncpoint.
  269. * t = thresh = the value we are checking
  270. * f = future_val = max_val = the value c will reach when all
  271. * outstanding increments have completed.
  272. *
  273. * Note that c always chases f until it reaches f.
  274. *
  275. * Dtf = (f - t)
  276. * Dtc = (c - t)
  277. *
  278. * Consider all cases:
  279. *
  280. * A) .....c..t..f..... Dtf < Dtc need to wait
  281. * B) .....c.....f..t.. Dtf > Dtc expired
  282. * C) ..t..c.....f..... Dtf > Dtc expired (Dct very large)
  283. *
  284. * Any case where f==c: always expired (for any t). Dtf == Dcf
  285. * Any case where t==c: always expired (for any f). Dtf >= Dtc (because Dtc==0)
  286. * Any case where t==f!=c: always wait. Dtf < Dtc (because Dtf==0,
  287. * Dtc!=0)
  288. *
  289. * Other cases:
  290. *
  291. * A) .....t..f..c..... Dtf < Dtc need to wait
  292. * A) .....f..c..t..... Dtf < Dtc need to wait
  293. * A) .....f..t..c..... Dtf > Dtc expired
  294. *
  295. * So:
  296. * Dtf >= Dtc implies EXPIRED (return true)
  297. * Dtf < Dtc implies WAIT (return false)
  298. *
  299. * Note: If t is expired then we *cannot* wait on it. We would wait
  300. * forever (hang the system).
  301. *
  302. * Note: do NOT get clever and remove the -thresh from both sides. It
  303. * is NOT the same.
  304. *
  305. * If future valueis zero, we have a client managed sync point. In that
  306. * case we do a direct comparison.
  307. */
  308. if (!host1x_syncpt_client_managed(sp))
  309. return future_val - thresh >= current_val - thresh;
  310. else
  311. return (s32)(current_val - thresh) >= 0;
  312. }
  313. /* remove a wait pointed to by patch_addr */
  314. int host1x_syncpt_patch_wait(struct host1x_syncpt *sp, void *patch_addr)
  315. {
  316. return host1x_hw_syncpt_patch_wait(sp->host, sp, patch_addr);
  317. }
  318. int host1x_syncpt_init(struct host1x *host)
  319. {
  320. struct host1x_syncpt_base *bases;
  321. struct host1x_syncpt *syncpt;
  322. unsigned int i;
  323. syncpt = devm_kcalloc(host->dev, host->info->nb_pts, sizeof(*syncpt),
  324. GFP_KERNEL);
  325. if (!syncpt)
  326. return -ENOMEM;
  327. bases = devm_kcalloc(host->dev, host->info->nb_bases, sizeof(*bases),
  328. GFP_KERNEL);
  329. if (!bases)
  330. return -ENOMEM;
  331. for (i = 0; i < host->info->nb_pts; i++) {
  332. syncpt[i].id = i;
  333. syncpt[i].host = host;
  334. }
  335. for (i = 0; i < host->info->nb_bases; i++)
  336. bases[i].id = i;
  337. mutex_init(&host->syncpt_mutex);
  338. host->syncpt = syncpt;
  339. host->bases = bases;
  340. host1x_syncpt_restore(host);
  341. /* Allocate sync point to use for clearing waits for expired fences */
  342. host->nop_sp = host1x_syncpt_alloc(host, NULL, 0);
  343. if (!host->nop_sp)
  344. return -ENOMEM;
  345. return 0;
  346. }
  347. /**
  348. * host1x_syncpt_request() - request a syncpoint
  349. * @dev: device requesting the syncpoint
  350. * @flags: flags
  351. *
  352. * host1x client drivers can use this function to allocate a syncpoint for
  353. * subsequent use. A syncpoint returned by this function will be reserved for
  354. * use by the client exclusively. When no longer using a syncpoint, a host1x
  355. * client driver needs to release it using host1x_syncpt_free().
  356. */
  357. struct host1x_syncpt *host1x_syncpt_request(struct device *dev,
  358. unsigned long flags)
  359. {
  360. struct host1x *host = dev_get_drvdata(dev->parent);
  361. return host1x_syncpt_alloc(host, dev, flags);
  362. }
  363. EXPORT_SYMBOL(host1x_syncpt_request);
  364. /**
  365. * host1x_syncpt_free() - free a requested syncpoint
  366. * @sp: host1x syncpoint
  367. *
  368. * Release a syncpoint previously allocated using host1x_syncpt_request(). A
  369. * host1x client driver should call this when the syncpoint is no longer in
  370. * use. Note that client drivers must ensure that the syncpoint doesn't remain
  371. * under the control of hardware after calling this function, otherwise two
  372. * clients may end up trying to access the same syncpoint concurrently.
  373. */
  374. void host1x_syncpt_free(struct host1x_syncpt *sp)
  375. {
  376. if (!sp)
  377. return;
  378. mutex_lock(&sp->host->syncpt_mutex);
  379. host1x_syncpt_base_free(sp->base);
  380. kfree(sp->name);
  381. sp->base = NULL;
  382. sp->dev = NULL;
  383. sp->name = NULL;
  384. sp->client_managed = false;
  385. mutex_unlock(&sp->host->syncpt_mutex);
  386. }
  387. EXPORT_SYMBOL(host1x_syncpt_free);
  388. void host1x_syncpt_deinit(struct host1x *host)
  389. {
  390. struct host1x_syncpt *sp = host->syncpt;
  391. unsigned int i;
  392. for (i = 0; i < host->info->nb_pts; i++, sp++)
  393. kfree(sp->name);
  394. }
  395. /**
  396. * host1x_syncpt_read_max() - read maximum syncpoint value
  397. * @sp: host1x syncpoint
  398. *
  399. * The maximum syncpoint value indicates how many operations there are in
  400. * queue, either in channel or in a software thread.
  401. */
  402. u32 host1x_syncpt_read_max(struct host1x_syncpt *sp)
  403. {
  404. smp_rmb();
  405. return (u32)atomic_read(&sp->max_val);
  406. }
  407. EXPORT_SYMBOL(host1x_syncpt_read_max);
  408. /**
  409. * host1x_syncpt_read_min() - read minimum syncpoint value
  410. * @sp: host1x syncpoint
  411. *
  412. * The minimum syncpoint value is a shadow of the current sync point value in
  413. * hardware.
  414. */
  415. u32 host1x_syncpt_read_min(struct host1x_syncpt *sp)
  416. {
  417. smp_rmb();
  418. return (u32)atomic_read(&sp->min_val);
  419. }
  420. EXPORT_SYMBOL(host1x_syncpt_read_min);
  421. /**
  422. * host1x_syncpt_read() - read the current syncpoint value
  423. * @sp: host1x syncpoint
  424. */
  425. u32 host1x_syncpt_read(struct host1x_syncpt *sp)
  426. {
  427. return host1x_syncpt_load(sp);
  428. }
  429. EXPORT_SYMBOL(host1x_syncpt_read);
  430. unsigned int host1x_syncpt_nb_pts(struct host1x *host)
  431. {
  432. return host->info->nb_pts;
  433. }
  434. unsigned int host1x_syncpt_nb_bases(struct host1x *host)
  435. {
  436. return host->info->nb_bases;
  437. }
  438. unsigned int host1x_syncpt_nb_mlocks(struct host1x *host)
  439. {
  440. return host->info->nb_mlocks;
  441. }
  442. /**
  443. * host1x_syncpt_get() - obtain a syncpoint by ID
  444. * @host: host1x controller
  445. * @id: syncpoint ID
  446. */
  447. struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, unsigned int id)
  448. {
  449. if (id >= host->info->nb_pts)
  450. return NULL;
  451. return host->syncpt + id;
  452. }
  453. EXPORT_SYMBOL(host1x_syncpt_get);
  454. /**
  455. * host1x_syncpt_get_base() - obtain the wait base associated with a syncpoint
  456. * @sp: host1x syncpoint
  457. */
  458. struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp)
  459. {
  460. return sp ? sp->base : NULL;
  461. }
  462. EXPORT_SYMBOL(host1x_syncpt_get_base);
  463. /**
  464. * host1x_syncpt_base_id() - retrieve the ID of a syncpoint wait base
  465. * @base: host1x syncpoint wait base
  466. */
  467. u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base)
  468. {
  469. return base->id;
  470. }
  471. EXPORT_SYMBOL(host1x_syncpt_base_id);