syncpt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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/dma-fence.h>
  10. #include <linux/slab.h>
  11. #include <trace/events/host1x.h>
  12. #include "syncpt.h"
  13. #include "dev.h"
  14. #include "intr.h"
  15. #include "debug.h"
  16. #define SYNCPT_CHECK_PERIOD (2 * HZ)
  17. #define MAX_STUCK_CHECK_COUNT 15
  18. static struct host1x_syncpt_base *
  19. host1x_syncpt_base_request(struct host1x *host)
  20. {
  21. struct host1x_syncpt_base *bases = host->bases;
  22. unsigned int i;
  23. for (i = 0; i < host->info->nb_bases; i++)
  24. if (!bases[i].requested)
  25. break;
  26. if (i >= host->info->nb_bases)
  27. return NULL;
  28. bases[i].requested = true;
  29. return &bases[i];
  30. }
  31. static void host1x_syncpt_base_free(struct host1x_syncpt_base *base)
  32. {
  33. if (base)
  34. base->requested = false;
  35. }
  36. /**
  37. * host1x_syncpt_alloc() - allocate a syncpoint
  38. * @host: host1x device data
  39. * @flags: bitfield of HOST1X_SYNCPT_* flags
  40. * @name: name for the syncpoint for use in debug prints
  41. *
  42. * Allocates a hardware syncpoint for the caller's use. The caller then has
  43. * the sole authority to mutate the syncpoint's value until it is freed again.
  44. *
  45. * If no free syncpoints are available, or a NULL name was specified, returns
  46. * NULL.
  47. */
  48. struct host1x_syncpt *host1x_syncpt_alloc(struct host1x *host,
  49. unsigned long flags,
  50. const char *name)
  51. {
  52. struct host1x_syncpt *sp = host->syncpt;
  53. char *full_name;
  54. unsigned int i;
  55. if (!name)
  56. return NULL;
  57. mutex_lock(&host->syncpt_mutex);
  58. for (i = 0; i < host->info->nb_pts && kref_read(&sp->ref); i++, sp++)
  59. ;
  60. if (i >= host->info->nb_pts)
  61. goto unlock;
  62. if (flags & HOST1X_SYNCPT_HAS_BASE) {
  63. sp->base = host1x_syncpt_base_request(host);
  64. if (!sp->base)
  65. goto unlock;
  66. }
  67. full_name = kasprintf(GFP_KERNEL, "%u-%s", sp->id, name);
  68. if (!full_name)
  69. goto free_base;
  70. sp->name = full_name;
  71. if (flags & HOST1X_SYNCPT_CLIENT_MANAGED)
  72. sp->client_managed = true;
  73. else
  74. sp->client_managed = false;
  75. kref_init(&sp->ref);
  76. mutex_unlock(&host->syncpt_mutex);
  77. return sp;
  78. free_base:
  79. host1x_syncpt_base_free(sp->base);
  80. sp->base = NULL;
  81. unlock:
  82. mutex_unlock(&host->syncpt_mutex);
  83. return NULL;
  84. }
  85. EXPORT_SYMBOL(host1x_syncpt_alloc);
  86. /**
  87. * host1x_syncpt_id() - retrieve syncpoint ID
  88. * @sp: host1x syncpoint
  89. *
  90. * Given a pointer to a struct host1x_syncpt, retrieves its ID. This ID is
  91. * often used as a value to program into registers that control how hardware
  92. * blocks interact with syncpoints.
  93. */
  94. u32 host1x_syncpt_id(struct host1x_syncpt *sp)
  95. {
  96. return sp->id;
  97. }
  98. EXPORT_SYMBOL(host1x_syncpt_id);
  99. /**
  100. * host1x_syncpt_incr_max() - update the value sent to hardware
  101. * @sp: host1x syncpoint
  102. * @incrs: number of increments
  103. */
  104. u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs)
  105. {
  106. return (u32)atomic_add_return(incrs, &sp->max_val);
  107. }
  108. EXPORT_SYMBOL(host1x_syncpt_incr_max);
  109. /*
  110. * Write cached syncpoint and waitbase values to hardware.
  111. */
  112. void host1x_syncpt_restore(struct host1x *host)
  113. {
  114. struct host1x_syncpt *sp_base = host->syncpt;
  115. unsigned int i;
  116. for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
  117. /*
  118. * Unassign syncpt from channels for purposes of Tegra186
  119. * syncpoint protection. This prevents any channel from
  120. * accessing it until it is reassigned.
  121. */
  122. host1x_hw_syncpt_assign_to_channel(host, sp_base + i, NULL);
  123. host1x_hw_syncpt_restore(host, sp_base + i);
  124. }
  125. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  126. host1x_hw_syncpt_restore_wait_base(host, sp_base + i);
  127. host1x_hw_syncpt_enable_protection(host);
  128. wmb();
  129. }
  130. /*
  131. * Update the cached syncpoint and waitbase values by reading them
  132. * from the registers.
  133. */
  134. void host1x_syncpt_save(struct host1x *host)
  135. {
  136. struct host1x_syncpt *sp_base = host->syncpt;
  137. unsigned int i;
  138. for (i = 0; i < host1x_syncpt_nb_pts(host); i++) {
  139. if (host1x_syncpt_client_managed(sp_base + i))
  140. host1x_hw_syncpt_load(host, sp_base + i);
  141. else
  142. WARN_ON(!host1x_syncpt_idle(sp_base + i));
  143. }
  144. for (i = 0; i < host1x_syncpt_nb_bases(host); i++)
  145. host1x_hw_syncpt_load_wait_base(host, sp_base + i);
  146. }
  147. /*
  148. * Updates the cached syncpoint value by reading a new value from the hardware
  149. * register
  150. */
  151. u32 host1x_syncpt_load(struct host1x_syncpt *sp)
  152. {
  153. u32 val;
  154. val = host1x_hw_syncpt_load(sp->host, sp);
  155. trace_host1x_syncpt_load_min(sp->id, val);
  156. return val;
  157. }
  158. /*
  159. * Get the current syncpoint base
  160. */
  161. u32 host1x_syncpt_load_wait_base(struct host1x_syncpt *sp)
  162. {
  163. host1x_hw_syncpt_load_wait_base(sp->host, sp);
  164. return sp->base_val;
  165. }
  166. /**
  167. * host1x_syncpt_incr() - increment syncpoint value from CPU, updating cache
  168. * @sp: host1x syncpoint
  169. */
  170. int host1x_syncpt_incr(struct host1x_syncpt *sp)
  171. {
  172. return host1x_hw_syncpt_cpu_incr(sp->host, sp);
  173. }
  174. EXPORT_SYMBOL(host1x_syncpt_incr);
  175. /**
  176. * host1x_syncpt_wait() - wait for a syncpoint to reach a given value
  177. * @sp: host1x syncpoint
  178. * @thresh: threshold
  179. * @timeout: maximum time to wait for the syncpoint to reach the given value
  180. * @value: return location for the syncpoint value
  181. */
  182. int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
  183. u32 *value)
  184. {
  185. struct dma_fence *fence;
  186. long wait_err;
  187. host1x_hw_syncpt_load(sp->host, sp);
  188. if (value)
  189. *value = host1x_syncpt_load(sp);
  190. if (host1x_syncpt_is_expired(sp, thresh))
  191. return 0;
  192. if (timeout < 0)
  193. timeout = LONG_MAX;
  194. else if (timeout == 0)
  195. return -EAGAIN;
  196. fence = host1x_fence_create(sp, thresh, false);
  197. if (IS_ERR(fence))
  198. return PTR_ERR(fence);
  199. wait_err = dma_fence_wait_timeout(fence, true, timeout);
  200. if (wait_err == 0)
  201. host1x_fence_cancel(fence);
  202. dma_fence_put(fence);
  203. if (value)
  204. *value = host1x_syncpt_load(sp);
  205. /*
  206. * Don't rely on dma_fence_wait_timeout return value,
  207. * since it returns zero both on timeout and if the
  208. * wait completed with 0 jiffies left.
  209. */
  210. host1x_hw_syncpt_load(sp->host, sp);
  211. if (wait_err == 0 && !host1x_syncpt_is_expired(sp, thresh))
  212. return -EAGAIN;
  213. else if (wait_err < 0)
  214. return wait_err;
  215. else
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(host1x_syncpt_wait);
  219. /*
  220. * Returns true if syncpoint is expired, false if we may need to wait
  221. */
  222. bool host1x_syncpt_is_expired(struct host1x_syncpt *sp, u32 thresh)
  223. {
  224. u32 current_val;
  225. smp_rmb();
  226. current_val = (u32)atomic_read(&sp->min_val);
  227. return ((current_val - thresh) & 0x80000000U) == 0U;
  228. }
  229. int host1x_syncpt_init(struct host1x *host)
  230. {
  231. struct host1x_syncpt_base *bases;
  232. struct host1x_syncpt *syncpt;
  233. unsigned int i;
  234. syncpt = devm_kcalloc(host->dev, host->info->nb_pts, sizeof(*syncpt),
  235. GFP_KERNEL);
  236. if (!syncpt)
  237. return -ENOMEM;
  238. bases = devm_kcalloc(host->dev, host->info->nb_bases, sizeof(*bases),
  239. GFP_KERNEL);
  240. if (!bases)
  241. return -ENOMEM;
  242. for (i = 0; i < host->info->nb_pts; i++) {
  243. syncpt[i].id = i;
  244. syncpt[i].host = host;
  245. }
  246. for (i = 0; i < host->info->nb_bases; i++)
  247. bases[i].id = i;
  248. mutex_init(&host->syncpt_mutex);
  249. host->syncpt = syncpt;
  250. host->bases = bases;
  251. /* Allocate sync point to use for clearing waits for expired fences */
  252. host->nop_sp = host1x_syncpt_alloc(host, 0, "reserved-nop");
  253. if (!host->nop_sp)
  254. return -ENOMEM;
  255. if (host->info->reserve_vblank_syncpts) {
  256. kref_init(&host->syncpt[26].ref);
  257. kref_init(&host->syncpt[27].ref);
  258. }
  259. return 0;
  260. }
  261. /**
  262. * host1x_syncpt_request() - request a syncpoint
  263. * @client: client requesting the syncpoint
  264. * @flags: flags
  265. *
  266. * host1x client drivers can use this function to allocate a syncpoint for
  267. * subsequent use. A syncpoint returned by this function will be reserved for
  268. * use by the client exclusively. When no longer using a syncpoint, a host1x
  269. * client driver needs to release it using host1x_syncpt_put().
  270. */
  271. struct host1x_syncpt *host1x_syncpt_request(struct host1x_client *client,
  272. unsigned long flags)
  273. {
  274. struct host1x *host = dev_get_drvdata(client->host->parent);
  275. return host1x_syncpt_alloc(host, flags, dev_name(client->dev));
  276. }
  277. EXPORT_SYMBOL(host1x_syncpt_request);
  278. static void syncpt_release(struct kref *ref)
  279. {
  280. struct host1x_syncpt *sp = container_of(ref, struct host1x_syncpt, ref);
  281. atomic_set(&sp->max_val, host1x_syncpt_read(sp));
  282. sp->locked = false;
  283. host1x_syncpt_base_free(sp->base);
  284. kfree(sp->name);
  285. sp->base = NULL;
  286. sp->name = NULL;
  287. sp->client_managed = false;
  288. mutex_unlock(&sp->host->syncpt_mutex);
  289. }
  290. /**
  291. * host1x_syncpt_put() - free a requested syncpoint
  292. * @sp: host1x syncpoint
  293. *
  294. * Release a syncpoint previously allocated using host1x_syncpt_request(). A
  295. * host1x client driver should call this when the syncpoint is no longer in
  296. * use.
  297. */
  298. void host1x_syncpt_put(struct host1x_syncpt *sp)
  299. {
  300. if (!sp)
  301. return;
  302. kref_put_mutex(&sp->ref, syncpt_release, &sp->host->syncpt_mutex);
  303. }
  304. EXPORT_SYMBOL(host1x_syncpt_put);
  305. void host1x_syncpt_deinit(struct host1x *host)
  306. {
  307. struct host1x_syncpt *sp = host->syncpt;
  308. unsigned int i;
  309. for (i = 0; i < host->info->nb_pts; i++, sp++)
  310. kfree(sp->name);
  311. }
  312. /**
  313. * host1x_syncpt_read_max() - read maximum syncpoint value
  314. * @sp: host1x syncpoint
  315. *
  316. * The maximum syncpoint value indicates how many operations there are in
  317. * queue, either in channel or in a software thread.
  318. */
  319. u32 host1x_syncpt_read_max(struct host1x_syncpt *sp)
  320. {
  321. smp_rmb();
  322. return (u32)atomic_read(&sp->max_val);
  323. }
  324. EXPORT_SYMBOL(host1x_syncpt_read_max);
  325. /**
  326. * host1x_syncpt_read_min() - read minimum syncpoint value
  327. * @sp: host1x syncpoint
  328. *
  329. * The minimum syncpoint value is a shadow of the current sync point value in
  330. * hardware.
  331. */
  332. u32 host1x_syncpt_read_min(struct host1x_syncpt *sp)
  333. {
  334. smp_rmb();
  335. return (u32)atomic_read(&sp->min_val);
  336. }
  337. EXPORT_SYMBOL(host1x_syncpt_read_min);
  338. /**
  339. * host1x_syncpt_read() - read the current syncpoint value
  340. * @sp: host1x syncpoint
  341. */
  342. u32 host1x_syncpt_read(struct host1x_syncpt *sp)
  343. {
  344. return host1x_syncpt_load(sp);
  345. }
  346. EXPORT_SYMBOL(host1x_syncpt_read);
  347. unsigned int host1x_syncpt_nb_pts(struct host1x *host)
  348. {
  349. return host->info->nb_pts;
  350. }
  351. unsigned int host1x_syncpt_nb_bases(struct host1x *host)
  352. {
  353. return host->info->nb_bases;
  354. }
  355. unsigned int host1x_syncpt_nb_mlocks(struct host1x *host)
  356. {
  357. return host->info->nb_mlocks;
  358. }
  359. /**
  360. * host1x_syncpt_get_by_id() - obtain a syncpoint by ID
  361. * @host: host1x controller
  362. * @id: syncpoint ID
  363. */
  364. struct host1x_syncpt *host1x_syncpt_get_by_id(struct host1x *host,
  365. unsigned int id)
  366. {
  367. if (id >= host->info->nb_pts)
  368. return NULL;
  369. if (kref_get_unless_zero(&host->syncpt[id].ref))
  370. return &host->syncpt[id];
  371. else
  372. return NULL;
  373. }
  374. EXPORT_SYMBOL(host1x_syncpt_get_by_id);
  375. /**
  376. * host1x_syncpt_get_by_id_noref() - obtain a syncpoint by ID but don't
  377. * increase the refcount.
  378. * @host: host1x controller
  379. * @id: syncpoint ID
  380. */
  381. struct host1x_syncpt *host1x_syncpt_get_by_id_noref(struct host1x *host,
  382. unsigned int id)
  383. {
  384. if (id >= host->info->nb_pts)
  385. return NULL;
  386. return &host->syncpt[id];
  387. }
  388. EXPORT_SYMBOL(host1x_syncpt_get_by_id_noref);
  389. /**
  390. * host1x_syncpt_get() - increment syncpoint refcount
  391. * @sp: syncpoint
  392. */
  393. struct host1x_syncpt *host1x_syncpt_get(struct host1x_syncpt *sp)
  394. {
  395. kref_get(&sp->ref);
  396. return sp;
  397. }
  398. EXPORT_SYMBOL(host1x_syncpt_get);
  399. /**
  400. * host1x_syncpt_get_base() - obtain the wait base associated with a syncpoint
  401. * @sp: host1x syncpoint
  402. */
  403. struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp)
  404. {
  405. return sp ? sp->base : NULL;
  406. }
  407. EXPORT_SYMBOL(host1x_syncpt_get_base);
  408. /**
  409. * host1x_syncpt_base_id() - retrieve the ID of a syncpoint wait base
  410. * @base: host1x syncpoint wait base
  411. */
  412. u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base)
  413. {
  414. return base->id;
  415. }
  416. EXPORT_SYMBOL(host1x_syncpt_base_id);
  417. static void do_nothing(struct kref *ref)
  418. {
  419. }
  420. /**
  421. * host1x_syncpt_release_vblank_reservation() - Make VBLANK syncpoint
  422. * available for allocation
  423. *
  424. * @client: host1x bus client
  425. * @syncpt_id: syncpoint ID to make available
  426. *
  427. * Makes VBLANK<i> syncpoint available for allocatation if it was
  428. * reserved at initialization time. This should be called by the display
  429. * driver after it has ensured that any VBLANK increment programming configured
  430. * by the boot chain has been disabled.
  431. */
  432. void host1x_syncpt_release_vblank_reservation(struct host1x_client *client,
  433. u32 syncpt_id)
  434. {
  435. struct host1x *host = dev_get_drvdata(client->host->parent);
  436. if (!host->info->reserve_vblank_syncpts)
  437. return;
  438. kref_put(&host->syncpt[syncpt_id].ref, do_nothing);
  439. }
  440. EXPORT_SYMBOL(host1x_syncpt_release_vblank_reservation);