bpmp.c 20 KB

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