msi.c 26 KB

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