msi.c 31 KB

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