msi.c 35 KB

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