msi.c 36 KB

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