platform-msi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MSI framework for platform devices
  4. *
  5. * Copyright (C) 2015 ARM Limited, All Rights Reserved.
  6. * Author: Marc Zyngier <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/idr.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/msi.h>
  13. #include <linux/slab.h>
  14. #define DEV_ID_SHIFT 21
  15. #define MAX_DEV_MSIS (1 << (32 - DEV_ID_SHIFT))
  16. /*
  17. * Internal data structure containing a (made up, but unique) devid
  18. * and the callback to write the MSI message.
  19. */
  20. struct platform_msi_priv_data {
  21. struct device *dev;
  22. void *host_data;
  23. msi_alloc_info_t arg;
  24. irq_write_msi_msg_t write_msg;
  25. int devid;
  26. };
  27. /* The devid allocator */
  28. static DEFINE_IDA(platform_msi_devid_ida);
  29. #ifdef GENERIC_MSI_DOMAIN_OPS
  30. /*
  31. * Convert an msi_desc to a globaly unique identifier (per-device
  32. * devid + msi_desc position in the msi_list).
  33. */
  34. static irq_hw_number_t platform_msi_calc_hwirq(struct msi_desc *desc)
  35. {
  36. u32 devid = desc->dev->msi.data->platform_data->devid;
  37. return (devid << (32 - DEV_ID_SHIFT)) | desc->msi_index;
  38. }
  39. static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  40. {
  41. arg->desc = desc;
  42. arg->hwirq = platform_msi_calc_hwirq(desc);
  43. }
  44. static int platform_msi_init(struct irq_domain *domain,
  45. struct msi_domain_info *info,
  46. unsigned int virq, irq_hw_number_t hwirq,
  47. msi_alloc_info_t *arg)
  48. {
  49. return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  50. info->chip, info->chip_data);
  51. }
  52. static void platform_msi_set_proxy_dev(msi_alloc_info_t *arg)
  53. {
  54. arg->flags |= MSI_ALLOC_FLAGS_PROXY_DEVICE;
  55. }
  56. #else
  57. #define platform_msi_set_desc NULL
  58. #define platform_msi_init NULL
  59. #define platform_msi_set_proxy_dev(x) do {} while(0)
  60. #endif
  61. static void platform_msi_update_dom_ops(struct msi_domain_info *info)
  62. {
  63. struct msi_domain_ops *ops = info->ops;
  64. BUG_ON(!ops);
  65. if (ops->msi_init == NULL)
  66. ops->msi_init = platform_msi_init;
  67. if (ops->set_desc == NULL)
  68. ops->set_desc = platform_msi_set_desc;
  69. }
  70. static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  71. {
  72. struct msi_desc *desc = irq_data_get_msi_desc(data);
  73. desc->dev->msi.data->platform_data->write_msg(desc, msg);
  74. }
  75. static void platform_msi_update_chip_ops(struct msi_domain_info *info)
  76. {
  77. struct irq_chip *chip = info->chip;
  78. BUG_ON(!chip);
  79. if (!chip->irq_mask)
  80. chip->irq_mask = irq_chip_mask_parent;
  81. if (!chip->irq_unmask)
  82. chip->irq_unmask = irq_chip_unmask_parent;
  83. if (!chip->irq_eoi)
  84. chip->irq_eoi = irq_chip_eoi_parent;
  85. if (!chip->irq_set_affinity)
  86. chip->irq_set_affinity = msi_domain_set_affinity;
  87. if (!chip->irq_write_msi_msg)
  88. chip->irq_write_msi_msg = platform_msi_write_msg;
  89. if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
  90. !(chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)))
  91. info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
  92. }
  93. /**
  94. * platform_msi_create_irq_domain - Create a platform MSI interrupt domain
  95. * @fwnode: Optional fwnode of the interrupt controller
  96. * @info: MSI domain info
  97. * @parent: Parent irq domain
  98. *
  99. * Updates the domain and chip ops and creates a platform MSI
  100. * interrupt domain.
  101. *
  102. * Returns:
  103. * A domain pointer or NULL in case of failure.
  104. */
  105. struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
  106. struct msi_domain_info *info,
  107. struct irq_domain *parent)
  108. {
  109. struct irq_domain *domain;
  110. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  111. platform_msi_update_dom_ops(info);
  112. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  113. platform_msi_update_chip_ops(info);
  114. info->flags |= MSI_FLAG_DEV_SYSFS | MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS |
  115. MSI_FLAG_FREE_MSI_DESCS;
  116. domain = msi_create_irq_domain(fwnode, info, parent);
  117. if (domain)
  118. irq_domain_update_bus_token(domain, DOMAIN_BUS_PLATFORM_MSI);
  119. return domain;
  120. }
  121. EXPORT_SYMBOL_GPL(platform_msi_create_irq_domain);
  122. static int platform_msi_alloc_priv_data(struct device *dev, unsigned int nvec,
  123. irq_write_msi_msg_t write_msi_msg)
  124. {
  125. struct platform_msi_priv_data *datap;
  126. int err;
  127. /*
  128. * Limit the number of interrupts to 2048 per device. Should we
  129. * need to bump this up, DEV_ID_SHIFT should be adjusted
  130. * accordingly (which would impact the max number of MSI
  131. * capable devices).
  132. */
  133. if (!dev->msi.domain || !write_msi_msg || !nvec || nvec > MAX_DEV_MSIS)
  134. return -EINVAL;
  135. if (dev->msi.domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) {
  136. dev_err(dev, "Incompatible msi_domain, giving up\n");
  137. return -EINVAL;
  138. }
  139. err = msi_setup_device_data(dev);
  140. if (err)
  141. return err;
  142. /* Already initialized? */
  143. if (dev->msi.data->platform_data)
  144. return -EBUSY;
  145. datap = kzalloc(sizeof(*datap), GFP_KERNEL);
  146. if (!datap)
  147. return -ENOMEM;
  148. datap->devid = ida_simple_get(&platform_msi_devid_ida,
  149. 0, 1 << DEV_ID_SHIFT, GFP_KERNEL);
  150. if (datap->devid < 0) {
  151. err = datap->devid;
  152. kfree(datap);
  153. return err;
  154. }
  155. datap->write_msg = write_msi_msg;
  156. datap->dev = dev;
  157. dev->msi.data->platform_data = datap;
  158. return 0;
  159. }
  160. static void platform_msi_free_priv_data(struct device *dev)
  161. {
  162. struct platform_msi_priv_data *data = dev->msi.data->platform_data;
  163. dev->msi.data->platform_data = NULL;
  164. ida_simple_remove(&platform_msi_devid_ida, data->devid);
  165. kfree(data);
  166. }
  167. /**
  168. * platform_msi_domain_alloc_irqs - Allocate MSI interrupts for @dev
  169. * @dev: The device for which to allocate interrupts
  170. * @nvec: The number of interrupts to allocate
  171. * @write_msi_msg: Callback to write an interrupt message for @dev
  172. *
  173. * Returns:
  174. * Zero for success, or an error code in case of failure
  175. */
  176. int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
  177. irq_write_msi_msg_t write_msi_msg)
  178. {
  179. int err;
  180. err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
  181. if (err)
  182. return err;
  183. err = msi_domain_alloc_irqs(dev->msi.domain, dev, nvec);
  184. if (err)
  185. platform_msi_free_priv_data(dev);
  186. return err;
  187. }
  188. EXPORT_SYMBOL_GPL(platform_msi_domain_alloc_irqs);
  189. /**
  190. * platform_msi_domain_free_irqs - Free MSI interrupts for @dev
  191. * @dev: The device for which to free interrupts
  192. */
  193. void platform_msi_domain_free_irqs(struct device *dev)
  194. {
  195. msi_domain_free_irqs(dev->msi.domain, dev);
  196. platform_msi_free_priv_data(dev);
  197. }
  198. EXPORT_SYMBOL_GPL(platform_msi_domain_free_irqs);
  199. /**
  200. * platform_msi_get_host_data - Query the private data associated with
  201. * a platform-msi domain
  202. * @domain: The platform-msi domain
  203. *
  204. * Return: The private data provided when calling
  205. * platform_msi_create_device_domain().
  206. */
  207. void *platform_msi_get_host_data(struct irq_domain *domain)
  208. {
  209. struct platform_msi_priv_data *data = domain->host_data;
  210. return data->host_data;
  211. }
  212. static struct lock_class_key platform_device_msi_lock_class;
  213. /**
  214. * __platform_msi_create_device_domain - Create a platform-msi device domain
  215. *
  216. * @dev: The device generating the MSIs
  217. * @nvec: The number of MSIs that need to be allocated
  218. * @is_tree: flag to indicate tree hierarchy
  219. * @write_msi_msg: Callback to write an interrupt message for @dev
  220. * @ops: The hierarchy domain operations to use
  221. * @host_data: Private data associated to this domain
  222. *
  223. * Return: An irqdomain for @nvec interrupts on success, NULL in case of error.
  224. *
  225. * This is for interrupt domains which stack on a platform-msi domain
  226. * created by platform_msi_create_irq_domain(). @dev->msi.domain points to
  227. * that platform-msi domain which is the parent for the new domain.
  228. */
  229. struct irq_domain *
  230. __platform_msi_create_device_domain(struct device *dev,
  231. unsigned int nvec,
  232. bool is_tree,
  233. irq_write_msi_msg_t write_msi_msg,
  234. const struct irq_domain_ops *ops,
  235. void *host_data)
  236. {
  237. struct platform_msi_priv_data *data;
  238. struct irq_domain *domain;
  239. int err;
  240. err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
  241. if (err)
  242. return NULL;
  243. /*
  244. * Use a separate lock class for the MSI descriptor mutex on
  245. * platform MSI device domains because the descriptor mutex nests
  246. * into the domain mutex. See alloc/free below.
  247. */
  248. lockdep_set_class(&dev->msi.data->mutex, &platform_device_msi_lock_class);
  249. data = dev->msi.data->platform_data;
  250. data->host_data = host_data;
  251. domain = irq_domain_create_hierarchy(dev->msi.domain, 0,
  252. is_tree ? 0 : nvec,
  253. dev->fwnode, ops, data);
  254. if (!domain)
  255. goto free_priv;
  256. platform_msi_set_proxy_dev(&data->arg);
  257. err = msi_domain_prepare_irqs(domain->parent, dev, nvec, &data->arg);
  258. if (err)
  259. goto free_domain;
  260. return domain;
  261. free_domain:
  262. irq_domain_remove(domain);
  263. free_priv:
  264. platform_msi_free_priv_data(dev);
  265. return NULL;
  266. }
  267. /**
  268. * platform_msi_device_domain_free - Free interrupts associated with a platform-msi
  269. * device domain
  270. *
  271. * @domain: The platform-msi device domain
  272. * @virq: The base irq from which to perform the free operation
  273. * @nr_irqs: How many interrupts to free from @virq
  274. */
  275. void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
  276. unsigned int nr_irqs)
  277. {
  278. struct platform_msi_priv_data *data = domain->host_data;
  279. msi_lock_descs(data->dev);
  280. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  281. msi_free_msi_descs_range(data->dev, MSI_DESC_ALL, virq, virq + nr_irqs - 1);
  282. msi_unlock_descs(data->dev);
  283. }
  284. /**
  285. * platform_msi_device_domain_alloc - Allocate interrupts associated with
  286. * a platform-msi device domain
  287. *
  288. * @domain: The platform-msi device domain
  289. * @virq: The base irq from which to perform the allocate operation
  290. * @nr_irqs: How many interrupts to allocate from @virq
  291. *
  292. * Return 0 on success, or an error code on failure. Must be called
  293. * with irq_domain_mutex held (which can only be done as part of a
  294. * top-level interrupt allocation).
  295. */
  296. int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
  297. unsigned int nr_irqs)
  298. {
  299. struct platform_msi_priv_data *data = domain->host_data;
  300. struct device *dev = data->dev;
  301. return msi_domain_populate_irqs(domain->parent, dev, virq, nr_irqs, &data->arg);
  302. }