bpmp.c 20 KB

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