msi.c 31 KB

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