bpmp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/cleanup.h>
  6. #include <linux/clk/tegra.h>
  7. #include <linux/genalloc.h>
  8. #include <linux/mailbox_client.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm.h>
  14. #include <linux/semaphore.h>
  15. #include <linux/sched/clock.h>
  16. #include <soc/tegra/bpmp.h>
  17. #include <soc/tegra/bpmp-abi.h>
  18. #include <soc/tegra/ivc.h>
  19. #include "bpmp-private.h"
  20. #define MSG_ACK BIT(0)
  21. #define MSG_RING BIT(1)
  22. #define TAG_SZ 32
  23. static inline const struct tegra_bpmp_ops *
  24. channel_to_ops(struct tegra_bpmp_channel *channel)
  25. {
  26. struct tegra_bpmp *bpmp = channel->bpmp;
  27. return bpmp->soc->ops;
  28. }
  29. struct tegra_bpmp *tegra_bpmp_get(struct device *dev)
  30. {
  31. struct device_node *np __free(device_node);
  32. struct platform_device *pdev;
  33. struct tegra_bpmp *bpmp;
  34. np = of_parse_phandle(dev->of_node, "nvidia,bpmp", 0);
  35. if (!np)
  36. return ERR_PTR(-ENOENT);
  37. pdev = of_find_device_by_node(np);
  38. if (!pdev)
  39. return ERR_PTR(-ENODEV);
  40. bpmp = platform_get_drvdata(pdev);
  41. if (!bpmp) {
  42. put_device(&pdev->dev);
  43. return ERR_PTR(-EPROBE_DEFER);
  44. }
  45. return bpmp;
  46. }
  47. EXPORT_SYMBOL_GPL(tegra_bpmp_get);
  48. void tegra_bpmp_put(struct tegra_bpmp *bpmp)
  49. {
  50. if (bpmp)
  51. put_device(bpmp->dev);
  52. }
  53. EXPORT_SYMBOL_GPL(tegra_bpmp_put);
  54. static int
  55. tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
  56. {
  57. struct tegra_bpmp *bpmp = channel->bpmp;
  58. unsigned int count;
  59. int index;
  60. count = bpmp->soc->channels.thread.count;
  61. index = channel - channel->bpmp->threaded_channels;
  62. if (index < 0 || index >= count)
  63. return -EINVAL;
  64. return index;
  65. }
  66. static bool tegra_bpmp_message_valid(const struct tegra_bpmp_message *msg)
  67. {
  68. return (msg->tx.size <= MSG_DATA_MIN_SZ) &&
  69. (msg->rx.size <= MSG_DATA_MIN_SZ) &&
  70. (msg->tx.size == 0 || msg->tx.data) &&
  71. (msg->rx.size == 0 || msg->rx.data);
  72. }
  73. static bool tegra_bpmp_is_response_ready(struct tegra_bpmp_channel *channel)
  74. {
  75. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  76. return ops->is_response_ready(channel);
  77. }
  78. static bool tegra_bpmp_is_request_ready(struct tegra_bpmp_channel *channel)
  79. {
  80. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  81. return ops->is_request_ready(channel);
  82. }
  83. static int tegra_bpmp_wait_response(struct tegra_bpmp_channel *channel)
  84. {
  85. unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
  86. ktime_t end;
  87. end = ktime_add_us(ktime_get(), timeout);
  88. do {
  89. if (tegra_bpmp_is_response_ready(channel))
  90. return 0;
  91. } while (ktime_before(ktime_get(), end));
  92. return -ETIMEDOUT;
  93. }
  94. static int tegra_bpmp_ack_response(struct tegra_bpmp_channel *channel)
  95. {
  96. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  97. return ops->ack_response(channel);
  98. }
  99. static int tegra_bpmp_ack_request(struct tegra_bpmp_channel *channel)
  100. {
  101. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  102. return ops->ack_request(channel);
  103. }
  104. static bool
  105. tegra_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel)
  106. {
  107. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  108. return ops->is_request_channel_free(channel);
  109. }
  110. static bool
  111. tegra_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel)
  112. {
  113. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  114. return ops->is_response_channel_free(channel);
  115. }
  116. static int
  117. tegra_bpmp_wait_request_channel_free(struct tegra_bpmp_channel *channel)
  118. {
  119. unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
  120. ktime_t start, now;
  121. start = ns_to_ktime(local_clock());
  122. do {
  123. if (tegra_bpmp_is_request_channel_free(channel))
  124. return 0;
  125. now = ns_to_ktime(local_clock());
  126. } while (ktime_us_delta(now, start) < timeout);
  127. return -ETIMEDOUT;
  128. }
  129. static int tegra_bpmp_post_request(struct tegra_bpmp_channel *channel)
  130. {
  131. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  132. return ops->post_request(channel);
  133. }
  134. static int tegra_bpmp_post_response(struct tegra_bpmp_channel *channel)
  135. {
  136. const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
  137. return ops->post_response(channel);
  138. }
  139. static int tegra_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
  140. {
  141. return bpmp->soc->ops->ring_doorbell(bpmp);
  142. }
  143. static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
  144. void *data, size_t size, int *ret)
  145. {
  146. int err;
  147. if (data && size > 0)
  148. tegra_bpmp_mb_read(data, &channel->ib, size);
  149. err = tegra_bpmp_ack_response(channel);
  150. if (err < 0)
  151. return err;
  152. *ret = tegra_bpmp_mb_read_field(&channel->ib, code);
  153. return 0;
  154. }
  155. static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
  156. void *data, size_t size, int *ret)
  157. {
  158. struct tegra_bpmp *bpmp = channel->bpmp;
  159. unsigned long flags;
  160. ssize_t err;
  161. int index;
  162. index = tegra_bpmp_channel_get_thread_index(channel);
  163. if (index < 0) {
  164. err = index;
  165. goto unlock;
  166. }
  167. spin_lock_irqsave(&bpmp->lock, flags);
  168. err = __tegra_bpmp_channel_read(channel, data, size, ret);
  169. clear_bit(index, bpmp->threaded.allocated);
  170. spin_unlock_irqrestore(&bpmp->lock, flags);
  171. unlock:
  172. up(&bpmp->threaded.lock);
  173. return err;
  174. }
  175. static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
  176. unsigned int mrq, unsigned long flags,
  177. const void *data, size_t size)
  178. {
  179. tegra_bpmp_mb_write_field(&channel->ob, code, mrq);
  180. tegra_bpmp_mb_write_field(&channel->ob, flags, flags);
  181. if (data && size > 0)
  182. tegra_bpmp_mb_write(&channel->ob, data, size);
  183. return tegra_bpmp_post_request(channel);
  184. }
  185. static struct tegra_bpmp_channel *
  186. tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
  187. const void *data, size_t size)
  188. {
  189. unsigned long timeout = bpmp->soc->channels.thread.timeout;
  190. unsigned int count = bpmp->soc->channels.thread.count;
  191. struct tegra_bpmp_channel *channel;
  192. unsigned long flags;
  193. unsigned int index;
  194. int err;
  195. err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
  196. if (err < 0)
  197. return ERR_PTR(err);
  198. spin_lock_irqsave(&bpmp->lock, flags);
  199. index = find_first_zero_bit(bpmp->threaded.allocated, count);
  200. if (index == count) {
  201. err = -EBUSY;
  202. goto unlock;
  203. }
  204. channel = &bpmp->threaded_channels[index];
  205. if (!tegra_bpmp_is_request_channel_free(channel)) {
  206. err = -EBUSY;
  207. goto unlock;
  208. }
  209. set_bit(index, bpmp->threaded.allocated);
  210. err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
  211. data, size);
  212. if (err < 0)
  213. goto clear_allocated;
  214. set_bit(index, bpmp->threaded.busy);
  215. spin_unlock_irqrestore(&bpmp->lock, flags);
  216. return channel;
  217. clear_allocated:
  218. clear_bit(index, bpmp->threaded.allocated);
  219. unlock:
  220. spin_unlock_irqrestore(&bpmp->lock, flags);
  221. up(&bpmp->threaded.lock);
  222. return ERR_PTR(err);
  223. }
  224. static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
  225. unsigned int mrq, unsigned long flags,
  226. const void *data, size_t size)
  227. {
  228. int err;
  229. err = tegra_bpmp_wait_request_channel_free(channel);
  230. if (err < 0)
  231. return err;
  232. return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
  233. }
  234. static int __maybe_unused tegra_bpmp_resume(struct device *dev);
  235. int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
  236. struct tegra_bpmp_message *msg)
  237. {
  238. struct tegra_bpmp_channel *channel;
  239. int err;
  240. if (WARN_ON(!irqs_disabled()))
  241. return -EPERM;
  242. if (!tegra_bpmp_message_valid(msg))
  243. return -EINVAL;
  244. if (bpmp->suspended) {
  245. /* Reset BPMP IPC channels during resume based on flags passed */
  246. if (msg->flags & TEGRA_BPMP_MESSAGE_RESET)
  247. tegra_bpmp_resume(bpmp->dev);
  248. else
  249. return -EAGAIN;
  250. }
  251. channel = bpmp->tx_channel;
  252. spin_lock(&bpmp->atomic_tx_lock);
  253. err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
  254. msg->tx.data, msg->tx.size);
  255. if (err < 0) {
  256. spin_unlock(&bpmp->atomic_tx_lock);
  257. return err;
  258. }
  259. spin_unlock(&bpmp->atomic_tx_lock);
  260. err = tegra_bpmp_ring_doorbell(bpmp);
  261. if (err < 0)
  262. return err;
  263. err = tegra_bpmp_wait_response(channel);
  264. if (err < 0)
  265. return err;
  266. return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
  267. &msg->rx.ret);
  268. }
  269. EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
  270. int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
  271. struct tegra_bpmp_message *msg)
  272. {
  273. struct tegra_bpmp_channel *channel;
  274. unsigned long timeout;
  275. int err;
  276. if (WARN_ON(irqs_disabled()))
  277. return -EPERM;
  278. if (!tegra_bpmp_message_valid(msg))
  279. return -EINVAL;
  280. if (bpmp->suspended) {
  281. /* Reset BPMP IPC channels during resume based on flags passed */
  282. if (msg->flags & TEGRA_BPMP_MESSAGE_RESET)
  283. tegra_bpmp_resume(bpmp->dev);
  284. else
  285. return -EAGAIN;
  286. }
  287. channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data,
  288. msg->tx.size);
  289. if (IS_ERR(channel))
  290. return PTR_ERR(channel);
  291. err = tegra_bpmp_ring_doorbell(bpmp);
  292. if (err < 0)
  293. return err;
  294. timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout);
  295. err = wait_for_completion_timeout(&channel->completion, timeout);
  296. if (err == 0)
  297. return -ETIMEDOUT;
  298. return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
  299. &msg->rx.ret);
  300. }
  301. EXPORT_SYMBOL_GPL(tegra_bpmp_transfer);
  302. static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp,
  303. unsigned int mrq)
  304. {
  305. struct tegra_bpmp_mrq *entry;
  306. list_for_each_entry(entry, &bpmp->mrqs, list)
  307. if (entry->mrq == mrq)
  308. return entry;
  309. return NULL;
  310. }
  311. void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel, int code,
  312. const void *data, size_t size)
  313. {
  314. unsigned long flags = tegra_bpmp_mb_read_field(&channel->ib, flags);
  315. struct tegra_bpmp *bpmp = channel->bpmp;
  316. int err;
  317. if (WARN_ON(size > MSG_DATA_MIN_SZ))
  318. return;
  319. err = tegra_bpmp_ack_request(channel);
  320. if (WARN_ON(err < 0))
  321. return;
  322. if ((flags & MSG_ACK) == 0)
  323. return;
  324. if (WARN_ON(!tegra_bpmp_is_response_channel_free(channel)))
  325. return;
  326. tegra_bpmp_mb_write_field(&channel->ob, code, code);
  327. if (data && size > 0)
  328. tegra_bpmp_mb_write(&channel->ob, data, size);
  329. err = tegra_bpmp_post_response(channel);
  330. if (WARN_ON(err < 0))
  331. return;
  332. if (flags & MSG_RING) {
  333. err = tegra_bpmp_ring_doorbell(bpmp);
  334. if (WARN_ON(err < 0))
  335. return;
  336. }
  337. }
  338. EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_return);
  339. static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp,
  340. unsigned int mrq,
  341. struct tegra_bpmp_channel *channel)
  342. {
  343. struct tegra_bpmp_mrq *entry;
  344. u32 zero = 0;
  345. spin_lock(&bpmp->lock);
  346. entry = tegra_bpmp_find_mrq(bpmp, mrq);
  347. if (!entry) {
  348. spin_unlock(&bpmp->lock);
  349. tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero));
  350. return;
  351. }
  352. entry->handler(mrq, channel, entry->data);
  353. spin_unlock(&bpmp->lock);
  354. }
  355. int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq,
  356. tegra_bpmp_mrq_handler_t handler, void *data)
  357. {
  358. struct tegra_bpmp_mrq *entry;
  359. unsigned long flags;
  360. if (!handler)
  361. return -EINVAL;
  362. entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL);
  363. if (!entry)
  364. return -ENOMEM;
  365. spin_lock_irqsave(&bpmp->lock, flags);
  366. entry->mrq = mrq;
  367. entry->handler = handler;
  368. entry->data = data;
  369. list_add(&entry->list, &bpmp->mrqs);
  370. spin_unlock_irqrestore(&bpmp->lock, flags);
  371. return 0;
  372. }
  373. EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq);
  374. void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data)
  375. {
  376. struct tegra_bpmp_mrq *entry;
  377. unsigned long flags;
  378. spin_lock_irqsave(&bpmp->lock, flags);
  379. entry = tegra_bpmp_find_mrq(bpmp, mrq);
  380. if (!entry)
  381. goto unlock;
  382. list_del(&entry->list);
  383. devm_kfree(bpmp->dev, entry);
  384. unlock:
  385. spin_unlock_irqrestore(&bpmp->lock, flags);
  386. }
  387. EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq);
  388. bool tegra_bpmp_mrq_is_supported(struct tegra_bpmp *bpmp, unsigned int mrq)
  389. {
  390. struct mrq_query_abi_request req = { .mrq = mrq };
  391. struct mrq_query_abi_response resp;
  392. struct tegra_bpmp_message msg = {
  393. .mrq = MRQ_QUERY_ABI,
  394. .tx = {
  395. .data = &req,
  396. .size = sizeof(req),
  397. },
  398. .rx = {
  399. .data = &resp,
  400. .size = sizeof(resp),
  401. },
  402. };
  403. int err;
  404. err = tegra_bpmp_transfer(bpmp, &msg);
  405. if (err || msg.rx.ret)
  406. return false;
  407. return resp.status == 0;
  408. }
  409. EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_is_supported);
  410. static void tegra_bpmp_mrq_handle_ping(unsigned int mrq,
  411. struct tegra_bpmp_channel *channel,
  412. void *data)
  413. {
  414. struct mrq_ping_request request;
  415. struct mrq_ping_response response;
  416. tegra_bpmp_mb_read(&request, &channel->ib, sizeof(request));
  417. memset(&response, 0, sizeof(response));
  418. response.reply = request.challenge << 1;
  419. tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response));
  420. }
  421. static int tegra_bpmp_ping(struct tegra_bpmp *bpmp)
  422. {
  423. struct mrq_ping_response response;
  424. struct mrq_ping_request request;
  425. struct tegra_bpmp_message msg;
  426. unsigned long flags;
  427. ktime_t start, end;
  428. int err;
  429. memset(&request, 0, sizeof(request));
  430. request.challenge = 1;
  431. memset(&response, 0, sizeof(response));
  432. memset(&msg, 0, sizeof(msg));
  433. msg.mrq = MRQ_PING;
  434. msg.tx.data = &request;
  435. msg.tx.size = sizeof(request);
  436. msg.rx.data = &response;
  437. msg.rx.size = sizeof(response);
  438. local_irq_save(flags);
  439. start = ktime_get();
  440. err = tegra_bpmp_transfer_atomic(bpmp, &msg);
  441. end = ktime_get();
  442. local_irq_restore(flags);
  443. if (!err)
  444. dev_dbg(bpmp->dev,
  445. "ping ok: challenge: %u, response: %u, time: %lld\n",
  446. request.challenge, response.reply,
  447. ktime_to_us(ktime_sub(end, start)));
  448. return err;
  449. }
  450. /* deprecated version of tag query */
  451. static int tegra_bpmp_get_firmware_tag_old(struct tegra_bpmp *bpmp, char *tag,
  452. size_t size)
  453. {
  454. struct mrq_query_tag_request request;
  455. struct tegra_bpmp_message msg;
  456. unsigned long flags;
  457. dma_addr_t phys;
  458. void *virt;
  459. int err;
  460. if (size != TAG_SZ)
  461. return -EINVAL;
  462. virt = dma_alloc_coherent(bpmp->dev, TAG_SZ, &phys,
  463. GFP_KERNEL | GFP_DMA32);
  464. if (!virt)
  465. return -ENOMEM;
  466. memset(&request, 0, sizeof(request));
  467. request.addr = phys;
  468. memset(&msg, 0, sizeof(msg));
  469. msg.mrq = MRQ_QUERY_TAG;
  470. msg.tx.data = &request;
  471. msg.tx.size = sizeof(request);
  472. local_irq_save(flags);
  473. err = tegra_bpmp_transfer_atomic(bpmp, &msg);
  474. local_irq_restore(flags);
  475. if (err == 0)
  476. memcpy(tag, virt, TAG_SZ);
  477. dma_free_coherent(bpmp->dev, TAG_SZ, virt, phys);
  478. return err;
  479. }
  480. static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag,
  481. size_t size)
  482. {
  483. if (tegra_bpmp_mrq_is_supported(bpmp, MRQ_QUERY_FW_TAG)) {
  484. struct mrq_query_fw_tag_response resp;
  485. struct tegra_bpmp_message msg = {
  486. .mrq = MRQ_QUERY_FW_TAG,
  487. .rx = {
  488. .data = &resp,
  489. .size = sizeof(resp),
  490. },
  491. };
  492. int err;
  493. if (size != sizeof(resp.tag))
  494. return -EINVAL;
  495. err = tegra_bpmp_transfer(bpmp, &msg);
  496. if (err)
  497. return err;
  498. if (msg.rx.ret < 0)
  499. return -EINVAL;
  500. memcpy(tag, resp.tag, sizeof(resp.tag));
  501. return 0;
  502. }
  503. return tegra_bpmp_get_firmware_tag_old(bpmp, tag, size);
  504. }
  505. static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel)
  506. {
  507. unsigned long flags = tegra_bpmp_mb_read_field(&channel->ob, flags);
  508. if ((flags & MSG_RING) == 0)
  509. return;
  510. complete(&channel->completion);
  511. }
  512. void tegra_bpmp_handle_rx(struct tegra_bpmp *bpmp)
  513. {
  514. struct tegra_bpmp_channel *channel;
  515. unsigned int i, count;
  516. unsigned long *busy;
  517. channel = bpmp->rx_channel;
  518. count = bpmp->soc->channels.thread.count;
  519. busy = bpmp->threaded.busy;
  520. if (tegra_bpmp_is_request_ready(channel)) {
  521. unsigned int mrq = tegra_bpmp_mb_read_field(&channel->ib, code);
  522. tegra_bpmp_handle_mrq(bpmp, mrq, channel);
  523. }
  524. spin_lock(&bpmp->lock);
  525. for_each_set_bit(i, busy, count) {
  526. struct tegra_bpmp_channel *channel;
  527. channel = &bpmp->threaded_channels[i];
  528. if (tegra_bpmp_is_response_ready(channel)) {
  529. tegra_bpmp_channel_signal(channel);
  530. clear_bit(i, busy);
  531. }
  532. }
  533. spin_unlock(&bpmp->lock);
  534. }
  535. static int tegra_bpmp_probe(struct platform_device *pdev)
  536. {
  537. struct tegra_bpmp *bpmp;
  538. char tag[TAG_SZ];
  539. size_t size;
  540. int err;
  541. bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
  542. if (!bpmp)
  543. return -ENOMEM;
  544. bpmp->soc = of_device_get_match_data(&pdev->dev);
  545. bpmp->dev = &pdev->dev;
  546. INIT_LIST_HEAD(&bpmp->mrqs);
  547. spin_lock_init(&bpmp->lock);
  548. bpmp->threaded.count = bpmp->soc->channels.thread.count;
  549. sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
  550. size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
  551. bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  552. if (!bpmp->threaded.allocated)
  553. return -ENOMEM;
  554. bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  555. if (!bpmp->threaded.busy)
  556. return -ENOMEM;
  557. spin_lock_init(&bpmp->atomic_tx_lock);
  558. bpmp->tx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->tx_channel),
  559. GFP_KERNEL);
  560. if (!bpmp->tx_channel)
  561. return -ENOMEM;
  562. bpmp->rx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->rx_channel),
  563. GFP_KERNEL);
  564. if (!bpmp->rx_channel)
  565. return -ENOMEM;
  566. bpmp->threaded_channels = devm_kcalloc(&pdev->dev, bpmp->threaded.count,
  567. sizeof(*bpmp->threaded_channels),
  568. GFP_KERNEL);
  569. if (!bpmp->threaded_channels)
  570. return -ENOMEM;
  571. platform_set_drvdata(pdev, bpmp);
  572. err = bpmp->soc->ops->init(bpmp);
  573. if (err < 0)
  574. return err;
  575. err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
  576. tegra_bpmp_mrq_handle_ping, bpmp);
  577. if (err < 0)
  578. goto deinit;
  579. err = tegra_bpmp_ping(bpmp);
  580. if (err < 0) {
  581. dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
  582. goto free_mrq;
  583. }
  584. err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag));
  585. if (err < 0) {
  586. dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
  587. goto free_mrq;
  588. }
  589. dev_info(&pdev->dev, "firmware: %.*s\n", (int)sizeof(tag), tag);
  590. err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
  591. if (err < 0)
  592. goto free_mrq;
  593. if (of_property_present(pdev->dev.of_node, "#clock-cells")) {
  594. err = tegra_bpmp_init_clocks(bpmp);
  595. if (err < 0)
  596. goto free_mrq;
  597. }
  598. if (of_property_present(pdev->dev.of_node, "#reset-cells")) {
  599. err = tegra_bpmp_init_resets(bpmp);
  600. if (err < 0)
  601. goto free_mrq;
  602. }
  603. if (of_property_present(pdev->dev.of_node, "#power-domain-cells")) {
  604. err = tegra_bpmp_init_powergates(bpmp);
  605. if (err < 0)
  606. goto free_mrq;
  607. }
  608. err = tegra_bpmp_init_debugfs(bpmp);
  609. if (err < 0)
  610. dev_err(&pdev->dev, "debugfs initialization failed: %d\n", err);
  611. return 0;
  612. free_mrq:
  613. tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
  614. deinit:
  615. if (bpmp->soc->ops->deinit)
  616. bpmp->soc->ops->deinit(bpmp);
  617. return err;
  618. }
  619. static int __maybe_unused tegra_bpmp_suspend(struct device *dev)
  620. {
  621. struct tegra_bpmp *bpmp = dev_get_drvdata(dev);
  622. bpmp->suspended = true;
  623. return 0;
  624. }
  625. static int __maybe_unused tegra_bpmp_resume(struct device *dev)
  626. {
  627. struct tegra_bpmp *bpmp = dev_get_drvdata(dev);
  628. bpmp->suspended = false;
  629. if (bpmp->soc->ops->resume)
  630. return bpmp->soc->ops->resume(bpmp);
  631. else
  632. return 0;
  633. }
  634. static const struct dev_pm_ops tegra_bpmp_pm_ops = {
  635. .suspend_noirq = tegra_bpmp_suspend,
  636. .resume_noirq = tegra_bpmp_resume,
  637. };
  638. #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
  639. IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
  640. IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
  641. static const struct tegra_bpmp_soc tegra186_soc = {
  642. .channels = {
  643. .cpu_tx = {
  644. .offset = 3,
  645. .timeout = 60 * USEC_PER_SEC,
  646. },
  647. .thread = {
  648. .offset = 0,
  649. .count = 3,
  650. .timeout = 600 * USEC_PER_SEC,
  651. },
  652. .cpu_rx = {
  653. .offset = 13,
  654. .timeout = 0,
  655. },
  656. },
  657. .ops = &tegra186_bpmp_ops,
  658. .num_resets = 193,
  659. };
  660. #endif
  661. #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
  662. static const struct tegra_bpmp_soc tegra210_soc = {
  663. .channels = {
  664. .cpu_tx = {
  665. .offset = 0,
  666. .count = 1,
  667. .timeout = 60 * USEC_PER_SEC,
  668. },
  669. .thread = {
  670. .offset = 4,
  671. .count = 1,
  672. .timeout = 600 * USEC_PER_SEC,
  673. },
  674. .cpu_rx = {
  675. .offset = 8,
  676. .count = 1,
  677. .timeout = 0,
  678. },
  679. },
  680. .ops = &tegra210_bpmp_ops,
  681. };
  682. #endif
  683. static const struct of_device_id tegra_bpmp_match[] = {
  684. #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
  685. IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
  686. IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
  687. { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
  688. #endif
  689. #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
  690. { .compatible = "nvidia,tegra210-bpmp", .data = &tegra210_soc },
  691. #endif
  692. { }
  693. };
  694. static struct platform_driver tegra_bpmp_driver = {
  695. .driver = {
  696. .name = "tegra-bpmp",
  697. .of_match_table = tegra_bpmp_match,
  698. .pm = &tegra_bpmp_pm_ops,
  699. .suppress_bind_attrs = true,
  700. },
  701. .probe = tegra_bpmp_probe,
  702. };
  703. builtin_platform_driver(tegra_bpmp_driver);