msi.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corp.
  4. * Author: Jiang Liu <jiang.liu@linux.intel.com>
  5. *
  6. * This file is licensed under GPLv2.
  7. *
  8. * This file contains common code to support Message Signaled Interrupts for
  9. * PCI compatible and non PCI compatible devices.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/msi.h>
  15. #include <linux/mutex.h>
  16. #include <linux/pci.h>
  17. #include <linux/slab.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/types.h>
  20. #include <linux/xarray.h>
  21. #include "internals.h"
  22. /**
  23. * struct msi_device_data - MSI per device data
  24. * @properties: MSI properties which are interesting to drivers
  25. * @mutex: Mutex protecting the MSI descriptor store
  26. * @__domains: Internal data for per device MSI domains
  27. * @__iter_idx: Index to search the next entry for iterators
  28. */
  29. struct msi_device_data {
  30. unsigned long properties;
  31. struct mutex mutex;
  32. struct msi_dev_domain __domains[MSI_MAX_DEVICE_IRQDOMAINS];
  33. unsigned long __iter_idx;
  34. };
  35. /**
  36. * struct msi_ctrl - MSI internal management control structure
  37. * @domid: ID of the domain on which management operations should be done
  38. * @first: First (hardware) slot index to operate on
  39. * @last: Last (hardware) slot index to operate on
  40. * @nirqs: The number of Linux interrupts to allocate. Can be larger
  41. * than the range due to PCI/multi-MSI.
  42. */
  43. struct msi_ctrl {
  44. unsigned int domid;
  45. unsigned int first;
  46. unsigned int last;
  47. unsigned int nirqs;
  48. };
  49. /* Invalid Xarray index which is outside of any searchable range */
  50. #define MSI_XA_MAX_INDEX (ULONG_MAX - 1)
  51. /* The maximum domain size */
  52. #define MSI_XA_DOMAIN_SIZE (MSI_MAX_INDEX + 1)
  53. static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl);
  54. static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid);
  55. static inline int msi_sysfs_create_group(struct device *dev);
  56. /**
  57. * msi_alloc_desc - Allocate an initialized msi_desc
  58. * @dev: Pointer to the device for which this is allocated
  59. * @nvec: The number of vectors used in this entry
  60. * @affinity: Optional pointer to an affinity mask array size of @nvec
  61. *
  62. * If @affinity is not %NULL then an affinity array[@nvec] is allocated
  63. * and the affinity masks and flags from @affinity are copied.
  64. *
  65. * Return: pointer to allocated &msi_desc on success or %NULL on failure
  66. */
  67. static struct msi_desc *msi_alloc_desc(struct device *dev, int nvec,
  68. const struct irq_affinity_desc *affinity)
  69. {
  70. struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  71. if (!desc)
  72. return NULL;
  73. desc->dev = dev;
  74. desc->nvec_used = nvec;
  75. if (affinity) {
  76. desc->affinity = kmemdup_array(affinity, nvec, sizeof(*desc->affinity), GFP_KERNEL);
  77. if (!desc->affinity) {
  78. kfree(desc);
  79. return NULL;
  80. }
  81. }
  82. return desc;
  83. }
  84. static void msi_free_desc(struct msi_desc *desc)
  85. {
  86. kfree(desc->affinity);
  87. kfree(desc);
  88. }
  89. static int msi_insert_desc(struct device *dev, struct msi_desc *desc,
  90. unsigned int domid, unsigned int index)
  91. {
  92. struct msi_device_data *md = dev->msi.data;
  93. struct xarray *xa = &md->__domains[domid].store;
  94. unsigned int hwsize;
  95. int ret;
  96. hwsize = msi_domain_get_hwsize(dev, domid);
  97. if (index == MSI_ANY_INDEX) {
  98. struct xa_limit limit = { .min = 0, .max = hwsize - 1 };
  99. unsigned int index;
  100. /* Let the xarray allocate a free index within the limit */
  101. ret = xa_alloc(xa, &index, desc, limit, GFP_KERNEL);
  102. if (ret)
  103. goto fail;
  104. desc->msi_index = index;
  105. return 0;
  106. } else {
  107. if (index >= hwsize) {
  108. ret = -ERANGE;
  109. goto fail;
  110. }
  111. desc->msi_index = index;
  112. ret = xa_insert(xa, index, desc, GFP_KERNEL);
  113. if (ret)
  114. goto fail;
  115. return 0;
  116. }
  117. fail:
  118. msi_free_desc(desc);
  119. return ret;
  120. }
  121. /**
  122. * msi_domain_insert_msi_desc - Allocate and initialize a MSI descriptor and
  123. * insert it at @init_desc->msi_index
  124. *
  125. * @dev: Pointer to the device for which the descriptor is allocated
  126. * @domid: The id of the interrupt domain to which the desriptor is added
  127. * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor
  128. *
  129. * Return: 0 on success or an appropriate failure code.
  130. */
  131. int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
  132. struct msi_desc *init_desc)
  133. {
  134. struct msi_desc *desc;
  135. lockdep_assert_held(&dev->msi.data->mutex);
  136. desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity);
  137. if (!desc)
  138. return -ENOMEM;
  139. /* Copy type specific data to the new descriptor. */
  140. desc->pci = init_desc->pci;
  141. return msi_insert_desc(dev, desc, domid, init_desc->msi_index);
  142. }
  143. static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter)
  144. {
  145. switch (filter) {
  146. case MSI_DESC_ALL:
  147. return true;
  148. case MSI_DESC_NOTASSOCIATED:
  149. return !desc->irq;
  150. case MSI_DESC_ASSOCIATED:
  151. return !!desc->irq;
  152. }
  153. WARN_ON_ONCE(1);
  154. return false;
  155. }
  156. static bool msi_ctrl_valid(struct device *dev, struct msi_ctrl *ctrl)
  157. {
  158. unsigned int hwsize;
  159. if (WARN_ON_ONCE(ctrl->domid >= MSI_MAX_DEVICE_IRQDOMAINS ||
  160. (dev->msi.domain &&
  161. !dev->msi.data->__domains[ctrl->domid].domain)))
  162. return false;
  163. hwsize = msi_domain_get_hwsize(dev, ctrl->domid);
  164. if (WARN_ON_ONCE(ctrl->first > ctrl->last ||
  165. ctrl->first >= hwsize ||
  166. ctrl->last >= hwsize))
  167. return false;
  168. return true;
  169. }
  170. static void msi_domain_free_descs(struct device *dev, struct msi_ctrl *ctrl)
  171. {
  172. struct msi_desc *desc;
  173. struct xarray *xa;
  174. unsigned long idx;
  175. lockdep_assert_held(&dev->msi.data->mutex);
  176. if (!msi_ctrl_valid(dev, ctrl))
  177. return;
  178. xa = &dev->msi.data->__domains[ctrl->domid].store;
  179. xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
  180. xa_erase(xa, idx);
  181. /* Leak the descriptor when it is still referenced */
  182. if (WARN_ON_ONCE(msi_desc_match(desc, MSI_DESC_ASSOCIATED)))
  183. continue;
  184. msi_free_desc(desc);
  185. }
  186. }
  187. /**
  188. * msi_domain_free_msi_descs_range - Free a range of MSI descriptors of a device in an irqdomain
  189. * @dev: Device for which to free the descriptors
  190. * @domid: Id of the domain to operate on
  191. * @first: Index to start freeing from (inclusive)
  192. * @last: Last index to be freed (inclusive)
  193. */
  194. void msi_domain_free_msi_descs_range(struct device *dev, unsigned int domid,
  195. unsigned int first, unsigned int last)
  196. {
  197. struct msi_ctrl ctrl = {
  198. .domid = domid,
  199. .first = first,
  200. .last = last,
  201. };
  202. msi_domain_free_descs(dev, &ctrl);
  203. }
  204. /**
  205. * msi_domain_add_simple_msi_descs - Allocate and initialize MSI descriptors
  206. * @dev: Pointer to the device for which the descriptors are allocated
  207. * @ctrl: Allocation control struct
  208. *
  209. * Return: 0 on success or an appropriate failure code.
  210. */
  211. static int msi_domain_add_simple_msi_descs(struct device *dev, struct msi_ctrl *ctrl)
  212. {
  213. struct msi_desc *desc;
  214. unsigned int idx;
  215. int ret;
  216. lockdep_assert_held(&dev->msi.data->mutex);
  217. if (!msi_ctrl_valid(dev, ctrl))
  218. return -EINVAL;
  219. for (idx = ctrl->first; idx <= ctrl->last; idx++) {
  220. desc = msi_alloc_desc(dev, 1, NULL);
  221. if (!desc)
  222. goto fail_mem;
  223. ret = msi_insert_desc(dev, desc, ctrl->domid, idx);
  224. if (ret)
  225. goto fail;
  226. }
  227. return 0;
  228. fail_mem:
  229. ret = -ENOMEM;
  230. fail:
  231. msi_domain_free_descs(dev, ctrl);
  232. return ret;
  233. }
  234. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  235. {
  236. *msg = entry->msg;
  237. }
  238. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  239. {
  240. struct msi_desc *entry = irq_get_msi_desc(irq);
  241. __get_cached_msi_msg(entry, msg);
  242. }
  243. EXPORT_SYMBOL_GPL(get_cached_msi_msg);
  244. static void msi_device_data_release(struct device *dev, void *res)
  245. {
  246. struct msi_device_data *md = res;
  247. int i;
  248. for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++) {
  249. msi_remove_device_irq_domain(dev, i);
  250. WARN_ON_ONCE(!xa_empty(&md->__domains[i].store));
  251. xa_destroy(&md->__domains[i].store);
  252. }
  253. dev->msi.data = NULL;
  254. }
  255. /**
  256. * msi_setup_device_data - Setup MSI device data
  257. * @dev: Device for which MSI device data should be set up
  258. *
  259. * Return: 0 on success, appropriate error code otherwise
  260. *
  261. * This can be called more than once for @dev. If the MSI device data is
  262. * already allocated the call succeeds. The allocated memory is
  263. * automatically released when the device is destroyed.
  264. */
  265. int msi_setup_device_data(struct device *dev)
  266. {
  267. struct msi_device_data *md;
  268. int ret, i;
  269. if (dev->msi.data)
  270. return 0;
  271. md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL);
  272. if (!md)
  273. return -ENOMEM;
  274. ret = msi_sysfs_create_group(dev);
  275. if (ret) {
  276. devres_free(md);
  277. return ret;
  278. }
  279. for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++)
  280. xa_init_flags(&md->__domains[i].store, XA_FLAGS_ALLOC);
  281. /*
  282. * If @dev::msi::domain is set and is a global MSI domain, copy the
  283. * pointer into the domain array so all code can operate on domain
  284. * ids. The NULL pointer check is required to keep the legacy
  285. * architecture specific PCI/MSI support working.
  286. */
  287. if (dev->msi.domain && !irq_domain_is_msi_parent(dev->msi.domain))
  288. md->__domains[MSI_DEFAULT_DOMAIN].domain = dev->msi.domain;
  289. mutex_init(&md->mutex);
  290. dev->msi.data = md;
  291. devres_add(dev, md);
  292. return 0;
  293. }
  294. /**
  295. * msi_lock_descs - Lock the MSI descriptor storage of a device
  296. * @dev: Device to operate on
  297. */
  298. void msi_lock_descs(struct device *dev)
  299. {
  300. mutex_lock(&dev->msi.data->mutex);
  301. }
  302. EXPORT_SYMBOL_GPL(msi_lock_descs);
  303. /**
  304. * msi_unlock_descs - Unlock the MSI descriptor storage of a device
  305. * @dev: Device to operate on
  306. */
  307. void msi_unlock_descs(struct device *dev)
  308. {
  309. /* Invalidate the index which was cached by the iterator */
  310. dev->msi.data->__iter_idx = MSI_XA_MAX_INDEX;
  311. mutex_unlock(&dev->msi.data->mutex);
  312. }
  313. EXPORT_SYMBOL_GPL(msi_unlock_descs);
  314. static struct msi_desc *msi_find_desc(struct msi_device_data *md, unsigned int domid,
  315. enum msi_desc_filter filter)
  316. {
  317. struct xarray *xa = &md->__domains[domid].store;
  318. struct msi_desc *desc;
  319. xa_for_each_start(xa, md->__iter_idx, desc, md->__iter_idx) {
  320. if (msi_desc_match(desc, filter))
  321. return desc;
  322. }
  323. md->__iter_idx = MSI_XA_MAX_INDEX;
  324. return NULL;
  325. }
  326. /**
  327. * msi_domain_first_desc - Get the first MSI descriptor of an irqdomain associated to a device
  328. * @dev: Device to operate on
  329. * @domid: The id of the interrupt domain which should be walked.
  330. * @filter: Descriptor state filter
  331. *
  332. * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
  333. * must be invoked before the call.
  334. *
  335. * Return: Pointer to the first MSI descriptor matching the search
  336. * criteria, NULL if none found.
  337. */
  338. struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
  339. enum msi_desc_filter filter)
  340. {
  341. struct msi_device_data *md = dev->msi.data;
  342. if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
  343. return NULL;
  344. lockdep_assert_held(&md->mutex);
  345. md->__iter_idx = 0;
  346. return msi_find_desc(md, domid, filter);
  347. }
  348. EXPORT_SYMBOL_GPL(msi_domain_first_desc);
  349. /**
  350. * msi_next_desc - Get the next MSI descriptor of a device
  351. * @dev: Device to operate on
  352. * @domid: The id of the interrupt domain which should be walked.
  353. * @filter: Descriptor state filter
  354. *
  355. * The first invocation of msi_next_desc() has to be preceeded by a
  356. * successful invocation of __msi_first_desc(). Consecutive invocations are
  357. * only valid if the previous one was successful. All these operations have
  358. * to be done within the same MSI mutex held region.
  359. *
  360. * Return: Pointer to the next MSI descriptor matching the search
  361. * criteria, NULL if none found.
  362. */
  363. struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
  364. enum msi_desc_filter filter)
  365. {
  366. struct msi_device_data *md = dev->msi.data;
  367. if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
  368. return NULL;
  369. lockdep_assert_held(&md->mutex);
  370. if (md->__iter_idx >= (unsigned long)MSI_MAX_INDEX)
  371. return NULL;
  372. md->__iter_idx++;
  373. return msi_find_desc(md, domid, filter);
  374. }
  375. EXPORT_SYMBOL_GPL(msi_next_desc);
  376. /**
  377. * msi_domain_get_virq - Lookup the Linux interrupt number for a MSI index on a interrupt domain
  378. * @dev: Device to operate on
  379. * @domid: Domain ID of the interrupt domain associated to the device
  380. * @index: MSI interrupt index to look for (0-based)
  381. *
  382. * Return: The Linux interrupt number on success (> 0), 0 if not found
  383. */
  384. unsigned int msi_domain_get_virq(struct device *dev, unsigned int domid, unsigned int index)
  385. {
  386. struct msi_desc *desc;
  387. unsigned int ret = 0;
  388. bool pcimsi = false;
  389. struct xarray *xa;
  390. if (!dev->msi.data)
  391. return 0;
  392. if (WARN_ON_ONCE(index > MSI_MAX_INDEX || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
  393. return 0;
  394. /* This check is only valid for the PCI default MSI domain */
  395. if (dev_is_pci(dev) && domid == MSI_DEFAULT_DOMAIN)
  396. pcimsi = to_pci_dev(dev)->msi_enabled;
  397. msi_lock_descs(dev);
  398. xa = &dev->msi.data->__domains[domid].store;
  399. desc = xa_load(xa, pcimsi ? 0 : index);
  400. if (desc && desc->irq) {
  401. /*
  402. * PCI-MSI has only one descriptor for multiple interrupts.
  403. * PCI-MSIX and platform MSI use a descriptor per
  404. * interrupt.
  405. */
  406. if (pcimsi) {
  407. if (index < desc->nvec_used)
  408. ret = desc->irq + index;
  409. } else {
  410. ret = desc->irq;
  411. }
  412. }
  413. msi_unlock_descs(dev);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(msi_domain_get_virq);
  417. #ifdef CONFIG_SYSFS
  418. static struct attribute *msi_dev_attrs[] = {
  419. NULL
  420. };
  421. static const struct attribute_group msi_irqs_group = {
  422. .name = "msi_irqs",
  423. .attrs = msi_dev_attrs,
  424. };
  425. static inline int msi_sysfs_create_group(struct device *dev)
  426. {
  427. return devm_device_add_group(dev, &msi_irqs_group);
  428. }
  429. static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
  430. char *buf)
  431. {
  432. /* MSI vs. MSIX is per device not per interrupt */
  433. bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false;
  434. return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi");
  435. }
  436. static void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc)
  437. {
  438. struct device_attribute *attrs = desc->sysfs_attrs;
  439. int i;
  440. if (!attrs)
  441. return;
  442. desc->sysfs_attrs = NULL;
  443. for (i = 0; i < desc->nvec_used; i++) {
  444. if (attrs[i].show)
  445. sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
  446. kfree(attrs[i].attr.name);
  447. }
  448. kfree(attrs);
  449. }
  450. static int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc)
  451. {
  452. struct device_attribute *attrs;
  453. int ret, i;
  454. attrs = kcalloc(desc->nvec_used, sizeof(*attrs), GFP_KERNEL);
  455. if (!attrs)
  456. return -ENOMEM;
  457. desc->sysfs_attrs = attrs;
  458. for (i = 0; i < desc->nvec_used; i++) {
  459. sysfs_attr_init(&attrs[i].attr);
  460. attrs[i].attr.name = kasprintf(GFP_KERNEL, "%d", desc->irq + i);
  461. if (!attrs[i].attr.name) {
  462. ret = -ENOMEM;
  463. goto fail;
  464. }
  465. attrs[i].attr.mode = 0444;
  466. attrs[i].show = msi_mode_show;
  467. ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
  468. if (ret) {
  469. attrs[i].show = NULL;
  470. goto fail;
  471. }
  472. }
  473. return 0;
  474. fail:
  475. msi_sysfs_remove_desc(dev, desc);
  476. return ret;
  477. }
  478. #if defined(CONFIG_PCI_MSI_ARCH_FALLBACKS) || defined(CONFIG_PCI_XEN)
  479. /**
  480. * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device
  481. * @dev: The device (PCI, platform etc) which will get sysfs entries
  482. */
  483. int msi_device_populate_sysfs(struct device *dev)
  484. {
  485. struct msi_desc *desc;
  486. int ret;
  487. msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) {
  488. if (desc->sysfs_attrs)
  489. continue;
  490. ret = msi_sysfs_populate_desc(dev, desc);
  491. if (ret)
  492. return ret;
  493. }
  494. return 0;
  495. }
  496. /**
  497. * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device
  498. * @dev: The device (PCI, platform etc) for which to remove
  499. * sysfs entries
  500. */
  501. void msi_device_destroy_sysfs(struct device *dev)
  502. {
  503. struct msi_desc *desc;
  504. msi_for_each_desc(desc, dev, MSI_DESC_ALL)
  505. msi_sysfs_remove_desc(dev, desc);
  506. }
  507. #endif /* CONFIG_PCI_MSI_ARCH_FALLBACK || CONFIG_PCI_XEN */
  508. #else /* CONFIG_SYSFS */
  509. static inline int msi_sysfs_create_group(struct device *dev) { return 0; }
  510. static inline int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) { return 0; }
  511. static inline void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) { }
  512. #endif /* !CONFIG_SYSFS */
  513. static struct irq_domain *msi_get_device_domain(struct device *dev, unsigned int domid)
  514. {
  515. struct irq_domain *domain;
  516. lockdep_assert_held(&dev->msi.data->mutex);
  517. if (WARN_ON_ONCE(domid >= MSI_MAX_DEVICE_IRQDOMAINS))
  518. return NULL;
  519. domain = dev->msi.data->__domains[domid].domain;
  520. if (!domain)
  521. return NULL;
  522. if (WARN_ON_ONCE(irq_domain_is_msi_parent(domain)))
  523. return NULL;
  524. return domain;
  525. }
  526. static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid)
  527. {
  528. struct msi_domain_info *info;
  529. struct irq_domain *domain;
  530. domain = msi_get_device_domain(dev, domid);
  531. if (domain) {
  532. info = domain->host_data;
  533. return info->hwsize;
  534. }
  535. /* No domain, default to MSI_XA_DOMAIN_SIZE */
  536. return MSI_XA_DOMAIN_SIZE;
  537. }
  538. static inline void irq_chip_write_msi_msg(struct irq_data *data,
  539. struct msi_msg *msg)
  540. {
  541. data->chip->irq_write_msi_msg(data, msg);
  542. }
  543. static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
  544. {
  545. struct msi_domain_info *info = domain->host_data;
  546. /*
  547. * If the MSI provider has messed with the second message and
  548. * not advertized that it is level-capable, signal the breakage.
  549. */
  550. WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
  551. (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
  552. (msg[1].address_lo || msg[1].address_hi || msg[1].data));
  553. }
  554. /**
  555. * msi_domain_set_affinity - Generic affinity setter function for MSI domains
  556. * @irq_data: The irq data associated to the interrupt
  557. * @mask: The affinity mask to set
  558. * @force: Flag to enforce setting (disable online checks)
  559. *
  560. * Intended to be used by MSI interrupt controllers which are
  561. * implemented with hierarchical domains.
  562. *
  563. * Return: IRQ_SET_MASK_* result code
  564. */
  565. int msi_domain_set_affinity(struct irq_data *irq_data,
  566. const struct cpumask *mask, bool force)
  567. {
  568. struct irq_data *parent = irq_data->parent_data;
  569. struct msi_msg msg[2] = { [1] = { }, };
  570. int ret;
  571. ret = parent->chip->irq_set_affinity(parent, mask, force);
  572. if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
  573. BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
  574. msi_check_level(irq_data->domain, msg);
  575. irq_chip_write_msi_msg(irq_data, msg);
  576. }
  577. return ret;
  578. }
  579. static int msi_domain_activate(struct irq_domain *domain,
  580. struct irq_data *irq_data, bool early)
  581. {
  582. struct msi_msg msg[2] = { [1] = { }, };
  583. BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
  584. msi_check_level(irq_data->domain, msg);
  585. irq_chip_write_msi_msg(irq_data, msg);
  586. return 0;
  587. }
  588. static void msi_domain_deactivate(struct irq_domain *domain,
  589. struct irq_data *irq_data)
  590. {
  591. struct msi_msg msg[2];
  592. memset(msg, 0, sizeof(msg));
  593. irq_chip_write_msi_msg(irq_data, msg);
  594. }
  595. static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
  596. unsigned int nr_irqs, void *arg)
  597. {
  598. struct msi_domain_info *info = domain->host_data;
  599. struct msi_domain_ops *ops = info->ops;
  600. irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
  601. int i, ret;
  602. if (irq_find_mapping(domain, hwirq) > 0)
  603. return -EEXIST;
  604. if (domain->parent) {
  605. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  606. if (ret < 0)
  607. return ret;
  608. }
  609. for (i = 0; i < nr_irqs; i++) {
  610. ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
  611. if (ret < 0) {
  612. if (ops->msi_free) {
  613. for (i--; i >= 0; i--)
  614. ops->msi_free(domain, info, virq + i);
  615. }
  616. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  617. return ret;
  618. }
  619. }
  620. return 0;
  621. }
  622. static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
  623. unsigned int nr_irqs)
  624. {
  625. struct msi_domain_info *info = domain->host_data;
  626. int i;
  627. if (info->ops->msi_free) {
  628. for (i = 0; i < nr_irqs; i++)
  629. info->ops->msi_free(domain, info, virq + i);
  630. }
  631. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  632. }
  633. static int msi_domain_translate(struct irq_domain *domain, struct irq_fwspec *fwspec,
  634. irq_hw_number_t *hwirq, unsigned int *type)
  635. {
  636. struct msi_domain_info *info = domain->host_data;
  637. /*
  638. * This will catch allocations through the regular irqdomain path except
  639. * for MSI domains which really support this, e.g. MBIGEN.
  640. */
  641. if (!info->ops->msi_translate)
  642. return -ENOTSUPP;
  643. return info->ops->msi_translate(domain, fwspec, hwirq, type);
  644. }
  645. static const struct irq_domain_ops msi_domain_ops = {
  646. .alloc = msi_domain_alloc,
  647. .free = msi_domain_free,
  648. .activate = msi_domain_activate,
  649. .deactivate = msi_domain_deactivate,
  650. .translate = msi_domain_translate,
  651. };
  652. static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
  653. msi_alloc_info_t *arg)
  654. {
  655. return arg->hwirq;
  656. }
  657. static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
  658. int nvec, msi_alloc_info_t *arg)
  659. {
  660. memset(arg, 0, sizeof(*arg));
  661. return 0;
  662. }
  663. static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
  664. struct msi_desc *desc)
  665. {
  666. arg->desc = desc;
  667. }
  668. static int msi_domain_ops_init(struct irq_domain *domain,
  669. struct msi_domain_info *info,
  670. unsigned int virq, irq_hw_number_t hwirq,
  671. msi_alloc_info_t *arg)
  672. {
  673. irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
  674. info->chip_data);
  675. if (info->handler && info->handler_name) {
  676. __irq_set_handler(virq, info->handler, 0, info->handler_name);
  677. if (info->handler_data)
  678. irq_set_handler_data(virq, info->handler_data);
  679. }
  680. return 0;
  681. }
  682. static struct msi_domain_ops msi_domain_ops_default = {
  683. .get_hwirq = msi_domain_ops_get_hwirq,
  684. .msi_init = msi_domain_ops_init,
  685. .msi_prepare = msi_domain_ops_prepare,
  686. .set_desc = msi_domain_ops_set_desc,
  687. };
  688. static void msi_domain_update_dom_ops(struct msi_domain_info *info)
  689. {
  690. struct msi_domain_ops *ops = info->ops;
  691. if (ops == NULL) {
  692. info->ops = &msi_domain_ops_default;
  693. return;
  694. }
  695. if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
  696. return;
  697. if (ops->get_hwirq == NULL)
  698. ops->get_hwirq = msi_domain_ops_default.get_hwirq;
  699. if (ops->msi_init == NULL)
  700. ops->msi_init = msi_domain_ops_default.msi_init;
  701. if (ops->msi_prepare == NULL)
  702. ops->msi_prepare = msi_domain_ops_default.msi_prepare;
  703. if (ops->set_desc == NULL)
  704. ops->set_desc = msi_domain_ops_default.set_desc;
  705. }
  706. static void msi_domain_update_chip_ops(struct msi_domain_info *info)
  707. {
  708. struct irq_chip *chip = info->chip;
  709. BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
  710. if (!chip->irq_set_affinity && !(info->flags & MSI_FLAG_NO_AFFINITY))
  711. chip->irq_set_affinity = msi_domain_set_affinity;
  712. }
  713. static struct irq_domain *__msi_create_irq_domain(struct fwnode_handle *fwnode,
  714. struct msi_domain_info *info,
  715. unsigned int flags,
  716. struct irq_domain *parent)
  717. {
  718. struct irq_domain *domain;
  719. if (info->hwsize > MSI_XA_DOMAIN_SIZE)
  720. return NULL;
  721. /*
  722. * Hardware size 0 is valid for backwards compatibility and for
  723. * domains which are not backed by a hardware table. Grant the
  724. * maximum index space.
  725. */
  726. if (!info->hwsize)
  727. info->hwsize = MSI_XA_DOMAIN_SIZE;
  728. msi_domain_update_dom_ops(info);
  729. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  730. msi_domain_update_chip_ops(info);
  731. domain = irq_domain_create_hierarchy(parent, flags | IRQ_DOMAIN_FLAG_MSI, 0,
  732. fwnode, &msi_domain_ops, info);
  733. if (domain) {
  734. irq_domain_update_bus_token(domain, info->bus_token);
  735. if (info->flags & MSI_FLAG_PARENT_PM_DEV)
  736. domain->pm_dev = parent->pm_dev;
  737. }
  738. return domain;
  739. }
  740. /**
  741. * msi_create_irq_domain - Create an MSI interrupt domain
  742. * @fwnode: Optional fwnode of the interrupt controller
  743. * @info: MSI domain info
  744. * @parent: Parent irq domain
  745. *
  746. * Return: pointer to the created &struct irq_domain or %NULL on failure
  747. */
  748. struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
  749. struct msi_domain_info *info,
  750. struct irq_domain *parent)
  751. {
  752. return __msi_create_irq_domain(fwnode, info, 0, parent);
  753. }
  754. /**
  755. * msi_parent_init_dev_msi_info - Delegate initialization of device MSI info down
  756. * in the domain hierarchy
  757. * @dev: The device for which the domain should be created
  758. * @domain: The domain in the hierarchy this op is being called on
  759. * @msi_parent_domain: The IRQ_DOMAIN_FLAG_MSI_PARENT domain for the child to
  760. * be created
  761. * @msi_child_info: The MSI domain info of the IRQ_DOMAIN_FLAG_MSI_DEVICE
  762. * domain to be created
  763. *
  764. * Return: true on success, false otherwise
  765. *
  766. * This is the most complex problem of per device MSI domains and the
  767. * underlying interrupt domain hierarchy:
  768. *
  769. * The device domain to be initialized requests the broadest feature set
  770. * possible and the underlying domain hierarchy puts restrictions on it.
  771. *
  772. * That's trivial for a simple parent->child relationship, but it gets
  773. * interesting with an intermediate domain: root->parent->child. The
  774. * intermediate 'parent' can expand the capabilities which the 'root'
  775. * domain is providing. So that creates a classic hen and egg problem:
  776. * Which entity is doing the restrictions/expansions?
  777. *
  778. * One solution is to let the root domain handle the initialization that's
  779. * why there is the @domain and the @msi_parent_domain pointer.
  780. */
  781. bool msi_parent_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
  782. struct irq_domain *msi_parent_domain,
  783. struct msi_domain_info *msi_child_info)
  784. {
  785. struct irq_domain *parent = domain->parent;
  786. if (WARN_ON_ONCE(!parent || !parent->msi_parent_ops ||
  787. !parent->msi_parent_ops->init_dev_msi_info))
  788. return false;
  789. return parent->msi_parent_ops->init_dev_msi_info(dev, parent, msi_parent_domain,
  790. msi_child_info);
  791. }
  792. /**
  793. * msi_create_device_irq_domain - Create a device MSI interrupt domain
  794. * @dev: Pointer to the device
  795. * @domid: Domain id
  796. * @template: MSI domain info bundle used as template
  797. * @hwsize: Maximum number of MSI table entries (0 if unknown or unlimited)
  798. * @domain_data: Optional pointer to domain specific data which is set in
  799. * msi_domain_info::data
  800. * @chip_data: Optional pointer to chip specific data which is set in
  801. * msi_domain_info::chip_data
  802. *
  803. * Return: True on success, false otherwise
  804. *
  805. * There is no firmware node required for this interface because the per
  806. * device domains are software constructs which are actually closer to the
  807. * hardware reality than any firmware can describe them.
  808. *
  809. * The domain name and the irq chip name for a MSI device domain are
  810. * composed by: "$(PREFIX)$(CHIPNAME)-$(DEVNAME)"
  811. *
  812. * $PREFIX: Optional prefix provided by the underlying MSI parent domain
  813. * via msi_parent_ops::prefix. If that pointer is NULL the prefix
  814. * is empty.
  815. * $CHIPNAME: The name of the irq_chip in @template
  816. * $DEVNAME: The name of the device
  817. *
  818. * This results in understandable chip names and hardware interrupt numbers
  819. * in e.g. /proc/interrupts
  820. *
  821. * PCI-MSI-0000:00:1c.0 0-edge Parent domain has no prefix
  822. * IR-PCI-MSI-0000:00:1c.4 0-edge Same with interrupt remapping prefix 'IR-'
  823. *
  824. * IR-PCI-MSIX-0000:3d:00.0 0-edge Hardware interrupt numbers reflect
  825. * IR-PCI-MSIX-0000:3d:00.0 1-edge the real MSI-X index on that device
  826. * IR-PCI-MSIX-0000:3d:00.0 2-edge
  827. *
  828. * On IMS domains the hardware interrupt number is either a table entry
  829. * index or a purely software managed index but it is guaranteed to be
  830. * unique.
  831. *
  832. * The domain pointer is stored in @dev::msi::data::__irqdomains[]. All
  833. * subsequent operations on the domain depend on the domain id.
  834. *
  835. * The domain is automatically freed when the device is removed via devres
  836. * in the context of @dev::msi::data freeing, but it can also be
  837. * independently removed via @msi_remove_device_irq_domain().
  838. */
  839. bool msi_create_device_irq_domain(struct device *dev, unsigned int domid,
  840. const struct msi_domain_template *template,
  841. unsigned int hwsize, void *domain_data,
  842. void *chip_data)
  843. {
  844. struct irq_domain *domain, *parent = dev->msi.domain;
  845. struct fwnode_handle *fwnode, *fwnalloced = NULL;
  846. struct msi_domain_template *bundle;
  847. const struct msi_parent_ops *pops;
  848. if (!irq_domain_is_msi_parent(parent))
  849. return false;
  850. if (domid >= MSI_MAX_DEVICE_IRQDOMAINS)
  851. return false;
  852. bundle = kmemdup(template, sizeof(*bundle), GFP_KERNEL);
  853. if (!bundle)
  854. return false;
  855. bundle->info.hwsize = hwsize;
  856. bundle->info.chip = &bundle->chip;
  857. bundle->info.ops = &bundle->ops;
  858. bundle->info.data = domain_data;
  859. bundle->info.chip_data = chip_data;
  860. pops = parent->msi_parent_ops;
  861. snprintf(bundle->name, sizeof(bundle->name), "%s%s-%s",
  862. pops->prefix ? : "", bundle->chip.name, dev_name(dev));
  863. bundle->chip.name = bundle->name;
  864. /*
  865. * Using the device firmware node is required for wire to MSI
  866. * device domains so that the existing firmware results in a domain
  867. * match.
  868. * All other device domains like PCI/MSI use the named firmware
  869. * node as they are not guaranteed to have a fwnode. They are never
  870. * looked up and always handled in the context of the device.
  871. */
  872. if (bundle->info.flags & MSI_FLAG_USE_DEV_FWNODE)
  873. fwnode = dev->fwnode;
  874. else
  875. fwnode = fwnalloced = irq_domain_alloc_named_fwnode(bundle->name);
  876. if (!fwnode)
  877. goto free_bundle;
  878. if (msi_setup_device_data(dev))
  879. goto free_fwnode;
  880. msi_lock_descs(dev);
  881. if (WARN_ON_ONCE(msi_get_device_domain(dev, domid)))
  882. goto fail;
  883. if (!pops->init_dev_msi_info(dev, parent, parent, &bundle->info))
  884. goto fail;
  885. domain = __msi_create_irq_domain(fwnode, &bundle->info, IRQ_DOMAIN_FLAG_MSI_DEVICE, parent);
  886. if (!domain)
  887. goto fail;
  888. domain->dev = dev;
  889. dev->msi.data->__domains[domid].domain = domain;
  890. msi_unlock_descs(dev);
  891. return true;
  892. fail:
  893. msi_unlock_descs(dev);
  894. free_fwnode:
  895. irq_domain_free_fwnode(fwnalloced);
  896. free_bundle:
  897. kfree(bundle);
  898. return false;
  899. }
  900. /**
  901. * msi_remove_device_irq_domain - Free a device MSI interrupt domain
  902. * @dev: Pointer to the device
  903. * @domid: Domain id
  904. */
  905. void msi_remove_device_irq_domain(struct device *dev, unsigned int domid)
  906. {
  907. struct fwnode_handle *fwnode = NULL;
  908. struct msi_domain_info *info;
  909. struct irq_domain *domain;
  910. msi_lock_descs(dev);
  911. domain = msi_get_device_domain(dev, domid);
  912. if (!domain || !irq_domain_is_msi_device(domain))
  913. goto unlock;
  914. dev->msi.data->__domains[domid].domain = NULL;
  915. info = domain->host_data;
  916. if (irq_domain_is_msi_device(domain))
  917. fwnode = domain->fwnode;
  918. irq_domain_remove(domain);
  919. irq_domain_free_fwnode(fwnode);
  920. kfree(container_of(info, struct msi_domain_template, info));
  921. unlock:
  922. msi_unlock_descs(dev);
  923. }
  924. /**
  925. * msi_match_device_irq_domain - Match a device irq domain against a bus token
  926. * @dev: Pointer to the device
  927. * @domid: Domain id
  928. * @bus_token: Bus token to match against the domain bus token
  929. *
  930. * Return: True if device domain exists and bus tokens match.
  931. */
  932. bool msi_match_device_irq_domain(struct device *dev, unsigned int domid,
  933. enum irq_domain_bus_token bus_token)
  934. {
  935. struct msi_domain_info *info;
  936. struct irq_domain *domain;
  937. bool ret = false;
  938. msi_lock_descs(dev);
  939. domain = msi_get_device_domain(dev, domid);
  940. if (domain && irq_domain_is_msi_device(domain)) {
  941. info = domain->host_data;
  942. ret = info->bus_token == bus_token;
  943. }
  944. msi_unlock_descs(dev);
  945. return ret;
  946. }
  947. static int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
  948. int nvec, msi_alloc_info_t *arg)
  949. {
  950. struct msi_domain_info *info = domain->host_data;
  951. struct msi_domain_ops *ops = info->ops;
  952. return ops->msi_prepare(domain, dev, nvec, arg);
  953. }
  954. /*
  955. * Carefully check whether the device can use reservation mode. If
  956. * reservation mode is enabled then the early activation will assign a
  957. * dummy vector to the device. If the PCI/MSI device does not support
  958. * masking of the entry then this can result in spurious interrupts when
  959. * the device driver is not absolutely careful. But even then a malfunction
  960. * of the hardware could result in a spurious interrupt on the dummy vector
  961. * and render the device unusable. If the entry can be masked then the core
  962. * logic will prevent the spurious interrupt and reservation mode can be
  963. * used. For now reservation mode is restricted to PCI/MSI.
  964. */
  965. static bool msi_check_reservation_mode(struct irq_domain *domain,
  966. struct msi_domain_info *info,
  967. struct device *dev)
  968. {
  969. struct msi_desc *desc;
  970. switch(domain->bus_token) {
  971. case DOMAIN_BUS_PCI_MSI:
  972. case DOMAIN_BUS_PCI_DEVICE_MSI:
  973. case DOMAIN_BUS_PCI_DEVICE_MSIX:
  974. case DOMAIN_BUS_VMD_MSI:
  975. break;
  976. default:
  977. return false;
  978. }
  979. if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
  980. return false;
  981. if (info->flags & MSI_FLAG_NO_MASK)
  982. return false;
  983. /*
  984. * Checking the first MSI descriptor is sufficient. MSIX supports
  985. * masking and MSI does so when the can_mask attribute is set.
  986. */
  987. desc = msi_first_desc(dev, MSI_DESC_ALL);
  988. return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask;
  989. }
  990. static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc,
  991. int allocated)
  992. {
  993. switch(domain->bus_token) {
  994. case DOMAIN_BUS_PCI_MSI:
  995. case DOMAIN_BUS_PCI_DEVICE_MSI:
  996. case DOMAIN_BUS_PCI_DEVICE_MSIX:
  997. case DOMAIN_BUS_VMD_MSI:
  998. if (IS_ENABLED(CONFIG_PCI_MSI))
  999. break;
  1000. fallthrough;
  1001. default:
  1002. return -ENOSPC;
  1003. }
  1004. /* Let a failed PCI multi MSI allocation retry */
  1005. if (desc->nvec_used > 1)
  1006. return 1;
  1007. /* If there was a successful allocation let the caller know */
  1008. return allocated ? allocated : -ENOSPC;
  1009. }
  1010. #define VIRQ_CAN_RESERVE 0x01
  1011. #define VIRQ_ACTIVATE 0x02
  1012. static int msi_init_virq(struct irq_domain *domain, int virq, unsigned int vflags)
  1013. {
  1014. struct irq_data *irqd = irq_domain_get_irq_data(domain, virq);
  1015. int ret;
  1016. if (!(vflags & VIRQ_CAN_RESERVE)) {
  1017. irqd_clr_can_reserve(irqd);
  1018. /*
  1019. * If the interrupt is managed but no CPU is available to
  1020. * service it, shut it down until better times. Note that
  1021. * we only do this on the !RESERVE path as x86 (the only
  1022. * architecture using this flag) deals with this in a
  1023. * different way by using a catch-all vector.
  1024. */
  1025. if ((vflags & VIRQ_ACTIVATE) &&
  1026. irqd_affinity_is_managed(irqd) &&
  1027. !cpumask_intersects(irq_data_get_affinity_mask(irqd),
  1028. cpu_online_mask)) {
  1029. irqd_set_managed_shutdown(irqd);
  1030. return 0;
  1031. }
  1032. }
  1033. if (!(vflags & VIRQ_ACTIVATE))
  1034. return 0;
  1035. ret = irq_domain_activate_irq(irqd, vflags & VIRQ_CAN_RESERVE);
  1036. if (ret)
  1037. return ret;
  1038. /*
  1039. * If the interrupt uses reservation mode, clear the activated bit
  1040. * so request_irq() will assign the final vector.
  1041. */
  1042. if (vflags & VIRQ_CAN_RESERVE)
  1043. irqd_clr_activated(irqd);
  1044. return 0;
  1045. }
  1046. static int __msi_domain_alloc_irqs(struct device *dev, struct irq_domain *domain,
  1047. struct msi_ctrl *ctrl)
  1048. {
  1049. struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store;
  1050. struct msi_domain_info *info = domain->host_data;
  1051. struct msi_domain_ops *ops = info->ops;
  1052. unsigned int vflags = 0, allocated = 0;
  1053. msi_alloc_info_t arg = { };
  1054. struct msi_desc *desc;
  1055. unsigned long idx;
  1056. int i, ret, virq;
  1057. ret = msi_domain_prepare_irqs(domain, dev, ctrl->nirqs, &arg);
  1058. if (ret)
  1059. return ret;
  1060. /*
  1061. * This flag is set by the PCI layer as we need to activate
  1062. * the MSI entries before the PCI layer enables MSI in the
  1063. * card. Otherwise the card latches a random msi message.
  1064. */
  1065. if (info->flags & MSI_FLAG_ACTIVATE_EARLY)
  1066. vflags |= VIRQ_ACTIVATE;
  1067. /*
  1068. * Interrupt can use a reserved vector and will not occupy
  1069. * a real device vector until the interrupt is requested.
  1070. */
  1071. if (msi_check_reservation_mode(domain, info, dev))
  1072. vflags |= VIRQ_CAN_RESERVE;
  1073. xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
  1074. if (!msi_desc_match(desc, MSI_DESC_NOTASSOCIATED))
  1075. continue;
  1076. /* This should return -ECONFUSED... */
  1077. if (WARN_ON_ONCE(allocated >= ctrl->nirqs))
  1078. return -EINVAL;
  1079. if (ops->prepare_desc)
  1080. ops->prepare_desc(domain, &arg, desc);
  1081. ops->set_desc(&arg, desc);
  1082. virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
  1083. dev_to_node(dev), &arg, false,
  1084. desc->affinity);
  1085. if (virq < 0)
  1086. return msi_handle_pci_fail(domain, desc, allocated);
  1087. for (i = 0; i < desc->nvec_used; i++) {
  1088. irq_set_msi_desc_off(virq, i, desc);
  1089. irq_debugfs_copy_devname(virq + i, dev);
  1090. ret = msi_init_virq(domain, virq + i, vflags);
  1091. if (ret)
  1092. return ret;
  1093. }
  1094. if (info->flags & MSI_FLAG_DEV_SYSFS) {
  1095. ret = msi_sysfs_populate_desc(dev, desc);
  1096. if (ret)
  1097. return ret;
  1098. }
  1099. allocated++;
  1100. }
  1101. return 0;
  1102. }
  1103. static int msi_domain_alloc_simple_msi_descs(struct device *dev,
  1104. struct msi_domain_info *info,
  1105. struct msi_ctrl *ctrl)
  1106. {
  1107. if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS))
  1108. return 0;
  1109. return msi_domain_add_simple_msi_descs(dev, ctrl);
  1110. }
  1111. static int __msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl)
  1112. {
  1113. struct msi_domain_info *info;
  1114. struct msi_domain_ops *ops;
  1115. struct irq_domain *domain;
  1116. int ret;
  1117. if (!msi_ctrl_valid(dev, ctrl))
  1118. return -EINVAL;
  1119. domain = msi_get_device_domain(dev, ctrl->domid);
  1120. if (!domain)
  1121. return -ENODEV;
  1122. info = domain->host_data;
  1123. ret = msi_domain_alloc_simple_msi_descs(dev, info, ctrl);
  1124. if (ret)
  1125. return ret;
  1126. ops = info->ops;
  1127. if (ops->domain_alloc_irqs)
  1128. return ops->domain_alloc_irqs(domain, dev, ctrl->nirqs);
  1129. return __msi_domain_alloc_irqs(dev, domain, ctrl);
  1130. }
  1131. static int msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl)
  1132. {
  1133. int ret = __msi_domain_alloc_locked(dev, ctrl);
  1134. if (ret)
  1135. msi_domain_free_locked(dev, ctrl);
  1136. return ret;
  1137. }
  1138. /**
  1139. * msi_domain_alloc_irqs_range_locked - Allocate interrupts from a MSI interrupt domain
  1140. * @dev: Pointer to device struct of the device for which the interrupts
  1141. * are allocated
  1142. * @domid: Id of the interrupt domain to operate on
  1143. * @first: First index to allocate (inclusive)
  1144. * @last: Last index to allocate (inclusive)
  1145. *
  1146. * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
  1147. * pair. Use this for MSI irqdomains which implement their own descriptor
  1148. * allocation/free.
  1149. *
  1150. * Return: %0 on success or an error code.
  1151. */
  1152. int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
  1153. unsigned int first, unsigned int last)
  1154. {
  1155. struct msi_ctrl ctrl = {
  1156. .domid = domid,
  1157. .first = first,
  1158. .last = last,
  1159. .nirqs = last + 1 - first,
  1160. };
  1161. return msi_domain_alloc_locked(dev, &ctrl);
  1162. }
  1163. /**
  1164. * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain
  1165. * @dev: Pointer to device struct of the device for which the interrupts
  1166. * are allocated
  1167. * @domid: Id of the interrupt domain to operate on
  1168. * @first: First index to allocate (inclusive)
  1169. * @last: Last index to allocate (inclusive)
  1170. *
  1171. * Return: %0 on success or an error code.
  1172. */
  1173. int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
  1174. unsigned int first, unsigned int last)
  1175. {
  1176. int ret;
  1177. msi_lock_descs(dev);
  1178. ret = msi_domain_alloc_irqs_range_locked(dev, domid, first, last);
  1179. msi_unlock_descs(dev);
  1180. return ret;
  1181. }
  1182. EXPORT_SYMBOL_GPL(msi_domain_alloc_irqs_range);
  1183. /**
  1184. * msi_domain_alloc_irqs_all_locked - Allocate all interrupts from a MSI interrupt domain
  1185. *
  1186. * @dev: Pointer to device struct of the device for which the interrupts
  1187. * are allocated
  1188. * @domid: Id of the interrupt domain to operate on
  1189. * @nirqs: The number of interrupts to allocate
  1190. *
  1191. * This function scans all MSI descriptors of the MSI domain and allocates interrupts
  1192. * for all unassigned ones. That function is to be used for MSI domain usage where
  1193. * the descriptor allocation is handled at the call site, e.g. PCI/MSI[X].
  1194. *
  1195. * Return: %0 on success or an error code.
  1196. */
  1197. int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs)
  1198. {
  1199. struct msi_ctrl ctrl = {
  1200. .domid = domid,
  1201. .first = 0,
  1202. .last = msi_domain_get_hwsize(dev, domid) - 1,
  1203. .nirqs = nirqs,
  1204. };
  1205. return msi_domain_alloc_locked(dev, &ctrl);
  1206. }
  1207. static struct msi_map __msi_domain_alloc_irq_at(struct device *dev, unsigned int domid,
  1208. unsigned int index,
  1209. const struct irq_affinity_desc *affdesc,
  1210. union msi_instance_cookie *icookie)
  1211. {
  1212. struct msi_ctrl ctrl = { .domid = domid, .nirqs = 1, };
  1213. struct irq_domain *domain;
  1214. struct msi_map map = { };
  1215. struct msi_desc *desc;
  1216. int ret;
  1217. domain = msi_get_device_domain(dev, domid);
  1218. if (!domain) {
  1219. map.index = -ENODEV;
  1220. return map;
  1221. }
  1222. desc = msi_alloc_desc(dev, 1, affdesc);
  1223. if (!desc) {
  1224. map.index = -ENOMEM;
  1225. return map;
  1226. }
  1227. if (icookie)
  1228. desc->data.icookie = *icookie;
  1229. ret = msi_insert_desc(dev, desc, domid, index);
  1230. if (ret) {
  1231. map.index = ret;
  1232. return map;
  1233. }
  1234. ctrl.first = ctrl.last = desc->msi_index;
  1235. ret = __msi_domain_alloc_irqs(dev, domain, &ctrl);
  1236. if (ret) {
  1237. map.index = ret;
  1238. msi_domain_free_locked(dev, &ctrl);
  1239. } else {
  1240. map.index = desc->msi_index;
  1241. map.virq = desc->irq;
  1242. }
  1243. return map;
  1244. }
  1245. /**
  1246. * msi_domain_alloc_irq_at - Allocate an interrupt from a MSI interrupt domain at
  1247. * a given index - or at the next free index
  1248. *
  1249. * @dev: Pointer to device struct of the device for which the interrupts
  1250. * are allocated
  1251. * @domid: Id of the interrupt domain to operate on
  1252. * @index: Index for allocation. If @index == %MSI_ANY_INDEX the allocation
  1253. * uses the next free index.
  1254. * @affdesc: Optional pointer to an interrupt affinity descriptor structure
  1255. * @icookie: Optional pointer to a domain specific per instance cookie. If
  1256. * non-NULL the content of the cookie is stored in msi_desc::data.
  1257. * Must be NULL for MSI-X allocations
  1258. *
  1259. * This requires a MSI interrupt domain which lets the core code manage the
  1260. * MSI descriptors.
  1261. *
  1262. * Return: struct msi_map
  1263. *
  1264. * On success msi_map::index contains the allocated index number and
  1265. * msi_map::virq the corresponding Linux interrupt number
  1266. *
  1267. * On failure msi_map::index contains the error code and msi_map::virq
  1268. * is %0.
  1269. */
  1270. struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, unsigned int index,
  1271. const struct irq_affinity_desc *affdesc,
  1272. union msi_instance_cookie *icookie)
  1273. {
  1274. struct msi_map map;
  1275. msi_lock_descs(dev);
  1276. map = __msi_domain_alloc_irq_at(dev, domid, index, affdesc, icookie);
  1277. msi_unlock_descs(dev);
  1278. return map;
  1279. }
  1280. /**
  1281. * msi_device_domain_alloc_wired - Allocate a "wired" interrupt on @domain
  1282. * @domain: The domain to allocate on
  1283. * @hwirq: The hardware interrupt number to allocate for
  1284. * @type: The interrupt type
  1285. *
  1286. * This weirdness supports wire to MSI controllers like MBIGEN.
  1287. *
  1288. * @hwirq is the hardware interrupt number which is handed in from
  1289. * irq_create_fwspec_mapping(). As the wire to MSI domain is sparse, but
  1290. * sized in firmware, the hardware interrupt number cannot be used as MSI
  1291. * index. For the underlying irq chip the MSI index is irrelevant and
  1292. * all it needs is the hardware interrupt number.
  1293. *
  1294. * To handle this the MSI index is allocated with MSI_ANY_INDEX and the
  1295. * hardware interrupt number is stored along with the type information in
  1296. * msi_desc::cookie so the underlying interrupt chip and domain code can
  1297. * retrieve it.
  1298. *
  1299. * Return: The Linux interrupt number (> 0) or an error code
  1300. */
  1301. int msi_device_domain_alloc_wired(struct irq_domain *domain, unsigned int hwirq,
  1302. unsigned int type)
  1303. {
  1304. unsigned int domid = MSI_DEFAULT_DOMAIN;
  1305. union msi_instance_cookie icookie = { };
  1306. struct device *dev = domain->dev;
  1307. struct msi_map map = { };
  1308. if (WARN_ON_ONCE(!dev || domain->bus_token != DOMAIN_BUS_WIRED_TO_MSI))
  1309. return -EINVAL;
  1310. icookie.value = ((u64)type << 32) | hwirq;
  1311. msi_lock_descs(dev);
  1312. if (WARN_ON_ONCE(msi_get_device_domain(dev, domid) != domain))
  1313. map.index = -EINVAL;
  1314. else
  1315. map = __msi_domain_alloc_irq_at(dev, domid, MSI_ANY_INDEX, NULL, &icookie);
  1316. msi_unlock_descs(dev);
  1317. return map.index >= 0 ? map.virq : map.index;
  1318. }
  1319. static void __msi_domain_free_irqs(struct device *dev, struct irq_domain *domain,
  1320. struct msi_ctrl *ctrl)
  1321. {
  1322. struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store;
  1323. struct msi_domain_info *info = domain->host_data;
  1324. struct irq_data *irqd;
  1325. struct msi_desc *desc;
  1326. unsigned long idx;
  1327. int i;
  1328. xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
  1329. /* Only handle MSI entries which have an interrupt associated */
  1330. if (!msi_desc_match(desc, MSI_DESC_ASSOCIATED))
  1331. continue;
  1332. /* Make sure all interrupts are deactivated */
  1333. for (i = 0; i < desc->nvec_used; i++) {
  1334. irqd = irq_domain_get_irq_data(domain, desc->irq + i);
  1335. if (irqd && irqd_is_activated(irqd))
  1336. irq_domain_deactivate_irq(irqd);
  1337. }
  1338. irq_domain_free_irqs(desc->irq, desc->nvec_used);
  1339. if (info->flags & MSI_FLAG_DEV_SYSFS)
  1340. msi_sysfs_remove_desc(dev, desc);
  1341. desc->irq = 0;
  1342. }
  1343. }
  1344. static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl)
  1345. {
  1346. struct msi_domain_info *info;
  1347. struct msi_domain_ops *ops;
  1348. struct irq_domain *domain;
  1349. if (!msi_ctrl_valid(dev, ctrl))
  1350. return;
  1351. domain = msi_get_device_domain(dev, ctrl->domid);
  1352. if (!domain)
  1353. return;
  1354. info = domain->host_data;
  1355. ops = info->ops;
  1356. if (ops->domain_free_irqs)
  1357. ops->domain_free_irqs(domain, dev);
  1358. else
  1359. __msi_domain_free_irqs(dev, domain, ctrl);
  1360. if (ops->msi_post_free)
  1361. ops->msi_post_free(domain, dev);
  1362. if (info->flags & MSI_FLAG_FREE_MSI_DESCS)
  1363. msi_domain_free_descs(dev, ctrl);
  1364. }
  1365. /**
  1366. * msi_domain_free_irqs_range_locked - Free a range of interrupts from a MSI interrupt domain
  1367. * associated to @dev with msi_lock held
  1368. * @dev: Pointer to device struct of the device for which the interrupts
  1369. * are freed
  1370. * @domid: Id of the interrupt domain to operate on
  1371. * @first: First index to free (inclusive)
  1372. * @last: Last index to free (inclusive)
  1373. */
  1374. void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
  1375. unsigned int first, unsigned int last)
  1376. {
  1377. struct msi_ctrl ctrl = {
  1378. .domid = domid,
  1379. .first = first,
  1380. .last = last,
  1381. };
  1382. msi_domain_free_locked(dev, &ctrl);
  1383. }
  1384. /**
  1385. * msi_domain_free_irqs_range - Free a range of interrupts from a MSI interrupt domain
  1386. * associated to @dev
  1387. * @dev: Pointer to device struct of the device for which the interrupts
  1388. * are freed
  1389. * @domid: Id of the interrupt domain to operate on
  1390. * @first: First index to free (inclusive)
  1391. * @last: Last index to free (inclusive)
  1392. */
  1393. void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
  1394. unsigned int first, unsigned int last)
  1395. {
  1396. msi_lock_descs(dev);
  1397. msi_domain_free_irqs_range_locked(dev, domid, first, last);
  1398. msi_unlock_descs(dev);
  1399. }
  1400. EXPORT_SYMBOL_GPL(msi_domain_free_irqs_all);
  1401. /**
  1402. * msi_domain_free_irqs_all_locked - Free all interrupts from a MSI interrupt domain
  1403. * associated to a device
  1404. * @dev: Pointer to device struct of the device for which the interrupts
  1405. * are freed
  1406. * @domid: The id of the domain to operate on
  1407. *
  1408. * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
  1409. * pair. Use this for MSI irqdomains which implement their own vector
  1410. * allocation.
  1411. */
  1412. void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid)
  1413. {
  1414. msi_domain_free_irqs_range_locked(dev, domid, 0,
  1415. msi_domain_get_hwsize(dev, domid) - 1);
  1416. }
  1417. /**
  1418. * msi_domain_free_irqs_all - Free all interrupts from a MSI interrupt domain
  1419. * associated to a device
  1420. * @dev: Pointer to device struct of the device for which the interrupts
  1421. * are freed
  1422. * @domid: The id of the domain to operate on
  1423. */
  1424. void msi_domain_free_irqs_all(struct device *dev, unsigned int domid)
  1425. {
  1426. msi_lock_descs(dev);
  1427. msi_domain_free_irqs_all_locked(dev, domid);
  1428. msi_unlock_descs(dev);
  1429. }
  1430. /**
  1431. * msi_device_domain_free_wired - Free a wired interrupt in @domain
  1432. * @domain: The domain to free the interrupt on
  1433. * @virq: The Linux interrupt number to free
  1434. *
  1435. * This is the counterpart of msi_device_domain_alloc_wired() for the
  1436. * weird wired to MSI converting domains.
  1437. */
  1438. void msi_device_domain_free_wired(struct irq_domain *domain, unsigned int virq)
  1439. {
  1440. struct msi_desc *desc = irq_get_msi_desc(virq);
  1441. struct device *dev = domain->dev;
  1442. if (WARN_ON_ONCE(!dev || !desc || domain->bus_token != DOMAIN_BUS_WIRED_TO_MSI))
  1443. return;
  1444. msi_lock_descs(dev);
  1445. if (!WARN_ON_ONCE(msi_get_device_domain(dev, MSI_DEFAULT_DOMAIN) != domain)) {
  1446. msi_domain_free_irqs_range_locked(dev, MSI_DEFAULT_DOMAIN, desc->msi_index,
  1447. desc->msi_index);
  1448. }
  1449. msi_unlock_descs(dev);
  1450. }
  1451. /**
  1452. * msi_get_domain_info - Get the MSI interrupt domain info for @domain
  1453. * @domain: The interrupt domain to retrieve data from
  1454. *
  1455. * Return: the pointer to the msi_domain_info stored in @domain->host_data.
  1456. */
  1457. struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
  1458. {
  1459. return (struct msi_domain_info *)domain->host_data;
  1460. }
  1461. /**
  1462. * msi_device_has_isolated_msi - True if the device has isolated MSI
  1463. * @dev: The device to check
  1464. *
  1465. * Isolated MSI means that HW modeled by an irq_domain on the path from the
  1466. * initiating device to the CPU will validate that the MSI message specifies an
  1467. * interrupt number that the device is authorized to trigger. This must block
  1468. * devices from triggering interrupts they are not authorized to trigger.
  1469. * Currently authorization means the MSI vector is one assigned to the device.
  1470. *
  1471. * This is interesting for securing VFIO use cases where a rouge MSI (eg created
  1472. * by abusing a normal PCI MemWr DMA) must not allow the VFIO userspace to
  1473. * impact outside its security domain, eg userspace triggering interrupts on
  1474. * kernel drivers, a VM triggering interrupts on the hypervisor, or a VM
  1475. * triggering interrupts on another VM.
  1476. */
  1477. bool msi_device_has_isolated_msi(struct device *dev)
  1478. {
  1479. struct irq_domain *domain = dev_get_msi_domain(dev);
  1480. for (; domain; domain = domain->parent)
  1481. if (domain->flags & IRQ_DOMAIN_FLAG_ISOLATED_MSI)
  1482. return true;
  1483. return arch_is_isolated_msi();
  1484. }
  1485. EXPORT_SYMBOL_GPL(msi_device_has_isolated_msi);