bpmp.c 20 KB

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