bpmp.c 20 KB

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