tegra-hsp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/mailbox_controller.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm.h>
  13. #include <linux/slab.h>
  14. #include <soc/tegra/fuse.h>
  15. #include <dt-bindings/mailbox/tegra186-hsp.h>
  16. #include "mailbox.h"
  17. #define HSP_INT_IE(x) (0x100 + ((x) * 4))
  18. #define HSP_INT_IV 0x300
  19. #define HSP_INT_IR 0x304
  20. #define HSP_INT_EMPTY_SHIFT 0
  21. #define HSP_INT_EMPTY_MASK 0xff
  22. #define HSP_INT_FULL_SHIFT 8
  23. #define HSP_INT_FULL_MASK 0xff
  24. #define HSP_INT_DIMENSIONING 0x380
  25. #define HSP_nSM_SHIFT 0
  26. #define HSP_nSS_SHIFT 4
  27. #define HSP_nAS_SHIFT 8
  28. #define HSP_nDB_SHIFT 12
  29. #define HSP_nSI_SHIFT 16
  30. #define HSP_nINT_MASK 0xf
  31. #define HSP_DB_TRIGGER 0x0
  32. #define HSP_DB_ENABLE 0x4
  33. #define HSP_DB_RAW 0x8
  34. #define HSP_DB_PENDING 0xc
  35. #define HSP_SM_SHRD_MBOX 0x0
  36. #define HSP_SM_SHRD_MBOX_FULL BIT(31)
  37. #define HSP_SM_SHRD_MBOX_FULL_INT_IE 0x04
  38. #define HSP_SM_SHRD_MBOX_EMPTY_INT_IE 0x08
  39. #define HSP_SHRD_MBOX_TYPE1_TAG 0x40
  40. #define HSP_SHRD_MBOX_TYPE1_DATA0 0x48
  41. #define HSP_SHRD_MBOX_TYPE1_DATA1 0x4c
  42. #define HSP_SHRD_MBOX_TYPE1_DATA2 0x50
  43. #define HSP_SHRD_MBOX_TYPE1_DATA3 0x54
  44. #define HSP_DB_CCPLEX 1
  45. #define HSP_DB_BPMP 3
  46. #define HSP_DB_MAX 7
  47. #define HSP_MBOX_TYPE_MASK 0xff
  48. struct tegra_hsp_channel;
  49. struct tegra_hsp;
  50. struct tegra_hsp_channel {
  51. struct tegra_hsp *hsp;
  52. struct mbox_chan *chan;
  53. void __iomem *regs;
  54. };
  55. struct tegra_hsp_doorbell {
  56. struct tegra_hsp_channel channel;
  57. struct list_head list;
  58. const char *name;
  59. unsigned int master;
  60. unsigned int index;
  61. };
  62. struct tegra_hsp_sm_ops {
  63. void (*send)(struct tegra_hsp_channel *channel, void *data);
  64. void (*recv)(struct tegra_hsp_channel *channel);
  65. };
  66. struct tegra_hsp_mailbox {
  67. struct tegra_hsp_channel channel;
  68. const struct tegra_hsp_sm_ops *ops;
  69. unsigned int index;
  70. bool producer;
  71. };
  72. struct tegra_hsp_db_map {
  73. const char *name;
  74. unsigned int master;
  75. unsigned int index;
  76. };
  77. struct tegra_hsp_soc {
  78. const struct tegra_hsp_db_map *map;
  79. bool has_per_mb_ie;
  80. bool has_128_bit_mb;
  81. };
  82. struct tegra_hsp {
  83. struct device *dev;
  84. const struct tegra_hsp_soc *soc;
  85. struct mbox_controller mbox_db;
  86. struct mbox_controller mbox_sm;
  87. void __iomem *regs;
  88. unsigned int doorbell_irq;
  89. unsigned int *shared_irqs;
  90. unsigned int shared_irq;
  91. unsigned int num_sm;
  92. unsigned int num_as;
  93. unsigned int num_ss;
  94. unsigned int num_db;
  95. unsigned int num_si;
  96. spinlock_t lock;
  97. struct lock_class_key lock_key;
  98. struct list_head doorbells;
  99. struct tegra_hsp_mailbox *mailboxes;
  100. unsigned long mask;
  101. };
  102. static inline u32 tegra_hsp_readl(struct tegra_hsp *hsp, unsigned int offset)
  103. {
  104. return readl(hsp->regs + offset);
  105. }
  106. static inline void tegra_hsp_writel(struct tegra_hsp *hsp, u32 value,
  107. unsigned int offset)
  108. {
  109. writel(value, hsp->regs + offset);
  110. }
  111. static inline u32 tegra_hsp_channel_readl(struct tegra_hsp_channel *channel,
  112. unsigned int offset)
  113. {
  114. return readl(channel->regs + offset);
  115. }
  116. static inline void tegra_hsp_channel_writel(struct tegra_hsp_channel *channel,
  117. u32 value, unsigned int offset)
  118. {
  119. writel(value, channel->regs + offset);
  120. }
  121. static bool tegra_hsp_doorbell_can_ring(struct tegra_hsp_doorbell *db)
  122. {
  123. u32 value;
  124. value = tegra_hsp_channel_readl(&db->channel, HSP_DB_ENABLE);
  125. return (value & BIT(TEGRA_HSP_DB_MASTER_CCPLEX)) != 0;
  126. }
  127. static struct tegra_hsp_doorbell *
  128. __tegra_hsp_doorbell_get(struct tegra_hsp *hsp, unsigned int master)
  129. {
  130. struct tegra_hsp_doorbell *entry;
  131. list_for_each_entry(entry, &hsp->doorbells, list)
  132. if (entry->master == master)
  133. return entry;
  134. return NULL;
  135. }
  136. static struct tegra_hsp_doorbell *
  137. tegra_hsp_doorbell_get(struct tegra_hsp *hsp, unsigned int master)
  138. {
  139. struct tegra_hsp_doorbell *db;
  140. unsigned long flags;
  141. spin_lock_irqsave(&hsp->lock, flags);
  142. db = __tegra_hsp_doorbell_get(hsp, master);
  143. spin_unlock_irqrestore(&hsp->lock, flags);
  144. return db;
  145. }
  146. static irqreturn_t tegra_hsp_doorbell_irq(int irq, void *data)
  147. {
  148. struct tegra_hsp *hsp = data;
  149. struct tegra_hsp_doorbell *db;
  150. unsigned long master, value;
  151. db = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
  152. if (!db)
  153. return IRQ_NONE;
  154. value = tegra_hsp_channel_readl(&db->channel, HSP_DB_PENDING);
  155. tegra_hsp_channel_writel(&db->channel, value, HSP_DB_PENDING);
  156. spin_lock(&hsp->lock);
  157. for_each_set_bit(master, &value, hsp->mbox_db.num_chans) {
  158. struct tegra_hsp_doorbell *db;
  159. db = __tegra_hsp_doorbell_get(hsp, master);
  160. /*
  161. * Depending on the bootloader chain, the CCPLEX doorbell will
  162. * have some doorbells enabled, which means that requesting an
  163. * interrupt will immediately fire.
  164. *
  165. * In that case, db->channel.chan will still be NULL here and
  166. * cause a crash if not properly guarded.
  167. *
  168. * It remains to be seen if ignoring the doorbell in that case
  169. * is the correct solution.
  170. */
  171. if (db && db->channel.chan)
  172. mbox_chan_received_data(db->channel.chan, NULL);
  173. }
  174. spin_unlock(&hsp->lock);
  175. return IRQ_HANDLED;
  176. }
  177. static irqreturn_t tegra_hsp_shared_irq(int irq, void *data)
  178. {
  179. struct tegra_hsp *hsp = data;
  180. unsigned long bit, mask;
  181. u32 status;
  182. status = tegra_hsp_readl(hsp, HSP_INT_IR) & hsp->mask;
  183. /* process EMPTY interrupts first */
  184. mask = (status >> HSP_INT_EMPTY_SHIFT) & HSP_INT_EMPTY_MASK;
  185. for_each_set_bit(bit, &mask, hsp->num_sm) {
  186. struct tegra_hsp_mailbox *mb = &hsp->mailboxes[bit];
  187. if (mb->producer) {
  188. /*
  189. * Disable EMPTY interrupts until data is sent with
  190. * the next message. These interrupts are level-
  191. * triggered, so if we kept them enabled they would
  192. * constantly trigger until we next write data into
  193. * the message.
  194. */
  195. spin_lock(&hsp->lock);
  196. hsp->mask &= ~BIT(HSP_INT_EMPTY_SHIFT + mb->index);
  197. tegra_hsp_writel(hsp, hsp->mask,
  198. HSP_INT_IE(hsp->shared_irq));
  199. spin_unlock(&hsp->lock);
  200. mbox_chan_txdone(mb->channel.chan, 0);
  201. }
  202. }
  203. /* process FULL interrupts */
  204. mask = (status >> HSP_INT_FULL_SHIFT) & HSP_INT_FULL_MASK;
  205. for_each_set_bit(bit, &mask, hsp->num_sm) {
  206. struct tegra_hsp_mailbox *mb = &hsp->mailboxes[bit];
  207. if (!mb->producer)
  208. mb->ops->recv(&mb->channel);
  209. }
  210. return IRQ_HANDLED;
  211. }
  212. static struct tegra_hsp_channel *
  213. tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name,
  214. unsigned int master, unsigned int index)
  215. {
  216. struct tegra_hsp_doorbell *db;
  217. unsigned int offset;
  218. unsigned long flags;
  219. db = devm_kzalloc(hsp->dev, sizeof(*db), GFP_KERNEL);
  220. if (!db)
  221. return ERR_PTR(-ENOMEM);
  222. offset = (1 + (hsp->num_sm / 2) + hsp->num_ss + hsp->num_as) * SZ_64K;
  223. offset += index * 0x100;
  224. db->channel.regs = hsp->regs + offset;
  225. db->channel.hsp = hsp;
  226. db->name = devm_kstrdup_const(hsp->dev, name, GFP_KERNEL);
  227. db->master = master;
  228. db->index = index;
  229. spin_lock_irqsave(&hsp->lock, flags);
  230. list_add_tail(&db->list, &hsp->doorbells);
  231. spin_unlock_irqrestore(&hsp->lock, flags);
  232. return &db->channel;
  233. }
  234. static int tegra_hsp_doorbell_send_data(struct mbox_chan *chan, void *data)
  235. {
  236. struct tegra_hsp_doorbell *db = chan->con_priv;
  237. tegra_hsp_channel_writel(&db->channel, 1, HSP_DB_TRIGGER);
  238. return 0;
  239. }
  240. static int tegra_hsp_doorbell_startup(struct mbox_chan *chan)
  241. {
  242. struct tegra_hsp_doorbell *db = chan->con_priv;
  243. struct tegra_hsp *hsp = db->channel.hsp;
  244. struct tegra_hsp_doorbell *ccplex;
  245. unsigned long flags;
  246. u32 value;
  247. if (db->master >= chan->mbox->num_chans) {
  248. dev_err(chan->mbox->dev,
  249. "invalid master ID %u for HSP channel\n",
  250. db->master);
  251. return -EINVAL;
  252. }
  253. ccplex = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
  254. if (!ccplex)
  255. return -ENODEV;
  256. /*
  257. * On simulation platforms the BPMP hasn't had a chance yet to mark
  258. * the doorbell as ringable by the CCPLEX, so we want to skip extra
  259. * checks here.
  260. */
  261. if (tegra_is_silicon() && !tegra_hsp_doorbell_can_ring(db))
  262. return -ENODEV;
  263. spin_lock_irqsave(&hsp->lock, flags);
  264. value = tegra_hsp_channel_readl(&ccplex->channel, HSP_DB_ENABLE);
  265. value |= BIT(db->master);
  266. tegra_hsp_channel_writel(&ccplex->channel, value, HSP_DB_ENABLE);
  267. spin_unlock_irqrestore(&hsp->lock, flags);
  268. return 0;
  269. }
  270. static void tegra_hsp_doorbell_shutdown(struct mbox_chan *chan)
  271. {
  272. struct tegra_hsp_doorbell *db = chan->con_priv;
  273. struct tegra_hsp *hsp = db->channel.hsp;
  274. struct tegra_hsp_doorbell *ccplex;
  275. unsigned long flags;
  276. u32 value;
  277. ccplex = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
  278. if (!ccplex)
  279. return;
  280. spin_lock_irqsave(&hsp->lock, flags);
  281. value = tegra_hsp_channel_readl(&ccplex->channel, HSP_DB_ENABLE);
  282. value &= ~BIT(db->master);
  283. tegra_hsp_channel_writel(&ccplex->channel, value, HSP_DB_ENABLE);
  284. spin_unlock_irqrestore(&hsp->lock, flags);
  285. }
  286. static const struct mbox_chan_ops tegra_hsp_db_ops = {
  287. .send_data = tegra_hsp_doorbell_send_data,
  288. .startup = tegra_hsp_doorbell_startup,
  289. .shutdown = tegra_hsp_doorbell_shutdown,
  290. };
  291. static void tegra_hsp_sm_send32(struct tegra_hsp_channel *channel, void *data)
  292. {
  293. u32 value;
  294. /* copy data and mark mailbox full */
  295. value = (u32)(unsigned long)data;
  296. value |= HSP_SM_SHRD_MBOX_FULL;
  297. tegra_hsp_channel_writel(channel, value, HSP_SM_SHRD_MBOX);
  298. }
  299. static void tegra_hsp_sm_recv32(struct tegra_hsp_channel *channel)
  300. {
  301. u32 value;
  302. void *msg;
  303. value = tegra_hsp_channel_readl(channel, HSP_SM_SHRD_MBOX);
  304. value &= ~HSP_SM_SHRD_MBOX_FULL;
  305. msg = (void *)(unsigned long)value;
  306. mbox_chan_received_data(channel->chan, msg);
  307. /*
  308. * Need to clear all bits here since some producers, such as TCU, depend
  309. * on fields in the register getting cleared by the consumer.
  310. *
  311. * The mailbox API doesn't give the consumers a way of doing that
  312. * explicitly, so we have to make sure we cover all possible cases.
  313. */
  314. tegra_hsp_channel_writel(channel, 0x0, HSP_SM_SHRD_MBOX);
  315. }
  316. static const struct tegra_hsp_sm_ops tegra_hsp_sm_32bit_ops = {
  317. .send = tegra_hsp_sm_send32,
  318. .recv = tegra_hsp_sm_recv32,
  319. };
  320. static void tegra_hsp_sm_send128(struct tegra_hsp_channel *channel, void *data)
  321. {
  322. u32 value[4];
  323. memcpy(value, data, sizeof(value));
  324. /* Copy data */
  325. tegra_hsp_channel_writel(channel, value[0], HSP_SHRD_MBOX_TYPE1_DATA0);
  326. tegra_hsp_channel_writel(channel, value[1], HSP_SHRD_MBOX_TYPE1_DATA1);
  327. tegra_hsp_channel_writel(channel, value[2], HSP_SHRD_MBOX_TYPE1_DATA2);
  328. tegra_hsp_channel_writel(channel, value[3], HSP_SHRD_MBOX_TYPE1_DATA3);
  329. /* Update tag to mark mailbox full */
  330. tegra_hsp_channel_writel(channel, HSP_SM_SHRD_MBOX_FULL,
  331. HSP_SHRD_MBOX_TYPE1_TAG);
  332. }
  333. static void tegra_hsp_sm_recv128(struct tegra_hsp_channel *channel)
  334. {
  335. u32 value[4];
  336. void *msg;
  337. value[0] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA0);
  338. value[1] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA1);
  339. value[2] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA2);
  340. value[3] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA3);
  341. msg = (void *)(unsigned long)value;
  342. mbox_chan_received_data(channel->chan, msg);
  343. /*
  344. * Clear data registers and tag.
  345. */
  346. tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA0);
  347. tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA1);
  348. tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA2);
  349. tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA3);
  350. tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_TAG);
  351. }
  352. static const struct tegra_hsp_sm_ops tegra_hsp_sm_128bit_ops = {
  353. .send = tegra_hsp_sm_send128,
  354. .recv = tegra_hsp_sm_recv128,
  355. };
  356. static int tegra_hsp_mailbox_send_data(struct mbox_chan *chan, void *data)
  357. {
  358. struct tegra_hsp_mailbox *mb = chan->con_priv;
  359. struct tegra_hsp *hsp = mb->channel.hsp;
  360. unsigned long flags;
  361. if (WARN_ON(!mb->producer))
  362. return -EPERM;
  363. mb->ops->send(&mb->channel, data);
  364. /* enable EMPTY interrupt for the shared mailbox */
  365. spin_lock_irqsave(&hsp->lock, flags);
  366. hsp->mask |= BIT(HSP_INT_EMPTY_SHIFT + mb->index);
  367. tegra_hsp_writel(hsp, hsp->mask, HSP_INT_IE(hsp->shared_irq));
  368. spin_unlock_irqrestore(&hsp->lock, flags);
  369. return 0;
  370. }
  371. static int tegra_hsp_mailbox_flush(struct mbox_chan *chan,
  372. unsigned long timeout)
  373. {
  374. struct tegra_hsp_mailbox *mb = chan->con_priv;
  375. struct tegra_hsp_channel *ch = &mb->channel;
  376. u32 value;
  377. timeout = jiffies + msecs_to_jiffies(timeout);
  378. while (time_before(jiffies, timeout)) {
  379. value = tegra_hsp_channel_readl(ch, HSP_SM_SHRD_MBOX);
  380. if ((value & HSP_SM_SHRD_MBOX_FULL) == 0) {
  381. mbox_chan_txdone(chan, 0);
  382. /* Wait until channel is empty */
  383. if (chan->active_req != NULL)
  384. continue;
  385. return 0;
  386. }
  387. udelay(1);
  388. }
  389. return -ETIME;
  390. }
  391. static int tegra_hsp_mailbox_startup(struct mbox_chan *chan)
  392. {
  393. struct tegra_hsp_mailbox *mb = chan->con_priv;
  394. struct tegra_hsp_channel *ch = &mb->channel;
  395. struct tegra_hsp *hsp = mb->channel.hsp;
  396. unsigned long flags;
  397. chan->txdone_method = TXDONE_BY_IRQ;
  398. /*
  399. * Shared mailboxes start out as consumers by default. FULL and EMPTY
  400. * interrupts are coalesced at the same shared interrupt.
  401. *
  402. * Keep EMPTY interrupts disabled at startup and only enable them when
  403. * the mailbox is actually full. This is required because the FULL and
  404. * EMPTY interrupts are level-triggered, so keeping EMPTY interrupts
  405. * enabled all the time would cause an interrupt storm while mailboxes
  406. * are idle.
  407. */
  408. spin_lock_irqsave(&hsp->lock, flags);
  409. if (mb->producer)
  410. hsp->mask &= ~BIT(HSP_INT_EMPTY_SHIFT + mb->index);
  411. else
  412. hsp->mask |= BIT(HSP_INT_FULL_SHIFT + mb->index);
  413. tegra_hsp_writel(hsp, hsp->mask, HSP_INT_IE(hsp->shared_irq));
  414. spin_unlock_irqrestore(&hsp->lock, flags);
  415. if (hsp->soc->has_per_mb_ie) {
  416. if (mb->producer)
  417. tegra_hsp_channel_writel(ch, 0x0,
  418. HSP_SM_SHRD_MBOX_EMPTY_INT_IE);
  419. else
  420. tegra_hsp_channel_writel(ch, 0x1,
  421. HSP_SM_SHRD_MBOX_FULL_INT_IE);
  422. }
  423. return 0;
  424. }
  425. static void tegra_hsp_mailbox_shutdown(struct mbox_chan *chan)
  426. {
  427. struct tegra_hsp_mailbox *mb = chan->con_priv;
  428. struct tegra_hsp_channel *ch = &mb->channel;
  429. struct tegra_hsp *hsp = mb->channel.hsp;
  430. unsigned long flags;
  431. if (hsp->soc->has_per_mb_ie) {
  432. if (mb->producer)
  433. tegra_hsp_channel_writel(ch, 0x0,
  434. HSP_SM_SHRD_MBOX_EMPTY_INT_IE);
  435. else
  436. tegra_hsp_channel_writel(ch, 0x0,
  437. HSP_SM_SHRD_MBOX_FULL_INT_IE);
  438. }
  439. spin_lock_irqsave(&hsp->lock, flags);
  440. if (mb->producer)
  441. hsp->mask &= ~BIT(HSP_INT_EMPTY_SHIFT + mb->index);
  442. else
  443. hsp->mask &= ~BIT(HSP_INT_FULL_SHIFT + mb->index);
  444. tegra_hsp_writel(hsp, hsp->mask, HSP_INT_IE(hsp->shared_irq));
  445. spin_unlock_irqrestore(&hsp->lock, flags);
  446. }
  447. static const struct mbox_chan_ops tegra_hsp_sm_ops = {
  448. .send_data = tegra_hsp_mailbox_send_data,
  449. .flush = tegra_hsp_mailbox_flush,
  450. .startup = tegra_hsp_mailbox_startup,
  451. .shutdown = tegra_hsp_mailbox_shutdown,
  452. };
  453. static struct mbox_chan *tegra_hsp_db_xlate(struct mbox_controller *mbox,
  454. const struct of_phandle_args *args)
  455. {
  456. struct tegra_hsp *hsp = container_of(mbox, struct tegra_hsp, mbox_db);
  457. unsigned int type = args->args[0], master = args->args[1];
  458. struct tegra_hsp_channel *channel = ERR_PTR(-ENODEV);
  459. struct tegra_hsp_doorbell *db;
  460. struct mbox_chan *chan;
  461. unsigned long flags;
  462. unsigned int i;
  463. if (type != TEGRA_HSP_MBOX_TYPE_DB || !hsp->doorbell_irq)
  464. return ERR_PTR(-ENODEV);
  465. db = tegra_hsp_doorbell_get(hsp, master);
  466. if (db)
  467. channel = &db->channel;
  468. if (IS_ERR(channel))
  469. return ERR_CAST(channel);
  470. spin_lock_irqsave(&hsp->lock, flags);
  471. for (i = 0; i < mbox->num_chans; i++) {
  472. chan = &mbox->chans[i];
  473. if (!chan->con_priv) {
  474. channel->chan = chan;
  475. chan->con_priv = db;
  476. break;
  477. }
  478. chan = NULL;
  479. }
  480. spin_unlock_irqrestore(&hsp->lock, flags);
  481. return chan ?: ERR_PTR(-EBUSY);
  482. }
  483. static struct mbox_chan *tegra_hsp_sm_xlate(struct mbox_controller *mbox,
  484. const struct of_phandle_args *args)
  485. {
  486. struct tegra_hsp *hsp = container_of(mbox, struct tegra_hsp, mbox_sm);
  487. unsigned int type = args->args[0], index;
  488. struct tegra_hsp_mailbox *mb;
  489. index = args->args[1] & TEGRA_HSP_SM_MASK;
  490. if ((type & HSP_MBOX_TYPE_MASK) != TEGRA_HSP_MBOX_TYPE_SM ||
  491. !hsp->shared_irqs || index >= hsp->num_sm)
  492. return ERR_PTR(-ENODEV);
  493. mb = &hsp->mailboxes[index];
  494. if (type & TEGRA_HSP_MBOX_TYPE_SM_128BIT) {
  495. if (!hsp->soc->has_128_bit_mb)
  496. return ERR_PTR(-ENODEV);
  497. mb->ops = &tegra_hsp_sm_128bit_ops;
  498. } else {
  499. mb->ops = &tegra_hsp_sm_32bit_ops;
  500. }
  501. if ((args->args[1] & TEGRA_HSP_SM_FLAG_TX) == 0)
  502. mb->producer = false;
  503. else
  504. mb->producer = true;
  505. return mb->channel.chan;
  506. }
  507. static int tegra_hsp_add_doorbells(struct tegra_hsp *hsp)
  508. {
  509. const struct tegra_hsp_db_map *map = hsp->soc->map;
  510. struct tegra_hsp_channel *channel;
  511. while (map->name) {
  512. channel = tegra_hsp_doorbell_create(hsp, map->name,
  513. map->master, map->index);
  514. if (IS_ERR(channel))
  515. return PTR_ERR(channel);
  516. map++;
  517. }
  518. return 0;
  519. }
  520. static int tegra_hsp_add_mailboxes(struct tegra_hsp *hsp, struct device *dev)
  521. {
  522. int i;
  523. hsp->mailboxes = devm_kcalloc(dev, hsp->num_sm, sizeof(*hsp->mailboxes),
  524. GFP_KERNEL);
  525. if (!hsp->mailboxes)
  526. return -ENOMEM;
  527. for (i = 0; i < hsp->num_sm; i++) {
  528. struct tegra_hsp_mailbox *mb = &hsp->mailboxes[i];
  529. mb->index = i;
  530. mb->channel.hsp = hsp;
  531. mb->channel.regs = hsp->regs + SZ_64K + i * SZ_32K;
  532. mb->channel.chan = &hsp->mbox_sm.chans[i];
  533. mb->channel.chan->con_priv = mb;
  534. }
  535. return 0;
  536. }
  537. static int tegra_hsp_request_shared_irq(struct tegra_hsp *hsp)
  538. {
  539. unsigned int i, irq = 0;
  540. int err;
  541. for (i = 0; i < hsp->num_si; i++) {
  542. irq = hsp->shared_irqs[i];
  543. if (irq <= 0)
  544. continue;
  545. err = devm_request_irq(hsp->dev, irq, tegra_hsp_shared_irq, 0,
  546. dev_name(hsp->dev), hsp);
  547. if (err < 0) {
  548. dev_err(hsp->dev, "failed to request interrupt: %d\n",
  549. err);
  550. continue;
  551. }
  552. hsp->shared_irq = i;
  553. /* disable all interrupts */
  554. tegra_hsp_writel(hsp, 0, HSP_INT_IE(hsp->shared_irq));
  555. dev_dbg(hsp->dev, "interrupt requested: %u\n", irq);
  556. break;
  557. }
  558. if (i == hsp->num_si) {
  559. dev_err(hsp->dev, "failed to find available interrupt\n");
  560. return -ENOENT;
  561. }
  562. return 0;
  563. }
  564. static int tegra_hsp_probe(struct platform_device *pdev)
  565. {
  566. struct tegra_hsp *hsp;
  567. struct resource *res;
  568. unsigned int i;
  569. u32 value;
  570. int err;
  571. hsp = devm_kzalloc(&pdev->dev, sizeof(*hsp), GFP_KERNEL);
  572. if (!hsp)
  573. return -ENOMEM;
  574. hsp->dev = &pdev->dev;
  575. hsp->soc = of_device_get_match_data(&pdev->dev);
  576. INIT_LIST_HEAD(&hsp->doorbells);
  577. spin_lock_init(&hsp->lock);
  578. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  579. hsp->regs = devm_ioremap_resource(&pdev->dev, res);
  580. if (IS_ERR(hsp->regs))
  581. return PTR_ERR(hsp->regs);
  582. value = tegra_hsp_readl(hsp, HSP_INT_DIMENSIONING);
  583. hsp->num_sm = (value >> HSP_nSM_SHIFT) & HSP_nINT_MASK;
  584. hsp->num_ss = (value >> HSP_nSS_SHIFT) & HSP_nINT_MASK;
  585. hsp->num_as = (value >> HSP_nAS_SHIFT) & HSP_nINT_MASK;
  586. hsp->num_db = (value >> HSP_nDB_SHIFT) & HSP_nINT_MASK;
  587. hsp->num_si = (value >> HSP_nSI_SHIFT) & HSP_nINT_MASK;
  588. err = platform_get_irq_byname_optional(pdev, "doorbell");
  589. if (err >= 0)
  590. hsp->doorbell_irq = err;
  591. if (hsp->num_si > 0) {
  592. unsigned int count = 0;
  593. hsp->shared_irqs = devm_kcalloc(&pdev->dev, hsp->num_si,
  594. sizeof(*hsp->shared_irqs),
  595. GFP_KERNEL);
  596. if (!hsp->shared_irqs)
  597. return -ENOMEM;
  598. for (i = 0; i < hsp->num_si; i++) {
  599. char *name;
  600. name = kasprintf(GFP_KERNEL, "shared%u", i);
  601. if (!name)
  602. return -ENOMEM;
  603. err = platform_get_irq_byname_optional(pdev, name);
  604. if (err >= 0) {
  605. hsp->shared_irqs[i] = err;
  606. count++;
  607. }
  608. kfree(name);
  609. }
  610. if (count == 0) {
  611. devm_kfree(&pdev->dev, hsp->shared_irqs);
  612. hsp->shared_irqs = NULL;
  613. }
  614. }
  615. /* setup the doorbell controller */
  616. hsp->mbox_db.of_xlate = tegra_hsp_db_xlate;
  617. hsp->mbox_db.num_chans = 32;
  618. hsp->mbox_db.dev = &pdev->dev;
  619. hsp->mbox_db.ops = &tegra_hsp_db_ops;
  620. hsp->mbox_db.chans = devm_kcalloc(&pdev->dev, hsp->mbox_db.num_chans,
  621. sizeof(*hsp->mbox_db.chans),
  622. GFP_KERNEL);
  623. if (!hsp->mbox_db.chans)
  624. return -ENOMEM;
  625. if (hsp->doorbell_irq) {
  626. err = tegra_hsp_add_doorbells(hsp);
  627. if (err < 0) {
  628. dev_err(&pdev->dev, "failed to add doorbells: %d\n",
  629. err);
  630. return err;
  631. }
  632. }
  633. err = devm_mbox_controller_register(&pdev->dev, &hsp->mbox_db);
  634. if (err < 0) {
  635. dev_err(&pdev->dev, "failed to register doorbell mailbox: %d\n",
  636. err);
  637. return err;
  638. }
  639. /* setup the shared mailbox controller */
  640. hsp->mbox_sm.of_xlate = tegra_hsp_sm_xlate;
  641. hsp->mbox_sm.num_chans = hsp->num_sm;
  642. hsp->mbox_sm.dev = &pdev->dev;
  643. hsp->mbox_sm.ops = &tegra_hsp_sm_ops;
  644. hsp->mbox_sm.chans = devm_kcalloc(&pdev->dev, hsp->mbox_sm.num_chans,
  645. sizeof(*hsp->mbox_sm.chans),
  646. GFP_KERNEL);
  647. if (!hsp->mbox_sm.chans)
  648. return -ENOMEM;
  649. if (hsp->shared_irqs) {
  650. err = tegra_hsp_add_mailboxes(hsp, &pdev->dev);
  651. if (err < 0) {
  652. dev_err(&pdev->dev, "failed to add mailboxes: %d\n",
  653. err);
  654. return err;
  655. }
  656. }
  657. err = devm_mbox_controller_register(&pdev->dev, &hsp->mbox_sm);
  658. if (err < 0) {
  659. dev_err(&pdev->dev, "failed to register shared mailbox: %d\n",
  660. err);
  661. return err;
  662. }
  663. platform_set_drvdata(pdev, hsp);
  664. if (hsp->doorbell_irq) {
  665. err = devm_request_irq(&pdev->dev, hsp->doorbell_irq,
  666. tegra_hsp_doorbell_irq, IRQF_NO_SUSPEND,
  667. dev_name(&pdev->dev), hsp);
  668. if (err < 0) {
  669. dev_err(&pdev->dev,
  670. "failed to request doorbell IRQ#%u: %d\n",
  671. hsp->doorbell_irq, err);
  672. return err;
  673. }
  674. }
  675. if (hsp->shared_irqs) {
  676. err = tegra_hsp_request_shared_irq(hsp);
  677. if (err < 0)
  678. return err;
  679. }
  680. lockdep_register_key(&hsp->lock_key);
  681. lockdep_set_class(&hsp->lock, &hsp->lock_key);
  682. return 0;
  683. }
  684. static int tegra_hsp_remove(struct platform_device *pdev)
  685. {
  686. struct tegra_hsp *hsp = platform_get_drvdata(pdev);
  687. lockdep_unregister_key(&hsp->lock_key);
  688. return 0;
  689. }
  690. static int __maybe_unused tegra_hsp_resume(struct device *dev)
  691. {
  692. struct tegra_hsp *hsp = dev_get_drvdata(dev);
  693. unsigned int i;
  694. struct tegra_hsp_doorbell *db;
  695. list_for_each_entry(db, &hsp->doorbells, list) {
  696. if (db->channel.chan)
  697. tegra_hsp_doorbell_startup(db->channel.chan);
  698. }
  699. if (hsp->mailboxes) {
  700. for (i = 0; i < hsp->num_sm; i++) {
  701. struct tegra_hsp_mailbox *mb = &hsp->mailboxes[i];
  702. if (mb->channel.chan->cl)
  703. tegra_hsp_mailbox_startup(mb->channel.chan);
  704. }
  705. }
  706. return 0;
  707. }
  708. static const struct dev_pm_ops tegra_hsp_pm_ops = {
  709. .resume_noirq = tegra_hsp_resume,
  710. };
  711. static const struct tegra_hsp_db_map tegra186_hsp_db_map[] = {
  712. { "ccplex", TEGRA_HSP_DB_MASTER_CCPLEX, HSP_DB_CCPLEX, },
  713. { "bpmp", TEGRA_HSP_DB_MASTER_BPMP, HSP_DB_BPMP, },
  714. { /* sentinel */ }
  715. };
  716. static const struct tegra_hsp_soc tegra186_hsp_soc = {
  717. .map = tegra186_hsp_db_map,
  718. .has_per_mb_ie = false,
  719. .has_128_bit_mb = false,
  720. };
  721. static const struct tegra_hsp_soc tegra194_hsp_soc = {
  722. .map = tegra186_hsp_db_map,
  723. .has_per_mb_ie = true,
  724. .has_128_bit_mb = false,
  725. };
  726. static const struct tegra_hsp_soc tegra234_hsp_soc = {
  727. .map = tegra186_hsp_db_map,
  728. .has_per_mb_ie = false,
  729. .has_128_bit_mb = true,
  730. };
  731. static const struct of_device_id tegra_hsp_match[] = {
  732. { .compatible = "nvidia,tegra186-hsp", .data = &tegra186_hsp_soc },
  733. { .compatible = "nvidia,tegra194-hsp", .data = &tegra194_hsp_soc },
  734. { .compatible = "nvidia,tegra234-hsp", .data = &tegra234_hsp_soc },
  735. { }
  736. };
  737. static struct platform_driver tegra_hsp_driver = {
  738. .driver = {
  739. .name = "tegra-hsp",
  740. .of_match_table = tegra_hsp_match,
  741. .pm = &tegra_hsp_pm_ops,
  742. },
  743. .probe = tegra_hsp_probe,
  744. .remove = tegra_hsp_remove,
  745. };
  746. static int __init tegra_hsp_init(void)
  747. {
  748. return platform_driver_register(&tegra_hsp_driver);
  749. }
  750. core_initcall(tegra_hsp_init);