msi.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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/mm.h>
  9. #include <linux/irq.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/init.h>
  12. #include <linux/config.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 <asm/errno.h>
  18. #include <asm/io.h>
  19. #include <asm/smp.h>
  20. #include "pci.h"
  21. #include "msi.h"
  22. static DEFINE_SPINLOCK(msi_lock);
  23. static struct msi_desc* msi_desc[NR_IRQS] = { [0 ... NR_IRQS-1] = NULL };
  24. static kmem_cache_t* msi_cachep;
  25. static int pci_msi_enable = 1;
  26. static int last_alloc_vector;
  27. static int nr_released_vectors;
  28. static int nr_reserved_vectors = NR_HP_RESERVED_VECTORS;
  29. static int nr_msix_devices;
  30. #ifndef CONFIG_X86_IO_APIC
  31. int vector_irq[NR_VECTORS] = { [0 ... NR_VECTORS - 1] = -1};
  32. #endif
  33. static struct msi_ops *msi_ops;
  34. int
  35. msi_register(struct msi_ops *ops)
  36. {
  37. msi_ops = ops;
  38. return 0;
  39. }
  40. static void msi_cache_ctor(void *p, kmem_cache_t *cache, unsigned long flags)
  41. {
  42. memset(p, 0, NR_IRQS * sizeof(struct msi_desc));
  43. }
  44. static int msi_cache_init(void)
  45. {
  46. msi_cachep = kmem_cache_create("msi_cache",
  47. NR_IRQS * sizeof(struct msi_desc),
  48. 0, SLAB_HWCACHE_ALIGN, msi_cache_ctor, NULL);
  49. if (!msi_cachep)
  50. return -ENOMEM;
  51. return 0;
  52. }
  53. static void msi_set_mask_bit(unsigned int vector, int flag)
  54. {
  55. struct msi_desc *entry;
  56. entry = (struct msi_desc *)msi_desc[vector];
  57. if (!entry || !entry->dev || !entry->mask_base)
  58. return;
  59. switch (entry->msi_attrib.type) {
  60. case PCI_CAP_ID_MSI:
  61. {
  62. int pos;
  63. u32 mask_bits;
  64. pos = (long)entry->mask_base;
  65. pci_read_config_dword(entry->dev, pos, &mask_bits);
  66. mask_bits &= ~(1);
  67. mask_bits |= flag;
  68. pci_write_config_dword(entry->dev, pos, mask_bits);
  69. break;
  70. }
  71. case PCI_CAP_ID_MSIX:
  72. {
  73. int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  74. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
  75. writel(flag, entry->mask_base + offset);
  76. break;
  77. }
  78. default:
  79. break;
  80. }
  81. }
  82. #ifdef CONFIG_SMP
  83. static void set_msi_affinity(unsigned int vector, cpumask_t cpu_mask)
  84. {
  85. struct msi_desc *entry;
  86. u32 address_hi, address_lo;
  87. unsigned int irq = vector;
  88. unsigned int dest_cpu = first_cpu(cpu_mask);
  89. entry = (struct msi_desc *)msi_desc[vector];
  90. if (!entry || !entry->dev)
  91. return;
  92. switch (entry->msi_attrib.type) {
  93. case PCI_CAP_ID_MSI:
  94. {
  95. int pos = pci_find_capability(entry->dev, PCI_CAP_ID_MSI);
  96. if (!pos)
  97. return;
  98. pci_read_config_dword(entry->dev, msi_upper_address_reg(pos),
  99. &address_hi);
  100. pci_read_config_dword(entry->dev, msi_lower_address_reg(pos),
  101. &address_lo);
  102. msi_ops->target(vector, dest_cpu, &address_hi, &address_lo);
  103. pci_write_config_dword(entry->dev, msi_upper_address_reg(pos),
  104. address_hi);
  105. pci_write_config_dword(entry->dev, msi_lower_address_reg(pos),
  106. address_lo);
  107. set_native_irq_info(irq, cpu_mask);
  108. break;
  109. }
  110. case PCI_CAP_ID_MSIX:
  111. {
  112. int offset_hi =
  113. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  114. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET;
  115. int offset_lo =
  116. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  117. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET;
  118. address_hi = readl(entry->mask_base + offset_hi);
  119. address_lo = readl(entry->mask_base + offset_lo);
  120. msi_ops->target(vector, dest_cpu, &address_hi, &address_lo);
  121. writel(address_hi, entry->mask_base + offset_hi);
  122. writel(address_lo, entry->mask_base + offset_lo);
  123. set_native_irq_info(irq, cpu_mask);
  124. break;
  125. }
  126. default:
  127. break;
  128. }
  129. }
  130. #else
  131. #define set_msi_affinity NULL
  132. #endif /* CONFIG_SMP */
  133. static void mask_MSI_irq(unsigned int vector)
  134. {
  135. msi_set_mask_bit(vector, 1);
  136. }
  137. static void unmask_MSI_irq(unsigned int vector)
  138. {
  139. msi_set_mask_bit(vector, 0);
  140. }
  141. static unsigned int startup_msi_irq_wo_maskbit(unsigned int vector)
  142. {
  143. struct msi_desc *entry;
  144. unsigned long flags;
  145. spin_lock_irqsave(&msi_lock, flags);
  146. entry = msi_desc[vector];
  147. if (!entry || !entry->dev) {
  148. spin_unlock_irqrestore(&msi_lock, flags);
  149. return 0;
  150. }
  151. entry->msi_attrib.state = 1; /* Mark it active */
  152. spin_unlock_irqrestore(&msi_lock, flags);
  153. return 0; /* never anything pending */
  154. }
  155. static unsigned int startup_msi_irq_w_maskbit(unsigned int vector)
  156. {
  157. startup_msi_irq_wo_maskbit(vector);
  158. unmask_MSI_irq(vector);
  159. return 0; /* never anything pending */
  160. }
  161. static void shutdown_msi_irq(unsigned int vector)
  162. {
  163. struct msi_desc *entry;
  164. unsigned long flags;
  165. spin_lock_irqsave(&msi_lock, flags);
  166. entry = msi_desc[vector];
  167. if (entry && entry->dev)
  168. entry->msi_attrib.state = 0; /* Mark it not active */
  169. spin_unlock_irqrestore(&msi_lock, flags);
  170. }
  171. static void end_msi_irq_wo_maskbit(unsigned int vector)
  172. {
  173. move_native_irq(vector);
  174. ack_APIC_irq();
  175. }
  176. static void end_msi_irq_w_maskbit(unsigned int vector)
  177. {
  178. move_native_irq(vector);
  179. unmask_MSI_irq(vector);
  180. ack_APIC_irq();
  181. }
  182. static void do_nothing(unsigned int vector)
  183. {
  184. }
  185. /*
  186. * Interrupt Type for MSI-X PCI/PCI-X/PCI-Express Devices,
  187. * which implement the MSI-X Capability Structure.
  188. */
  189. static struct hw_interrupt_type msix_irq_type = {
  190. .typename = "PCI-MSI-X",
  191. .startup = startup_msi_irq_w_maskbit,
  192. .shutdown = shutdown_msi_irq,
  193. .enable = unmask_MSI_irq,
  194. .disable = mask_MSI_irq,
  195. .ack = mask_MSI_irq,
  196. .end = end_msi_irq_w_maskbit,
  197. .set_affinity = set_msi_affinity
  198. };
  199. /*
  200. * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
  201. * which implement the MSI Capability Structure with
  202. * Mask-and-Pending Bits.
  203. */
  204. static struct hw_interrupt_type msi_irq_w_maskbit_type = {
  205. .typename = "PCI-MSI",
  206. .startup = startup_msi_irq_w_maskbit,
  207. .shutdown = shutdown_msi_irq,
  208. .enable = unmask_MSI_irq,
  209. .disable = mask_MSI_irq,
  210. .ack = mask_MSI_irq,
  211. .end = end_msi_irq_w_maskbit,
  212. .set_affinity = set_msi_affinity
  213. };
  214. /*
  215. * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
  216. * which implement the MSI Capability Structure without
  217. * Mask-and-Pending Bits.
  218. */
  219. static struct hw_interrupt_type msi_irq_wo_maskbit_type = {
  220. .typename = "PCI-MSI",
  221. .startup = startup_msi_irq_wo_maskbit,
  222. .shutdown = shutdown_msi_irq,
  223. .enable = do_nothing,
  224. .disable = do_nothing,
  225. .ack = do_nothing,
  226. .end = end_msi_irq_wo_maskbit,
  227. .set_affinity = set_msi_affinity
  228. };
  229. static int msi_free_vector(struct pci_dev* dev, int vector, int reassign);
  230. static int assign_msi_vector(void)
  231. {
  232. static int new_vector_avail = 1;
  233. int vector;
  234. unsigned long flags;
  235. /*
  236. * msi_lock is provided to ensure that successful allocation of MSI
  237. * vector is assigned unique among drivers.
  238. */
  239. spin_lock_irqsave(&msi_lock, flags);
  240. if (!new_vector_avail) {
  241. int free_vector = 0;
  242. /*
  243. * vector_irq[] = -1 indicates that this specific vector is:
  244. * - assigned for MSI (since MSI have no associated IRQ) or
  245. * - assigned for legacy if less than 16, or
  246. * - having no corresponding 1:1 vector-to-IOxAPIC IRQ mapping
  247. * vector_irq[] = 0 indicates that this vector, previously
  248. * assigned for MSI, is freed by hotplug removed operations.
  249. * This vector will be reused for any subsequent hotplug added
  250. * operations.
  251. * vector_irq[] > 0 indicates that this vector is assigned for
  252. * IOxAPIC IRQs. This vector and its value provides a 1-to-1
  253. * vector-to-IOxAPIC IRQ mapping.
  254. */
  255. for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
  256. if (vector_irq[vector] != 0)
  257. continue;
  258. free_vector = vector;
  259. if (!msi_desc[vector])
  260. break;
  261. else
  262. continue;
  263. }
  264. if (!free_vector) {
  265. spin_unlock_irqrestore(&msi_lock, flags);
  266. return -EBUSY;
  267. }
  268. vector_irq[free_vector] = -1;
  269. nr_released_vectors--;
  270. spin_unlock_irqrestore(&msi_lock, flags);
  271. if (msi_desc[free_vector] != NULL) {
  272. struct pci_dev *dev;
  273. int tail;
  274. /* free all linked vectors before re-assign */
  275. do {
  276. spin_lock_irqsave(&msi_lock, flags);
  277. dev = msi_desc[free_vector]->dev;
  278. tail = msi_desc[free_vector]->link.tail;
  279. spin_unlock_irqrestore(&msi_lock, flags);
  280. msi_free_vector(dev, tail, 1);
  281. } while (free_vector != tail);
  282. }
  283. return free_vector;
  284. }
  285. vector = assign_irq_vector(AUTO_ASSIGN);
  286. last_alloc_vector = vector;
  287. if (vector == LAST_DEVICE_VECTOR)
  288. new_vector_avail = 0;
  289. spin_unlock_irqrestore(&msi_lock, flags);
  290. return vector;
  291. }
  292. static int get_new_vector(void)
  293. {
  294. int vector = assign_msi_vector();
  295. if (vector > 0)
  296. set_intr_gate(vector, interrupt[vector]);
  297. return vector;
  298. }
  299. static int msi_init(void)
  300. {
  301. static int status = -ENOMEM;
  302. if (!status)
  303. return status;
  304. if (pci_msi_quirk) {
  305. pci_msi_enable = 0;
  306. printk(KERN_WARNING "PCI: MSI quirk detected. MSI disabled.\n");
  307. status = -EINVAL;
  308. return status;
  309. }
  310. status = msi_arch_init();
  311. if (status < 0) {
  312. pci_msi_enable = 0;
  313. printk(KERN_WARNING
  314. "PCI: MSI arch init failed. MSI disabled.\n");
  315. return status;
  316. }
  317. if (! msi_ops) {
  318. printk(KERN_WARNING
  319. "PCI: MSI ops not registered. MSI disabled.\n");
  320. status = -EINVAL;
  321. return status;
  322. }
  323. last_alloc_vector = assign_irq_vector(AUTO_ASSIGN);
  324. status = msi_cache_init();
  325. if (status < 0) {
  326. pci_msi_enable = 0;
  327. printk(KERN_WARNING "PCI: MSI cache init failed\n");
  328. return status;
  329. }
  330. if (last_alloc_vector < 0) {
  331. pci_msi_enable = 0;
  332. printk(KERN_WARNING "PCI: No interrupt vectors available for MSI\n");
  333. status = -EBUSY;
  334. return status;
  335. }
  336. vector_irq[last_alloc_vector] = 0;
  337. nr_released_vectors++;
  338. return status;
  339. }
  340. static int get_msi_vector(struct pci_dev *dev)
  341. {
  342. return get_new_vector();
  343. }
  344. static struct msi_desc* alloc_msi_entry(void)
  345. {
  346. struct msi_desc *entry;
  347. entry = kmem_cache_alloc(msi_cachep, SLAB_KERNEL);
  348. if (!entry)
  349. return NULL;
  350. memset(entry, 0, sizeof(struct msi_desc));
  351. entry->link.tail = entry->link.head = 0; /* single message */
  352. entry->dev = NULL;
  353. return entry;
  354. }
  355. static void attach_msi_entry(struct msi_desc *entry, int vector)
  356. {
  357. unsigned long flags;
  358. spin_lock_irqsave(&msi_lock, flags);
  359. msi_desc[vector] = entry;
  360. spin_unlock_irqrestore(&msi_lock, flags);
  361. }
  362. static void irq_handler_init(int cap_id, int pos, int mask)
  363. {
  364. unsigned long flags;
  365. spin_lock_irqsave(&irq_desc[pos].lock, flags);
  366. if (cap_id == PCI_CAP_ID_MSIX)
  367. irq_desc[pos].handler = &msix_irq_type;
  368. else {
  369. if (!mask)
  370. irq_desc[pos].handler = &msi_irq_wo_maskbit_type;
  371. else
  372. irq_desc[pos].handler = &msi_irq_w_maskbit_type;
  373. }
  374. spin_unlock_irqrestore(&irq_desc[pos].lock, flags);
  375. }
  376. static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
  377. {
  378. u16 control;
  379. pci_read_config_word(dev, msi_control_reg(pos), &control);
  380. if (type == PCI_CAP_ID_MSI) {
  381. /* Set enabled bits to single MSI & enable MSI_enable bit */
  382. msi_enable(control, 1);
  383. pci_write_config_word(dev, msi_control_reg(pos), control);
  384. dev->msi_enabled = 1;
  385. } else {
  386. msix_enable(control);
  387. pci_write_config_word(dev, msi_control_reg(pos), control);
  388. dev->msix_enabled = 1;
  389. }
  390. if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
  391. /* PCI Express Endpoint device detected */
  392. pci_intx(dev, 0); /* disable intx */
  393. }
  394. }
  395. void disable_msi_mode(struct pci_dev *dev, int pos, int type)
  396. {
  397. u16 control;
  398. pci_read_config_word(dev, msi_control_reg(pos), &control);
  399. if (type == PCI_CAP_ID_MSI) {
  400. /* Set enabled bits to single MSI & enable MSI_enable bit */
  401. msi_disable(control);
  402. pci_write_config_word(dev, msi_control_reg(pos), control);
  403. dev->msi_enabled = 0;
  404. } else {
  405. msix_disable(control);
  406. pci_write_config_word(dev, msi_control_reg(pos), control);
  407. dev->msix_enabled = 0;
  408. }
  409. if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
  410. /* PCI Express Endpoint device detected */
  411. pci_intx(dev, 1); /* enable intx */
  412. }
  413. }
  414. static int msi_lookup_vector(struct pci_dev *dev, int type)
  415. {
  416. int vector;
  417. unsigned long flags;
  418. spin_lock_irqsave(&msi_lock, flags);
  419. for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
  420. if (!msi_desc[vector] || msi_desc[vector]->dev != dev ||
  421. msi_desc[vector]->msi_attrib.type != type ||
  422. msi_desc[vector]->msi_attrib.default_vector != dev->irq)
  423. continue;
  424. spin_unlock_irqrestore(&msi_lock, flags);
  425. /* This pre-assigned MSI vector for this device
  426. already exits. Override dev->irq with this vector */
  427. dev->irq = vector;
  428. return 0;
  429. }
  430. spin_unlock_irqrestore(&msi_lock, flags);
  431. return -EACCES;
  432. }
  433. void pci_scan_msi_device(struct pci_dev *dev)
  434. {
  435. if (!dev)
  436. return;
  437. if (pci_find_capability(dev, PCI_CAP_ID_MSIX) > 0)
  438. nr_msix_devices++;
  439. else if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0)
  440. nr_reserved_vectors++;
  441. }
  442. #ifdef CONFIG_PM
  443. int pci_save_msi_state(struct pci_dev *dev)
  444. {
  445. int pos, i = 0;
  446. u16 control;
  447. struct pci_cap_saved_state *save_state;
  448. u32 *cap;
  449. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  450. if (pos <= 0 || dev->no_msi)
  451. return 0;
  452. pci_read_config_word(dev, msi_control_reg(pos), &control);
  453. if (!(control & PCI_MSI_FLAGS_ENABLE))
  454. return 0;
  455. save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
  456. GFP_KERNEL);
  457. if (!save_state) {
  458. printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
  459. return -ENOMEM;
  460. }
  461. cap = &save_state->data[0];
  462. pci_read_config_dword(dev, pos, &cap[i++]);
  463. control = cap[0] >> 16;
  464. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
  465. if (control & PCI_MSI_FLAGS_64BIT) {
  466. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
  467. pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
  468. } else
  469. pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
  470. if (control & PCI_MSI_FLAGS_MASKBIT)
  471. pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
  472. save_state->cap_nr = PCI_CAP_ID_MSI;
  473. pci_add_saved_cap(dev, save_state);
  474. return 0;
  475. }
  476. void pci_restore_msi_state(struct pci_dev *dev)
  477. {
  478. int i = 0, pos;
  479. u16 control;
  480. struct pci_cap_saved_state *save_state;
  481. u32 *cap;
  482. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
  483. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  484. if (!save_state || pos <= 0)
  485. return;
  486. cap = &save_state->data[0];
  487. control = cap[i++] >> 16;
  488. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
  489. if (control & PCI_MSI_FLAGS_64BIT) {
  490. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
  491. pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
  492. } else
  493. pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
  494. if (control & PCI_MSI_FLAGS_MASKBIT)
  495. pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
  496. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  497. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  498. pci_remove_saved_cap(save_state);
  499. kfree(save_state);
  500. }
  501. int pci_save_msix_state(struct pci_dev *dev)
  502. {
  503. int pos;
  504. int temp;
  505. int vector, head, tail = 0;
  506. u16 control;
  507. struct pci_cap_saved_state *save_state;
  508. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  509. if (pos <= 0 || dev->no_msi)
  510. return 0;
  511. /* save the capability */
  512. pci_read_config_word(dev, msi_control_reg(pos), &control);
  513. if (!(control & PCI_MSIX_FLAGS_ENABLE))
  514. return 0;
  515. save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
  516. GFP_KERNEL);
  517. if (!save_state) {
  518. printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
  519. return -ENOMEM;
  520. }
  521. *((u16 *)&save_state->data[0]) = control;
  522. /* save the table */
  523. temp = dev->irq;
  524. if (msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  525. kfree(save_state);
  526. return -EINVAL;
  527. }
  528. vector = head = dev->irq;
  529. while (head != tail) {
  530. int j;
  531. void __iomem *base;
  532. struct msi_desc *entry;
  533. entry = msi_desc[vector];
  534. base = entry->mask_base;
  535. j = entry->msi_attrib.entry_nr;
  536. entry->address_lo_save =
  537. readl(base + j * PCI_MSIX_ENTRY_SIZE +
  538. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  539. entry->address_hi_save =
  540. readl(base + j * PCI_MSIX_ENTRY_SIZE +
  541. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  542. entry->data_save =
  543. readl(base + j * PCI_MSIX_ENTRY_SIZE +
  544. PCI_MSIX_ENTRY_DATA_OFFSET);
  545. tail = msi_desc[vector]->link.tail;
  546. vector = tail;
  547. }
  548. dev->irq = temp;
  549. save_state->cap_nr = PCI_CAP_ID_MSIX;
  550. pci_add_saved_cap(dev, save_state);
  551. return 0;
  552. }
  553. void pci_restore_msix_state(struct pci_dev *dev)
  554. {
  555. u16 save;
  556. int pos;
  557. int vector, head, tail = 0;
  558. void __iomem *base;
  559. int j;
  560. struct msi_desc *entry;
  561. int temp;
  562. struct pci_cap_saved_state *save_state;
  563. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
  564. if (!save_state)
  565. return;
  566. save = *((u16 *)&save_state->data[0]);
  567. pci_remove_saved_cap(save_state);
  568. kfree(save_state);
  569. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  570. if (pos <= 0)
  571. return;
  572. /* route the table */
  573. temp = dev->irq;
  574. if (msi_lookup_vector(dev, PCI_CAP_ID_MSIX))
  575. return;
  576. vector = head = dev->irq;
  577. while (head != tail) {
  578. entry = msi_desc[vector];
  579. base = entry->mask_base;
  580. j = entry->msi_attrib.entry_nr;
  581. writel(entry->address_lo_save,
  582. base + j * PCI_MSIX_ENTRY_SIZE +
  583. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  584. writel(entry->address_hi_save,
  585. base + j * PCI_MSIX_ENTRY_SIZE +
  586. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  587. writel(entry->data_save,
  588. base + j * PCI_MSIX_ENTRY_SIZE +
  589. PCI_MSIX_ENTRY_DATA_OFFSET);
  590. tail = msi_desc[vector]->link.tail;
  591. vector = tail;
  592. }
  593. dev->irq = temp;
  594. pci_write_config_word(dev, msi_control_reg(pos), save);
  595. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  596. }
  597. #endif
  598. static int msi_register_init(struct pci_dev *dev, struct msi_desc *entry)
  599. {
  600. int status;
  601. u32 address_hi;
  602. u32 address_lo;
  603. u32 data;
  604. int pos, vector = dev->irq;
  605. u16 control;
  606. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  607. pci_read_config_word(dev, msi_control_reg(pos), &control);
  608. /* Configure MSI capability structure */
  609. status = msi_ops->setup(dev, vector, &address_hi, &address_lo, &data);
  610. if (status < 0)
  611. return status;
  612. pci_write_config_dword(dev, msi_lower_address_reg(pos), address_lo);
  613. if (is_64bit_address(control)) {
  614. pci_write_config_dword(dev,
  615. msi_upper_address_reg(pos), address_hi);
  616. pci_write_config_word(dev,
  617. msi_data_reg(pos, 1), data);
  618. } else
  619. pci_write_config_word(dev,
  620. msi_data_reg(pos, 0), data);
  621. if (entry->msi_attrib.maskbit) {
  622. unsigned int maskbits, temp;
  623. /* All MSIs are unmasked by default, Mask them all */
  624. pci_read_config_dword(dev,
  625. msi_mask_bits_reg(pos, is_64bit_address(control)),
  626. &maskbits);
  627. temp = (1 << multi_msi_capable(control));
  628. temp = ((temp - 1) & ~temp);
  629. maskbits |= temp;
  630. pci_write_config_dword(dev,
  631. msi_mask_bits_reg(pos, is_64bit_address(control)),
  632. maskbits);
  633. }
  634. return 0;
  635. }
  636. /**
  637. * msi_capability_init - configure device's MSI capability structure
  638. * @dev: pointer to the pci_dev data structure of MSI device function
  639. *
  640. * Setup the MSI capability structure of device function with a single
  641. * MSI vector, regardless of device function is capable of handling
  642. * multiple messages. A return of zero indicates the successful setup
  643. * of an entry zero with the new MSI vector or non-zero for otherwise.
  644. **/
  645. static int msi_capability_init(struct pci_dev *dev)
  646. {
  647. int status;
  648. struct msi_desc *entry;
  649. int pos, vector;
  650. u16 control;
  651. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  652. pci_read_config_word(dev, msi_control_reg(pos), &control);
  653. /* MSI Entry Initialization */
  654. entry = alloc_msi_entry();
  655. if (!entry)
  656. return -ENOMEM;
  657. vector = get_msi_vector(dev);
  658. if (vector < 0) {
  659. kmem_cache_free(msi_cachep, entry);
  660. return -EBUSY;
  661. }
  662. entry->link.head = vector;
  663. entry->link.tail = vector;
  664. entry->msi_attrib.type = PCI_CAP_ID_MSI;
  665. entry->msi_attrib.state = 0; /* Mark it not active */
  666. entry->msi_attrib.entry_nr = 0;
  667. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  668. entry->msi_attrib.default_vector = dev->irq; /* Save IOAPIC IRQ */
  669. dev->irq = vector;
  670. entry->dev = dev;
  671. if (is_mask_bit_support(control)) {
  672. entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
  673. is_64bit_address(control));
  674. }
  675. /* Replace with MSI handler */
  676. irq_handler_init(PCI_CAP_ID_MSI, vector, entry->msi_attrib.maskbit);
  677. /* Configure MSI capability structure */
  678. status = msi_register_init(dev, entry);
  679. if (status != 0) {
  680. dev->irq = entry->msi_attrib.default_vector;
  681. kmem_cache_free(msi_cachep, entry);
  682. return status;
  683. }
  684. attach_msi_entry(entry, vector);
  685. /* Set MSI enabled bits */
  686. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  687. return 0;
  688. }
  689. /**
  690. * msix_capability_init - configure device's MSI-X capability
  691. * @dev: pointer to the pci_dev data structure of MSI-X device function
  692. * @entries: pointer to an array of struct msix_entry entries
  693. * @nvec: number of @entries
  694. *
  695. * Setup the MSI-X capability structure of device function with a
  696. * single MSI-X vector. A return of zero indicates the successful setup of
  697. * requested MSI-X entries with allocated vectors or non-zero for otherwise.
  698. **/
  699. static int msix_capability_init(struct pci_dev *dev,
  700. struct msix_entry *entries, int nvec)
  701. {
  702. struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
  703. u32 address_hi;
  704. u32 address_lo;
  705. u32 data;
  706. int status;
  707. int vector, pos, i, j, nr_entries, temp = 0;
  708. unsigned long phys_addr;
  709. u32 table_offset;
  710. u16 control;
  711. u8 bir;
  712. void __iomem *base;
  713. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  714. /* Request & Map MSI-X table region */
  715. pci_read_config_word(dev, msi_control_reg(pos), &control);
  716. nr_entries = multi_msix_capable(control);
  717. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  718. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  719. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  720. phys_addr = pci_resource_start (dev, bir) + table_offset;
  721. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  722. if (base == NULL)
  723. return -ENOMEM;
  724. /* MSI-X Table Initialization */
  725. for (i = 0; i < nvec; i++) {
  726. entry = alloc_msi_entry();
  727. if (!entry)
  728. break;
  729. vector = get_msi_vector(dev);
  730. if (vector < 0) {
  731. kmem_cache_free(msi_cachep, entry);
  732. break;
  733. }
  734. j = entries[i].entry;
  735. entries[i].vector = vector;
  736. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  737. entry->msi_attrib.state = 0; /* Mark it not active */
  738. entry->msi_attrib.entry_nr = j;
  739. entry->msi_attrib.maskbit = 1;
  740. entry->msi_attrib.default_vector = dev->irq;
  741. entry->dev = dev;
  742. entry->mask_base = base;
  743. if (!head) {
  744. entry->link.head = vector;
  745. entry->link.tail = vector;
  746. head = entry;
  747. } else {
  748. entry->link.head = temp;
  749. entry->link.tail = tail->link.tail;
  750. tail->link.tail = vector;
  751. head->link.head = vector;
  752. }
  753. temp = vector;
  754. tail = entry;
  755. /* Replace with MSI-X handler */
  756. irq_handler_init(PCI_CAP_ID_MSIX, vector, 1);
  757. /* Configure MSI-X capability structure */
  758. status = msi_ops->setup(dev, vector,
  759. &address_hi,
  760. &address_lo,
  761. &data);
  762. if (status < 0)
  763. break;
  764. writel(address_lo,
  765. base + j * PCI_MSIX_ENTRY_SIZE +
  766. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  767. writel(address_hi,
  768. base + j * PCI_MSIX_ENTRY_SIZE +
  769. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  770. writel(data,
  771. base + j * PCI_MSIX_ENTRY_SIZE +
  772. PCI_MSIX_ENTRY_DATA_OFFSET);
  773. attach_msi_entry(entry, vector);
  774. }
  775. if (i != nvec) {
  776. i--;
  777. for (; i >= 0; i--) {
  778. vector = (entries + i)->vector;
  779. msi_free_vector(dev, vector, 0);
  780. (entries + i)->vector = 0;
  781. }
  782. return -EBUSY;
  783. }
  784. /* Set MSI-X enabled bits */
  785. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  786. return 0;
  787. }
  788. /**
  789. * pci_enable_msi - configure device's MSI capability structure
  790. * @dev: pointer to the pci_dev data structure of MSI device function
  791. *
  792. * Setup the MSI capability structure of device function with
  793. * a single MSI vector upon its software driver call to request for
  794. * MSI mode enabled on its hardware device function. A return of zero
  795. * indicates the successful setup of an entry zero with the new MSI
  796. * vector or non-zero for otherwise.
  797. **/
  798. int pci_enable_msi(struct pci_dev* dev)
  799. {
  800. struct pci_bus *bus;
  801. int pos, temp, status = -EINVAL;
  802. u16 control;
  803. if (!pci_msi_enable || !dev)
  804. return status;
  805. if (dev->no_msi)
  806. return status;
  807. for (bus = dev->bus; bus; bus = bus->parent)
  808. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  809. return -EINVAL;
  810. temp = dev->irq;
  811. status = msi_init();
  812. if (status < 0)
  813. return status;
  814. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  815. if (!pos)
  816. return -EINVAL;
  817. if (!msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
  818. /* Lookup Sucess */
  819. unsigned long flags;
  820. pci_read_config_word(dev, msi_control_reg(pos), &control);
  821. if (control & PCI_MSI_FLAGS_ENABLE)
  822. return 0; /* Already in MSI mode */
  823. spin_lock_irqsave(&msi_lock, flags);
  824. if (!vector_irq[dev->irq]) {
  825. msi_desc[dev->irq]->msi_attrib.state = 0;
  826. vector_irq[dev->irq] = -1;
  827. nr_released_vectors--;
  828. spin_unlock_irqrestore(&msi_lock, flags);
  829. status = msi_register_init(dev, msi_desc[dev->irq]);
  830. if (status == 0)
  831. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  832. return status;
  833. }
  834. spin_unlock_irqrestore(&msi_lock, flags);
  835. dev->irq = temp;
  836. }
  837. /* Check whether driver already requested for MSI-X vectors */
  838. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  839. if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  840. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  841. "Device already has MSI-X vectors assigned\n",
  842. pci_name(dev));
  843. dev->irq = temp;
  844. return -EINVAL;
  845. }
  846. status = msi_capability_init(dev);
  847. if (!status) {
  848. if (!pos)
  849. nr_reserved_vectors--; /* Only MSI capable */
  850. else if (nr_msix_devices > 0)
  851. nr_msix_devices--; /* Both MSI and MSI-X capable,
  852. but choose enabling MSI */
  853. }
  854. return status;
  855. }
  856. void pci_disable_msi(struct pci_dev* dev)
  857. {
  858. struct msi_desc *entry;
  859. int pos, default_vector;
  860. u16 control;
  861. unsigned long flags;
  862. if (!pci_msi_enable)
  863. return;
  864. if (!dev)
  865. return;
  866. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  867. if (!pos)
  868. return;
  869. pci_read_config_word(dev, msi_control_reg(pos), &control);
  870. if (!(control & PCI_MSI_FLAGS_ENABLE))
  871. return;
  872. spin_lock_irqsave(&msi_lock, flags);
  873. entry = msi_desc[dev->irq];
  874. if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  875. spin_unlock_irqrestore(&msi_lock, flags);
  876. return;
  877. }
  878. if (entry->msi_attrib.state) {
  879. spin_unlock_irqrestore(&msi_lock, flags);
  880. printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
  881. "free_irq() on MSI vector %d\n",
  882. pci_name(dev), dev->irq);
  883. BUG_ON(entry->msi_attrib.state > 0);
  884. } else {
  885. vector_irq[dev->irq] = 0; /* free it */
  886. nr_released_vectors++;
  887. default_vector = entry->msi_attrib.default_vector;
  888. spin_unlock_irqrestore(&msi_lock, flags);
  889. /* Restore dev->irq to its default pin-assertion vector */
  890. dev->irq = default_vector;
  891. disable_msi_mode(dev, pci_find_capability(dev, PCI_CAP_ID_MSI),
  892. PCI_CAP_ID_MSI);
  893. }
  894. }
  895. static int msi_free_vector(struct pci_dev* dev, int vector, int reassign)
  896. {
  897. struct msi_desc *entry;
  898. int head, entry_nr, type;
  899. void __iomem *base;
  900. unsigned long flags;
  901. msi_ops->teardown(vector);
  902. spin_lock_irqsave(&msi_lock, flags);
  903. entry = msi_desc[vector];
  904. if (!entry || entry->dev != dev) {
  905. spin_unlock_irqrestore(&msi_lock, flags);
  906. return -EINVAL;
  907. }
  908. type = entry->msi_attrib.type;
  909. entry_nr = entry->msi_attrib.entry_nr;
  910. head = entry->link.head;
  911. base = entry->mask_base;
  912. msi_desc[entry->link.head]->link.tail = entry->link.tail;
  913. msi_desc[entry->link.tail]->link.head = entry->link.head;
  914. entry->dev = NULL;
  915. if (!reassign) {
  916. vector_irq[vector] = 0;
  917. nr_released_vectors++;
  918. }
  919. msi_desc[vector] = NULL;
  920. spin_unlock_irqrestore(&msi_lock, flags);
  921. kmem_cache_free(msi_cachep, entry);
  922. if (type == PCI_CAP_ID_MSIX) {
  923. if (!reassign)
  924. writel(1, base +
  925. entry_nr * PCI_MSIX_ENTRY_SIZE +
  926. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  927. if (head == vector)
  928. iounmap(base);
  929. }
  930. return 0;
  931. }
  932. static int reroute_msix_table(int head, struct msix_entry *entries, int *nvec)
  933. {
  934. int vector = head, tail = 0;
  935. int i, j = 0, nr_entries = 0;
  936. void __iomem *base;
  937. unsigned long flags;
  938. spin_lock_irqsave(&msi_lock, flags);
  939. while (head != tail) {
  940. nr_entries++;
  941. tail = msi_desc[vector]->link.tail;
  942. if (entries[0].entry == msi_desc[vector]->msi_attrib.entry_nr)
  943. j = vector;
  944. vector = tail;
  945. }
  946. if (*nvec > nr_entries) {
  947. spin_unlock_irqrestore(&msi_lock, flags);
  948. *nvec = nr_entries;
  949. return -EINVAL;
  950. }
  951. vector = ((j > 0) ? j : head);
  952. for (i = 0; i < *nvec; i++) {
  953. j = msi_desc[vector]->msi_attrib.entry_nr;
  954. msi_desc[vector]->msi_attrib.state = 0; /* Mark it not active */
  955. vector_irq[vector] = -1; /* Mark it busy */
  956. nr_released_vectors--;
  957. entries[i].vector = vector;
  958. if (j != (entries + i)->entry) {
  959. base = msi_desc[vector]->mask_base;
  960. msi_desc[vector]->msi_attrib.entry_nr =
  961. (entries + i)->entry;
  962. writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
  963. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET), base +
  964. (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
  965. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  966. writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
  967. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET), base +
  968. (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
  969. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  970. writel( (readl(base + j * PCI_MSIX_ENTRY_SIZE +
  971. PCI_MSIX_ENTRY_DATA_OFFSET) & 0xff00) | vector,
  972. base + (entries+i)->entry*PCI_MSIX_ENTRY_SIZE +
  973. PCI_MSIX_ENTRY_DATA_OFFSET);
  974. }
  975. vector = msi_desc[vector]->link.tail;
  976. }
  977. spin_unlock_irqrestore(&msi_lock, flags);
  978. return 0;
  979. }
  980. /**
  981. * pci_enable_msix - configure device's MSI-X capability structure
  982. * @dev: pointer to the pci_dev data structure of MSI-X device function
  983. * @entries: pointer to an array of MSI-X entries
  984. * @nvec: number of MSI-X vectors requested for allocation by device driver
  985. *
  986. * Setup the MSI-X capability structure of device function with the number
  987. * of requested vectors upon its software driver call to request for
  988. * MSI-X mode enabled on its hardware device function. A return of zero
  989. * indicates the successful configuration of MSI-X capability structure
  990. * with new allocated MSI-X vectors. A return of < 0 indicates a failure.
  991. * Or a return of > 0 indicates that driver request is exceeding the number
  992. * of vectors available. Driver should use the returned value to re-send
  993. * its request.
  994. **/
  995. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  996. {
  997. struct pci_bus *bus;
  998. int status, pos, nr_entries, free_vectors;
  999. int i, j, temp;
  1000. u16 control;
  1001. unsigned long flags;
  1002. if (!pci_msi_enable || !dev || !entries)
  1003. return -EINVAL;
  1004. if (dev->no_msi)
  1005. return -EINVAL;
  1006. for (bus = dev->bus; bus; bus = bus->parent)
  1007. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  1008. return -EINVAL;
  1009. status = msi_init();
  1010. if (status < 0)
  1011. return status;
  1012. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1013. if (!pos)
  1014. return -EINVAL;
  1015. pci_read_config_word(dev, msi_control_reg(pos), &control);
  1016. if (control & PCI_MSIX_FLAGS_ENABLE)
  1017. return -EINVAL; /* Already in MSI-X mode */
  1018. nr_entries = multi_msix_capable(control);
  1019. if (nvec > nr_entries)
  1020. return -EINVAL;
  1021. /* Check for any invalid entries */
  1022. for (i = 0; i < nvec; i++) {
  1023. if (entries[i].entry >= nr_entries)
  1024. return -EINVAL; /* invalid entry */
  1025. for (j = i + 1; j < nvec; j++) {
  1026. if (entries[i].entry == entries[j].entry)
  1027. return -EINVAL; /* duplicate entry */
  1028. }
  1029. }
  1030. temp = dev->irq;
  1031. if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  1032. /* Lookup Sucess */
  1033. nr_entries = nvec;
  1034. /* Reroute MSI-X table */
  1035. if (reroute_msix_table(dev->irq, entries, &nr_entries)) {
  1036. /* #requested > #previous-assigned */
  1037. dev->irq = temp;
  1038. return nr_entries;
  1039. }
  1040. dev->irq = temp;
  1041. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  1042. return 0;
  1043. }
  1044. /* Check whether driver already requested for MSI vector */
  1045. if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
  1046. !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
  1047. printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
  1048. "Device already has an MSI vector assigned\n",
  1049. pci_name(dev));
  1050. dev->irq = temp;
  1051. return -EINVAL;
  1052. }
  1053. spin_lock_irqsave(&msi_lock, flags);
  1054. /*
  1055. * msi_lock is provided to ensure that enough vectors resources are
  1056. * available before granting.
  1057. */
  1058. free_vectors = pci_vector_resources(last_alloc_vector,
  1059. nr_released_vectors);
  1060. /* Ensure that each MSI/MSI-X device has one vector reserved by
  1061. default to avoid any MSI-X driver to take all available
  1062. resources */
  1063. free_vectors -= nr_reserved_vectors;
  1064. /* Find the average of free vectors among MSI-X devices */
  1065. if (nr_msix_devices > 0)
  1066. free_vectors /= nr_msix_devices;
  1067. spin_unlock_irqrestore(&msi_lock, flags);
  1068. if (nvec > free_vectors) {
  1069. if (free_vectors > 0)
  1070. return free_vectors;
  1071. else
  1072. return -EBUSY;
  1073. }
  1074. status = msix_capability_init(dev, entries, nvec);
  1075. if (!status && nr_msix_devices > 0)
  1076. nr_msix_devices--;
  1077. return status;
  1078. }
  1079. void pci_disable_msix(struct pci_dev* dev)
  1080. {
  1081. int pos, temp;
  1082. u16 control;
  1083. if (!pci_msi_enable)
  1084. return;
  1085. if (!dev)
  1086. return;
  1087. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1088. if (!pos)
  1089. return;
  1090. pci_read_config_word(dev, msi_control_reg(pos), &control);
  1091. if (!(control & PCI_MSIX_FLAGS_ENABLE))
  1092. return;
  1093. temp = dev->irq;
  1094. if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  1095. int state, vector, head, tail = 0, warning = 0;
  1096. unsigned long flags;
  1097. vector = head = dev->irq;
  1098. spin_lock_irqsave(&msi_lock, flags);
  1099. while (head != tail) {
  1100. state = msi_desc[vector]->msi_attrib.state;
  1101. if (state)
  1102. warning = 1;
  1103. else {
  1104. vector_irq[vector] = 0; /* free it */
  1105. nr_released_vectors++;
  1106. }
  1107. tail = msi_desc[vector]->link.tail;
  1108. vector = tail;
  1109. }
  1110. spin_unlock_irqrestore(&msi_lock, flags);
  1111. if (warning) {
  1112. dev->irq = temp;
  1113. printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
  1114. "free_irq() on all MSI-X vectors\n",
  1115. pci_name(dev));
  1116. BUG_ON(warning > 0);
  1117. } else {
  1118. dev->irq = temp;
  1119. disable_msi_mode(dev,
  1120. pci_find_capability(dev, PCI_CAP_ID_MSIX),
  1121. PCI_CAP_ID_MSIX);
  1122. }
  1123. }
  1124. }
  1125. /**
  1126. * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state
  1127. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  1128. *
  1129. * Being called during hotplug remove, from which the device function
  1130. * is hot-removed. All previous assigned MSI/MSI-X vectors, if
  1131. * allocated for this device function, are reclaimed to unused state,
  1132. * which may be used later on.
  1133. **/
  1134. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  1135. {
  1136. int state, pos, temp;
  1137. unsigned long flags;
  1138. if (!pci_msi_enable || !dev)
  1139. return;
  1140. temp = dev->irq; /* Save IOAPIC IRQ */
  1141. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1142. if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
  1143. spin_lock_irqsave(&msi_lock, flags);
  1144. state = msi_desc[dev->irq]->msi_attrib.state;
  1145. spin_unlock_irqrestore(&msi_lock, flags);
  1146. if (state) {
  1147. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  1148. "called without free_irq() on MSI vector %d\n",
  1149. pci_name(dev), dev->irq);
  1150. BUG_ON(state > 0);
  1151. } else /* Release MSI vector assigned to this device */
  1152. msi_free_vector(dev, dev->irq, 0);
  1153. dev->irq = temp; /* Restore IOAPIC IRQ */
  1154. }
  1155. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1156. if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  1157. int vector, head, tail = 0, warning = 0;
  1158. void __iomem *base = NULL;
  1159. vector = head = dev->irq;
  1160. while (head != tail) {
  1161. spin_lock_irqsave(&msi_lock, flags);
  1162. state = msi_desc[vector]->msi_attrib.state;
  1163. tail = msi_desc[vector]->link.tail;
  1164. base = msi_desc[vector]->mask_base;
  1165. spin_unlock_irqrestore(&msi_lock, flags);
  1166. if (state)
  1167. warning = 1;
  1168. else if (vector != head) /* Release MSI-X vector */
  1169. msi_free_vector(dev, vector, 0);
  1170. vector = tail;
  1171. }
  1172. msi_free_vector(dev, vector, 0);
  1173. if (warning) {
  1174. iounmap(base);
  1175. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  1176. "called without free_irq() on all MSI-X vectors\n",
  1177. pci_name(dev));
  1178. BUG_ON(warning > 0);
  1179. }
  1180. dev->irq = temp; /* Restore IOAPIC IRQ */
  1181. }
  1182. }
  1183. void pci_no_msi(void)
  1184. {
  1185. pci_msi_enable = 0;
  1186. }
  1187. EXPORT_SYMBOL(pci_enable_msi);
  1188. EXPORT_SYMBOL(pci_disable_msi);
  1189. EXPORT_SYMBOL(pci_enable_msix);
  1190. EXPORT_SYMBOL(pci_disable_msix);