platform-msi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * MSI framework for platform devices
  3. *
  4. * Copyright (C) 2015 ARM Limited, All Rights Reserved.
  5. * Author: Marc Zyngier <marc.zyngier@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/idr.h>
  21. #include <linux/irq.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/msi.h>
  24. #include <linux/slab.h>
  25. #define DEV_ID_SHIFT 24
  26. /*
  27. * Internal data structure containing a (made up, but unique) devid
  28. * and the callback to write the MSI message.
  29. */
  30. struct platform_msi_priv_data {
  31. irq_write_msi_msg_t write_msg;
  32. int devid;
  33. };
  34. /* The devid allocator */
  35. static DEFINE_IDA(platform_msi_devid_ida);
  36. #ifdef GENERIC_MSI_DOMAIN_OPS
  37. /*
  38. * Convert an msi_desc to a globaly unique identifier (per-device
  39. * devid + msi_desc position in the msi_list).
  40. */
  41. static irq_hw_number_t platform_msi_calc_hwirq(struct msi_desc *desc)
  42. {
  43. u32 devid;
  44. devid = desc->platform.msi_priv_data->devid;
  45. return (devid << (32 - DEV_ID_SHIFT)) | desc->platform.msi_index;
  46. }
  47. static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  48. {
  49. arg->desc = desc;
  50. arg->hwirq = platform_msi_calc_hwirq(desc);
  51. }
  52. static int platform_msi_init(struct irq_domain *domain,
  53. struct msi_domain_info *info,
  54. unsigned int virq, irq_hw_number_t hwirq,
  55. msi_alloc_info_t *arg)
  56. {
  57. struct irq_data *data;
  58. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  59. info->chip, info->chip_data);
  60. /*
  61. * Save the MSI descriptor in handler_data so that the
  62. * irq_write_msi_msg callback can retrieve it (and the
  63. * associated device).
  64. */
  65. data = irq_domain_get_irq_data(domain, virq);
  66. data->handler_data = arg->desc;
  67. return 0;
  68. }
  69. #else
  70. #define platform_msi_set_desc NULL
  71. #define platform_msi_init NULL
  72. #endif
  73. static void platform_msi_update_dom_ops(struct msi_domain_info *info)
  74. {
  75. struct msi_domain_ops *ops = info->ops;
  76. BUG_ON(!ops);
  77. if (ops->msi_init == NULL)
  78. ops->msi_init = platform_msi_init;
  79. if (ops->set_desc == NULL)
  80. ops->set_desc = platform_msi_set_desc;
  81. }
  82. static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  83. {
  84. struct msi_desc *desc = irq_data_get_irq_handler_data(data);
  85. struct platform_msi_priv_data *priv_data;
  86. priv_data = desc->platform.msi_priv_data;
  87. priv_data->write_msg(desc, msg);
  88. }
  89. static void platform_msi_update_chip_ops(struct msi_domain_info *info)
  90. {
  91. struct irq_chip *chip = info->chip;
  92. BUG_ON(!chip);
  93. if (!chip->irq_mask)
  94. chip->irq_mask = irq_chip_mask_parent;
  95. if (!chip->irq_unmask)
  96. chip->irq_unmask = irq_chip_unmask_parent;
  97. if (!chip->irq_eoi)
  98. chip->irq_eoi = irq_chip_eoi_parent;
  99. if (!chip->irq_set_affinity)
  100. chip->irq_set_affinity = msi_domain_set_affinity;
  101. if (!chip->irq_write_msi_msg)
  102. chip->irq_write_msi_msg = platform_msi_write_msg;
  103. }
  104. static void platform_msi_free_descs(struct device *dev)
  105. {
  106. struct msi_desc *desc, *tmp;
  107. list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) {
  108. list_del(&desc->list);
  109. free_msi_entry(desc);
  110. }
  111. }
  112. static int platform_msi_alloc_descs(struct device *dev, int nvec,
  113. struct platform_msi_priv_data *data)
  114. {
  115. int i;
  116. for (i = 0; i < nvec; i++) {
  117. struct msi_desc *desc;
  118. desc = alloc_msi_entry(dev);
  119. if (!desc)
  120. break;
  121. desc->platform.msi_priv_data = data;
  122. desc->platform.msi_index = i;
  123. desc->nvec_used = 1;
  124. list_add_tail(&desc->list, dev_to_msi_list(dev));
  125. }
  126. if (i != nvec) {
  127. /* Clean up the mess */
  128. platform_msi_free_descs(dev);
  129. return -ENOMEM;
  130. }
  131. return 0;
  132. }
  133. /**
  134. * platform_msi_create_irq_domain - Create a platform MSI interrupt domain
  135. * @np: Optional device-tree node of the interrupt controller
  136. * @info: MSI domain info
  137. * @parent: Parent irq domain
  138. *
  139. * Updates the domain and chip ops and creates a platform MSI
  140. * interrupt domain.
  141. *
  142. * Returns:
  143. * A domain pointer or NULL in case of failure.
  144. */
  145. struct irq_domain *platform_msi_create_irq_domain(struct device_node *np,
  146. struct msi_domain_info *info,
  147. struct irq_domain *parent)
  148. {
  149. struct irq_domain *domain;
  150. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  151. platform_msi_update_dom_ops(info);
  152. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  153. platform_msi_update_chip_ops(info);
  154. domain = msi_create_irq_domain(np, info, parent);
  155. if (domain)
  156. domain->bus_token = DOMAIN_BUS_PLATFORM_MSI;
  157. return domain;
  158. }
  159. /**
  160. * platform_msi_domain_alloc_irqs - Allocate MSI interrupts for @dev
  161. * @dev: The device for which to allocate interrupts
  162. * @nvec: The number of interrupts to allocate
  163. * @write_msi_msg: Callback to write an interrupt message for @dev
  164. *
  165. * Returns:
  166. * Zero for success, or an error code in case of failure
  167. */
  168. int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
  169. irq_write_msi_msg_t write_msi_msg)
  170. {
  171. struct platform_msi_priv_data *priv_data;
  172. int err;
  173. /*
  174. * Limit the number of interrupts to 256 per device. Should we
  175. * need to bump this up, DEV_ID_SHIFT should be adjusted
  176. * accordingly (which would impact the max number of MSI
  177. * capable devices).
  178. */
  179. if (!dev->msi_domain || !write_msi_msg || !nvec ||
  180. nvec > (1 << (32 - DEV_ID_SHIFT)))
  181. return -EINVAL;
  182. if (dev->msi_domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) {
  183. dev_err(dev, "Incompatible msi_domain, giving up\n");
  184. return -EINVAL;
  185. }
  186. /* Already had a helping of MSI? Greed... */
  187. if (!list_empty(dev_to_msi_list(dev)))
  188. return -EBUSY;
  189. priv_data = kzalloc(sizeof(*priv_data), GFP_KERNEL);
  190. if (!priv_data)
  191. return -ENOMEM;
  192. priv_data->devid = ida_simple_get(&platform_msi_devid_ida,
  193. 0, 1 << DEV_ID_SHIFT, GFP_KERNEL);
  194. if (priv_data->devid < 0) {
  195. err = priv_data->devid;
  196. goto out_free_data;
  197. }
  198. priv_data->write_msg = write_msi_msg;
  199. err = platform_msi_alloc_descs(dev, nvec, priv_data);
  200. if (err)
  201. goto out_free_id;
  202. err = msi_domain_alloc_irqs(dev->msi_domain, dev, nvec);
  203. if (err)
  204. goto out_free_desc;
  205. return 0;
  206. out_free_desc:
  207. platform_msi_free_descs(dev);
  208. out_free_id:
  209. ida_simple_remove(&platform_msi_devid_ida, priv_data->devid);
  210. out_free_data:
  211. kfree(priv_data);
  212. return err;
  213. }
  214. /**
  215. * platform_msi_domain_free_irqs - Free MSI interrupts for @dev
  216. * @dev: The device for which to free interrupts
  217. */
  218. void platform_msi_domain_free_irqs(struct device *dev)
  219. {
  220. struct msi_desc *desc;
  221. desc = first_msi_entry(dev);
  222. if (desc) {
  223. struct platform_msi_priv_data *data;
  224. data = desc->platform.msi_priv_data;
  225. ida_simple_remove(&platform_msi_devid_ida, data->devid);
  226. kfree(data);
  227. }
  228. msi_domain_free_irqs(dev->msi_domain, dev);
  229. platform_msi_free_descs(dev);
  230. }