msi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 kmem_cache_t* 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. if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
  221. /* PCI Express Endpoint device detected */
  222. pci_intx(dev, 0); /* disable intx */
  223. }
  224. }
  225. void disable_msi_mode(struct pci_dev *dev, int pos, int type)
  226. {
  227. u16 control;
  228. pci_read_config_word(dev, msi_control_reg(pos), &control);
  229. if (type == PCI_CAP_ID_MSI) {
  230. /* Set enabled bits to single MSI & enable MSI_enable bit */
  231. msi_disable(control);
  232. pci_write_config_word(dev, msi_control_reg(pos), control);
  233. dev->msi_enabled = 0;
  234. } else {
  235. msix_disable(control);
  236. pci_write_config_word(dev, msi_control_reg(pos), control);
  237. dev->msix_enabled = 0;
  238. }
  239. if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
  240. /* PCI Express Endpoint device detected */
  241. pci_intx(dev, 1); /* enable intx */
  242. }
  243. }
  244. static int msi_lookup_irq(struct pci_dev *dev, int type)
  245. {
  246. int irq;
  247. unsigned long flags;
  248. spin_lock_irqsave(&msi_lock, flags);
  249. for (irq = 0; irq < NR_IRQS; irq++) {
  250. if (!msi_desc[irq] || msi_desc[irq]->dev != dev ||
  251. msi_desc[irq]->msi_attrib.type != type ||
  252. msi_desc[irq]->msi_attrib.default_irq != dev->irq)
  253. continue;
  254. spin_unlock_irqrestore(&msi_lock, flags);
  255. /* This pre-assigned MSI irq for this device
  256. already exits. Override dev->irq with this irq */
  257. dev->irq = irq;
  258. return 0;
  259. }
  260. spin_unlock_irqrestore(&msi_lock, flags);
  261. return -EACCES;
  262. }
  263. void pci_scan_msi_device(struct pci_dev *dev)
  264. {
  265. if (!dev)
  266. return;
  267. }
  268. #ifdef CONFIG_PM
  269. int pci_save_msi_state(struct pci_dev *dev)
  270. {
  271. int pos, i = 0;
  272. u16 control;
  273. struct pci_cap_saved_state *save_state;
  274. u32 *cap;
  275. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  276. if (pos <= 0 || dev->no_msi)
  277. return 0;
  278. pci_read_config_word(dev, msi_control_reg(pos), &control);
  279. if (!(control & PCI_MSI_FLAGS_ENABLE))
  280. return 0;
  281. save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
  282. GFP_KERNEL);
  283. if (!save_state) {
  284. printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
  285. return -ENOMEM;
  286. }
  287. cap = &save_state->data[0];
  288. pci_read_config_dword(dev, pos, &cap[i++]);
  289. control = cap[0] >> 16;
  290. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
  291. if (control & PCI_MSI_FLAGS_64BIT) {
  292. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
  293. pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
  294. } else
  295. pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
  296. if (control & PCI_MSI_FLAGS_MASKBIT)
  297. pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
  298. save_state->cap_nr = PCI_CAP_ID_MSI;
  299. pci_add_saved_cap(dev, save_state);
  300. return 0;
  301. }
  302. void pci_restore_msi_state(struct pci_dev *dev)
  303. {
  304. int i = 0, pos;
  305. u16 control;
  306. struct pci_cap_saved_state *save_state;
  307. u32 *cap;
  308. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
  309. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  310. if (!save_state || pos <= 0)
  311. return;
  312. cap = &save_state->data[0];
  313. control = cap[i++] >> 16;
  314. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
  315. if (control & PCI_MSI_FLAGS_64BIT) {
  316. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
  317. pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
  318. } else
  319. pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
  320. if (control & PCI_MSI_FLAGS_MASKBIT)
  321. pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
  322. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  323. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  324. pci_remove_saved_cap(save_state);
  325. kfree(save_state);
  326. }
  327. int pci_save_msix_state(struct pci_dev *dev)
  328. {
  329. int pos;
  330. int temp;
  331. int irq, head, tail = 0;
  332. u16 control;
  333. struct pci_cap_saved_state *save_state;
  334. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  335. if (pos <= 0 || dev->no_msi)
  336. return 0;
  337. /* save the capability */
  338. pci_read_config_word(dev, msi_control_reg(pos), &control);
  339. if (!(control & PCI_MSIX_FLAGS_ENABLE))
  340. return 0;
  341. save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
  342. GFP_KERNEL);
  343. if (!save_state) {
  344. printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
  345. return -ENOMEM;
  346. }
  347. *((u16 *)&save_state->data[0]) = control;
  348. /* save the table */
  349. temp = dev->irq;
  350. if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
  351. kfree(save_state);
  352. return -EINVAL;
  353. }
  354. irq = head = dev->irq;
  355. while (head != tail) {
  356. struct msi_desc *entry;
  357. entry = msi_desc[irq];
  358. read_msi_msg(irq, &entry->msg_save);
  359. tail = msi_desc[irq]->link.tail;
  360. irq = tail;
  361. }
  362. dev->irq = temp;
  363. save_state->cap_nr = PCI_CAP_ID_MSIX;
  364. pci_add_saved_cap(dev, save_state);
  365. return 0;
  366. }
  367. void pci_restore_msix_state(struct pci_dev *dev)
  368. {
  369. u16 save;
  370. int pos;
  371. int irq, head, tail = 0;
  372. struct msi_desc *entry;
  373. int temp;
  374. struct pci_cap_saved_state *save_state;
  375. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
  376. if (!save_state)
  377. return;
  378. save = *((u16 *)&save_state->data[0]);
  379. pci_remove_saved_cap(save_state);
  380. kfree(save_state);
  381. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  382. if (pos <= 0)
  383. return;
  384. /* route the table */
  385. temp = dev->irq;
  386. if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX))
  387. return;
  388. irq = head = dev->irq;
  389. while (head != tail) {
  390. entry = msi_desc[irq];
  391. write_msi_msg(irq, &entry->msg_save);
  392. tail = msi_desc[irq]->link.tail;
  393. irq = tail;
  394. }
  395. dev->irq = temp;
  396. pci_write_config_word(dev, msi_control_reg(pos), save);
  397. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  398. }
  399. #endif
  400. /**
  401. * msi_capability_init - configure device's MSI capability structure
  402. * @dev: pointer to the pci_dev data structure of MSI device function
  403. *
  404. * Setup the MSI capability structure of device function with a single
  405. * MSI irq, regardless of device function is capable of handling
  406. * multiple messages. A return of zero indicates the successful setup
  407. * of an entry zero with the new MSI irq or non-zero for otherwise.
  408. **/
  409. static int msi_capability_init(struct pci_dev *dev)
  410. {
  411. int status;
  412. struct msi_desc *entry;
  413. int pos, irq;
  414. u16 control;
  415. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  416. pci_read_config_word(dev, msi_control_reg(pos), &control);
  417. /* MSI Entry Initialization */
  418. irq = create_msi_irq();
  419. if (irq < 0)
  420. return irq;
  421. entry = get_irq_data(irq);
  422. entry->link.head = irq;
  423. entry->link.tail = irq;
  424. entry->msi_attrib.type = PCI_CAP_ID_MSI;
  425. entry->msi_attrib.is_64 = is_64bit_address(control);
  426. entry->msi_attrib.entry_nr = 0;
  427. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  428. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  429. entry->msi_attrib.pos = pos;
  430. if (is_mask_bit_support(control)) {
  431. entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
  432. is_64bit_address(control));
  433. }
  434. entry->dev = dev;
  435. if (entry->msi_attrib.maskbit) {
  436. unsigned int maskbits, temp;
  437. /* All MSIs are unmasked by default, Mask them all */
  438. pci_read_config_dword(dev,
  439. msi_mask_bits_reg(pos, is_64bit_address(control)),
  440. &maskbits);
  441. temp = (1 << multi_msi_capable(control));
  442. temp = ((temp - 1) & ~temp);
  443. maskbits |= temp;
  444. pci_write_config_dword(dev,
  445. msi_mask_bits_reg(pos, is_64bit_address(control)),
  446. maskbits);
  447. }
  448. /* Configure MSI capability structure */
  449. status = arch_setup_msi_irq(irq, dev);
  450. if (status < 0) {
  451. destroy_msi_irq(irq);
  452. return status;
  453. }
  454. attach_msi_entry(entry, irq);
  455. /* Set MSI enabled bits */
  456. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  457. dev->irq = irq;
  458. return 0;
  459. }
  460. /**
  461. * msix_capability_init - configure device's MSI-X capability
  462. * @dev: pointer to the pci_dev data structure of MSI-X device function
  463. * @entries: pointer to an array of struct msix_entry entries
  464. * @nvec: number of @entries
  465. *
  466. * Setup the MSI-X capability structure of device function with a
  467. * single MSI-X irq. A return of zero indicates the successful setup of
  468. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  469. **/
  470. static int msix_capability_init(struct pci_dev *dev,
  471. struct msix_entry *entries, int nvec)
  472. {
  473. struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
  474. int status;
  475. int irq, pos, i, j, nr_entries, temp = 0;
  476. unsigned long phys_addr;
  477. u32 table_offset;
  478. u16 control;
  479. u8 bir;
  480. void __iomem *base;
  481. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  482. /* Request & Map MSI-X table region */
  483. pci_read_config_word(dev, msi_control_reg(pos), &control);
  484. nr_entries = multi_msix_capable(control);
  485. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  486. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  487. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  488. phys_addr = pci_resource_start (dev, bir) + table_offset;
  489. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  490. if (base == NULL)
  491. return -ENOMEM;
  492. /* MSI-X Table Initialization */
  493. for (i = 0; i < nvec; i++) {
  494. irq = create_msi_irq();
  495. if (irq < 0)
  496. break;
  497. entry = get_irq_data(irq);
  498. j = entries[i].entry;
  499. entries[i].vector = irq;
  500. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  501. entry->msi_attrib.is_64 = 1;
  502. entry->msi_attrib.entry_nr = j;
  503. entry->msi_attrib.maskbit = 1;
  504. entry->msi_attrib.default_irq = dev->irq;
  505. entry->msi_attrib.pos = pos;
  506. entry->dev = dev;
  507. entry->mask_base = base;
  508. if (!head) {
  509. entry->link.head = irq;
  510. entry->link.tail = irq;
  511. head = entry;
  512. } else {
  513. entry->link.head = temp;
  514. entry->link.tail = tail->link.tail;
  515. tail->link.tail = irq;
  516. head->link.head = irq;
  517. }
  518. temp = irq;
  519. tail = entry;
  520. /* Configure MSI-X capability structure */
  521. status = arch_setup_msi_irq(irq, dev);
  522. if (status < 0) {
  523. destroy_msi_irq(irq);
  524. break;
  525. }
  526. attach_msi_entry(entry, irq);
  527. }
  528. if (i != nvec) {
  529. int avail = i - 1;
  530. i--;
  531. for (; i >= 0; i--) {
  532. irq = (entries + i)->vector;
  533. msi_free_irq(dev, irq);
  534. (entries + i)->vector = 0;
  535. }
  536. /* If we had some success report the number of irqs
  537. * we succeeded in setting up.
  538. */
  539. if (avail <= 0)
  540. avail = -EBUSY;
  541. return avail;
  542. }
  543. /* Set MSI-X enabled bits */
  544. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  545. return 0;
  546. }
  547. /**
  548. * pci_msi_supported - check whether MSI may be enabled on device
  549. * @dev: pointer to the pci_dev data structure of MSI device function
  550. *
  551. * Look at global flags, the device itself, and its parent busses
  552. * to return 0 if MSI are supported for the device.
  553. **/
  554. static
  555. int pci_msi_supported(struct pci_dev * dev)
  556. {
  557. struct pci_bus *bus;
  558. /* MSI must be globally enabled and supported by the device */
  559. if (!pci_msi_enable || !dev || dev->no_msi)
  560. return -EINVAL;
  561. /* Any bridge which does NOT route MSI transactions from it's
  562. * secondary bus to it's primary bus must set NO_MSI flag on
  563. * the secondary pci_bus.
  564. * We expect only arch-specific PCI host bus controller driver
  565. * or quirks for specific PCI bridges to be setting NO_MSI.
  566. */
  567. for (bus = dev->bus; bus; bus = bus->parent)
  568. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  569. return -EINVAL;
  570. return 0;
  571. }
  572. /**
  573. * pci_enable_msi - configure device's MSI capability structure
  574. * @dev: pointer to the pci_dev data structure of MSI device function
  575. *
  576. * Setup the MSI capability structure of device function with
  577. * a single MSI irq upon its software driver call to request for
  578. * MSI mode enabled on its hardware device function. A return of zero
  579. * indicates the successful setup of an entry zero with the new MSI
  580. * irq or non-zero for otherwise.
  581. **/
  582. int pci_enable_msi(struct pci_dev* dev)
  583. {
  584. int pos, temp, status;
  585. if (pci_msi_supported(dev) < 0)
  586. return -EINVAL;
  587. temp = dev->irq;
  588. status = msi_init();
  589. if (status < 0)
  590. return status;
  591. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  592. if (!pos)
  593. return -EINVAL;
  594. WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSI));
  595. /* Check whether driver already requested for MSI-X irqs */
  596. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  597. if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
  598. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  599. "Device already has MSI-X irq assigned\n",
  600. pci_name(dev));
  601. dev->irq = temp;
  602. return -EINVAL;
  603. }
  604. status = msi_capability_init(dev);
  605. return status;
  606. }
  607. void pci_disable_msi(struct pci_dev* dev)
  608. {
  609. struct msi_desc *entry;
  610. int pos, default_irq;
  611. u16 control;
  612. unsigned long flags;
  613. if (!pci_msi_enable)
  614. return;
  615. if (!dev)
  616. return;
  617. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  618. if (!pos)
  619. return;
  620. pci_read_config_word(dev, msi_control_reg(pos), &control);
  621. if (!(control & PCI_MSI_FLAGS_ENABLE))
  622. return;
  623. disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  624. spin_lock_irqsave(&msi_lock, flags);
  625. entry = msi_desc[dev->irq];
  626. if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  627. spin_unlock_irqrestore(&msi_lock, flags);
  628. return;
  629. }
  630. if (irq_has_action(dev->irq)) {
  631. spin_unlock_irqrestore(&msi_lock, flags);
  632. printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
  633. "free_irq() on MSI irq %d\n",
  634. pci_name(dev), dev->irq);
  635. BUG_ON(irq_has_action(dev->irq));
  636. } else {
  637. default_irq = entry->msi_attrib.default_irq;
  638. spin_unlock_irqrestore(&msi_lock, flags);
  639. msi_free_irq(dev, dev->irq);
  640. /* Restore dev->irq to its default pin-assertion irq */
  641. dev->irq = default_irq;
  642. }
  643. }
  644. static int msi_free_irq(struct pci_dev* dev, int irq)
  645. {
  646. struct msi_desc *entry;
  647. int head, entry_nr, type;
  648. void __iomem *base;
  649. unsigned long flags;
  650. arch_teardown_msi_irq(irq);
  651. spin_lock_irqsave(&msi_lock, flags);
  652. entry = msi_desc[irq];
  653. if (!entry || entry->dev != dev) {
  654. spin_unlock_irqrestore(&msi_lock, flags);
  655. return -EINVAL;
  656. }
  657. type = entry->msi_attrib.type;
  658. entry_nr = entry->msi_attrib.entry_nr;
  659. head = entry->link.head;
  660. base = entry->mask_base;
  661. msi_desc[entry->link.head]->link.tail = entry->link.tail;
  662. msi_desc[entry->link.tail]->link.head = entry->link.head;
  663. entry->dev = NULL;
  664. msi_desc[irq] = NULL;
  665. spin_unlock_irqrestore(&msi_lock, flags);
  666. destroy_msi_irq(irq);
  667. if (type == PCI_CAP_ID_MSIX) {
  668. writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
  669. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  670. if (head == irq)
  671. iounmap(base);
  672. }
  673. return 0;
  674. }
  675. /**
  676. * pci_enable_msix - configure device's MSI-X capability structure
  677. * @dev: pointer to the pci_dev data structure of MSI-X device function
  678. * @entries: pointer to an array of MSI-X entries
  679. * @nvec: number of MSI-X irqs requested for allocation by device driver
  680. *
  681. * Setup the MSI-X capability structure of device function with the number
  682. * of requested irqs upon its software driver call to request for
  683. * MSI-X mode enabled on its hardware device function. A return of zero
  684. * indicates the successful configuration of MSI-X capability structure
  685. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  686. * Or a return of > 0 indicates that driver request is exceeding the number
  687. * of irqs available. Driver should use the returned value to re-send
  688. * its request.
  689. **/
  690. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  691. {
  692. int status, pos, nr_entries;
  693. int i, j, temp;
  694. u16 control;
  695. if (!entries || pci_msi_supported(dev) < 0)
  696. return -EINVAL;
  697. status = msi_init();
  698. if (status < 0)
  699. return status;
  700. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  701. if (!pos)
  702. return -EINVAL;
  703. pci_read_config_word(dev, msi_control_reg(pos), &control);
  704. nr_entries = multi_msix_capable(control);
  705. if (nvec > nr_entries)
  706. return -EINVAL;
  707. /* Check for any invalid entries */
  708. for (i = 0; i < nvec; i++) {
  709. if (entries[i].entry >= nr_entries)
  710. return -EINVAL; /* invalid entry */
  711. for (j = i + 1; j < nvec; j++) {
  712. if (entries[i].entry == entries[j].entry)
  713. return -EINVAL; /* duplicate entry */
  714. }
  715. }
  716. temp = dev->irq;
  717. WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSIX));
  718. /* Check whether driver already requested for MSI irq */
  719. if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
  720. !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
  721. printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
  722. "Device already has an MSI irq assigned\n",
  723. pci_name(dev));
  724. dev->irq = temp;
  725. return -EINVAL;
  726. }
  727. status = msix_capability_init(dev, entries, nvec);
  728. return status;
  729. }
  730. void pci_disable_msix(struct pci_dev* dev)
  731. {
  732. int pos, temp;
  733. u16 control;
  734. if (!pci_msi_enable)
  735. return;
  736. if (!dev)
  737. return;
  738. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  739. if (!pos)
  740. return;
  741. pci_read_config_word(dev, msi_control_reg(pos), &control);
  742. if (!(control & PCI_MSIX_FLAGS_ENABLE))
  743. return;
  744. disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  745. temp = dev->irq;
  746. if (!msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
  747. int irq, head, tail = 0, warning = 0;
  748. unsigned long flags;
  749. irq = head = dev->irq;
  750. dev->irq = temp; /* Restore pin IRQ */
  751. while (head != tail) {
  752. spin_lock_irqsave(&msi_lock, flags);
  753. tail = msi_desc[irq]->link.tail;
  754. spin_unlock_irqrestore(&msi_lock, flags);
  755. if (irq_has_action(irq))
  756. warning = 1;
  757. else if (irq != head) /* Release MSI-X irq */
  758. msi_free_irq(dev, irq);
  759. irq = tail;
  760. }
  761. msi_free_irq(dev, irq);
  762. if (warning) {
  763. printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
  764. "free_irq() on all MSI-X irqs\n",
  765. pci_name(dev));
  766. BUG_ON(warning > 0);
  767. }
  768. }
  769. }
  770. /**
  771. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  772. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  773. *
  774. * Being called during hotplug remove, from which the device function
  775. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  776. * allocated for this device function, are reclaimed to unused state,
  777. * which may be used later on.
  778. **/
  779. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  780. {
  781. int pos, temp;
  782. unsigned long flags;
  783. if (!pci_msi_enable || !dev)
  784. return;
  785. temp = dev->irq; /* Save IOAPIC IRQ */
  786. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  787. if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
  788. if (irq_has_action(dev->irq)) {
  789. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  790. "called without free_irq() on MSI irq %d\n",
  791. pci_name(dev), dev->irq);
  792. BUG_ON(irq_has_action(dev->irq));
  793. } else /* Release MSI irq assigned to this device */
  794. msi_free_irq(dev, dev->irq);
  795. dev->irq = temp; /* Restore IOAPIC IRQ */
  796. }
  797. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  798. if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
  799. int irq, head, tail = 0, warning = 0;
  800. void __iomem *base = NULL;
  801. irq = head = dev->irq;
  802. while (head != tail) {
  803. spin_lock_irqsave(&msi_lock, flags);
  804. tail = msi_desc[irq]->link.tail;
  805. base = msi_desc[irq]->mask_base;
  806. spin_unlock_irqrestore(&msi_lock, flags);
  807. if (irq_has_action(irq))
  808. warning = 1;
  809. else if (irq != head) /* Release MSI-X irq */
  810. msi_free_irq(dev, irq);
  811. irq = tail;
  812. }
  813. msi_free_irq(dev, irq);
  814. if (warning) {
  815. iounmap(base);
  816. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  817. "called without free_irq() on all MSI-X irqs\n",
  818. pci_name(dev));
  819. BUG_ON(warning > 0);
  820. }
  821. dev->irq = temp; /* Restore IOAPIC IRQ */
  822. }
  823. }
  824. void pci_no_msi(void)
  825. {
  826. pci_msi_enable = 0;
  827. }
  828. EXPORT_SYMBOL(pci_enable_msi);
  829. EXPORT_SYMBOL(pci_disable_msi);
  830. EXPORT_SYMBOL(pci_enable_msix);
  831. EXPORT_SYMBOL(pci_disable_msix);