msi.c 19 KB

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