msi.c 27 KB

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