msi.c 30 KB

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