msi.c 32 KB

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