msi.c 36 KB

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