syncpt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. mutex_lock(&sp->host->syncpt_mutex);
  284. host1x_syncpt_base_free(sp->base);
  285. kfree(sp->name);
  286. sp->base = NULL;
  287. sp->name = NULL;
  288. sp->client_managed = false;
  289. mutex_unlock(&sp->host->syncpt_mutex);
  290. }
  291. /**
  292. * host1x_syncpt_put() - free a requested syncpoint
  293. * @sp: host1x syncpoint
  294. *
  295. * Release a syncpoint previously allocated using host1x_syncpt_request(). A
  296. * host1x client driver should call this when the syncpoint is no longer in
  297. * use.
  298. */
  299. void host1x_syncpt_put(struct host1x_syncpt *sp)
  300. {
  301. if (!sp)
  302. return;
  303. kref_put(&sp->ref, syncpt_release);
  304. }
  305. EXPORT_SYMBOL(host1x_syncpt_put);
  306. void host1x_syncpt_deinit(struct host1x *host)
  307. {
  308. struct host1x_syncpt *sp = host->syncpt;
  309. unsigned int i;
  310. for (i = 0; i < host->info->nb_pts; i++, sp++)
  311. kfree(sp->name);
  312. }
  313. /**
  314. * host1x_syncpt_read_max() - read maximum syncpoint value
  315. * @sp: host1x syncpoint
  316. *
  317. * The maximum syncpoint value indicates how many operations there are in
  318. * queue, either in channel or in a software thread.
  319. */
  320. u32 host1x_syncpt_read_max(struct host1x_syncpt *sp)
  321. {
  322. smp_rmb();
  323. return (u32)atomic_read(&sp->max_val);
  324. }
  325. EXPORT_SYMBOL(host1x_syncpt_read_max);
  326. /**
  327. * host1x_syncpt_read_min() - read minimum syncpoint value
  328. * @sp: host1x syncpoint
  329. *
  330. * The minimum syncpoint value is a shadow of the current sync point value in
  331. * hardware.
  332. */
  333. u32 host1x_syncpt_read_min(struct host1x_syncpt *sp)
  334. {
  335. smp_rmb();
  336. return (u32)atomic_read(&sp->min_val);
  337. }
  338. EXPORT_SYMBOL(host1x_syncpt_read_min);
  339. /**
  340. * host1x_syncpt_read() - read the current syncpoint value
  341. * @sp: host1x syncpoint
  342. */
  343. u32 host1x_syncpt_read(struct host1x_syncpt *sp)
  344. {
  345. return host1x_syncpt_load(sp);
  346. }
  347. EXPORT_SYMBOL(host1x_syncpt_read);
  348. unsigned int host1x_syncpt_nb_pts(struct host1x *host)
  349. {
  350. return host->info->nb_pts;
  351. }
  352. unsigned int host1x_syncpt_nb_bases(struct host1x *host)
  353. {
  354. return host->info->nb_bases;
  355. }
  356. unsigned int host1x_syncpt_nb_mlocks(struct host1x *host)
  357. {
  358. return host->info->nb_mlocks;
  359. }
  360. /**
  361. * host1x_syncpt_get_by_id() - obtain a syncpoint by ID
  362. * @host: host1x controller
  363. * @id: syncpoint ID
  364. */
  365. struct host1x_syncpt *host1x_syncpt_get_by_id(struct host1x *host,
  366. unsigned int id)
  367. {
  368. if (id >= host->info->nb_pts)
  369. return NULL;
  370. if (kref_get_unless_zero(&host->syncpt[id].ref))
  371. return &host->syncpt[id];
  372. else
  373. return NULL;
  374. }
  375. EXPORT_SYMBOL(host1x_syncpt_get_by_id);
  376. /**
  377. * host1x_syncpt_get_by_id_noref() - obtain a syncpoint by ID but don't
  378. * increase the refcount.
  379. * @host: host1x controller
  380. * @id: syncpoint ID
  381. */
  382. struct host1x_syncpt *host1x_syncpt_get_by_id_noref(struct host1x *host,
  383. unsigned int id)
  384. {
  385. if (id >= host->info->nb_pts)
  386. return NULL;
  387. return &host->syncpt[id];
  388. }
  389. EXPORT_SYMBOL(host1x_syncpt_get_by_id_noref);
  390. /**
  391. * host1x_syncpt_get() - increment syncpoint refcount
  392. * @sp: syncpoint
  393. */
  394. struct host1x_syncpt *host1x_syncpt_get(struct host1x_syncpt *sp)
  395. {
  396. kref_get(&sp->ref);
  397. return sp;
  398. }
  399. EXPORT_SYMBOL(host1x_syncpt_get);
  400. /**
  401. * host1x_syncpt_get_base() - obtain the wait base associated with a syncpoint
  402. * @sp: host1x syncpoint
  403. */
  404. struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp)
  405. {
  406. return sp ? sp->base : NULL;
  407. }
  408. EXPORT_SYMBOL(host1x_syncpt_get_base);
  409. /**
  410. * host1x_syncpt_base_id() - retrieve the ID of a syncpoint wait base
  411. * @base: host1x syncpoint wait base
  412. */
  413. u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base)
  414. {
  415. return base->id;
  416. }
  417. EXPORT_SYMBOL(host1x_syncpt_base_id);
  418. static void do_nothing(struct kref *ref)
  419. {
  420. }
  421. /**
  422. * host1x_syncpt_release_vblank_reservation() - Make VBLANK syncpoint
  423. * available for allocation
  424. *
  425. * @client: host1x bus client
  426. * @syncpt_id: syncpoint ID to make available
  427. *
  428. * Makes VBLANK<i> syncpoint available for allocatation if it was
  429. * reserved at initialization time. This should be called by the display
  430. * driver after it has ensured that any VBLANK increment programming configured
  431. * by the boot chain has been disabled.
  432. */
  433. void host1x_syncpt_release_vblank_reservation(struct host1x_client *client,
  434. u32 syncpt_id)
  435. {
  436. struct host1x *host = dev_get_drvdata(client->host->parent);
  437. if (!host->info->reserve_vblank_syncpts)
  438. return;
  439. kref_put(&host->syncpt[syncpt_id].ref, do_nothing);
  440. }
  441. EXPORT_SYMBOL(host1x_syncpt_release_vblank_reservation);