msi.c 26 KB

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