msi.c 26 KB

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