msi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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/smp_lock.h>
  15. #include <linux/pci.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/msi.h>
  18. #include <asm/errno.h>
  19. #include <asm/io.h>
  20. #include <asm/smp.h>
  21. #include "pci.h"
  22. #include "msi.h"
  23. static struct kmem_cache* msi_cachep;
  24. static int pci_msi_enable = 1;
  25. static int msi_cache_init(void)
  26. {
  27. msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
  28. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  29. if (!msi_cachep)
  30. return -ENOMEM;
  31. return 0;
  32. }
  33. static void msi_set_mask_bit(unsigned int irq, int flag)
  34. {
  35. struct msi_desc *entry;
  36. entry = get_irq_msi(irq);
  37. BUG_ON(!entry || !entry->dev);
  38. switch (entry->msi_attrib.type) {
  39. case PCI_CAP_ID_MSI:
  40. if (entry->msi_attrib.maskbit) {
  41. int pos;
  42. u32 mask_bits;
  43. pos = (long)entry->mask_base;
  44. pci_read_config_dword(entry->dev, pos, &mask_bits);
  45. mask_bits &= ~(1);
  46. mask_bits |= flag;
  47. pci_write_config_dword(entry->dev, pos, mask_bits);
  48. }
  49. break;
  50. case PCI_CAP_ID_MSIX:
  51. {
  52. int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  53. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
  54. writel(flag, entry->mask_base + offset);
  55. break;
  56. }
  57. default:
  58. BUG();
  59. break;
  60. }
  61. }
  62. void read_msi_msg(unsigned int irq, struct msi_msg *msg)
  63. {
  64. struct msi_desc *entry = get_irq_msi(irq);
  65. switch(entry->msi_attrib.type) {
  66. case PCI_CAP_ID_MSI:
  67. {
  68. struct pci_dev *dev = entry->dev;
  69. int pos = entry->msi_attrib.pos;
  70. u16 data;
  71. pci_read_config_dword(dev, msi_lower_address_reg(pos),
  72. &msg->address_lo);
  73. if (entry->msi_attrib.is_64) {
  74. pci_read_config_dword(dev, msi_upper_address_reg(pos),
  75. &msg->address_hi);
  76. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  77. } else {
  78. msg->address_hi = 0;
  79. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  80. }
  81. msg->data = data;
  82. break;
  83. }
  84. case PCI_CAP_ID_MSIX:
  85. {
  86. void __iomem *base;
  87. base = entry->mask_base +
  88. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  89. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  90. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  91. msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
  92. break;
  93. }
  94. default:
  95. BUG();
  96. }
  97. }
  98. void write_msi_msg(unsigned int irq, struct msi_msg *msg)
  99. {
  100. struct msi_desc *entry = get_irq_msi(irq);
  101. switch (entry->msi_attrib.type) {
  102. case PCI_CAP_ID_MSI:
  103. {
  104. struct pci_dev *dev = entry->dev;
  105. int pos = entry->msi_attrib.pos;
  106. pci_write_config_dword(dev, msi_lower_address_reg(pos),
  107. msg->address_lo);
  108. if (entry->msi_attrib.is_64) {
  109. pci_write_config_dword(dev, msi_upper_address_reg(pos),
  110. msg->address_hi);
  111. pci_write_config_word(dev, msi_data_reg(pos, 1),
  112. msg->data);
  113. } else {
  114. pci_write_config_word(dev, msi_data_reg(pos, 0),
  115. msg->data);
  116. }
  117. break;
  118. }
  119. case PCI_CAP_ID_MSIX:
  120. {
  121. void __iomem *base;
  122. base = entry->mask_base +
  123. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  124. writel(msg->address_lo,
  125. base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  126. writel(msg->address_hi,
  127. base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  128. writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
  129. break;
  130. }
  131. default:
  132. BUG();
  133. }
  134. }
  135. void mask_msi_irq(unsigned int irq)
  136. {
  137. msi_set_mask_bit(irq, 1);
  138. }
  139. void unmask_msi_irq(unsigned int irq)
  140. {
  141. msi_set_mask_bit(irq, 0);
  142. }
  143. static int msi_free_irq(struct pci_dev* dev, int irq);
  144. static int msi_init(void)
  145. {
  146. static int status = -ENOMEM;
  147. if (!status)
  148. return status;
  149. status = msi_cache_init();
  150. if (status < 0) {
  151. pci_msi_enable = 0;
  152. printk(KERN_WARNING "PCI: MSI cache init failed\n");
  153. return status;
  154. }
  155. return status;
  156. }
  157. static struct msi_desc* alloc_msi_entry(void)
  158. {
  159. struct msi_desc *entry;
  160. entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
  161. if (!entry)
  162. return NULL;
  163. entry->link.tail = entry->link.head = 0; /* single message */
  164. entry->dev = NULL;
  165. return entry;
  166. }
  167. static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
  168. {
  169. u16 control;
  170. pci_read_config_word(dev, msi_control_reg(pos), &control);
  171. if (type == PCI_CAP_ID_MSI) {
  172. /* Set enabled bits to single MSI & enable MSI_enable bit */
  173. msi_enable(control, 1);
  174. pci_write_config_word(dev, msi_control_reg(pos), control);
  175. dev->msi_enabled = 1;
  176. } else {
  177. msix_enable(control);
  178. pci_write_config_word(dev, msi_control_reg(pos), control);
  179. dev->msix_enabled = 1;
  180. }
  181. pci_intx(dev, 0); /* disable intx */
  182. }
  183. void disable_msi_mode(struct pci_dev *dev, int pos, int type)
  184. {
  185. u16 control;
  186. pci_read_config_word(dev, msi_control_reg(pos), &control);
  187. if (type == PCI_CAP_ID_MSI) {
  188. /* Set enabled bits to single MSI & enable MSI_enable bit */
  189. msi_disable(control);
  190. pci_write_config_word(dev, msi_control_reg(pos), control);
  191. dev->msi_enabled = 0;
  192. } else {
  193. msix_disable(control);
  194. pci_write_config_word(dev, msi_control_reg(pos), control);
  195. dev->msix_enabled = 0;
  196. }
  197. pci_intx(dev, 1); /* enable intx */
  198. }
  199. #ifdef CONFIG_PM
  200. static int __pci_save_msi_state(struct pci_dev *dev)
  201. {
  202. int pos, i = 0;
  203. u16 control;
  204. struct pci_cap_saved_state *save_state;
  205. u32 *cap;
  206. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  207. if (pos <= 0 || dev->no_msi)
  208. return 0;
  209. pci_read_config_word(dev, msi_control_reg(pos), &control);
  210. if (!(control & PCI_MSI_FLAGS_ENABLE))
  211. return 0;
  212. save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
  213. GFP_KERNEL);
  214. if (!save_state) {
  215. printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
  216. return -ENOMEM;
  217. }
  218. cap = &save_state->data[0];
  219. pci_read_config_dword(dev, pos, &cap[i++]);
  220. control = cap[0] >> 16;
  221. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
  222. if (control & PCI_MSI_FLAGS_64BIT) {
  223. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
  224. pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
  225. } else
  226. pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
  227. if (control & PCI_MSI_FLAGS_MASKBIT)
  228. pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
  229. save_state->cap_nr = PCI_CAP_ID_MSI;
  230. pci_add_saved_cap(dev, save_state);
  231. return 0;
  232. }
  233. static void __pci_restore_msi_state(struct pci_dev *dev)
  234. {
  235. int i = 0, pos;
  236. u16 control;
  237. struct pci_cap_saved_state *save_state;
  238. u32 *cap;
  239. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
  240. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  241. if (!save_state || pos <= 0)
  242. return;
  243. cap = &save_state->data[0];
  244. control = cap[i++] >> 16;
  245. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
  246. if (control & PCI_MSI_FLAGS_64BIT) {
  247. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
  248. pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
  249. } else
  250. pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
  251. if (control & PCI_MSI_FLAGS_MASKBIT)
  252. pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
  253. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  254. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  255. pci_remove_saved_cap(save_state);
  256. kfree(save_state);
  257. }
  258. static int __pci_save_msix_state(struct pci_dev *dev)
  259. {
  260. int pos;
  261. int irq, head, tail = 0;
  262. u16 control;
  263. struct pci_cap_saved_state *save_state;
  264. if (!dev->msix_enabled)
  265. return 0;
  266. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  267. if (pos <= 0 || dev->no_msi)
  268. return 0;
  269. /* save the capability */
  270. pci_read_config_word(dev, msi_control_reg(pos), &control);
  271. if (!(control & PCI_MSIX_FLAGS_ENABLE))
  272. return 0;
  273. save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
  274. GFP_KERNEL);
  275. if (!save_state) {
  276. printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
  277. return -ENOMEM;
  278. }
  279. *((u16 *)&save_state->data[0]) = control;
  280. /* save the table */
  281. irq = head = dev->first_msi_irq;
  282. while (head != tail) {
  283. struct msi_desc *entry;
  284. entry = get_irq_msi(irq);
  285. read_msi_msg(irq, &entry->msg_save);
  286. tail = entry->link.tail;
  287. irq = tail;
  288. }
  289. save_state->cap_nr = PCI_CAP_ID_MSIX;
  290. pci_add_saved_cap(dev, save_state);
  291. return 0;
  292. }
  293. int pci_save_msi_state(struct pci_dev *dev)
  294. {
  295. int rc;
  296. rc = __pci_save_msi_state(dev);
  297. if (rc)
  298. return rc;
  299. rc = __pci_save_msix_state(dev);
  300. return rc;
  301. }
  302. static void __pci_restore_msix_state(struct pci_dev *dev)
  303. {
  304. u16 save;
  305. int pos;
  306. int irq, head, tail = 0;
  307. struct msi_desc *entry;
  308. struct pci_cap_saved_state *save_state;
  309. if (!dev->msix_enabled)
  310. return;
  311. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
  312. if (!save_state)
  313. return;
  314. save = *((u16 *)&save_state->data[0]);
  315. pci_remove_saved_cap(save_state);
  316. kfree(save_state);
  317. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  318. if (pos <= 0)
  319. return;
  320. /* route the table */
  321. irq = head = dev->first_msi_irq;
  322. while (head != tail) {
  323. entry = get_irq_msi(irq);
  324. write_msi_msg(irq, &entry->msg_save);
  325. tail = entry->link.tail;
  326. irq = tail;
  327. }
  328. pci_write_config_word(dev, msi_control_reg(pos), save);
  329. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  330. }
  331. void pci_restore_msi_state(struct pci_dev *dev)
  332. {
  333. __pci_restore_msi_state(dev);
  334. __pci_restore_msix_state(dev);
  335. }
  336. #endif /* CONFIG_PM */
  337. /**
  338. * msi_capability_init - configure device's MSI capability structure
  339. * @dev: pointer to the pci_dev data structure of MSI device function
  340. *
  341. * Setup the MSI capability structure of device function with a single
  342. * MSI irq, regardless of device function is capable of handling
  343. * multiple messages. A return of zero indicates the successful setup
  344. * of an entry zero with the new MSI irq or non-zero for otherwise.
  345. **/
  346. static int msi_capability_init(struct pci_dev *dev)
  347. {
  348. struct msi_desc *entry;
  349. int pos, irq;
  350. u16 control;
  351. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  352. pci_read_config_word(dev, msi_control_reg(pos), &control);
  353. /* MSI Entry Initialization */
  354. entry = alloc_msi_entry();
  355. if (!entry)
  356. return -ENOMEM;
  357. entry->msi_attrib.type = PCI_CAP_ID_MSI;
  358. entry->msi_attrib.is_64 = is_64bit_address(control);
  359. entry->msi_attrib.entry_nr = 0;
  360. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  361. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  362. entry->msi_attrib.pos = pos;
  363. if (is_mask_bit_support(control)) {
  364. entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
  365. is_64bit_address(control));
  366. }
  367. entry->dev = dev;
  368. if (entry->msi_attrib.maskbit) {
  369. unsigned int maskbits, temp;
  370. /* All MSIs are unmasked by default, Mask them all */
  371. pci_read_config_dword(dev,
  372. msi_mask_bits_reg(pos, is_64bit_address(control)),
  373. &maskbits);
  374. temp = (1 << multi_msi_capable(control));
  375. temp = ((temp - 1) & ~temp);
  376. maskbits |= temp;
  377. pci_write_config_dword(dev,
  378. msi_mask_bits_reg(pos, is_64bit_address(control)),
  379. maskbits);
  380. }
  381. /* Configure MSI capability structure */
  382. irq = arch_setup_msi_irq(dev, entry);
  383. if (irq < 0) {
  384. kmem_cache_free(msi_cachep, entry);
  385. return irq;
  386. }
  387. entry->link.head = irq;
  388. entry->link.tail = irq;
  389. dev->first_msi_irq = irq;
  390. set_irq_msi(irq, entry);
  391. /* Set MSI enabled bits */
  392. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  393. dev->irq = irq;
  394. return 0;
  395. }
  396. /**
  397. * msix_capability_init - configure device's MSI-X capability
  398. * @dev: pointer to the pci_dev data structure of MSI-X device function
  399. * @entries: pointer to an array of struct msix_entry entries
  400. * @nvec: number of @entries
  401. *
  402. * Setup the MSI-X capability structure of device function with a
  403. * single MSI-X irq. A return of zero indicates the successful setup of
  404. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  405. **/
  406. static int msix_capability_init(struct pci_dev *dev,
  407. struct msix_entry *entries, int nvec)
  408. {
  409. struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
  410. int irq, pos, i, j, nr_entries, temp = 0;
  411. unsigned long phys_addr;
  412. u32 table_offset;
  413. u16 control;
  414. u8 bir;
  415. void __iomem *base;
  416. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  417. /* Request & Map MSI-X table region */
  418. pci_read_config_word(dev, msi_control_reg(pos), &control);
  419. nr_entries = multi_msix_capable(control);
  420. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  421. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  422. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  423. phys_addr = pci_resource_start (dev, bir) + table_offset;
  424. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  425. if (base == NULL)
  426. return -ENOMEM;
  427. /* MSI-X Table Initialization */
  428. for (i = 0; i < nvec; i++) {
  429. entry = alloc_msi_entry();
  430. if (!entry)
  431. break;
  432. j = entries[i].entry;
  433. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  434. entry->msi_attrib.is_64 = 1;
  435. entry->msi_attrib.entry_nr = j;
  436. entry->msi_attrib.maskbit = 1;
  437. entry->msi_attrib.default_irq = dev->irq;
  438. entry->msi_attrib.pos = pos;
  439. entry->dev = dev;
  440. entry->mask_base = base;
  441. /* Configure MSI-X capability structure */
  442. irq = arch_setup_msi_irq(dev, entry);
  443. if (irq < 0) {
  444. kmem_cache_free(msi_cachep, entry);
  445. break;
  446. }
  447. entries[i].vector = irq;
  448. if (!head) {
  449. entry->link.head = irq;
  450. entry->link.tail = irq;
  451. head = entry;
  452. } else {
  453. entry->link.head = temp;
  454. entry->link.tail = tail->link.tail;
  455. tail->link.tail = irq;
  456. head->link.head = irq;
  457. }
  458. temp = irq;
  459. tail = entry;
  460. set_irq_msi(irq, entry);
  461. }
  462. if (i != nvec) {
  463. int avail = i - 1;
  464. i--;
  465. for (; i >= 0; i--) {
  466. irq = (entries + i)->vector;
  467. msi_free_irq(dev, irq);
  468. (entries + i)->vector = 0;
  469. }
  470. /* If we had some success report the number of irqs
  471. * we succeeded in setting up.
  472. */
  473. if (avail <= 0)
  474. avail = -EBUSY;
  475. return avail;
  476. }
  477. dev->first_msi_irq = entries[0].vector;
  478. /* Set MSI-X enabled bits */
  479. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  480. return 0;
  481. }
  482. /**
  483. * pci_msi_supported - check whether MSI may be enabled on device
  484. * @dev: pointer to the pci_dev data structure of MSI device function
  485. *
  486. * Look at global flags, the device itself, and its parent busses
  487. * to return 0 if MSI are supported for the device.
  488. **/
  489. static
  490. int pci_msi_supported(struct pci_dev * dev)
  491. {
  492. struct pci_bus *bus;
  493. /* MSI must be globally enabled and supported by the device */
  494. if (!pci_msi_enable || !dev || dev->no_msi)
  495. return -EINVAL;
  496. /* Any bridge which does NOT route MSI transactions from it's
  497. * secondary bus to it's primary bus must set NO_MSI flag on
  498. * the secondary pci_bus.
  499. * We expect only arch-specific PCI host bus controller driver
  500. * or quirks for specific PCI bridges to be setting NO_MSI.
  501. */
  502. for (bus = dev->bus; bus; bus = bus->parent)
  503. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  504. return -EINVAL;
  505. return 0;
  506. }
  507. /**
  508. * pci_enable_msi - configure device's MSI capability structure
  509. * @dev: pointer to the pci_dev data structure of MSI device function
  510. *
  511. * Setup the MSI capability structure of device function with
  512. * a single MSI irq upon its software driver call to request for
  513. * MSI mode enabled on its hardware device function. A return of zero
  514. * indicates the successful setup of an entry zero with the new MSI
  515. * irq or non-zero for otherwise.
  516. **/
  517. int pci_enable_msi(struct pci_dev* dev)
  518. {
  519. int pos, status;
  520. if (pci_msi_supported(dev) < 0)
  521. return -EINVAL;
  522. status = msi_init();
  523. if (status < 0)
  524. return status;
  525. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  526. if (!pos)
  527. return -EINVAL;
  528. WARN_ON(!!dev->msi_enabled);
  529. /* Check whether driver already requested for MSI-X irqs */
  530. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  531. if (pos > 0 && dev->msix_enabled) {
  532. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  533. "Device already has MSI-X enabled\n",
  534. pci_name(dev));
  535. return -EINVAL;
  536. }
  537. status = msi_capability_init(dev);
  538. return status;
  539. }
  540. void pci_disable_msi(struct pci_dev* dev)
  541. {
  542. struct msi_desc *entry;
  543. int pos, default_irq;
  544. u16 control;
  545. if (!pci_msi_enable)
  546. return;
  547. if (!dev)
  548. return;
  549. if (!dev->msi_enabled)
  550. return;
  551. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  552. if (!pos)
  553. return;
  554. pci_read_config_word(dev, msi_control_reg(pos), &control);
  555. if (!(control & PCI_MSI_FLAGS_ENABLE))
  556. return;
  557. disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  558. entry = get_irq_msi(dev->first_msi_irq);
  559. if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  560. return;
  561. }
  562. if (irq_has_action(dev->first_msi_irq)) {
  563. printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
  564. "free_irq() on MSI irq %d\n",
  565. pci_name(dev), dev->first_msi_irq);
  566. BUG_ON(irq_has_action(dev->first_msi_irq));
  567. } else {
  568. default_irq = entry->msi_attrib.default_irq;
  569. msi_free_irq(dev, dev->first_msi_irq);
  570. /* Restore dev->irq to its default pin-assertion irq */
  571. dev->irq = default_irq;
  572. }
  573. dev->first_msi_irq = 0;
  574. }
  575. static int msi_free_irq(struct pci_dev* dev, int irq)
  576. {
  577. struct msi_desc *entry;
  578. int head, entry_nr, type;
  579. void __iomem *base;
  580. entry = get_irq_msi(irq);
  581. if (!entry || entry->dev != dev) {
  582. return -EINVAL;
  583. }
  584. type = entry->msi_attrib.type;
  585. entry_nr = entry->msi_attrib.entry_nr;
  586. head = entry->link.head;
  587. base = entry->mask_base;
  588. get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
  589. get_irq_msi(entry->link.tail)->link.head = entry->link.head;
  590. arch_teardown_msi_irq(irq);
  591. kmem_cache_free(msi_cachep, entry);
  592. if (type == PCI_CAP_ID_MSIX) {
  593. writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
  594. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  595. if (head == irq)
  596. iounmap(base);
  597. }
  598. return 0;
  599. }
  600. /**
  601. * pci_enable_msix - configure device's MSI-X capability structure
  602. * @dev: pointer to the pci_dev data structure of MSI-X device function
  603. * @entries: pointer to an array of MSI-X entries
  604. * @nvec: number of MSI-X irqs requested for allocation by device driver
  605. *
  606. * Setup the MSI-X capability structure of device function with the number
  607. * of requested irqs upon its software driver call to request for
  608. * MSI-X mode enabled on its hardware device function. A return of zero
  609. * indicates the successful configuration of MSI-X capability structure
  610. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  611. * Or a return of > 0 indicates that driver request is exceeding the number
  612. * of irqs available. Driver should use the returned value to re-send
  613. * its request.
  614. **/
  615. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  616. {
  617. int status, pos, nr_entries;
  618. int i, j;
  619. u16 control;
  620. if (!entries || pci_msi_supported(dev) < 0)
  621. return -EINVAL;
  622. status = msi_init();
  623. if (status < 0)
  624. return status;
  625. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  626. if (!pos)
  627. return -EINVAL;
  628. pci_read_config_word(dev, msi_control_reg(pos), &control);
  629. nr_entries = multi_msix_capable(control);
  630. if (nvec > nr_entries)
  631. return -EINVAL;
  632. /* Check for any invalid entries */
  633. for (i = 0; i < nvec; i++) {
  634. if (entries[i].entry >= nr_entries)
  635. return -EINVAL; /* invalid entry */
  636. for (j = i + 1; j < nvec; j++) {
  637. if (entries[i].entry == entries[j].entry)
  638. return -EINVAL; /* duplicate entry */
  639. }
  640. }
  641. WARN_ON(!!dev->msix_enabled);
  642. /* Check whether driver already requested for MSI irq */
  643. if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
  644. dev->msi_enabled) {
  645. printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
  646. "Device already has an MSI irq assigned\n",
  647. pci_name(dev));
  648. return -EINVAL;
  649. }
  650. status = msix_capability_init(dev, entries, nvec);
  651. return status;
  652. }
  653. void pci_disable_msix(struct pci_dev* dev)
  654. {
  655. int irq, head, tail = 0, warning = 0;
  656. int pos;
  657. u16 control;
  658. if (!pci_msi_enable)
  659. return;
  660. if (!dev)
  661. return;
  662. if (!dev->msix_enabled)
  663. return;
  664. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  665. if (!pos)
  666. return;
  667. pci_read_config_word(dev, msi_control_reg(pos), &control);
  668. if (!(control & PCI_MSIX_FLAGS_ENABLE))
  669. return;
  670. disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  671. irq = head = dev->first_msi_irq;
  672. while (head != tail) {
  673. tail = get_irq_msi(irq)->link.tail;
  674. if (irq_has_action(irq))
  675. warning = 1;
  676. else if (irq != head) /* Release MSI-X irq */
  677. msi_free_irq(dev, irq);
  678. irq = tail;
  679. }
  680. msi_free_irq(dev, irq);
  681. if (warning) {
  682. printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
  683. "free_irq() on all MSI-X irqs\n",
  684. pci_name(dev));
  685. BUG_ON(warning > 0);
  686. }
  687. dev->first_msi_irq = 0;
  688. }
  689. /**
  690. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  691. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  692. *
  693. * Being called during hotplug remove, from which the device function
  694. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  695. * allocated for this device function, are reclaimed to unused state,
  696. * which may be used later on.
  697. **/
  698. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  699. {
  700. if (!pci_msi_enable || !dev)
  701. return;
  702. if (dev->msi_enabled) {
  703. if (irq_has_action(dev->first_msi_irq)) {
  704. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  705. "called without free_irq() on MSI irq %d\n",
  706. pci_name(dev), dev->first_msi_irq);
  707. BUG_ON(irq_has_action(dev->first_msi_irq));
  708. } else /* Release MSI irq assigned to this device */
  709. msi_free_irq(dev, dev->first_msi_irq);
  710. }
  711. if (dev->msix_enabled) {
  712. int irq, head, tail = 0, warning = 0;
  713. void __iomem *base = NULL;
  714. irq = head = dev->first_msi_irq;
  715. while (head != tail) {
  716. tail = get_irq_msi(irq)->link.tail;
  717. base = get_irq_msi(irq)->mask_base;
  718. if (irq_has_action(irq))
  719. warning = 1;
  720. else if (irq != head) /* Release MSI-X irq */
  721. msi_free_irq(dev, irq);
  722. irq = tail;
  723. }
  724. msi_free_irq(dev, irq);
  725. if (warning) {
  726. iounmap(base);
  727. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  728. "called without free_irq() on all MSI-X irqs\n",
  729. pci_name(dev));
  730. BUG_ON(warning > 0);
  731. }
  732. }
  733. }
  734. void pci_no_msi(void)
  735. {
  736. pci_msi_enable = 0;
  737. }
  738. EXPORT_SYMBOL(pci_enable_msi);
  739. EXPORT_SYMBOL(pci_disable_msi);
  740. EXPORT_SYMBOL(pci_enable_msix);
  741. EXPORT_SYMBOL(pci_disable_msix);