syncpt.c 13 KB

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