msi.c 36 KB

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