msi.c 36 KB

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