bpmp.c 20 KB

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