msi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * File: msi.c
  3. * Purpose: PCI Message Signaled Interrupt (MSI)
  4. *
  5. * Copyright (C) 2003-2004 Intel
  6. * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
  7. */
  8. #include <linux/err.h>
  9. #include <linux/mm.h>
  10. #include <linux/irq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/init.h>
  13. #include <linux/export.h>
  14. #include <linux/ioport.h>
  15. #include <linux/pci.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/msi.h>
  18. #include <linux/smp.h>
  19. #include <linux/errno.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include "pci.h"
  23. static int pci_msi_enable = 1;
  24. #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
  25. /* Arch hooks */
  26. int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  27. {
  28. struct msi_chip *chip = dev->bus->msi;
  29. int err;
  30. if (!chip || !chip->setup_irq)
  31. return -EINVAL;
  32. err = chip->setup_irq(chip, dev, desc);
  33. if (err < 0)
  34. return err;
  35. irq_set_chip_data(desc->irq, chip);
  36. return 0;
  37. }
  38. void __weak arch_teardown_msi_irq(unsigned int irq)
  39. {
  40. struct msi_chip *chip = irq_get_chip_data(irq);
  41. if (!chip || !chip->teardown_irq)
  42. return;
  43. chip->teardown_irq(chip, irq);
  44. }
  45. int __weak arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
  46. {
  47. struct msi_chip *chip = dev->bus->msi;
  48. if (!chip || !chip->check_device)
  49. return 0;
  50. return chip->check_device(chip, dev, nvec, type);
  51. }
  52. int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  53. {
  54. struct msi_desc *entry;
  55. int ret;
  56. /*
  57. * If an architecture wants to support multiple MSI, it needs to
  58. * override arch_setup_msi_irqs()
  59. */
  60. if (type == PCI_CAP_ID_MSI && nvec > 1)
  61. return 1;
  62. list_for_each_entry(entry, &dev->msi_list, list) {
  63. ret = arch_setup_msi_irq(dev, entry);
  64. if (ret < 0)
  65. return ret;
  66. if (ret > 0)
  67. return -ENOSPC;
  68. }
  69. return 0;
  70. }
  71. /*
  72. * We have a default implementation available as a separate non-weak
  73. * function, as it is used by the Xen x86 PCI code
  74. */
  75. void default_teardown_msi_irqs(struct pci_dev *dev)
  76. {
  77. struct msi_desc *entry;
  78. list_for_each_entry(entry, &dev->msi_list, list) {
  79. int i, nvec;
  80. if (entry->irq == 0)
  81. continue;
  82. if (entry->nvec_used)
  83. nvec = entry->nvec_used;
  84. else
  85. nvec = 1 << entry->msi_attrib.multiple;
  86. for (i = 0; i < nvec; i++)
  87. arch_teardown_msi_irq(entry->irq + i);
  88. }
  89. }
  90. void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
  91. {
  92. return default_teardown_msi_irqs(dev);
  93. }
  94. void default_restore_msi_irqs(struct pci_dev *dev, int irq)
  95. {
  96. struct msi_desc *entry;
  97. entry = NULL;
  98. if (dev->msix_enabled) {
  99. list_for_each_entry(entry, &dev->msi_list, list) {
  100. if (irq == entry->irq)
  101. break;
  102. }
  103. } else if (dev->msi_enabled) {
  104. entry = irq_get_msi_desc(irq);
  105. }
  106. if (entry)
  107. write_msi_msg(irq, &entry->msg);
  108. }
  109. void __weak arch_restore_msi_irqs(struct pci_dev *dev, int irq)
  110. {
  111. return default_restore_msi_irqs(dev, irq);
  112. }
  113. static void msi_set_enable(struct pci_dev *dev, int enable)
  114. {
  115. u16 control;
  116. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  117. control &= ~PCI_MSI_FLAGS_ENABLE;
  118. if (enable)
  119. control |= PCI_MSI_FLAGS_ENABLE;
  120. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  121. }
  122. static void msix_set_enable(struct pci_dev *dev, int enable)
  123. {
  124. u16 control;
  125. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  126. control &= ~PCI_MSIX_FLAGS_ENABLE;
  127. if (enable)
  128. control |= PCI_MSIX_FLAGS_ENABLE;
  129. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  130. }
  131. static inline __attribute_const__ u32 msi_mask(unsigned x)
  132. {
  133. /* Don't shift by >= width of type */
  134. if (x >= 5)
  135. return 0xffffffff;
  136. return (1 << (1 << x)) - 1;
  137. }
  138. static inline __attribute_const__ u32 msi_capable_mask(u16 control)
  139. {
  140. return msi_mask((control >> 1) & 7);
  141. }
  142. static inline __attribute_const__ u32 msi_enabled_mask(u16 control)
  143. {
  144. return msi_mask((control >> 4) & 7);
  145. }
  146. /*
  147. * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
  148. * mask all MSI interrupts by clearing the MSI enable bit does not work
  149. * reliably as devices without an INTx disable bit will then generate a
  150. * level IRQ which will never be cleared.
  151. */
  152. u32 default_msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  153. {
  154. u32 mask_bits = desc->masked;
  155. if (!desc->msi_attrib.maskbit)
  156. return 0;
  157. mask_bits &= ~mask;
  158. mask_bits |= flag;
  159. pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
  160. return mask_bits;
  161. }
  162. __weak u32 arch_msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  163. {
  164. return default_msi_mask_irq(desc, mask, flag);
  165. }
  166. static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  167. {
  168. desc->masked = arch_msi_mask_irq(desc, mask, flag);
  169. }
  170. /*
  171. * This internal function does not flush PCI writes to the device.
  172. * All users must ensure that they read from the device before either
  173. * assuming that the device state is up to date, or returning out of this
  174. * file. This saves a few milliseconds when initialising devices with lots
  175. * of MSI-X interrupts.
  176. */
  177. u32 default_msix_mask_irq(struct msi_desc *desc, u32 flag)
  178. {
  179. u32 mask_bits = desc->masked;
  180. unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  181. PCI_MSIX_ENTRY_VECTOR_CTRL;
  182. mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
  183. if (flag)
  184. mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
  185. writel(mask_bits, desc->mask_base + offset);
  186. return mask_bits;
  187. }
  188. __weak u32 arch_msix_mask_irq(struct msi_desc *desc, u32 flag)
  189. {
  190. return default_msix_mask_irq(desc, flag);
  191. }
  192. static void msix_mask_irq(struct msi_desc *desc, u32 flag)
  193. {
  194. desc->masked = arch_msix_mask_irq(desc, flag);
  195. }
  196. static void msi_set_mask_bit(struct irq_data *data, u32 flag)
  197. {
  198. struct msi_desc *desc = irq_data_get_msi(data);
  199. if (desc->msi_attrib.is_msix) {
  200. msix_mask_irq(desc, flag);
  201. readl(desc->mask_base); /* Flush write to device */
  202. } else {
  203. unsigned offset = data->irq - desc->dev->irq;
  204. msi_mask_irq(desc, 1 << offset, flag << offset);
  205. }
  206. }
  207. void mask_msi_irq(struct irq_data *data)
  208. {
  209. msi_set_mask_bit(data, 1);
  210. }
  211. void unmask_msi_irq(struct irq_data *data)
  212. {
  213. msi_set_mask_bit(data, 0);
  214. }
  215. void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  216. {
  217. BUG_ON(entry->dev->current_state != PCI_D0);
  218. if (entry->msi_attrib.is_msix) {
  219. void __iomem *base = entry->mask_base +
  220. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  221. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
  222. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
  223. msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
  224. } else {
  225. struct pci_dev *dev = entry->dev;
  226. int pos = dev->msi_cap;
  227. u16 data;
  228. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  229. &msg->address_lo);
  230. if (entry->msi_attrib.is_64) {
  231. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  232. &msg->address_hi);
  233. pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
  234. } else {
  235. msg->address_hi = 0;
  236. pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
  237. }
  238. msg->data = data;
  239. }
  240. }
  241. void read_msi_msg(unsigned int irq, struct msi_msg *msg)
  242. {
  243. struct msi_desc *entry = irq_get_msi_desc(irq);
  244. __read_msi_msg(entry, msg);
  245. }
  246. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  247. {
  248. /* Assert that the cache is valid, assuming that
  249. * valid messages are not all-zeroes. */
  250. BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
  251. entry->msg.data));
  252. *msg = entry->msg;
  253. }
  254. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  255. {
  256. struct msi_desc *entry = irq_get_msi_desc(irq);
  257. __get_cached_msi_msg(entry, msg);
  258. }
  259. void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  260. {
  261. if (entry->dev->current_state != PCI_D0) {
  262. /* Don't touch the hardware now */
  263. } else if (entry->msi_attrib.is_msix) {
  264. void __iomem *base;
  265. base = entry->mask_base +
  266. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  267. writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
  268. writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
  269. writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
  270. } else {
  271. struct pci_dev *dev = entry->dev;
  272. int pos = dev->msi_cap;
  273. u16 msgctl;
  274. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
  275. msgctl &= ~PCI_MSI_FLAGS_QSIZE;
  276. msgctl |= entry->msi_attrib.multiple << 4;
  277. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
  278. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  279. msg->address_lo);
  280. if (entry->msi_attrib.is_64) {
  281. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  282. msg->address_hi);
  283. pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
  284. msg->data);
  285. } else {
  286. pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
  287. msg->data);
  288. }
  289. }
  290. entry->msg = *msg;
  291. }
  292. void write_msi_msg(unsigned int irq, struct msi_msg *msg)
  293. {
  294. struct msi_desc *entry = irq_get_msi_desc(irq);
  295. __write_msi_msg(entry, msg);
  296. }
  297. static void free_msi_irqs(struct pci_dev *dev)
  298. {
  299. struct msi_desc *entry, *tmp;
  300. list_for_each_entry(entry, &dev->msi_list, list) {
  301. int i, nvec;
  302. if (!entry->irq)
  303. continue;
  304. if (entry->nvec_used)
  305. nvec = entry->nvec_used;
  306. else
  307. nvec = 1 << entry->msi_attrib.multiple;
  308. for (i = 0; i < nvec; i++)
  309. BUG_ON(irq_has_action(entry->irq + i));
  310. }
  311. arch_teardown_msi_irqs(dev);
  312. list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
  313. if (entry->msi_attrib.is_msix) {
  314. if (list_is_last(&entry->list, &dev->msi_list))
  315. iounmap(entry->mask_base);
  316. }
  317. /*
  318. * Its possible that we get into this path
  319. * When populate_msi_sysfs fails, which means the entries
  320. * were not registered with sysfs. In that case don't
  321. * unregister them.
  322. */
  323. if (entry->kobj.parent) {
  324. kobject_del(&entry->kobj);
  325. kobject_put(&entry->kobj);
  326. }
  327. list_del(&entry->list);
  328. kfree(entry);
  329. }
  330. }
  331. static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
  332. {
  333. struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  334. if (!desc)
  335. return NULL;
  336. INIT_LIST_HEAD(&desc->list);
  337. desc->dev = dev;
  338. return desc;
  339. }
  340. static void pci_intx_for_msi(struct pci_dev *dev, int enable)
  341. {
  342. if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
  343. pci_intx(dev, enable);
  344. }
  345. static void __pci_restore_msi_state(struct pci_dev *dev)
  346. {
  347. u16 control;
  348. struct msi_desc *entry;
  349. if (!dev->msi_enabled)
  350. return;
  351. entry = irq_get_msi_desc(dev->irq);
  352. pci_intx_for_msi(dev, 0);
  353. msi_set_enable(dev, 0);
  354. arch_restore_msi_irqs(dev, dev->irq);
  355. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  356. msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
  357. control &= ~PCI_MSI_FLAGS_QSIZE;
  358. control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
  359. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  360. }
  361. static void __pci_restore_msix_state(struct pci_dev *dev)
  362. {
  363. struct msi_desc *entry;
  364. u16 control;
  365. if (!dev->msix_enabled)
  366. return;
  367. BUG_ON(list_empty(&dev->msi_list));
  368. entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
  369. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  370. /* route the table */
  371. pci_intx_for_msi(dev, 0);
  372. control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL;
  373. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  374. list_for_each_entry(entry, &dev->msi_list, list) {
  375. arch_restore_msi_irqs(dev, entry->irq);
  376. msix_mask_irq(entry, entry->masked);
  377. }
  378. control &= ~PCI_MSIX_FLAGS_MASKALL;
  379. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  380. }
  381. void pci_restore_msi_state(struct pci_dev *dev)
  382. {
  383. __pci_restore_msi_state(dev);
  384. __pci_restore_msix_state(dev);
  385. }
  386. EXPORT_SYMBOL_GPL(pci_restore_msi_state);
  387. #define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
  388. #define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
  389. struct msi_attribute {
  390. struct attribute attr;
  391. ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr,
  392. char *buf);
  393. ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr,
  394. const char *buf, size_t count);
  395. };
  396. static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr,
  397. char *buf)
  398. {
  399. return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi");
  400. }
  401. static ssize_t msi_irq_attr_show(struct kobject *kobj,
  402. struct attribute *attr, char *buf)
  403. {
  404. struct msi_attribute *attribute = to_msi_attr(attr);
  405. struct msi_desc *entry = to_msi_desc(kobj);
  406. if (!attribute->show)
  407. return -EIO;
  408. return attribute->show(entry, attribute, buf);
  409. }
  410. static const struct sysfs_ops msi_irq_sysfs_ops = {
  411. .show = msi_irq_attr_show,
  412. };
  413. static struct msi_attribute mode_attribute =
  414. __ATTR(mode, S_IRUGO, show_msi_mode, NULL);
  415. static struct attribute *msi_irq_default_attrs[] = {
  416. &mode_attribute.attr,
  417. NULL
  418. };
  419. static void msi_kobj_release(struct kobject *kobj)
  420. {
  421. struct msi_desc *entry = to_msi_desc(kobj);
  422. pci_dev_put(entry->dev);
  423. }
  424. static struct kobj_type msi_irq_ktype = {
  425. .release = msi_kobj_release,
  426. .sysfs_ops = &msi_irq_sysfs_ops,
  427. .default_attrs = msi_irq_default_attrs,
  428. };
  429. static int populate_msi_sysfs(struct pci_dev *pdev)
  430. {
  431. struct msi_desc *entry;
  432. struct kobject *kobj;
  433. int ret;
  434. int count = 0;
  435. pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj);
  436. if (!pdev->msi_kset)
  437. return -ENOMEM;
  438. list_for_each_entry(entry, &pdev->msi_list, list) {
  439. kobj = &entry->kobj;
  440. kobj->kset = pdev->msi_kset;
  441. pci_dev_get(pdev);
  442. ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL,
  443. "%u", entry->irq);
  444. if (ret)
  445. goto out_unroll;
  446. count++;
  447. }
  448. return 0;
  449. out_unroll:
  450. list_for_each_entry(entry, &pdev->msi_list, list) {
  451. if (!count)
  452. break;
  453. kobject_del(&entry->kobj);
  454. kobject_put(&entry->kobj);
  455. count--;
  456. }
  457. return ret;
  458. }
  459. /**
  460. * msi_capability_init - configure device's MSI capability structure
  461. * @dev: pointer to the pci_dev data structure of MSI device function
  462. * @nvec: number of interrupts to allocate
  463. *
  464. * Setup the MSI capability structure of the device with the requested
  465. * number of interrupts. A return value of zero indicates the successful
  466. * setup of an entry with the new MSI irq. A negative return value indicates
  467. * an error, and a positive return value indicates the number of interrupts
  468. * which could have been allocated.
  469. */
  470. static int msi_capability_init(struct pci_dev *dev, int nvec)
  471. {
  472. struct msi_desc *entry;
  473. int ret;
  474. u16 control;
  475. unsigned mask;
  476. msi_set_enable(dev, 0); /* Disable MSI during set up */
  477. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  478. /* MSI Entry Initialization */
  479. entry = alloc_msi_entry(dev);
  480. if (!entry)
  481. return -ENOMEM;
  482. entry->msi_attrib.is_msix = 0;
  483. entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
  484. entry->msi_attrib.entry_nr = 0;
  485. entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
  486. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  487. entry->msi_attrib.pos = dev->msi_cap;
  488. if (control & PCI_MSI_FLAGS_64BIT)
  489. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
  490. else
  491. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
  492. /* All MSIs are unmasked by default, Mask them all */
  493. if (entry->msi_attrib.maskbit)
  494. pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
  495. mask = msi_capable_mask(control);
  496. msi_mask_irq(entry, mask, mask);
  497. list_add_tail(&entry->list, &dev->msi_list);
  498. /* Configure MSI capability structure */
  499. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
  500. if (ret) {
  501. msi_mask_irq(entry, mask, ~mask);
  502. free_msi_irqs(dev);
  503. return ret;
  504. }
  505. ret = populate_msi_sysfs(dev);
  506. if (ret) {
  507. msi_mask_irq(entry, mask, ~mask);
  508. free_msi_irqs(dev);
  509. return ret;
  510. }
  511. /* Set MSI enabled bits */
  512. pci_intx_for_msi(dev, 0);
  513. msi_set_enable(dev, 1);
  514. dev->msi_enabled = 1;
  515. dev->irq = entry->irq;
  516. return 0;
  517. }
  518. static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
  519. {
  520. resource_size_t phys_addr;
  521. u32 table_offset;
  522. u8 bir;
  523. pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
  524. &table_offset);
  525. bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
  526. table_offset &= PCI_MSIX_TABLE_OFFSET;
  527. phys_addr = pci_resource_start(dev, bir) + table_offset;
  528. return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  529. }
  530. static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
  531. struct msix_entry *entries, int nvec)
  532. {
  533. struct msi_desc *entry;
  534. int i;
  535. for (i = 0; i < nvec; i++) {
  536. entry = alloc_msi_entry(dev);
  537. if (!entry) {
  538. if (!i)
  539. iounmap(base);
  540. else
  541. free_msi_irqs(dev);
  542. /* No enough memory. Don't try again */
  543. return -ENOMEM;
  544. }
  545. entry->msi_attrib.is_msix = 1;
  546. entry->msi_attrib.is_64 = 1;
  547. entry->msi_attrib.entry_nr = entries[i].entry;
  548. entry->msi_attrib.default_irq = dev->irq;
  549. entry->msi_attrib.pos = dev->msix_cap;
  550. entry->mask_base = base;
  551. list_add_tail(&entry->list, &dev->msi_list);
  552. }
  553. return 0;
  554. }
  555. static void msix_program_entries(struct pci_dev *dev,
  556. struct msix_entry *entries)
  557. {
  558. struct msi_desc *entry;
  559. int i = 0;
  560. list_for_each_entry(entry, &dev->msi_list, list) {
  561. int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
  562. PCI_MSIX_ENTRY_VECTOR_CTRL;
  563. entries[i].vector = entry->irq;
  564. irq_set_msi_desc(entry->irq, entry);
  565. entry->masked = readl(entry->mask_base + offset);
  566. msix_mask_irq(entry, 1);
  567. i++;
  568. }
  569. }
  570. /**
  571. * msix_capability_init - configure device's MSI-X capability
  572. * @dev: pointer to the pci_dev data structure of MSI-X device function
  573. * @entries: pointer to an array of struct msix_entry entries
  574. * @nvec: number of @entries
  575. *
  576. * Setup the MSI-X capability structure of device function with a
  577. * single MSI-X irq. A return of zero indicates the successful setup of
  578. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  579. **/
  580. static int msix_capability_init(struct pci_dev *dev,
  581. struct msix_entry *entries, int nvec)
  582. {
  583. int ret;
  584. u16 control;
  585. void __iomem *base;
  586. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  587. /* Ensure MSI-X is disabled while it is set up */
  588. control &= ~PCI_MSIX_FLAGS_ENABLE;
  589. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  590. /* Request & Map MSI-X table region */
  591. base = msix_map_region(dev, msix_table_size(control));
  592. if (!base)
  593. return -ENOMEM;
  594. ret = msix_setup_entries(dev, base, entries, nvec);
  595. if (ret)
  596. return ret;
  597. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  598. if (ret)
  599. goto error;
  600. /*
  601. * Some devices require MSI-X to be enabled before we can touch the
  602. * MSI-X registers. We need to mask all the vectors to prevent
  603. * interrupts coming in before they're fully set up.
  604. */
  605. control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
  606. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  607. msix_program_entries(dev, entries);
  608. ret = populate_msi_sysfs(dev);
  609. if (ret) {
  610. ret = 0;
  611. goto error;
  612. }
  613. /* Set MSI-X enabled bits and unmask the function */
  614. pci_intx_for_msi(dev, 0);
  615. dev->msix_enabled = 1;
  616. control &= ~PCI_MSIX_FLAGS_MASKALL;
  617. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  618. return 0;
  619. error:
  620. if (ret < 0) {
  621. /*
  622. * If we had some success, report the number of irqs
  623. * we succeeded in setting up.
  624. */
  625. struct msi_desc *entry;
  626. int avail = 0;
  627. list_for_each_entry(entry, &dev->msi_list, list) {
  628. if (entry->irq != 0)
  629. avail++;
  630. }
  631. if (avail != 0)
  632. ret = avail;
  633. }
  634. free_msi_irqs(dev);
  635. return ret;
  636. }
  637. /**
  638. * pci_msi_check_device - check whether MSI may be enabled on a device
  639. * @dev: pointer to the pci_dev data structure of MSI device function
  640. * @nvec: how many MSIs have been requested ?
  641. * @type: are we checking for MSI or MSI-X ?
  642. *
  643. * Look at global flags, the device itself, and its parent busses
  644. * to determine if MSI/-X are supported for the device. If MSI/-X is
  645. * supported return 0, else return an error code.
  646. **/
  647. static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
  648. {
  649. struct pci_bus *bus;
  650. int ret;
  651. /* MSI must be globally enabled and supported by the device */
  652. if (!pci_msi_enable || !dev || dev->no_msi)
  653. return -EINVAL;
  654. /*
  655. * You can't ask to have 0 or less MSIs configured.
  656. * a) it's stupid ..
  657. * b) the list manipulation code assumes nvec >= 1.
  658. */
  659. if (nvec < 1)
  660. return -ERANGE;
  661. /*
  662. * Any bridge which does NOT route MSI transactions from its
  663. * secondary bus to its primary bus must set NO_MSI flag on
  664. * the secondary pci_bus.
  665. * We expect only arch-specific PCI host bus controller driver
  666. * or quirks for specific PCI bridges to be setting NO_MSI.
  667. */
  668. for (bus = dev->bus; bus; bus = bus->parent)
  669. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  670. return -EINVAL;
  671. ret = arch_msi_check_device(dev, nvec, type);
  672. if (ret)
  673. return ret;
  674. return 0;
  675. }
  676. /**
  677. * pci_enable_msi_block - configure device's MSI capability structure
  678. * @dev: device to configure
  679. * @nvec: number of interrupts to configure
  680. *
  681. * Allocate IRQs for a device with the MSI capability.
  682. * This function returns a negative errno if an error occurs. If it
  683. * is unable to allocate the number of interrupts requested, it returns
  684. * the number of interrupts it might be able to allocate. If it successfully
  685. * allocates at least the number of interrupts requested, it returns 0 and
  686. * updates the @dev's irq member to the lowest new interrupt number; the
  687. * other interrupt numbers allocated to this device are consecutive.
  688. */
  689. int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
  690. {
  691. int status, maxvec;
  692. u16 msgctl;
  693. if (!dev->msi_cap || dev->current_state != PCI_D0)
  694. return -EINVAL;
  695. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
  696. maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  697. if (nvec > maxvec)
  698. return maxvec;
  699. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
  700. if (status)
  701. return status;
  702. WARN_ON(!!dev->msi_enabled);
  703. /* Check whether driver already requested MSI-X irqs */
  704. if (dev->msix_enabled) {
  705. dev_info(&dev->dev, "can't enable MSI "
  706. "(MSI-X already enabled)\n");
  707. return -EINVAL;
  708. }
  709. status = msi_capability_init(dev, nvec);
  710. return status;
  711. }
  712. EXPORT_SYMBOL(pci_enable_msi_block);
  713. int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *maxvec)
  714. {
  715. int ret, nvec;
  716. u16 msgctl;
  717. if (!dev->msi_cap || dev->current_state != PCI_D0)
  718. return -EINVAL;
  719. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
  720. ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  721. if (maxvec)
  722. *maxvec = ret;
  723. do {
  724. nvec = ret;
  725. ret = pci_enable_msi_block(dev, nvec);
  726. } while (ret > 0);
  727. if (ret < 0)
  728. return ret;
  729. return nvec;
  730. }
  731. EXPORT_SYMBOL(pci_enable_msi_block_auto);
  732. void pci_msi_shutdown(struct pci_dev *dev)
  733. {
  734. struct msi_desc *desc;
  735. u32 mask;
  736. u16 ctrl;
  737. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  738. return;
  739. BUG_ON(list_empty(&dev->msi_list));
  740. desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
  741. msi_set_enable(dev, 0);
  742. pci_intx_for_msi(dev, 1);
  743. dev->msi_enabled = 0;
  744. /* Return the device with MSI unmasked as initial states */
  745. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
  746. mask = msi_capable_mask(ctrl);
  747. /* Keep cached state to be restored */
  748. arch_msi_mask_irq(desc, mask, ~mask);
  749. /* Restore dev->irq to its default pin-assertion irq */
  750. dev->irq = desc->msi_attrib.default_irq;
  751. }
  752. void pci_disable_msi(struct pci_dev *dev)
  753. {
  754. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  755. return;
  756. pci_msi_shutdown(dev);
  757. free_msi_irqs(dev);
  758. kset_unregister(dev->msi_kset);
  759. dev->msi_kset = NULL;
  760. }
  761. EXPORT_SYMBOL(pci_disable_msi);
  762. /**
  763. * pci_msix_table_size - return the number of device's MSI-X table entries
  764. * @dev: pointer to the pci_dev data structure of MSI-X device function
  765. */
  766. int pci_msix_table_size(struct pci_dev *dev)
  767. {
  768. u16 control;
  769. if (!dev->msix_cap)
  770. return 0;
  771. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  772. return msix_table_size(control);
  773. }
  774. /**
  775. * pci_enable_msix - configure device's MSI-X capability structure
  776. * @dev: pointer to the pci_dev data structure of MSI-X device function
  777. * @entries: pointer to an array of MSI-X entries
  778. * @nvec: number of MSI-X irqs requested for allocation by device driver
  779. *
  780. * Setup the MSI-X capability structure of device function with the number
  781. * of requested irqs upon its software driver call to request for
  782. * MSI-X mode enabled on its hardware device function. A return of zero
  783. * indicates the successful configuration of MSI-X capability structure
  784. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  785. * Or a return of > 0 indicates that driver request is exceeding the number
  786. * of irqs or MSI-X vectors available. Driver should use the returned value to
  787. * re-send its request.
  788. **/
  789. int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
  790. {
  791. int status, nr_entries;
  792. int i, j;
  793. if (!entries || !dev->msix_cap || dev->current_state != PCI_D0)
  794. return -EINVAL;
  795. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
  796. if (status)
  797. return status;
  798. nr_entries = pci_msix_table_size(dev);
  799. if (nvec > nr_entries)
  800. return nr_entries;
  801. /* Check for any invalid entries */
  802. for (i = 0; i < nvec; i++) {
  803. if (entries[i].entry >= nr_entries)
  804. return -EINVAL; /* invalid entry */
  805. for (j = i + 1; j < nvec; j++) {
  806. if (entries[i].entry == entries[j].entry)
  807. return -EINVAL; /* duplicate entry */
  808. }
  809. }
  810. WARN_ON(!!dev->msix_enabled);
  811. /* Check whether driver already requested for MSI irq */
  812. if (dev->msi_enabled) {
  813. dev_info(&dev->dev, "can't enable MSI-X "
  814. "(MSI IRQ already assigned)\n");
  815. return -EINVAL;
  816. }
  817. status = msix_capability_init(dev, entries, nvec);
  818. return status;
  819. }
  820. EXPORT_SYMBOL(pci_enable_msix);
  821. void pci_msix_shutdown(struct pci_dev *dev)
  822. {
  823. struct msi_desc *entry;
  824. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  825. return;
  826. /* Return the device with MSI-X masked as initial states */
  827. list_for_each_entry(entry, &dev->msi_list, list) {
  828. /* Keep cached states to be restored */
  829. arch_msix_mask_irq(entry, 1);
  830. }
  831. msix_set_enable(dev, 0);
  832. pci_intx_for_msi(dev, 1);
  833. dev->msix_enabled = 0;
  834. }
  835. void pci_disable_msix(struct pci_dev *dev)
  836. {
  837. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  838. return;
  839. pci_msix_shutdown(dev);
  840. free_msi_irqs(dev);
  841. kset_unregister(dev->msi_kset);
  842. dev->msi_kset = NULL;
  843. }
  844. EXPORT_SYMBOL(pci_disable_msix);
  845. /**
  846. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  847. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  848. *
  849. * Being called during hotplug remove, from which the device function
  850. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  851. * allocated for this device function, are reclaimed to unused state,
  852. * which may be used later on.
  853. **/
  854. void msi_remove_pci_irq_vectors(struct pci_dev *dev)
  855. {
  856. if (!pci_msi_enable || !dev)
  857. return;
  858. if (dev->msi_enabled || dev->msix_enabled)
  859. free_msi_irqs(dev);
  860. }
  861. void pci_no_msi(void)
  862. {
  863. pci_msi_enable = 0;
  864. }
  865. /**
  866. * pci_msi_enabled - is MSI enabled?
  867. *
  868. * Returns true if MSI has not been disabled by the command-line option
  869. * pci=nomsi.
  870. **/
  871. int pci_msi_enabled(void)
  872. {
  873. return pci_msi_enable;
  874. }
  875. EXPORT_SYMBOL(pci_msi_enabled);
  876. void pci_msi_init_pci_dev(struct pci_dev *dev)
  877. {
  878. INIT_LIST_HEAD(&dev->msi_list);
  879. /* Disable the msi hardware to avoid screaming interrupts
  880. * during boot. This is the power on reset default so
  881. * usually this should be a noop.
  882. */
  883. dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
  884. if (dev->msi_cap)
  885. msi_set_enable(dev, 0);
  886. dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  887. if (dev->msix_cap)
  888. msix_set_enable(dev, 0);
  889. }