msi.c 31 KB

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