platform-msi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  58. info->chip, info->chip_data);
  59. }
  60. #else
  61. #define platform_msi_set_desc NULL
  62. #define platform_msi_init NULL
  63. #endif
  64. static void platform_msi_update_dom_ops(struct msi_domain_info *info)
  65. {
  66. struct msi_domain_ops *ops = info->ops;
  67. BUG_ON(!ops);
  68. if (ops->msi_init == NULL)
  69. ops->msi_init = platform_msi_init;
  70. if (ops->set_desc == NULL)
  71. ops->set_desc = platform_msi_set_desc;
  72. }
  73. static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  74. {
  75. struct msi_desc *desc = irq_data_get_msi_desc(data);
  76. struct platform_msi_priv_data *priv_data;
  77. priv_data = desc->platform.msi_priv_data;
  78. priv_data->write_msg(desc, msg);
  79. }
  80. static void platform_msi_update_chip_ops(struct msi_domain_info *info)
  81. {
  82. struct irq_chip *chip = info->chip;
  83. BUG_ON(!chip);
  84. if (!chip->irq_mask)
  85. chip->irq_mask = irq_chip_mask_parent;
  86. if (!chip->irq_unmask)
  87. chip->irq_unmask = irq_chip_unmask_parent;
  88. if (!chip->irq_eoi)
  89. chip->irq_eoi = irq_chip_eoi_parent;
  90. if (!chip->irq_set_affinity)
  91. chip->irq_set_affinity = msi_domain_set_affinity;
  92. if (!chip->irq_write_msi_msg)
  93. chip->irq_write_msi_msg = platform_msi_write_msg;
  94. }
  95. static void platform_msi_free_descs(struct device *dev)
  96. {
  97. struct msi_desc *desc, *tmp;
  98. list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) {
  99. list_del(&desc->list);
  100. free_msi_entry(desc);
  101. }
  102. }
  103. static int platform_msi_alloc_descs(struct device *dev, int nvec,
  104. struct platform_msi_priv_data *data)
  105. {
  106. int i;
  107. for (i = 0; i < nvec; i++) {
  108. struct msi_desc *desc;
  109. desc = alloc_msi_entry(dev);
  110. if (!desc)
  111. break;
  112. desc->platform.msi_priv_data = data;
  113. desc->platform.msi_index = i;
  114. desc->nvec_used = 1;
  115. list_add_tail(&desc->list, dev_to_msi_list(dev));
  116. }
  117. if (i != nvec) {
  118. /* Clean up the mess */
  119. platform_msi_free_descs(dev);
  120. return -ENOMEM;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * platform_msi_create_irq_domain - Create a platform MSI interrupt domain
  126. * @fwnode: Optional fwnode of the interrupt controller
  127. * @info: MSI domain info
  128. * @parent: Parent irq domain
  129. *
  130. * Updates the domain and chip ops and creates a platform MSI
  131. * interrupt domain.
  132. *
  133. * Returns:
  134. * A domain pointer or NULL in case of failure.
  135. */
  136. struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
  137. struct msi_domain_info *info,
  138. struct irq_domain *parent)
  139. {
  140. struct irq_domain *domain;
  141. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  142. platform_msi_update_dom_ops(info);
  143. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  144. platform_msi_update_chip_ops(info);
  145. domain = msi_create_irq_domain(fwnode, info, parent);
  146. if (domain)
  147. domain->bus_token = DOMAIN_BUS_PLATFORM_MSI;
  148. return domain;
  149. }
  150. /**
  151. * platform_msi_domain_alloc_irqs - Allocate MSI interrupts for @dev
  152. * @dev: The device for which to allocate interrupts
  153. * @nvec: The number of interrupts to allocate
  154. * @write_msi_msg: Callback to write an interrupt message for @dev
  155. *
  156. * Returns:
  157. * Zero for success, or an error code in case of failure
  158. */
  159. int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
  160. irq_write_msi_msg_t write_msi_msg)
  161. {
  162. struct platform_msi_priv_data *priv_data;
  163. int err;
  164. /*
  165. * Limit the number of interrupts to 256 per device. Should we
  166. * need to bump this up, DEV_ID_SHIFT should be adjusted
  167. * accordingly (which would impact the max number of MSI
  168. * capable devices).
  169. */
  170. if (!dev->msi_domain || !write_msi_msg || !nvec ||
  171. nvec > (1 << (32 - DEV_ID_SHIFT)))
  172. return -EINVAL;
  173. if (dev->msi_domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) {
  174. dev_err(dev, "Incompatible msi_domain, giving up\n");
  175. return -EINVAL;
  176. }
  177. /* Already had a helping of MSI? Greed... */
  178. if (!list_empty(dev_to_msi_list(dev)))
  179. return -EBUSY;
  180. priv_data = kzalloc(sizeof(*priv_data), GFP_KERNEL);
  181. if (!priv_data)
  182. return -ENOMEM;
  183. priv_data->devid = ida_simple_get(&platform_msi_devid_ida,
  184. 0, 1 << DEV_ID_SHIFT, GFP_KERNEL);
  185. if (priv_data->devid < 0) {
  186. err = priv_data->devid;
  187. goto out_free_data;
  188. }
  189. priv_data->write_msg = write_msi_msg;
  190. err = platform_msi_alloc_descs(dev, nvec, priv_data);
  191. if (err)
  192. goto out_free_id;
  193. err = msi_domain_alloc_irqs(dev->msi_domain, dev, nvec);
  194. if (err)
  195. goto out_free_desc;
  196. return 0;
  197. out_free_desc:
  198. platform_msi_free_descs(dev);
  199. out_free_id:
  200. ida_simple_remove(&platform_msi_devid_ida, priv_data->devid);
  201. out_free_data:
  202. kfree(priv_data);
  203. return err;
  204. }
  205. /**
  206. * platform_msi_domain_free_irqs - Free MSI interrupts for @dev
  207. * @dev: The device for which to free interrupts
  208. */
  209. void platform_msi_domain_free_irqs(struct device *dev)
  210. {
  211. struct msi_desc *desc;
  212. desc = first_msi_entry(dev);
  213. if (desc) {
  214. struct platform_msi_priv_data *data;
  215. data = desc->platform.msi_priv_data;
  216. ida_simple_remove(&platform_msi_devid_ida, data->devid);
  217. kfree(data);
  218. }
  219. msi_domain_free_irqs(dev->msi_domain, dev);
  220. platform_msi_free_descs(dev);
  221. }