msi.c 31 KB

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