msi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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/ioport.h>
  14. #include <linux/pci.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/msi.h>
  17. #include <linux/smp.h>
  18. #include <asm/errno.h>
  19. #include <asm/io.h>
  20. #include "pci.h"
  21. #include "msi.h"
  22. static int pci_msi_enable = 1;
  23. /* Arch hooks */
  24. int __attribute__ ((weak))
  25. arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
  26. {
  27. return 0;
  28. }
  29. int __attribute__ ((weak))
  30. arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
  31. {
  32. return 0;
  33. }
  34. int __attribute__ ((weak))
  35. arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  36. {
  37. struct msi_desc *entry;
  38. int ret;
  39. list_for_each_entry(entry, &dev->msi_list, list) {
  40. ret = arch_setup_msi_irq(dev, entry);
  41. if (ret)
  42. return ret;
  43. }
  44. return 0;
  45. }
  46. void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
  47. {
  48. return;
  49. }
  50. void __attribute__ ((weak))
  51. arch_teardown_msi_irqs(struct pci_dev *dev)
  52. {
  53. struct msi_desc *entry;
  54. list_for_each_entry(entry, &dev->msi_list, list) {
  55. if (entry->irq != 0)
  56. arch_teardown_msi_irq(entry->irq);
  57. }
  58. }
  59. static void __msi_set_enable(struct pci_dev *dev, int pos, int enable)
  60. {
  61. u16 control;
  62. if (pos) {
  63. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  64. control &= ~PCI_MSI_FLAGS_ENABLE;
  65. if (enable)
  66. control |= PCI_MSI_FLAGS_ENABLE;
  67. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  68. }
  69. }
  70. static void msi_set_enable(struct pci_dev *dev, int enable)
  71. {
  72. __msi_set_enable(dev, pci_find_capability(dev, PCI_CAP_ID_MSI), enable);
  73. }
  74. static void msix_set_enable(struct pci_dev *dev, int enable)
  75. {
  76. int pos;
  77. u16 control;
  78. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  79. if (pos) {
  80. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  81. control &= ~PCI_MSIX_FLAGS_ENABLE;
  82. if (enable)
  83. control |= PCI_MSIX_FLAGS_ENABLE;
  84. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  85. }
  86. }
  87. /*
  88. * Essentially, this is ((1 << (1 << x)) - 1), but without the
  89. * undefinedness of a << 32.
  90. */
  91. static inline __attribute_const__ u32 msi_mask(unsigned x)
  92. {
  93. static const u32 mask[] = { 1, 2, 4, 0xf, 0xff, 0xffff, 0xffffffff };
  94. return mask[x];
  95. }
  96. static void msix_flush_writes(struct irq_desc *desc)
  97. {
  98. struct msi_desc *entry;
  99. entry = get_irq_desc_msi(desc);
  100. BUG_ON(!entry || !entry->dev);
  101. switch (entry->msi_attrib.type) {
  102. case PCI_CAP_ID_MSI:
  103. /* nothing to do */
  104. break;
  105. case PCI_CAP_ID_MSIX:
  106. {
  107. int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  108. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
  109. readl(entry->mask_base + offset);
  110. break;
  111. }
  112. default:
  113. BUG();
  114. break;
  115. }
  116. }
  117. /*
  118. * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
  119. * mask all MSI interrupts by clearing the MSI enable bit does not work
  120. * reliably as devices without an INTx disable bit will then generate a
  121. * level IRQ which will never be cleared.
  122. *
  123. * Returns 1 if it succeeded in masking the interrupt and 0 if the device
  124. * doesn't support MSI masking.
  125. */
  126. static int msi_set_mask_bits(struct irq_desc *desc, u32 mask, u32 flag)
  127. {
  128. struct msi_desc *entry;
  129. entry = get_irq_desc_msi(desc);
  130. BUG_ON(!entry || !entry->dev);
  131. switch (entry->msi_attrib.type) {
  132. case PCI_CAP_ID_MSI:
  133. if (entry->msi_attrib.maskbit) {
  134. int pos;
  135. u32 mask_bits;
  136. pos = (long)entry->mask_base;
  137. pci_read_config_dword(entry->dev, pos, &mask_bits);
  138. mask_bits &= ~(mask);
  139. mask_bits |= flag & mask;
  140. pci_write_config_dword(entry->dev, pos, mask_bits);
  141. } else {
  142. return 0;
  143. }
  144. break;
  145. case PCI_CAP_ID_MSIX:
  146. {
  147. int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  148. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
  149. writel(flag, entry->mask_base + offset);
  150. readl(entry->mask_base + offset);
  151. break;
  152. }
  153. default:
  154. BUG();
  155. break;
  156. }
  157. entry->msi_attrib.masked = !!flag;
  158. return 1;
  159. }
  160. void read_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
  161. {
  162. struct msi_desc *entry = get_irq_desc_msi(desc);
  163. switch(entry->msi_attrib.type) {
  164. case PCI_CAP_ID_MSI:
  165. {
  166. struct pci_dev *dev = entry->dev;
  167. int pos = entry->msi_attrib.pos;
  168. u16 data;
  169. pci_read_config_dword(dev, msi_lower_address_reg(pos),
  170. &msg->address_lo);
  171. if (entry->msi_attrib.is_64) {
  172. pci_read_config_dword(dev, msi_upper_address_reg(pos),
  173. &msg->address_hi);
  174. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  175. } else {
  176. msg->address_hi = 0;
  177. pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
  178. }
  179. msg->data = data;
  180. break;
  181. }
  182. case PCI_CAP_ID_MSIX:
  183. {
  184. void __iomem *base;
  185. base = entry->mask_base +
  186. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  187. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  188. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  189. msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
  190. break;
  191. }
  192. default:
  193. BUG();
  194. }
  195. }
  196. void read_msi_msg(unsigned int irq, struct msi_msg *msg)
  197. {
  198. struct irq_desc *desc = irq_to_desc(irq);
  199. read_msi_msg_desc(desc, msg);
  200. }
  201. void write_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
  202. {
  203. struct msi_desc *entry = get_irq_desc_msi(desc);
  204. switch (entry->msi_attrib.type) {
  205. case PCI_CAP_ID_MSI:
  206. {
  207. struct pci_dev *dev = entry->dev;
  208. int pos = entry->msi_attrib.pos;
  209. pci_write_config_dword(dev, msi_lower_address_reg(pos),
  210. msg->address_lo);
  211. if (entry->msi_attrib.is_64) {
  212. pci_write_config_dword(dev, msi_upper_address_reg(pos),
  213. msg->address_hi);
  214. pci_write_config_word(dev, msi_data_reg(pos, 1),
  215. msg->data);
  216. } else {
  217. pci_write_config_word(dev, msi_data_reg(pos, 0),
  218. msg->data);
  219. }
  220. break;
  221. }
  222. case PCI_CAP_ID_MSIX:
  223. {
  224. void __iomem *base;
  225. base = entry->mask_base +
  226. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  227. writel(msg->address_lo,
  228. base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  229. writel(msg->address_hi,
  230. base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  231. writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
  232. break;
  233. }
  234. default:
  235. BUG();
  236. }
  237. entry->msg = *msg;
  238. }
  239. void write_msi_msg(unsigned int irq, struct msi_msg *msg)
  240. {
  241. struct irq_desc *desc = irq_to_desc(irq);
  242. write_msi_msg_desc(desc, msg);
  243. }
  244. void mask_msi_irq(unsigned int irq)
  245. {
  246. struct irq_desc *desc = irq_to_desc(irq);
  247. msi_set_mask_bits(desc, 1, 1);
  248. msix_flush_writes(desc);
  249. }
  250. void unmask_msi_irq(unsigned int irq)
  251. {
  252. struct irq_desc *desc = irq_to_desc(irq);
  253. msi_set_mask_bits(desc, 1, 0);
  254. msix_flush_writes(desc);
  255. }
  256. static int msi_free_irqs(struct pci_dev* dev);
  257. static struct msi_desc* alloc_msi_entry(void)
  258. {
  259. struct msi_desc *entry;
  260. entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
  261. if (!entry)
  262. return NULL;
  263. INIT_LIST_HEAD(&entry->list);
  264. entry->irq = 0;
  265. entry->dev = NULL;
  266. return entry;
  267. }
  268. static void pci_intx_for_msi(struct pci_dev *dev, int enable)
  269. {
  270. if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
  271. pci_intx(dev, enable);
  272. }
  273. static void __pci_restore_msi_state(struct pci_dev *dev)
  274. {
  275. int pos;
  276. u16 control;
  277. struct msi_desc *entry;
  278. if (!dev->msi_enabled)
  279. return;
  280. entry = get_irq_msi(dev->irq);
  281. pos = entry->msi_attrib.pos;
  282. pci_intx_for_msi(dev, 0);
  283. msi_set_enable(dev, 0);
  284. write_msi_msg(dev->irq, &entry->msg);
  285. if (entry->msi_attrib.maskbit) {
  286. struct irq_desc *desc = irq_to_desc(dev->irq);
  287. msi_set_mask_bits(desc, entry->msi_attrib.maskbits_mask,
  288. entry->msi_attrib.masked);
  289. }
  290. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  291. control &= ~PCI_MSI_FLAGS_QSIZE;
  292. control |= PCI_MSI_FLAGS_ENABLE;
  293. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  294. }
  295. static void __pci_restore_msix_state(struct pci_dev *dev)
  296. {
  297. int pos;
  298. struct msi_desc *entry;
  299. u16 control;
  300. if (!dev->msix_enabled)
  301. return;
  302. /* route the table */
  303. pci_intx_for_msi(dev, 0);
  304. msix_set_enable(dev, 0);
  305. list_for_each_entry(entry, &dev->msi_list, list) {
  306. struct irq_desc *desc = irq_to_desc(entry->irq);
  307. write_msi_msg(entry->irq, &entry->msg);
  308. msi_set_mask_bits(desc, 1, entry->msi_attrib.masked);
  309. }
  310. BUG_ON(list_empty(&dev->msi_list));
  311. entry = list_entry(dev->msi_list.next, struct msi_desc, list);
  312. pos = entry->msi_attrib.pos;
  313. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  314. control &= ~PCI_MSIX_FLAGS_MASKALL;
  315. control |= PCI_MSIX_FLAGS_ENABLE;
  316. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  317. }
  318. void pci_restore_msi_state(struct pci_dev *dev)
  319. {
  320. __pci_restore_msi_state(dev);
  321. __pci_restore_msix_state(dev);
  322. }
  323. EXPORT_SYMBOL_GPL(pci_restore_msi_state);
  324. /**
  325. * msi_capability_init - configure device's MSI capability structure
  326. * @dev: pointer to the pci_dev data structure of MSI device function
  327. *
  328. * Setup the MSI capability structure of device function with a single
  329. * MSI irq, regardless of device function is capable of handling
  330. * multiple messages. A return of zero indicates the successful setup
  331. * of an entry zero with the new MSI irq or non-zero for otherwise.
  332. **/
  333. static int msi_capability_init(struct pci_dev *dev)
  334. {
  335. struct msi_desc *entry;
  336. int pos, ret;
  337. u16 control;
  338. msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
  339. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  340. pci_read_config_word(dev, msi_control_reg(pos), &control);
  341. /* MSI Entry Initialization */
  342. entry = alloc_msi_entry();
  343. if (!entry)
  344. return -ENOMEM;
  345. entry->msi_attrib.type = PCI_CAP_ID_MSI;
  346. entry->msi_attrib.is_64 = is_64bit_address(control);
  347. entry->msi_attrib.entry_nr = 0;
  348. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  349. entry->msi_attrib.masked = 1;
  350. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  351. entry->msi_attrib.pos = pos;
  352. entry->dev = dev;
  353. if (entry->msi_attrib.maskbit) {
  354. unsigned int base, maskbits, temp;
  355. base = msi_mask_bits_reg(pos, entry->msi_attrib.is_64);
  356. entry->mask_base = (void __iomem *)(long)base;
  357. /* All MSIs are unmasked by default, Mask them all */
  358. pci_read_config_dword(dev, base, &maskbits);
  359. temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1);
  360. maskbits |= temp;
  361. pci_write_config_dword(dev, base, maskbits);
  362. entry->msi_attrib.maskbits_mask = temp;
  363. }
  364. list_add_tail(&entry->list, &dev->msi_list);
  365. /* Configure MSI capability structure */
  366. ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
  367. if (ret) {
  368. msi_free_irqs(dev);
  369. return ret;
  370. }
  371. /* Set MSI enabled bits */
  372. pci_intx_for_msi(dev, 0);
  373. msi_set_enable(dev, 1);
  374. dev->msi_enabled = 1;
  375. dev->irq = entry->irq;
  376. return 0;
  377. }
  378. /**
  379. * msix_capability_init - configure device's MSI-X capability
  380. * @dev: pointer to the pci_dev data structure of MSI-X device function
  381. * @entries: pointer to an array of struct msix_entry entries
  382. * @nvec: number of @entries
  383. *
  384. * Setup the MSI-X capability structure of device function with a
  385. * single MSI-X irq. A return of zero indicates the successful setup of
  386. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  387. **/
  388. static int msix_capability_init(struct pci_dev *dev,
  389. struct msix_entry *entries, int nvec)
  390. {
  391. struct msi_desc *entry;
  392. int pos, i, j, nr_entries, ret;
  393. unsigned long phys_addr;
  394. u32 table_offset;
  395. u16 control;
  396. u8 bir;
  397. void __iomem *base;
  398. msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
  399. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  400. /* Request & Map MSI-X table region */
  401. pci_read_config_word(dev, msi_control_reg(pos), &control);
  402. nr_entries = multi_msix_capable(control);
  403. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  404. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  405. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  406. phys_addr = pci_resource_start (dev, bir) + table_offset;
  407. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  408. if (base == NULL)
  409. return -ENOMEM;
  410. /* MSI-X Table Initialization */
  411. for (i = 0; i < nvec; i++) {
  412. entry = alloc_msi_entry();
  413. if (!entry)
  414. break;
  415. j = entries[i].entry;
  416. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  417. entry->msi_attrib.is_64 = 1;
  418. entry->msi_attrib.entry_nr = j;
  419. entry->msi_attrib.maskbit = 1;
  420. entry->msi_attrib.masked = 1;
  421. entry->msi_attrib.default_irq = dev->irq;
  422. entry->msi_attrib.pos = pos;
  423. entry->dev = dev;
  424. entry->mask_base = base;
  425. list_add_tail(&entry->list, &dev->msi_list);
  426. }
  427. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  428. if (ret) {
  429. int avail = 0;
  430. list_for_each_entry(entry, &dev->msi_list, list) {
  431. if (entry->irq != 0) {
  432. avail++;
  433. }
  434. }
  435. msi_free_irqs(dev);
  436. /* If we had some success report the number of irqs
  437. * we succeeded in setting up.
  438. */
  439. if (avail == 0)
  440. avail = ret;
  441. return avail;
  442. }
  443. i = 0;
  444. list_for_each_entry(entry, &dev->msi_list, list) {
  445. entries[i].vector = entry->irq;
  446. set_irq_msi(entry->irq, entry);
  447. i++;
  448. }
  449. /* Set MSI-X enabled bits */
  450. pci_intx_for_msi(dev, 0);
  451. msix_set_enable(dev, 1);
  452. dev->msix_enabled = 1;
  453. return 0;
  454. }
  455. /**
  456. * pci_msi_check_device - check whether MSI may be enabled on a device
  457. * @dev: pointer to the pci_dev data structure of MSI device function
  458. * @nvec: how many MSIs have been requested ?
  459. * @type: are we checking for MSI or MSI-X ?
  460. *
  461. * Look at global flags, the device itself, and its parent busses
  462. * to determine if MSI/-X are supported for the device. If MSI/-X is
  463. * supported return 0, else return an error code.
  464. **/
  465. static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
  466. {
  467. struct pci_bus *bus;
  468. int ret;
  469. /* MSI must be globally enabled and supported by the device */
  470. if (!pci_msi_enable || !dev || dev->no_msi)
  471. return -EINVAL;
  472. /*
  473. * You can't ask to have 0 or less MSIs configured.
  474. * a) it's stupid ..
  475. * b) the list manipulation code assumes nvec >= 1.
  476. */
  477. if (nvec < 1)
  478. return -ERANGE;
  479. /* Any bridge which does NOT route MSI transactions from it's
  480. * secondary bus to it's primary bus must set NO_MSI flag on
  481. * the secondary pci_bus.
  482. * We expect only arch-specific PCI host bus controller driver
  483. * or quirks for specific PCI bridges to be setting NO_MSI.
  484. */
  485. for (bus = dev->bus; bus; bus = bus->parent)
  486. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  487. return -EINVAL;
  488. ret = arch_msi_check_device(dev, nvec, type);
  489. if (ret)
  490. return ret;
  491. if (!pci_find_capability(dev, type))
  492. return -EINVAL;
  493. return 0;
  494. }
  495. /**
  496. * pci_enable_msi - configure device's MSI capability structure
  497. * @dev: pointer to the pci_dev data structure of MSI device function
  498. *
  499. * Setup the MSI capability structure of device function with
  500. * a single MSI irq upon its software driver call to request for
  501. * MSI mode enabled on its hardware device function. A return of zero
  502. * indicates the successful setup of an entry zero with the new MSI
  503. * irq or non-zero for otherwise.
  504. **/
  505. int pci_enable_msi(struct pci_dev* dev)
  506. {
  507. int status;
  508. status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
  509. if (status)
  510. return status;
  511. WARN_ON(!!dev->msi_enabled);
  512. /* Check whether driver already requested for MSI-X irqs */
  513. if (dev->msix_enabled) {
  514. dev_info(&dev->dev, "can't enable MSI "
  515. "(MSI-X already enabled)\n");
  516. return -EINVAL;
  517. }
  518. status = msi_capability_init(dev);
  519. return status;
  520. }
  521. EXPORT_SYMBOL(pci_enable_msi);
  522. void pci_msi_shutdown(struct pci_dev* dev)
  523. {
  524. struct msi_desc *entry;
  525. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  526. return;
  527. msi_set_enable(dev, 0);
  528. pci_intx_for_msi(dev, 1);
  529. dev->msi_enabled = 0;
  530. BUG_ON(list_empty(&dev->msi_list));
  531. entry = list_entry(dev->msi_list.next, struct msi_desc, list);
  532. /* Return the the pci reset with msi irqs unmasked */
  533. if (entry->msi_attrib.maskbit) {
  534. u32 mask = entry->msi_attrib.maskbits_mask;
  535. struct irq_desc *desc = irq_to_desc(dev->irq);
  536. msi_set_mask_bits(desc, mask, ~mask);
  537. }
  538. if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
  539. return;
  540. /* Restore dev->irq to its default pin-assertion irq */
  541. dev->irq = entry->msi_attrib.default_irq;
  542. }
  543. void pci_disable_msi(struct pci_dev* dev)
  544. {
  545. struct msi_desc *entry;
  546. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  547. return;
  548. pci_msi_shutdown(dev);
  549. entry = list_entry(dev->msi_list.next, struct msi_desc, list);
  550. if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
  551. return;
  552. msi_free_irqs(dev);
  553. }
  554. EXPORT_SYMBOL(pci_disable_msi);
  555. static int msi_free_irqs(struct pci_dev* dev)
  556. {
  557. struct msi_desc *entry, *tmp;
  558. list_for_each_entry(entry, &dev->msi_list, list) {
  559. if (entry->irq)
  560. BUG_ON(irq_has_action(entry->irq));
  561. }
  562. arch_teardown_msi_irqs(dev);
  563. list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
  564. if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
  565. writel(1, entry->mask_base + entry->msi_attrib.entry_nr
  566. * PCI_MSIX_ENTRY_SIZE
  567. + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  568. if (list_is_last(&entry->list, &dev->msi_list))
  569. iounmap(entry->mask_base);
  570. }
  571. list_del(&entry->list);
  572. kfree(entry);
  573. }
  574. return 0;
  575. }
  576. /**
  577. * pci_enable_msix - configure device's MSI-X capability structure
  578. * @dev: pointer to the pci_dev data structure of MSI-X device function
  579. * @entries: pointer to an array of MSI-X entries
  580. * @nvec: number of MSI-X irqs requested for allocation by device driver
  581. *
  582. * Setup the MSI-X capability structure of device function with the number
  583. * of requested irqs upon its software driver call to request for
  584. * MSI-X mode enabled on its hardware device function. A return of zero
  585. * indicates the successful configuration of MSI-X capability structure
  586. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  587. * Or a return of > 0 indicates that driver request is exceeding the number
  588. * of irqs available. Driver should use the returned value to re-send
  589. * its request.
  590. **/
  591. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  592. {
  593. int status, pos, nr_entries;
  594. int i, j;
  595. u16 control;
  596. if (!entries)
  597. return -EINVAL;
  598. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
  599. if (status)
  600. return status;
  601. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  602. pci_read_config_word(dev, msi_control_reg(pos), &control);
  603. nr_entries = multi_msix_capable(control);
  604. if (nvec > nr_entries)
  605. return -EINVAL;
  606. /* Check for any invalid entries */
  607. for (i = 0; i < nvec; i++) {
  608. if (entries[i].entry >= nr_entries)
  609. return -EINVAL; /* invalid entry */
  610. for (j = i + 1; j < nvec; j++) {
  611. if (entries[i].entry == entries[j].entry)
  612. return -EINVAL; /* duplicate entry */
  613. }
  614. }
  615. WARN_ON(!!dev->msix_enabled);
  616. /* Check whether driver already requested for MSI irq */
  617. if (dev->msi_enabled) {
  618. dev_info(&dev->dev, "can't enable MSI-X "
  619. "(MSI IRQ already assigned)\n");
  620. return -EINVAL;
  621. }
  622. status = msix_capability_init(dev, entries, nvec);
  623. return status;
  624. }
  625. EXPORT_SYMBOL(pci_enable_msix);
  626. static void msix_free_all_irqs(struct pci_dev *dev)
  627. {
  628. msi_free_irqs(dev);
  629. }
  630. void pci_msix_shutdown(struct pci_dev* dev)
  631. {
  632. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  633. return;
  634. msix_set_enable(dev, 0);
  635. pci_intx_for_msi(dev, 1);
  636. dev->msix_enabled = 0;
  637. }
  638. void pci_disable_msix(struct pci_dev* dev)
  639. {
  640. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  641. return;
  642. pci_msix_shutdown(dev);
  643. msix_free_all_irqs(dev);
  644. }
  645. EXPORT_SYMBOL(pci_disable_msix);
  646. /**
  647. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  648. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  649. *
  650. * Being called during hotplug remove, from which the device function
  651. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  652. * allocated for this device function, are reclaimed to unused state,
  653. * which may be used later on.
  654. **/
  655. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  656. {
  657. if (!pci_msi_enable || !dev)
  658. return;
  659. if (dev->msi_enabled)
  660. msi_free_irqs(dev);
  661. if (dev->msix_enabled)
  662. msix_free_all_irqs(dev);
  663. }
  664. void pci_no_msi(void)
  665. {
  666. pci_msi_enable = 0;
  667. }
  668. /**
  669. * pci_msi_enabled - is MSI enabled?
  670. *
  671. * Returns true if MSI has not been disabled by the command-line option
  672. * pci=nomsi.
  673. **/
  674. int pci_msi_enabled(void)
  675. {
  676. return pci_msi_enable;
  677. }
  678. EXPORT_SYMBOL(pci_msi_enabled);
  679. void pci_msi_init_pci_dev(struct pci_dev *dev)
  680. {
  681. INIT_LIST_HEAD(&dev->msi_list);
  682. }