platform-msi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <marc.zyngier@arm.com>
  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;
  37. devid = desc->platform.msi_priv_data->devid;
  38. return (devid << (32 - DEV_ID_SHIFT)) | desc->platform.msi_index;
  39. }
  40. static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  41. {
  42. arg->desc = desc;
  43. arg->hwirq = platform_msi_calc_hwirq(desc);
  44. }
  45. static int platform_msi_init(struct irq_domain *domain,
  46. struct msi_domain_info *info,
  47. unsigned int virq, irq_hw_number_t hwirq,
  48. msi_alloc_info_t *arg)
  49. {
  50. return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  51. info->chip, info->chip_data);
  52. }
  53. #else
  54. #define platform_msi_set_desc NULL
  55. #define platform_msi_init NULL
  56. #endif
  57. static void platform_msi_update_dom_ops(struct msi_domain_info *info)
  58. {
  59. struct msi_domain_ops *ops = info->ops;
  60. BUG_ON(!ops);
  61. if (ops->msi_init == NULL)
  62. ops->msi_init = platform_msi_init;
  63. if (ops->set_desc == NULL)
  64. ops->set_desc = platform_msi_set_desc;
  65. }
  66. static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  67. {
  68. struct msi_desc *desc = irq_data_get_msi_desc(data);
  69. struct platform_msi_priv_data *priv_data;
  70. priv_data = desc->platform.msi_priv_data;
  71. priv_data->write_msg(desc, msg);
  72. }
  73. static void platform_msi_update_chip_ops(struct msi_domain_info *info)
  74. {
  75. struct irq_chip *chip = info->chip;
  76. BUG_ON(!chip);
  77. if (!chip->irq_mask)
  78. chip->irq_mask = irq_chip_mask_parent;
  79. if (!chip->irq_unmask)
  80. chip->irq_unmask = irq_chip_unmask_parent;
  81. if (!chip->irq_eoi)
  82. chip->irq_eoi = irq_chip_eoi_parent;
  83. if (!chip->irq_set_affinity)
  84. chip->irq_set_affinity = msi_domain_set_affinity;
  85. if (!chip->irq_write_msi_msg)
  86. chip->irq_write_msi_msg = platform_msi_write_msg;
  87. if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
  88. !(chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)))
  89. info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
  90. }
  91. static void platform_msi_free_descs(struct device *dev, int base, int nvec)
  92. {
  93. struct msi_desc *desc, *tmp;
  94. list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) {
  95. if (desc->platform.msi_index >= base &&
  96. desc->platform.msi_index < (base + nvec)) {
  97. list_del(&desc->list);
  98. free_msi_entry(desc);
  99. }
  100. }
  101. }
  102. static int platform_msi_alloc_descs_with_irq(struct device *dev, int virq,
  103. int nvec,
  104. struct platform_msi_priv_data *data)
  105. {
  106. struct msi_desc *desc;
  107. int i, base = 0;
  108. if (!list_empty(dev_to_msi_list(dev))) {
  109. desc = list_last_entry(dev_to_msi_list(dev),
  110. struct msi_desc, list);
  111. base = desc->platform.msi_index + 1;
  112. }
  113. for (i = 0; i < nvec; i++) {
  114. desc = alloc_msi_entry(dev, 1, NULL);
  115. if (!desc)
  116. break;
  117. desc->platform.msi_priv_data = data;
  118. desc->platform.msi_index = base + i;
  119. desc->irq = virq ? virq + i : 0;
  120. list_add_tail(&desc->list, dev_to_msi_list(dev));
  121. }
  122. if (i != nvec) {
  123. /* Clean up the mess */
  124. platform_msi_free_descs(dev, base, nvec);
  125. return -ENOMEM;
  126. }
  127. return 0;
  128. }
  129. static int platform_msi_alloc_descs(struct device *dev, int nvec,
  130. struct platform_msi_priv_data *data)
  131. {
  132. return platform_msi_alloc_descs_with_irq(dev, 0, nvec, data);
  133. }
  134. /**
  135. * platform_msi_create_irq_domain - Create a platform MSI interrupt domain
  136. * @fwnode: Optional fwnode of the interrupt controller
  137. * @info: MSI domain info
  138. * @parent: Parent irq domain
  139. *
  140. * Updates the domain and chip ops and creates a platform MSI
  141. * interrupt domain.
  142. *
  143. * Returns:
  144. * A domain pointer or NULL in case of failure.
  145. */
  146. struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
  147. struct msi_domain_info *info,
  148. struct irq_domain *parent)
  149. {
  150. struct irq_domain *domain;
  151. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  152. platform_msi_update_dom_ops(info);
  153. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  154. platform_msi_update_chip_ops(info);
  155. domain = msi_create_irq_domain(fwnode, info, parent);
  156. if (domain)
  157. irq_domain_update_bus_token(domain, DOMAIN_BUS_PLATFORM_MSI);
  158. return domain;
  159. }
  160. static struct platform_msi_priv_data *
  161. platform_msi_alloc_priv_data(struct device *dev, unsigned int nvec,
  162. irq_write_msi_msg_t write_msi_msg)
  163. {
  164. struct platform_msi_priv_data *datap;
  165. /*
  166. * Limit the number of interrupts to 2048 per device. Should we
  167. * need to bump this up, DEV_ID_SHIFT should be adjusted
  168. * accordingly (which would impact the max number of MSI
  169. * capable devices).
  170. */
  171. if (!dev->msi_domain || !write_msi_msg || !nvec || nvec > MAX_DEV_MSIS)
  172. return ERR_PTR(-EINVAL);
  173. if (dev->msi_domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) {
  174. dev_err(dev, "Incompatible msi_domain, giving up\n");
  175. return ERR_PTR(-EINVAL);
  176. }
  177. /* Already had a helping of MSI? Greed... */
  178. if (!list_empty(dev_to_msi_list(dev)))
  179. return ERR_PTR(-EBUSY);
  180. datap = kzalloc(sizeof(*datap), GFP_KERNEL);
  181. if (!datap)
  182. return ERR_PTR(-ENOMEM);
  183. datap->devid = ida_simple_get(&platform_msi_devid_ida,
  184. 0, 1 << DEV_ID_SHIFT, GFP_KERNEL);
  185. if (datap->devid < 0) {
  186. int err = datap->devid;
  187. kfree(datap);
  188. return ERR_PTR(err);
  189. }
  190. datap->write_msg = write_msi_msg;
  191. datap->dev = dev;
  192. return datap;
  193. }
  194. static void platform_msi_free_priv_data(struct platform_msi_priv_data *data)
  195. {
  196. ida_simple_remove(&platform_msi_devid_ida, data->devid);
  197. kfree(data);
  198. }
  199. /**
  200. * platform_msi_domain_alloc_irqs - Allocate MSI interrupts for @dev
  201. * @dev: The device for which to allocate interrupts
  202. * @nvec: The number of interrupts to allocate
  203. * @write_msi_msg: Callback to write an interrupt message for @dev
  204. *
  205. * Returns:
  206. * Zero for success, or an error code in case of failure
  207. */
  208. int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
  209. irq_write_msi_msg_t write_msi_msg)
  210. {
  211. struct platform_msi_priv_data *priv_data;
  212. int err;
  213. priv_data = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
  214. if (IS_ERR(priv_data))
  215. return PTR_ERR(priv_data);
  216. err = platform_msi_alloc_descs(dev, nvec, priv_data);
  217. if (err)
  218. goto out_free_priv_data;
  219. err = msi_domain_alloc_irqs(dev->msi_domain, dev, nvec);
  220. if (err)
  221. goto out_free_desc;
  222. return 0;
  223. out_free_desc:
  224. platform_msi_free_descs(dev, 0, nvec);
  225. out_free_priv_data:
  226. platform_msi_free_priv_data(priv_data);
  227. return err;
  228. }
  229. EXPORT_SYMBOL_GPL(platform_msi_domain_alloc_irqs);
  230. /**
  231. * platform_msi_domain_free_irqs - Free MSI interrupts for @dev
  232. * @dev: The device for which to free interrupts
  233. */
  234. void platform_msi_domain_free_irqs(struct device *dev)
  235. {
  236. if (!list_empty(dev_to_msi_list(dev))) {
  237. struct msi_desc *desc;
  238. desc = first_msi_entry(dev);
  239. platform_msi_free_priv_data(desc->platform.msi_priv_data);
  240. }
  241. msi_domain_free_irqs(dev->msi_domain, dev);
  242. platform_msi_free_descs(dev, 0, MAX_DEV_MSIS);
  243. }
  244. EXPORT_SYMBOL_GPL(platform_msi_domain_free_irqs);
  245. /**
  246. * platform_msi_get_host_data - Query the private data associated with
  247. * a platform-msi domain
  248. * @domain: The platform-msi domain
  249. *
  250. * Returns the private data provided when calling
  251. * platform_msi_create_device_domain.
  252. */
  253. void *platform_msi_get_host_data(struct irq_domain *domain)
  254. {
  255. struct platform_msi_priv_data *data = domain->host_data;
  256. return data->host_data;
  257. }
  258. /**
  259. * platform_msi_create_device_domain - Create a platform-msi domain
  260. *
  261. * @dev: The device generating the MSIs
  262. * @nvec: The number of MSIs that need to be allocated
  263. * @write_msi_msg: Callback to write an interrupt message for @dev
  264. * @ops: The hierarchy domain operations to use
  265. * @host_data: Private data associated to this domain
  266. *
  267. * Returns an irqdomain for @nvec interrupts
  268. */
  269. struct irq_domain *
  270. platform_msi_create_device_domain(struct device *dev,
  271. unsigned int nvec,
  272. irq_write_msi_msg_t write_msi_msg,
  273. const struct irq_domain_ops *ops,
  274. void *host_data)
  275. {
  276. struct platform_msi_priv_data *data;
  277. struct irq_domain *domain;
  278. int err;
  279. data = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
  280. if (IS_ERR(data))
  281. return NULL;
  282. data->host_data = host_data;
  283. domain = irq_domain_create_hierarchy(dev->msi_domain, 0, nvec,
  284. dev->fwnode, ops, data);
  285. if (!domain)
  286. goto free_priv;
  287. err = msi_domain_prepare_irqs(domain->parent, dev, nvec, &data->arg);
  288. if (err)
  289. goto free_domain;
  290. return domain;
  291. free_domain:
  292. irq_domain_remove(domain);
  293. free_priv:
  294. platform_msi_free_priv_data(data);
  295. return NULL;
  296. }
  297. /**
  298. * platform_msi_domain_free - Free interrupts associated with a platform-msi
  299. * domain
  300. *
  301. * @domain: The platform-msi domain
  302. * @virq: The base irq from which to perform the free operation
  303. * @nvec: How many interrupts to free from @virq
  304. */
  305. void platform_msi_domain_free(struct irq_domain *domain, unsigned int virq,
  306. unsigned int nvec)
  307. {
  308. struct platform_msi_priv_data *data = domain->host_data;
  309. struct msi_desc *desc, *tmp;
  310. for_each_msi_entry_safe(desc, tmp, data->dev) {
  311. if (WARN_ON(!desc->irq || desc->nvec_used != 1))
  312. return;
  313. if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
  314. continue;
  315. irq_domain_free_irqs_common(domain, desc->irq, 1);
  316. list_del(&desc->list);
  317. free_msi_entry(desc);
  318. }
  319. }
  320. /**
  321. * platform_msi_domain_alloc - Allocate interrupts associated with
  322. * a platform-msi domain
  323. *
  324. * @domain: The platform-msi domain
  325. * @virq: The base irq from which to perform the allocate operation
  326. * @nvec: How many interrupts to free from @virq
  327. *
  328. * Return 0 on success, or an error code on failure. Must be called
  329. * with irq_domain_mutex held (which can only be done as part of a
  330. * top-level interrupt allocation).
  331. */
  332. int platform_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
  333. unsigned int nr_irqs)
  334. {
  335. struct platform_msi_priv_data *data = domain->host_data;
  336. int err;
  337. err = platform_msi_alloc_descs_with_irq(data->dev, virq, nr_irqs, data);
  338. if (err)
  339. return err;
  340. err = msi_domain_populate_irqs(domain->parent, data->dev,
  341. virq, nr_irqs, &data->arg);
  342. if (err)
  343. platform_msi_domain_free(domain, virq, nr_irqs);
  344. return err;
  345. }