msi.c 31 KB

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