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. 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. static 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. temp = dev->irq;
  623. if ((status = msi_init()) < 0)
  624. return status;
  625. if (!(pos = pci_find_capability(dev, PCI_CAP_ID_MSI)))
  626. return -EINVAL;
  627. pci_read_config_word(dev, msi_control_reg(pos), &control);
  628. if (control & PCI_MSI_FLAGS_ENABLE)
  629. return 0; /* Already in MSI mode */
  630. if (!msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
  631. /* Lookup Sucess */
  632. unsigned long flags;
  633. spin_lock_irqsave(&msi_lock, flags);
  634. if (!vector_irq[dev->irq]) {
  635. msi_desc[dev->irq]->msi_attrib.state = 0;
  636. vector_irq[dev->irq] = -1;
  637. nr_released_vectors--;
  638. spin_unlock_irqrestore(&msi_lock, flags);
  639. enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
  640. return 0;
  641. }
  642. spin_unlock_irqrestore(&msi_lock, flags);
  643. dev->irq = temp;
  644. }
  645. /* Check whether driver already requested for MSI-X vectors */
  646. if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)) > 0 &&
  647. !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  648. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  649. "Device already has MSI-X vectors assigned\n",
  650. pci_name(dev));
  651. dev->irq = temp;
  652. return -EINVAL;
  653. }
  654. status = msi_capability_init(dev);
  655. if (!status) {
  656. if (!pos)
  657. nr_reserved_vectors--; /* Only MSI capable */
  658. else if (nr_msix_devices > 0)
  659. nr_msix_devices--; /* Both MSI and MSI-X capable,
  660. but choose enabling MSI */
  661. }
  662. return status;
  663. }
  664. void pci_disable_msi(struct pci_dev* dev)
  665. {
  666. struct msi_desc *entry;
  667. int pos, default_vector;
  668. u16 control;
  669. unsigned long flags;
  670. if (!dev || !(pos = pci_find_capability(dev, PCI_CAP_ID_MSI)))
  671. return;
  672. pci_read_config_word(dev, msi_control_reg(pos), &control);
  673. if (!(control & PCI_MSI_FLAGS_ENABLE))
  674. return;
  675. spin_lock_irqsave(&msi_lock, flags);
  676. entry = msi_desc[dev->irq];
  677. if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  678. spin_unlock_irqrestore(&msi_lock, flags);
  679. return;
  680. }
  681. if (entry->msi_attrib.state) {
  682. spin_unlock_irqrestore(&msi_lock, flags);
  683. printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
  684. "free_irq() on MSI vector %d\n",
  685. pci_name(dev), dev->irq);
  686. BUG_ON(entry->msi_attrib.state > 0);
  687. } else {
  688. vector_irq[dev->irq] = 0; /* free it */
  689. nr_released_vectors++;
  690. default_vector = entry->msi_attrib.default_vector;
  691. spin_unlock_irqrestore(&msi_lock, flags);
  692. /* Restore dev->irq to its default pin-assertion vector */
  693. dev->irq = default_vector;
  694. disable_msi_mode(dev, pci_find_capability(dev, PCI_CAP_ID_MSI),
  695. PCI_CAP_ID_MSI);
  696. }
  697. }
  698. static int msi_free_vector(struct pci_dev* dev, int vector, int reassign)
  699. {
  700. struct msi_desc *entry;
  701. int head, entry_nr, type;
  702. void __iomem *base;
  703. unsigned long flags;
  704. spin_lock_irqsave(&msi_lock, flags);
  705. entry = msi_desc[vector];
  706. if (!entry || entry->dev != dev) {
  707. spin_unlock_irqrestore(&msi_lock, flags);
  708. return -EINVAL;
  709. }
  710. type = entry->msi_attrib.type;
  711. entry_nr = entry->msi_attrib.entry_nr;
  712. head = entry->link.head;
  713. base = entry->mask_base;
  714. msi_desc[entry->link.head]->link.tail = entry->link.tail;
  715. msi_desc[entry->link.tail]->link.head = entry->link.head;
  716. entry->dev = NULL;
  717. if (!reassign) {
  718. vector_irq[vector] = 0;
  719. nr_released_vectors++;
  720. }
  721. msi_desc[vector] = NULL;
  722. spin_unlock_irqrestore(&msi_lock, flags);
  723. kmem_cache_free(msi_cachep, entry);
  724. if (type == PCI_CAP_ID_MSIX) {
  725. if (!reassign)
  726. writel(1, base +
  727. entry_nr * PCI_MSIX_ENTRY_SIZE +
  728. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  729. if (head == vector) {
  730. /*
  731. * Detect last MSI-X vector to be released.
  732. * Release the MSI-X memory-mapped table.
  733. */
  734. int pos, nr_entries;
  735. u32 phys_addr, table_offset;
  736. u16 control;
  737. u8 bir;
  738. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  739. pci_read_config_word(dev, msi_control_reg(pos),
  740. &control);
  741. nr_entries = multi_msix_capable(control);
  742. pci_read_config_dword(dev, msix_table_offset_reg(pos),
  743. &table_offset);
  744. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  745. phys_addr = pci_resource_start (dev, bir);
  746. phys_addr += (u32)(table_offset &
  747. ~PCI_MSIX_FLAGS_BIRMASK);
  748. iounmap(base);
  749. }
  750. }
  751. return 0;
  752. }
  753. static int reroute_msix_table(int head, struct msix_entry *entries, int *nvec)
  754. {
  755. int vector = head, tail = 0;
  756. int i, j = 0, nr_entries = 0;
  757. void __iomem *base;
  758. unsigned long flags;
  759. spin_lock_irqsave(&msi_lock, flags);
  760. while (head != tail) {
  761. nr_entries++;
  762. tail = msi_desc[vector]->link.tail;
  763. if (entries[0].entry == msi_desc[vector]->msi_attrib.entry_nr)
  764. j = vector;
  765. vector = tail;
  766. }
  767. if (*nvec > nr_entries) {
  768. spin_unlock_irqrestore(&msi_lock, flags);
  769. *nvec = nr_entries;
  770. return -EINVAL;
  771. }
  772. vector = ((j > 0) ? j : head);
  773. for (i = 0; i < *nvec; i++) {
  774. j = msi_desc[vector]->msi_attrib.entry_nr;
  775. msi_desc[vector]->msi_attrib.state = 0; /* Mark it not active */
  776. vector_irq[vector] = -1; /* Mark it busy */
  777. nr_released_vectors--;
  778. entries[i].vector = vector;
  779. if (j != (entries + i)->entry) {
  780. base = msi_desc[vector]->mask_base;
  781. msi_desc[vector]->msi_attrib.entry_nr =
  782. (entries + i)->entry;
  783. writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
  784. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET), base +
  785. (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
  786. PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  787. writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
  788. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET), base +
  789. (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
  790. PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  791. writel( (readl(base + j * PCI_MSIX_ENTRY_SIZE +
  792. PCI_MSIX_ENTRY_DATA_OFFSET) & 0xff00) | vector,
  793. base + (entries+i)->entry*PCI_MSIX_ENTRY_SIZE +
  794. PCI_MSIX_ENTRY_DATA_OFFSET);
  795. }
  796. vector = msi_desc[vector]->link.tail;
  797. }
  798. spin_unlock_irqrestore(&msi_lock, flags);
  799. return 0;
  800. }
  801. /**
  802. * pci_enable_msix - configure device's MSI-X capability structure
  803. * @dev: pointer to the pci_dev data structure of MSI-X device function
  804. * @entries: pointer to an array of MSI-X entries
  805. * @nvec: number of MSI-X vectors requested for allocation by device driver
  806. *
  807. * Setup the MSI-X capability structure of device function with the number
  808. * of requested vectors upon its software driver call to request for
  809. * MSI-X mode enabled on its hardware device function. A return of zero
  810. * indicates the successful configuration of MSI-X capability structure
  811. * with new allocated MSI-X vectors. A return of < 0 indicates a failure.
  812. * Or a return of > 0 indicates that driver request is exceeding the number
  813. * of vectors available. Driver should use the returned value to re-send
  814. * its request.
  815. **/
  816. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  817. {
  818. int status, pos, nr_entries, free_vectors;
  819. int i, j, temp;
  820. u16 control;
  821. unsigned long flags;
  822. if (!pci_msi_enable || !dev || !entries)
  823. return -EINVAL;
  824. if ((status = msi_init()) < 0)
  825. return status;
  826. if (!(pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)))
  827. return -EINVAL;
  828. pci_read_config_word(dev, msi_control_reg(pos), &control);
  829. if (control & PCI_MSIX_FLAGS_ENABLE)
  830. return -EINVAL; /* Already in MSI-X mode */
  831. nr_entries = multi_msix_capable(control);
  832. if (nvec > nr_entries)
  833. return -EINVAL;
  834. /* Check for any invalid entries */
  835. for (i = 0; i < nvec; i++) {
  836. if (entries[i].entry >= nr_entries)
  837. return -EINVAL; /* invalid entry */
  838. for (j = i + 1; j < nvec; j++) {
  839. if (entries[i].entry == entries[j].entry)
  840. return -EINVAL; /* duplicate entry */
  841. }
  842. }
  843. temp = dev->irq;
  844. if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  845. /* Lookup Sucess */
  846. nr_entries = nvec;
  847. /* Reroute MSI-X table */
  848. if (reroute_msix_table(dev->irq, entries, &nr_entries)) {
  849. /* #requested > #previous-assigned */
  850. dev->irq = temp;
  851. return nr_entries;
  852. }
  853. dev->irq = temp;
  854. enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
  855. return 0;
  856. }
  857. /* Check whether driver already requested for MSI vector */
  858. if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
  859. !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
  860. printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
  861. "Device already has an MSI vector assigned\n",
  862. pci_name(dev));
  863. dev->irq = temp;
  864. return -EINVAL;
  865. }
  866. spin_lock_irqsave(&msi_lock, flags);
  867. /*
  868. * msi_lock is provided to ensure that enough vectors resources are
  869. * available before granting.
  870. */
  871. free_vectors = pci_vector_resources(last_alloc_vector,
  872. nr_released_vectors);
  873. /* Ensure that each MSI/MSI-X device has one vector reserved by
  874. default to avoid any MSI-X driver to take all available
  875. resources */
  876. free_vectors -= nr_reserved_vectors;
  877. /* Find the average of free vectors among MSI-X devices */
  878. if (nr_msix_devices > 0)
  879. free_vectors /= nr_msix_devices;
  880. spin_unlock_irqrestore(&msi_lock, flags);
  881. if (nvec > free_vectors) {
  882. if (free_vectors > 0)
  883. return free_vectors;
  884. else
  885. return -EBUSY;
  886. }
  887. status = msix_capability_init(dev, entries, nvec);
  888. if (!status && nr_msix_devices > 0)
  889. nr_msix_devices--;
  890. return status;
  891. }
  892. void pci_disable_msix(struct pci_dev* dev)
  893. {
  894. int pos, temp;
  895. u16 control;
  896. if (!dev || !(pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)))
  897. return;
  898. pci_read_config_word(dev, msi_control_reg(pos), &control);
  899. if (!(control & PCI_MSIX_FLAGS_ENABLE))
  900. return;
  901. temp = dev->irq;
  902. if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  903. int state, vector, head, tail = 0, warning = 0;
  904. unsigned long flags;
  905. vector = head = dev->irq;
  906. spin_lock_irqsave(&msi_lock, flags);
  907. while (head != tail) {
  908. state = msi_desc[vector]->msi_attrib.state;
  909. if (state)
  910. warning = 1;
  911. else {
  912. vector_irq[vector] = 0; /* free it */
  913. nr_released_vectors++;
  914. }
  915. tail = msi_desc[vector]->link.tail;
  916. vector = tail;
  917. }
  918. spin_unlock_irqrestore(&msi_lock, flags);
  919. if (warning) {
  920. dev->irq = temp;
  921. printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
  922. "free_irq() on all MSI-X vectors\n",
  923. pci_name(dev));
  924. BUG_ON(warning > 0);
  925. } else {
  926. dev->irq = temp;
  927. disable_msi_mode(dev,
  928. pci_find_capability(dev, PCI_CAP_ID_MSIX),
  929. PCI_CAP_ID_MSIX);
  930. }
  931. }
  932. }
  933. /**
  934. * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state
  935. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  936. *
  937. * Being called during hotplug remove, from which the device function
  938. * is hot-removed. All previous assigned MSI/MSI-X vectors, if
  939. * allocated for this device function, are reclaimed to unused state,
  940. * which may be used later on.
  941. **/
  942. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  943. {
  944. int state, pos, temp;
  945. unsigned long flags;
  946. if (!pci_msi_enable || !dev)
  947. return;
  948. temp = dev->irq; /* Save IOAPIC IRQ */
  949. if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) > 0 &&
  950. !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
  951. spin_lock_irqsave(&msi_lock, flags);
  952. state = msi_desc[dev->irq]->msi_attrib.state;
  953. spin_unlock_irqrestore(&msi_lock, flags);
  954. if (state) {
  955. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  956. "called without free_irq() on MSI vector %d\n",
  957. pci_name(dev), dev->irq);
  958. BUG_ON(state > 0);
  959. } else /* Release MSI vector assigned to this device */
  960. msi_free_vector(dev, dev->irq, 0);
  961. dev->irq = temp; /* Restore IOAPIC IRQ */
  962. }
  963. if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)) > 0 &&
  964. !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
  965. int vector, head, tail = 0, warning = 0;
  966. void __iomem *base = NULL;
  967. vector = head = dev->irq;
  968. while (head != tail) {
  969. spin_lock_irqsave(&msi_lock, flags);
  970. state = msi_desc[vector]->msi_attrib.state;
  971. tail = msi_desc[vector]->link.tail;
  972. base = msi_desc[vector]->mask_base;
  973. spin_unlock_irqrestore(&msi_lock, flags);
  974. if (state)
  975. warning = 1;
  976. else if (vector != head) /* Release MSI-X vector */
  977. msi_free_vector(dev, vector, 0);
  978. vector = tail;
  979. }
  980. msi_free_vector(dev, vector, 0);
  981. if (warning) {
  982. /* Force to release the MSI-X memory-mapped table */
  983. u32 phys_addr, table_offset;
  984. u16 control;
  985. u8 bir;
  986. pci_read_config_word(dev, msi_control_reg(pos),
  987. &control);
  988. pci_read_config_dword(dev, msix_table_offset_reg(pos),
  989. &table_offset);
  990. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  991. phys_addr = pci_resource_start (dev, bir);
  992. phys_addr += (u32)(table_offset &
  993. ~PCI_MSIX_FLAGS_BIRMASK);
  994. iounmap(base);
  995. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  996. "called without free_irq() on all MSI-X vectors\n",
  997. pci_name(dev));
  998. BUG_ON(warning > 0);
  999. }
  1000. dev->irq = temp; /* Restore IOAPIC IRQ */
  1001. }
  1002. }
  1003. EXPORT_SYMBOL(pci_enable_msi);
  1004. EXPORT_SYMBOL(pci_disable_msi);
  1005. EXPORT_SYMBOL(pci_enable_msix);
  1006. EXPORT_SYMBOL(pci_disable_msix);