msi.c 27 KB

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