msi.c 18 KB

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