msi.c 31 KB

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