gh_dbl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/gunyah/gh_dbl.h>
  11. #include <linux/gunyah_rsc_mgr.h>
  12. #include "hcall_dbl.h"
  13. struct gh_dbl_desc {
  14. enum gh_dbl_label label;
  15. };
  16. enum gh_dbl_dir {
  17. GH_DBL_DIRECTION_TX,
  18. GH_DBL_DIRECTION_RX
  19. };
  20. struct gh_dbl_cap_table {
  21. struct gh_dbl_desc *client_desc;
  22. spinlock_t cap_entry_lock;
  23. gh_capid_t tx_cap_id;
  24. int tx_reg_done;
  25. gh_capid_t rx_cap_id;
  26. int rx_irq;
  27. int rx_reg_done;
  28. const char *rx_irq_name;
  29. dbl_rx_cb_t rx_callback;
  30. void *rx_priv_data;
  31. wait_queue_head_t cap_wq;
  32. };
  33. static bool gh_dbl_initialized;
  34. static struct gh_dbl_cap_table gh_dbl_cap_table[GH_DBL_LABEL_MAX];
  35. /**
  36. * gh_dbl_validate_params - Validate doorbell common parameters
  37. */
  38. static int gh_dbl_validate_params(struct gh_dbl_desc *client_desc,
  39. enum gh_dbl_dir dir, const unsigned long flags)
  40. {
  41. struct gh_dbl_cap_table *cap_table_entry;
  42. int ret;
  43. if (IS_ERR_OR_NULL(client_desc))
  44. return -EINVAL;
  45. /* Check if the client has manipulated the label */
  46. if (client_desc->label < 0 || client_desc->label >= GH_DBL_LABEL_MAX)
  47. return -EINVAL;
  48. cap_table_entry = &gh_dbl_cap_table[client_desc->label];
  49. spin_lock(&cap_table_entry->cap_entry_lock);
  50. if (cap_table_entry->client_desc != client_desc) {
  51. spin_unlock(&cap_table_entry->cap_entry_lock);
  52. pr_err("%s: Invalid client descriptor\n", __func__);
  53. return -EINVAL;
  54. }
  55. /*
  56. * rx_cap_id == NULL and tx_cap_id == NULL means TWO things
  57. * either "gh_dbl_populate_cap_info()" call from RM is not over
  58. * or
  59. * There are no doorbell setup for Tx or Rx
  60. */
  61. if (dir == GH_DBL_DIRECTION_RX) {
  62. if (!cap_table_entry->rx_reg_done) {
  63. ret = -EINVAL;
  64. goto err;
  65. }
  66. if (flags & GH_DBL_NONBLOCK) {
  67. ret = cap_table_entry->rx_cap_id == GH_CAPID_INVAL ? -EAGAIN : 0;
  68. goto err;
  69. }
  70. spin_unlock(&cap_table_entry->cap_entry_lock);
  71. if (wait_event_interruptible(cap_table_entry->cap_wq,
  72. cap_table_entry->rx_cap_id != GH_CAPID_INVAL))
  73. return -ERESTARTSYS;
  74. } else {
  75. if (!cap_table_entry->tx_reg_done) {
  76. ret = -EINVAL;
  77. goto err;
  78. }
  79. if (flags & GH_DBL_NONBLOCK) {
  80. ret = cap_table_entry->tx_cap_id == GH_CAPID_INVAL ? -EAGAIN : 0;
  81. goto err;
  82. }
  83. spin_unlock(&cap_table_entry->cap_entry_lock);
  84. if (wait_event_interruptible(cap_table_entry->cap_wq,
  85. cap_table_entry->tx_cap_id != GH_CAPID_INVAL))
  86. return -ERESTARTSYS;
  87. }
  88. return 0;
  89. err:
  90. spin_unlock(&cap_table_entry->cap_entry_lock);
  91. return ret;
  92. }
  93. /**
  94. * gh_dbl_read_and_clean - Automatically read and clear the flags in doorbell
  95. * @client_desc: client handle to indetify the doorbell object
  96. * @clear_flags: clear the bits mentioned in the clear_flags
  97. * @flags: Optional flags to pass to send the data. For the list of flags,
  98. * see linux/gunyah/gh_dbl.h
  99. *
  100. * Reads and clears the flags of the Doorbell object. If there is a pending
  101. * bound virtual interrupt, it will be de-asserted
  102. *
  103. * Returns:
  104. * 0 on success, @clear_flags contains the doorbell’s previous unmasked flags
  105. * before the @clear_flags were removed.
  106. */
  107. int gh_dbl_read_and_clean(void *dbl_client_desc, gh_dbl_flags_t *clear_flags,
  108. const unsigned long flags)
  109. {
  110. struct gh_dbl_cap_table *cap_table_entry;
  111. struct gh_hcall_dbl_recv_resp recv_resp;
  112. struct gh_dbl_desc *client_desc = dbl_client_desc;
  113. int ret, gh_ret;
  114. if (!clear_flags)
  115. return -EINVAL;
  116. ret = gh_dbl_validate_params(client_desc, GH_DBL_DIRECTION_RX, flags);
  117. if (ret)
  118. return ret;
  119. cap_table_entry = &gh_dbl_cap_table[client_desc->label];
  120. gh_ret = gh_hcall_dbl_recv(cap_table_entry->rx_cap_id,
  121. *clear_flags, &recv_resp);
  122. ret = gh_error_remap(gh_ret);
  123. if (ret != 0)
  124. pr_err("%s: Hypercall failed, ret = %d\n", __func__, gh_ret);
  125. else
  126. *clear_flags = recv_resp.old_flags;
  127. return ret;
  128. }
  129. EXPORT_SYMBOL(gh_dbl_read_and_clean);
  130. /**
  131. * gh_dbl_set_mask - Set doorbell object mask
  132. * @client_desc: client handle to indetify the doorbell object
  133. * @enable_mask: The mask of flags that will cause an assertion of
  134. * the doorbell's bound virtual interrupt
  135. * @ack_mask: Controls which flags should be automatically cleared
  136. * when the interrupt is asserted
  137. * @flags: Optional flags to pass to send the data. For the list of flags,
  138. * see linux/gunyah/gh_dbl.h
  139. *
  140. * Sets the Doorbell object’s masks. A doorbell object has two masks
  141. * which are configured by the receiver to control which flags it is
  142. * interested in, and which flags if any should be automatically acknowledged.
  143. *
  144. * Returns:
  145. * 0 on success
  146. */
  147. int gh_dbl_set_mask(void *dbl_client_desc, gh_dbl_flags_t enable_mask,
  148. gh_dbl_flags_t ack_mask, const unsigned long flags)
  149. {
  150. struct gh_dbl_cap_table *cap_table_entry;
  151. struct gh_dbl_desc *client_desc = dbl_client_desc;
  152. int ret, gh_ret;
  153. ret = gh_dbl_validate_params(client_desc, GH_DBL_DIRECTION_RX, flags);
  154. if (ret)
  155. return ret;
  156. cap_table_entry = &gh_dbl_cap_table[client_desc->label];
  157. gh_ret = gh_hcall_dbl_mask(cap_table_entry->rx_cap_id,
  158. enable_mask, ack_mask);
  159. ret = gh_error_remap(gh_ret);
  160. if (ret != 0)
  161. pr_err("%s: Hypercall failed ret = %d\n", __func__, gh_ret);
  162. return ret;
  163. }
  164. EXPORT_SYMBOL(gh_dbl_set_mask);
  165. /**
  166. * gh_dbl_send - Set flags in the doorbell
  167. * @client_desc: client handle to indetify the doorbell object
  168. * @newflags: flags to set in the doorbell. This flag along with enable_mask
  169. * in the doorbell decide whehter to raise vIRQ are not.
  170. * @flags: Optional flags to pass to send the data. For the list of flags,
  171. * see linux/gunyah/gh_dbl.h
  172. *
  173. * Set flags in the doorbell. If following the send, the set of enabled flags
  174. * as defined by the bitwise-AND of the doorbell flags with the EnableMask,
  175. * is non-zero, any bound virtual interrupt will be asserted.
  176. *
  177. * Returns:
  178. * 0 on success, @newflags contains the doorbell’s previous unmasked flags
  179. * before the @newflags were added.
  180. */
  181. int gh_dbl_send(void *dbl_client_desc, gh_dbl_flags_t *newflags,
  182. unsigned long flags)
  183. {
  184. struct gh_dbl_cap_table *cap_table_entry;
  185. struct gh_hcall_dbl_send_resp send_resp;
  186. struct gh_dbl_desc *client_desc = dbl_client_desc;
  187. int ret, gh_ret;
  188. if (!newflags)
  189. return -EINVAL;
  190. ret = gh_dbl_validate_params(client_desc, GH_DBL_DIRECTION_TX, flags);
  191. if (ret)
  192. return ret;
  193. cap_table_entry = &gh_dbl_cap_table[client_desc->label];
  194. gh_ret = gh_hcall_dbl_send(cap_table_entry->tx_cap_id, *newflags,
  195. &send_resp);
  196. ret = gh_error_remap(gh_ret);
  197. if (ret != 0)
  198. pr_err("%s: Hypercall failed ret = %d\n", __func__, gh_ret);
  199. else
  200. *newflags = send_resp.old_flags;
  201. return ret;
  202. }
  203. EXPORT_SYMBOL(gh_dbl_send);
  204. /**
  205. * gh_dbl_reset - clear all the flags of the doorbell and sets all bits in
  206. * the Doorbell’s mask.
  207. * @client_desc: client handle to indetify the doorbell object
  208. * @flags: Optional flags to pass to send the data. For the list of flags,
  209. * see linux/gunyah/gh_dbl.h
  210. *
  211. * Clears all the flags of the doorbell and sets all bits in the doorbell’s
  212. * mask. If there is a pending bound virtual interrupt, it will be de-asserted.
  213. *
  214. * Returns:
  215. * 0 on success
  216. */
  217. int gh_dbl_reset(void *dbl_client_desc, const unsigned long flags)
  218. {
  219. struct gh_dbl_cap_table *cap_table_entry;
  220. struct gh_dbl_desc *client_desc = dbl_client_desc;
  221. int ret, gh_ret;
  222. ret = gh_dbl_validate_params(client_desc, GH_DBL_DIRECTION_RX, flags);
  223. if (ret)
  224. return ret;
  225. cap_table_entry = &gh_dbl_cap_table[client_desc->label];
  226. gh_ret = gh_hcall_dbl_reset(cap_table_entry->rx_cap_id);
  227. ret = gh_error_remap(gh_ret);
  228. if (ret != 0)
  229. pr_err("%s: Hypercall failed ret = %d\n", __func__, gh_ret);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL(gh_dbl_reset);
  233. static irqreturn_t gh_dbl_rx_callback_thread(int irq, void *data)
  234. {
  235. struct gh_dbl_cap_table *cap_table_entry = data;
  236. if (!cap_table_entry->rx_callback)
  237. return IRQ_HANDLED;
  238. cap_table_entry->rx_callback(irq, cap_table_entry->rx_priv_data);
  239. return IRQ_HANDLED;
  240. }
  241. /**
  242. * gh_dbl_tx_register: Register as a Tx client to use the doorbell
  243. * @label: The label associated to the doorbell that the client wants
  244. * to send a message to other VM.
  245. *
  246. * The function returns a descriptor for the clients to send a message.
  247. * Else, returns -EBUSY if some other client is already registered
  248. * to this label, and -EINVAL for invalid arguments. The caller should check
  249. * the return value using IS_ERR_OR_NULL() and PTR_ERR() to extract the error
  250. * code.
  251. */
  252. void *gh_dbl_tx_register(enum gh_dbl_label label)
  253. {
  254. struct gh_dbl_cap_table *cap_table_entry;
  255. struct gh_dbl_desc *client_desc;
  256. int ret;
  257. if (label < 0 || label >= GH_DBL_LABEL_MAX)
  258. return ERR_PTR(-EINVAL);
  259. if (!gh_dbl_initialized)
  260. return ERR_PTR(-EPROBE_DEFER);
  261. cap_table_entry = &gh_dbl_cap_table[label];
  262. spin_lock(&cap_table_entry->cap_entry_lock);
  263. /* Avoid multiple client Tx registration for the same doorbell */
  264. if (cap_table_entry->tx_reg_done) {
  265. ret = -EBUSY;
  266. goto err;
  267. }
  268. if (cap_table_entry->client_desc) {
  269. client_desc = cap_table_entry->client_desc;
  270. } else {
  271. client_desc = kzalloc(sizeof(*client_desc), GFP_ATOMIC);
  272. if (!client_desc) {
  273. ret = -ENOMEM;
  274. goto err;
  275. }
  276. client_desc->label = label;
  277. cap_table_entry->client_desc = client_desc;
  278. }
  279. cap_table_entry->tx_reg_done = 1;
  280. pr_debug("%s: Registered Tx client for label: %d\n", __func__, label);
  281. spin_unlock(&cap_table_entry->cap_entry_lock);
  282. return client_desc;
  283. err:
  284. spin_unlock(&cap_table_entry->cap_entry_lock);
  285. return ERR_PTR(ret);
  286. }
  287. EXPORT_SYMBOL(gh_dbl_tx_register);
  288. /**
  289. * gh_dbl_rx_register: Register as a Rx client to use the doorbell
  290. * @label: The label associated to the doorbell that the client wants
  291. * to read a message.
  292. * @rx_cb: Callback of the client when there is a vIRQ on doorbell
  293. * @priv: Private data of the driver
  294. *
  295. * The function returns a descriptor for the clients to receive a message.
  296. * Else, returns -EBUSY if some other client is already registered
  297. * to this label, and -EINVAL for invalid arguments. The caller should check
  298. * the return value using IS_ERR_OR_NULL() and PTR_ERR() to extract the error
  299. * code.
  300. */
  301. void *gh_dbl_rx_register(enum gh_dbl_label label, dbl_rx_cb_t rx_cb, void *priv)
  302. {
  303. struct gh_dbl_cap_table *cap_table_entry;
  304. struct gh_dbl_desc *client_desc;
  305. int ret;
  306. if (label < 0 || label >= GH_DBL_LABEL_MAX)
  307. return ERR_PTR(-EINVAL);
  308. if (!gh_dbl_initialized)
  309. return ERR_PTR(-EPROBE_DEFER);
  310. cap_table_entry = &gh_dbl_cap_table[label];
  311. spin_lock(&cap_table_entry->cap_entry_lock);
  312. /* Avoid multiple client Rx registration for the same doorbell */
  313. if (cap_table_entry->rx_reg_done) {
  314. ret = -EBUSY;
  315. goto err;
  316. }
  317. if (cap_table_entry->client_desc) {
  318. client_desc = cap_table_entry->client_desc;
  319. } else {
  320. client_desc = kzalloc(sizeof(*client_desc), GFP_ATOMIC);
  321. if (!client_desc) {
  322. ret = -ENOMEM;
  323. goto err;
  324. }
  325. client_desc->label = label;
  326. cap_table_entry->client_desc = client_desc;
  327. }
  328. cap_table_entry->rx_callback = rx_cb;
  329. cap_table_entry->rx_priv_data = priv;
  330. cap_table_entry->rx_reg_done = 1;
  331. pr_debug("%s: Registered Rx client for label: %d\n", __func__, label);
  332. spin_unlock(&cap_table_entry->cap_entry_lock);
  333. return client_desc;
  334. err:
  335. pr_debug("%s: Registration for Rx client for label failed: %d\n",
  336. __func__, label);
  337. spin_unlock(&cap_table_entry->cap_entry_lock);
  338. return ERR_PTR(ret);
  339. }
  340. EXPORT_SYMBOL(gh_dbl_rx_register);
  341. /**
  342. * gh_dbl_tx_unregister: Unregister Tx client to use the doorbell
  343. * @client_desc: The descriptor that was passed via gh_dbl_tx_register() or
  344. * gh_dbl_rx_register()
  345. *
  346. * The function returns 0 is the client was unregistered successfully. Else,
  347. * -EINVAL for invalid arguments.
  348. */
  349. int gh_dbl_tx_unregister(void *dbl_client_desc)
  350. {
  351. struct gh_dbl_desc *client_desc = dbl_client_desc;
  352. struct gh_dbl_cap_table *cap_table_entry;
  353. if (IS_ERR_OR_NULL(client_desc))
  354. return -EINVAL;
  355. /* Check if the client has manipulated the label */
  356. if (client_desc->label < 0 || client_desc->label >= GH_DBL_LABEL_MAX)
  357. return -EINVAL;
  358. cap_table_entry = &gh_dbl_cap_table[client_desc->label];
  359. spin_lock(&cap_table_entry->cap_entry_lock);
  360. /* Is the client trying to free someone else's doorbell? */
  361. if (cap_table_entry->client_desc != client_desc) {
  362. pr_err("%s: Trying to free invalid client descriptor!\n",
  363. __func__);
  364. spin_unlock(&cap_table_entry->cap_entry_lock);
  365. return -EINVAL;
  366. }
  367. pr_debug("%s: Unregistering client for label: %d\n",
  368. __func__, client_desc->label);
  369. /* Rx client still holding the "client_desc". Do not remove now. */
  370. if (!cap_table_entry->rx_reg_done) {
  371. cap_table_entry->client_desc = NULL;
  372. kfree(client_desc);
  373. } else {
  374. pr_debug("%s: Rx client holding the client_desc.\n", __func__);
  375. }
  376. cap_table_entry->tx_reg_done = 0;
  377. spin_unlock(&cap_table_entry->cap_entry_lock);
  378. return 0;
  379. }
  380. EXPORT_SYMBOL(gh_dbl_tx_unregister);
  381. /**
  382. * gh_dbl_rx_unregister: Unregister Rx client to use the doorbell
  383. * @client_desc: The descriptor that was passed via gh_dbl_tx_register() or
  384. * gh_dbl_rx_register()
  385. *
  386. * The function returns 0 is the client was unregistered successfully. Else,
  387. * -EINVAL for invalid arguments.
  388. */
  389. int gh_dbl_rx_unregister(void *dbl_client_desc)
  390. {
  391. struct gh_dbl_desc *client_desc = dbl_client_desc;
  392. struct gh_dbl_cap_table *cap_table_entry;
  393. if (IS_ERR_OR_NULL(client_desc))
  394. return -EINVAL;
  395. /* Check if the client has manipulated the label */
  396. if (client_desc->label < 0 || client_desc->label >= GH_DBL_LABEL_MAX)
  397. return -EINVAL;
  398. cap_table_entry = &gh_dbl_cap_table[client_desc->label];
  399. spin_lock(&cap_table_entry->cap_entry_lock);
  400. /* Is the client trying to free someone else's doorbell? */
  401. if (cap_table_entry->client_desc != client_desc) {
  402. pr_err("%s: Trying to free invalid client descriptor!\n",
  403. __func__);
  404. spin_unlock(&cap_table_entry->cap_entry_lock);
  405. return -EINVAL;
  406. }
  407. pr_debug("%s: Unregistering client for label: %d\n", __func__,
  408. client_desc->label);
  409. /* Tx client still holding the "client_desc". Do not remove now.*/
  410. if (!cap_table_entry->tx_reg_done) {
  411. cap_table_entry->client_desc = NULL;
  412. kfree(client_desc);
  413. } else {
  414. pr_debug("%s: Tx client holding the client_desc.\n", __func__);
  415. }
  416. cap_table_entry->rx_callback = NULL;
  417. cap_table_entry->rx_priv_data = NULL;
  418. cap_table_entry->rx_reg_done = 0;
  419. spin_unlock(&cap_table_entry->cap_entry_lock);
  420. return 0;
  421. }
  422. EXPORT_SYMBOL(gh_dbl_rx_unregister);
  423. /**
  424. * This API is called by RM driver to populate doorbell objects
  425. */
  426. int gh_dbl_populate_cap_info(enum gh_dbl_label label, u64 cap_id,
  427. int direction, int rx_irq)
  428. {
  429. struct gh_dbl_cap_table *cap_table_entry;
  430. int ret = 0;
  431. if (!gh_dbl_initialized)
  432. return -EAGAIN;
  433. if (label < 0 || label >= GH_DBL_LABEL_MAX) {
  434. pr_err("%s: Invalid label passed\n", __func__);
  435. return -EINVAL;
  436. }
  437. cap_table_entry = &gh_dbl_cap_table[label];
  438. switch (direction) {
  439. case GH_DBL_DIRECTION_TX:
  440. /* No interrupt should associated with Tx doorbell*/
  441. if (rx_irq > 0) {
  442. pr_err("%s: No IRQ associated for Tx doorbell!\n",
  443. __func__);
  444. ret = -ENXIO;
  445. goto err;
  446. }
  447. spin_lock(&cap_table_entry->cap_entry_lock);
  448. cap_table_entry->tx_cap_id = cap_id;
  449. spin_unlock(&cap_table_entry->cap_entry_lock);
  450. wake_up_interruptible(&cap_table_entry->cap_wq);
  451. pr_debug("%s: label: %d; tx_cap_id: %llu; dir: %d; rx_irq: %d\n",
  452. __func__, label, cap_id, direction, rx_irq);
  453. break;
  454. case GH_DBL_DIRECTION_RX:
  455. if (rx_irq <= 0) {
  456. pr_err("%s: Invalid IRQ number for Rx doorbell\n",
  457. __func__);
  458. ret = -ENXIO;
  459. goto err;
  460. }
  461. cap_table_entry->rx_irq = rx_irq;
  462. ret = request_threaded_irq(cap_table_entry->rx_irq,
  463. NULL,
  464. gh_dbl_rx_callback_thread,
  465. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  466. cap_table_entry->rx_irq_name,
  467. cap_table_entry);
  468. if (ret < 0) {
  469. pr_err("%s: IRQ registration failed\n", __func__);
  470. goto err;
  471. }
  472. irq_set_irq_wake(rx_irq, 1);
  473. spin_lock(&cap_table_entry->cap_entry_lock);
  474. cap_table_entry->rx_cap_id = cap_id;
  475. spin_unlock(&cap_table_entry->cap_entry_lock);
  476. wake_up_interruptible(&cap_table_entry->cap_wq);
  477. pr_debug("%s: label: %d; rx_cap_id: %llu; dir: %d; rx_irq: %d\n",
  478. __func__, label, cap_id, direction, rx_irq);
  479. break;
  480. default:
  481. pr_err("%s: Invalid direction(%d) for doorbell\n",
  482. __func__, direction);
  483. ret = -EINVAL;
  484. }
  485. err:
  486. return ret;
  487. }
  488. EXPORT_SYMBOL(gh_dbl_populate_cap_info);
  489. /**
  490. * This API is called by RM driver to free up doorbell objects
  491. */
  492. int gh_dbl_reset_cap_info(enum gh_dbl_label label, int direction, int *irq)
  493. {
  494. struct gh_dbl_cap_table *cap_table_entry;
  495. int ret = 0;
  496. if (!gh_dbl_initialized)
  497. return -EAGAIN;
  498. if (label < 0 || label >= GH_DBL_LABEL_MAX) {
  499. pr_err("%s: Invalid label passed\n", __func__);
  500. return -EINVAL;
  501. }
  502. if (!irq)
  503. return -EINVAL;
  504. cap_table_entry = &gh_dbl_cap_table[label];
  505. spin_lock(&cap_table_entry->cap_entry_lock);
  506. switch (direction) {
  507. case GH_DBL_DIRECTION_TX:
  508. *irq = 0;
  509. cap_table_entry->tx_cap_id = GH_CAPID_INVAL;
  510. break;
  511. case GH_DBL_DIRECTION_RX:
  512. if (!cap_table_entry->rx_irq) {
  513. pr_err("%s: Rx IRQ not setup\n", __func__);
  514. ret = -ENXIO;
  515. goto err_unlock;
  516. }
  517. *irq = cap_table_entry->rx_irq;
  518. cap_table_entry->rx_irq = 0;
  519. cap_table_entry->rx_cap_id = GH_CAPID_INVAL;
  520. break;
  521. default:
  522. pr_err("%s: Invalid direction(%d) for doorbell\n",
  523. __func__, direction);
  524. ret = -EINVAL;
  525. }
  526. err_unlock:
  527. spin_unlock(&cap_table_entry->cap_entry_lock);
  528. if (*irq && !ret)
  529. free_irq(*irq, cap_table_entry);
  530. return ret;
  531. }
  532. EXPORT_SYMBOL(gh_dbl_reset_cap_info);
  533. static void gh_dbl_cleanup(int begin_idx)
  534. {
  535. struct gh_dbl_cap_table *cap_table_entry;
  536. int i;
  537. if (begin_idx >= GH_DBL_LABEL_MAX)
  538. begin_idx = GH_DBL_LABEL_MAX - 1;
  539. for (i = begin_idx; i >= 0; i--) {
  540. cap_table_entry = &gh_dbl_cap_table[i];
  541. kfree(cap_table_entry->rx_irq_name);
  542. }
  543. }
  544. static int __init gh_dbl_init(void)
  545. {
  546. struct gh_dbl_cap_table *entry;
  547. int ret;
  548. int i;
  549. for (i = 0; i < GH_DBL_LABEL_MAX; i++) {
  550. entry = &gh_dbl_cap_table[i];
  551. spin_lock_init(&entry->cap_entry_lock);
  552. init_waitqueue_head(&entry->cap_wq);
  553. entry->tx_cap_id = GH_CAPID_INVAL;
  554. entry->rx_cap_id = GH_CAPID_INVAL;
  555. entry->rx_irq_name = kasprintf(GFP_KERNEL, "gh_dbl_rx_%d", i);
  556. if (!entry->rx_irq_name) {
  557. ret = -ENOMEM;
  558. goto err;
  559. }
  560. }
  561. gh_dbl_initialized = true;
  562. return 0;
  563. err:
  564. gh_dbl_cleanup(i);
  565. return ret;
  566. }
  567. module_init(gh_dbl_init);
  568. static void __exit gh_dbl_exit(void)
  569. {
  570. gh_dbl_cleanup(GH_DBL_LABEL_MAX - 1);
  571. }
  572. module_exit(gh_dbl_exit);
  573. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Gunyah Doorbell Driver");
  574. MODULE_LICENSE("GPL");