bpmp.c 20 KB

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