msi.c 24 KB

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