msi.c 35 KB

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