msi.c 31 KB

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