msi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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/err.h>
  9. #include <linux/mm.h>
  10. #include <linux/irq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/init.h>
  13. #include <linux/export.h>
  14. #include <linux/ioport.h>
  15. #include <linux/pci.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/msi.h>
  18. #include <linux/smp.h>
  19. #include <linux/errno.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include "pci.h"
  23. static int pci_msi_enable = 1;
  24. #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
  25. /* Arch hooks */
  26. #ifndef arch_msi_check_device
  27. int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
  28. {
  29. return 0;
  30. }
  31. #endif
  32. #ifndef arch_setup_msi_irqs
  33. # define arch_setup_msi_irqs default_setup_msi_irqs
  34. # define HAVE_DEFAULT_MSI_SETUP_IRQS
  35. #endif
  36. #ifdef HAVE_DEFAULT_MSI_SETUP_IRQS
  37. int default_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  38. {
  39. struct msi_desc *entry;
  40. int ret;
  41. /*
  42. * If an architecture wants to support multiple MSI, it needs to
  43. * override arch_setup_msi_irqs()
  44. */
  45. if (type == PCI_CAP_ID_MSI && nvec > 1)
  46. return 1;
  47. list_for_each_entry(entry, &dev->msi_list, list) {
  48. ret = arch_setup_msi_irq(dev, entry);
  49. if (ret < 0)
  50. return ret;
  51. if (ret > 0)
  52. return -ENOSPC;
  53. }
  54. return 0;
  55. }
  56. #endif
  57. #ifndef arch_teardown_msi_irqs
  58. # define arch_teardown_msi_irqs default_teardown_msi_irqs
  59. # define HAVE_DEFAULT_MSI_TEARDOWN_IRQS
  60. #endif
  61. #ifdef HAVE_DEFAULT_MSI_TEARDOWN_IRQS
  62. void default_teardown_msi_irqs(struct pci_dev *dev)
  63. {
  64. struct msi_desc *entry;
  65. list_for_each_entry(entry, &dev->msi_list, list) {
  66. int i, nvec;
  67. if (entry->irq == 0)
  68. continue;
  69. nvec = 1 << entry->msi_attrib.multiple;
  70. for (i = 0; i < nvec; i++)
  71. arch_teardown_msi_irq(entry->irq + i);
  72. }
  73. }
  74. #endif
  75. #ifndef arch_restore_msi_irqs
  76. # define arch_restore_msi_irqs default_restore_msi_irqs
  77. # define HAVE_DEFAULT_MSI_RESTORE_IRQS
  78. #endif
  79. #ifdef HAVE_DEFAULT_MSI_RESTORE_IRQS
  80. void default_restore_msi_irqs(struct pci_dev *dev, int irq)
  81. {
  82. struct msi_desc *entry;
  83. entry = NULL;
  84. if (dev->msix_enabled) {
  85. list_for_each_entry(entry, &dev->msi_list, list) {
  86. if (irq == entry->irq)
  87. break;
  88. }
  89. } else if (dev->msi_enabled) {
  90. entry = irq_get_msi_desc(irq);
  91. }
  92. if (entry)
  93. write_msi_msg(irq, &entry->msg);
  94. }
  95. #endif
  96. static void msi_set_enable(struct pci_dev *dev, int enable)
  97. {
  98. u16 control;
  99. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  100. control &= ~PCI_MSI_FLAGS_ENABLE;
  101. if (enable)
  102. control |= PCI_MSI_FLAGS_ENABLE;
  103. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  104. }
  105. static void msix_set_enable(struct pci_dev *dev, int enable)
  106. {
  107. u16 control;
  108. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  109. control &= ~PCI_MSIX_FLAGS_ENABLE;
  110. if (enable)
  111. control |= PCI_MSIX_FLAGS_ENABLE;
  112. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  113. }
  114. static inline __attribute_const__ u32 msi_mask(unsigned x)
  115. {
  116. /* Don't shift by >= width of type */
  117. if (x >= 5)
  118. return 0xffffffff;
  119. return (1 << (1 << x)) - 1;
  120. }
  121. static inline __attribute_const__ u32 msi_capable_mask(u16 control)
  122. {
  123. return msi_mask((control >> 1) & 7);
  124. }
  125. static inline __attribute_const__ u32 msi_enabled_mask(u16 control)
  126. {
  127. return msi_mask((control >> 4) & 7);
  128. }
  129. /*
  130. * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
  131. * mask all MSI interrupts by clearing the MSI enable bit does not work
  132. * reliably as devices without an INTx disable bit will then generate a
  133. * level IRQ which will never be cleared.
  134. */
  135. static u32 __msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  136. {
  137. u32 mask_bits = desc->masked;
  138. if (!desc->msi_attrib.maskbit)
  139. return 0;
  140. mask_bits &= ~mask;
  141. mask_bits |= flag;
  142. pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
  143. return mask_bits;
  144. }
  145. static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  146. {
  147. desc->masked = __msi_mask_irq(desc, mask, flag);
  148. }
  149. /*
  150. * This internal function does not flush PCI writes to the device.
  151. * All users must ensure that they read from the device before either
  152. * assuming that the device state is up to date, or returning out of this
  153. * file. This saves a few milliseconds when initialising devices with lots
  154. * of MSI-X interrupts.
  155. */
  156. static u32 __msix_mask_irq(struct msi_desc *desc, u32 flag)
  157. {
  158. u32 mask_bits = desc->masked;
  159. unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  160. PCI_MSIX_ENTRY_VECTOR_CTRL;
  161. mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
  162. if (flag)
  163. mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
  164. writel(mask_bits, desc->mask_base + offset);
  165. return mask_bits;
  166. }
  167. static void msix_mask_irq(struct msi_desc *desc, u32 flag)
  168. {
  169. desc->masked = __msix_mask_irq(desc, flag);
  170. }
  171. #ifdef CONFIG_GENERIC_HARDIRQS
  172. static void msi_set_mask_bit(struct irq_data *data, u32 flag)
  173. {
  174. struct msi_desc *desc = irq_data_get_msi(data);
  175. if (desc->msi_attrib.is_msix) {
  176. msix_mask_irq(desc, flag);
  177. readl(desc->mask_base); /* Flush write to device */
  178. } else {
  179. unsigned offset = data->irq - desc->dev->irq;
  180. msi_mask_irq(desc, 1 << offset, flag << offset);
  181. }
  182. }
  183. void mask_msi_irq(struct irq_data *data)
  184. {
  185. msi_set_mask_bit(data, 1);
  186. }
  187. void unmask_msi_irq(struct irq_data *data)
  188. {
  189. msi_set_mask_bit(data, 0);
  190. }
  191. #endif /* CONFIG_GENERIC_HARDIRQS */
  192. void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  193. {
  194. BUG_ON(entry->dev->current_state != PCI_D0);
  195. if (entry->msi_attrib.is_msix) {
  196. void __iomem *base = entry->mask_base +
  197. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  198. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
  199. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
  200. msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
  201. } else {
  202. struct pci_dev *dev = entry->dev;
  203. int pos = dev->msi_cap;
  204. u16 data;
  205. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  206. &msg->address_lo);
  207. if (entry->msi_attrib.is_64) {
  208. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  209. &msg->address_hi);
  210. pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
  211. } else {
  212. msg->address_hi = 0;
  213. pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
  214. }
  215. msg->data = data;
  216. }
  217. }
  218. void read_msi_msg(unsigned int irq, struct msi_msg *msg)
  219. {
  220. struct msi_desc *entry = irq_get_msi_desc(irq);
  221. __read_msi_msg(entry, msg);
  222. }
  223. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  224. {
  225. /* Assert that the cache is valid, assuming that
  226. * valid messages are not all-zeroes. */
  227. BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
  228. entry->msg.data));
  229. *msg = entry->msg;
  230. }
  231. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  232. {
  233. struct msi_desc *entry = irq_get_msi_desc(irq);
  234. __get_cached_msi_msg(entry, msg);
  235. }
  236. void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  237. {
  238. if (entry->dev->current_state != PCI_D0) {
  239. /* Don't touch the hardware now */
  240. } else if (entry->msi_attrib.is_msix) {
  241. void __iomem *base;
  242. base = entry->mask_base +
  243. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  244. writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
  245. writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
  246. writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
  247. } else {
  248. struct pci_dev *dev = entry->dev;
  249. int pos = dev->msi_cap;
  250. u16 msgctl;
  251. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
  252. msgctl &= ~PCI_MSI_FLAGS_QSIZE;
  253. msgctl |= entry->msi_attrib.multiple << 4;
  254. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
  255. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  256. msg->address_lo);
  257. if (entry->msi_attrib.is_64) {
  258. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  259. msg->address_hi);
  260. pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
  261. msg->data);
  262. } else {
  263. pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
  264. msg->data);
  265. }
  266. }
  267. entry->msg = *msg;
  268. }
  269. void write_msi_msg(unsigned int irq, struct msi_msg *msg)
  270. {
  271. struct msi_desc *entry = irq_get_msi_desc(irq);
  272. __write_msi_msg(entry, msg);
  273. }
  274. static void free_msi_irqs(struct pci_dev *dev)
  275. {
  276. struct msi_desc *entry, *tmp;
  277. list_for_each_entry(entry, &dev->msi_list, list) {
  278. int i, nvec;
  279. if (!entry->irq)
  280. continue;
  281. nvec = 1 << entry->msi_attrib.multiple;
  282. #ifdef CONFIG_GENERIC_HARDIRQS
  283. for (i = 0; i < nvec; i++)
  284. BUG_ON(irq_has_action(entry->irq + i));
  285. #endif
  286. }
  287. arch_teardown_msi_irqs(dev);
  288. list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
  289. if (entry->msi_attrib.is_msix) {
  290. if (list_is_last(&entry->list, &dev->msi_list))
  291. iounmap(entry->mask_base);
  292. }
  293. /*
  294. * Its possible that we get into this path
  295. * When populate_msi_sysfs fails, which means the entries
  296. * were not registered with sysfs. In that case don't
  297. * unregister them.
  298. */
  299. if (entry->kobj.parent) {
  300. kobject_del(&entry->kobj);
  301. kobject_put(&entry->kobj);
  302. }
  303. list_del(&entry->list);
  304. kfree(entry);
  305. }
  306. }
  307. static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
  308. {
  309. struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  310. if (!desc)
  311. return NULL;
  312. INIT_LIST_HEAD(&desc->list);
  313. desc->dev = dev;
  314. return desc;
  315. }
  316. static void pci_intx_for_msi(struct pci_dev *dev, int enable)
  317. {
  318. if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
  319. pci_intx(dev, enable);
  320. }
  321. static void __pci_restore_msi_state(struct pci_dev *dev)
  322. {
  323. u16 control;
  324. struct msi_desc *entry;
  325. if (!dev->msi_enabled)
  326. return;
  327. entry = irq_get_msi_desc(dev->irq);
  328. pci_intx_for_msi(dev, 0);
  329. msi_set_enable(dev, 0);
  330. arch_restore_msi_irqs(dev, dev->irq);
  331. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  332. msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
  333. control &= ~PCI_MSI_FLAGS_QSIZE;
  334. control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
  335. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  336. }
  337. static void __pci_restore_msix_state(struct pci_dev *dev)
  338. {
  339. struct msi_desc *entry;
  340. u16 control;
  341. if (!dev->msix_enabled)
  342. return;
  343. BUG_ON(list_empty(&dev->msi_list));
  344. entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
  345. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  346. /* route the table */
  347. pci_intx_for_msi(dev, 0);
  348. control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL;
  349. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  350. list_for_each_entry(entry, &dev->msi_list, list) {
  351. arch_restore_msi_irqs(dev, entry->irq);
  352. msix_mask_irq(entry, entry->masked);
  353. }
  354. control &= ~PCI_MSIX_FLAGS_MASKALL;
  355. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  356. }
  357. void pci_restore_msi_state(struct pci_dev *dev)
  358. {
  359. __pci_restore_msi_state(dev);
  360. __pci_restore_msix_state(dev);
  361. }
  362. EXPORT_SYMBOL_GPL(pci_restore_msi_state);
  363. #define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
  364. #define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
  365. struct msi_attribute {
  366. struct attribute attr;
  367. ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr,
  368. char *buf);
  369. ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr,
  370. const char *buf, size_t count);
  371. };
  372. static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr,
  373. char *buf)
  374. {
  375. return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi");
  376. }
  377. static ssize_t msi_irq_attr_show(struct kobject *kobj,
  378. struct attribute *attr, char *buf)
  379. {
  380. struct msi_attribute *attribute = to_msi_attr(attr);
  381. struct msi_desc *entry = to_msi_desc(kobj);
  382. if (!attribute->show)
  383. return -EIO;
  384. return attribute->show(entry, attribute, buf);
  385. }
  386. static const struct sysfs_ops msi_irq_sysfs_ops = {
  387. .show = msi_irq_attr_show,
  388. };
  389. static struct msi_attribute mode_attribute =
  390. __ATTR(mode, S_IRUGO, show_msi_mode, NULL);
  391. static struct attribute *msi_irq_default_attrs[] = {
  392. &mode_attribute.attr,
  393. NULL
  394. };
  395. static void msi_kobj_release(struct kobject *kobj)
  396. {
  397. struct msi_desc *entry = to_msi_desc(kobj);
  398. pci_dev_put(entry->dev);
  399. }
  400. static struct kobj_type msi_irq_ktype = {
  401. .release = msi_kobj_release,
  402. .sysfs_ops = &msi_irq_sysfs_ops,
  403. .default_attrs = msi_irq_default_attrs,
  404. };
  405. static int populate_msi_sysfs(struct pci_dev *pdev)
  406. {
  407. struct msi_desc *entry;
  408. struct kobject *kobj;
  409. int ret;
  410. int count = 0;
  411. pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj);
  412. if (!pdev->msi_kset)
  413. return -ENOMEM;
  414. list_for_each_entry(entry, &pdev->msi_list, list) {
  415. kobj = &entry->kobj;
  416. kobj->kset = pdev->msi_kset;
  417. pci_dev_get(pdev);
  418. ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL,
  419. "%u", entry->irq);
  420. if (ret)
  421. goto out_unroll;
  422. count++;
  423. }
  424. return 0;
  425. out_unroll:
  426. list_for_each_entry(entry, &pdev->msi_list, list) {
  427. if (!count)
  428. break;
  429. kobject_del(&entry->kobj);
  430. kobject_put(&entry->kobj);
  431. count--;
  432. }
  433. return ret;
  434. }
  435. /**
  436. * msi_capability_init - configure device's MSI capability structure
  437. * @dev: pointer to the pci_dev data structure of MSI device function
  438. * @nvec: number of interrupts to allocate
  439. *
  440. * Setup the MSI capability structure of the device with the requested
  441. * number of interrupts. A return value of zero indicates the successful
  442. * setup of an entry with the new MSI irq. A negative return value indicates
  443. * an error, and a positive return value indicates the number of interrupts
  444. * which could have been allocated.
  445. */
  446. static int msi_capability_init(struct pci_dev *dev, int nvec)
  447. {
  448. struct msi_desc *entry;
  449. int ret;
  450. u16 control;
  451. unsigned mask;
  452. msi_set_enable(dev, 0); /* Disable MSI during set up */
  453. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  454. /* MSI Entry Initialization */
  455. entry = alloc_msi_entry(dev);
  456. if (!entry)
  457. return -ENOMEM;
  458. entry->msi_attrib.is_msix = 0;
  459. entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
  460. entry->msi_attrib.entry_nr = 0;
  461. entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
  462. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  463. entry->msi_attrib.pos = dev->msi_cap;
  464. entry->mask_pos = dev->msi_cap + (control & PCI_MSI_FLAGS_64BIT) ?
  465. PCI_MSI_MASK_64 : PCI_MSI_MASK_32;
  466. /* All MSIs are unmasked by default, Mask them all */
  467. if (entry->msi_attrib.maskbit)
  468. pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
  469. mask = msi_capable_mask(control);
  470. msi_mask_irq(entry, mask, mask);
  471. list_add_tail(&entry->list, &dev->msi_list);
  472. /* Configure MSI capability structure */
  473. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
  474. if (ret) {
  475. msi_mask_irq(entry, mask, ~mask);
  476. free_msi_irqs(dev);
  477. return ret;
  478. }
  479. ret = populate_msi_sysfs(dev);
  480. if (ret) {
  481. msi_mask_irq(entry, mask, ~mask);
  482. free_msi_irqs(dev);
  483. return ret;
  484. }
  485. /* Set MSI enabled bits */
  486. pci_intx_for_msi(dev, 0);
  487. msi_set_enable(dev, 1);
  488. dev->msi_enabled = 1;
  489. dev->irq = entry->irq;
  490. return 0;
  491. }
  492. static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
  493. {
  494. resource_size_t phys_addr;
  495. u32 table_offset;
  496. u8 bir;
  497. pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
  498. &table_offset);
  499. bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
  500. table_offset &= PCI_MSIX_TABLE_OFFSET;
  501. phys_addr = pci_resource_start(dev, bir) + table_offset;
  502. return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  503. }
  504. static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
  505. struct msix_entry *entries, int nvec)
  506. {
  507. struct msi_desc *entry;
  508. int i;
  509. for (i = 0; i < nvec; i++) {
  510. entry = alloc_msi_entry(dev);
  511. if (!entry) {
  512. if (!i)
  513. iounmap(base);
  514. else
  515. free_msi_irqs(dev);
  516. /* No enough memory. Don't try again */
  517. return -ENOMEM;
  518. }
  519. entry->msi_attrib.is_msix = 1;
  520. entry->msi_attrib.is_64 = 1;
  521. entry->msi_attrib.entry_nr = entries[i].entry;
  522. entry->msi_attrib.default_irq = dev->irq;
  523. entry->msi_attrib.pos = dev->msix_cap;
  524. entry->mask_base = base;
  525. list_add_tail(&entry->list, &dev->msi_list);
  526. }
  527. return 0;
  528. }
  529. static void msix_program_entries(struct pci_dev *dev,
  530. struct msix_entry *entries)
  531. {
  532. struct msi_desc *entry;
  533. int i = 0;
  534. list_for_each_entry(entry, &dev->msi_list, list) {
  535. int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
  536. PCI_MSIX_ENTRY_VECTOR_CTRL;
  537. entries[i].vector = entry->irq;
  538. irq_set_msi_desc(entry->irq, entry);
  539. entry->masked = readl(entry->mask_base + offset);
  540. msix_mask_irq(entry, 1);
  541. i++;
  542. }
  543. }
  544. /**
  545. * msix_capability_init - configure device's MSI-X capability
  546. * @dev: pointer to the pci_dev data structure of MSI-X device function
  547. * @entries: pointer to an array of struct msix_entry entries
  548. * @nvec: number of @entries
  549. *
  550. * Setup the MSI-X capability structure of device function with a
  551. * single MSI-X irq. A return of zero indicates the successful setup of
  552. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  553. **/
  554. static int msix_capability_init(struct pci_dev *dev,
  555. struct msix_entry *entries, int nvec)
  556. {
  557. int ret;
  558. u16 control;
  559. void __iomem *base;
  560. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  561. /* Ensure MSI-X is disabled while it is set up */
  562. control &= ~PCI_MSIX_FLAGS_ENABLE;
  563. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  564. /* Request & Map MSI-X table region */
  565. base = msix_map_region(dev, msix_table_size(control));
  566. if (!base)
  567. return -ENOMEM;
  568. ret = msix_setup_entries(dev, base, entries, nvec);
  569. if (ret)
  570. return ret;
  571. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  572. if (ret)
  573. goto error;
  574. /*
  575. * Some devices require MSI-X to be enabled before we can touch the
  576. * MSI-X registers. We need to mask all the vectors to prevent
  577. * interrupts coming in before they're fully set up.
  578. */
  579. control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
  580. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  581. msix_program_entries(dev, entries);
  582. ret = populate_msi_sysfs(dev);
  583. if (ret) {
  584. ret = 0;
  585. goto error;
  586. }
  587. /* Set MSI-X enabled bits and unmask the function */
  588. pci_intx_for_msi(dev, 0);
  589. dev->msix_enabled = 1;
  590. control &= ~PCI_MSIX_FLAGS_MASKALL;
  591. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
  592. return 0;
  593. error:
  594. if (ret < 0) {
  595. /*
  596. * If we had some success, report the number of irqs
  597. * we succeeded in setting up.
  598. */
  599. struct msi_desc *entry;
  600. int avail = 0;
  601. list_for_each_entry(entry, &dev->msi_list, list) {
  602. if (entry->irq != 0)
  603. avail++;
  604. }
  605. if (avail != 0)
  606. ret = avail;
  607. }
  608. free_msi_irqs(dev);
  609. return ret;
  610. }
  611. /**
  612. * pci_msi_check_device - check whether MSI may be enabled on a device
  613. * @dev: pointer to the pci_dev data structure of MSI device function
  614. * @nvec: how many MSIs have been requested ?
  615. * @type: are we checking for MSI or MSI-X ?
  616. *
  617. * Look at global flags, the device itself, and its parent busses
  618. * to determine if MSI/-X are supported for the device. If MSI/-X is
  619. * supported return 0, else return an error code.
  620. **/
  621. static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
  622. {
  623. struct pci_bus *bus;
  624. int ret;
  625. /* MSI must be globally enabled and supported by the device */
  626. if (!pci_msi_enable || !dev || dev->no_msi)
  627. return -EINVAL;
  628. /*
  629. * You can't ask to have 0 or less MSIs configured.
  630. * a) it's stupid ..
  631. * b) the list manipulation code assumes nvec >= 1.
  632. */
  633. if (nvec < 1)
  634. return -ERANGE;
  635. /*
  636. * Any bridge which does NOT route MSI transactions from its
  637. * secondary bus to its primary bus must set NO_MSI flag on
  638. * the secondary pci_bus.
  639. * We expect only arch-specific PCI host bus controller driver
  640. * or quirks for specific PCI bridges to be setting NO_MSI.
  641. */
  642. for (bus = dev->bus; bus; bus = bus->parent)
  643. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  644. return -EINVAL;
  645. ret = arch_msi_check_device(dev, nvec, type);
  646. if (ret)
  647. return ret;
  648. return 0;
  649. }
  650. /**
  651. * pci_enable_msi_block - configure device's MSI capability structure
  652. * @dev: device to configure
  653. * @nvec: number of interrupts to configure
  654. *
  655. * Allocate IRQs for a device with the MSI capability.
  656. * This function returns a negative errno if an error occurs. If it
  657. * is unable to allocate the number of interrupts requested, it returns
  658. * the number of interrupts it might be able to allocate. If it successfully
  659. * allocates at least the number of interrupts requested, it returns 0 and
  660. * updates the @dev's irq member to the lowest new interrupt number; the
  661. * other interrupt numbers allocated to this device are consecutive.
  662. */
  663. int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
  664. {
  665. int status, maxvec;
  666. u16 msgctl;
  667. if (!dev->msi_cap)
  668. return -EINVAL;
  669. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
  670. maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  671. if (nvec > maxvec)
  672. return maxvec;
  673. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
  674. if (status)
  675. return status;
  676. WARN_ON(!!dev->msi_enabled);
  677. /* Check whether driver already requested MSI-X irqs */
  678. if (dev->msix_enabled) {
  679. dev_info(&dev->dev, "can't enable MSI "
  680. "(MSI-X already enabled)\n");
  681. return -EINVAL;
  682. }
  683. status = msi_capability_init(dev, nvec);
  684. return status;
  685. }
  686. EXPORT_SYMBOL(pci_enable_msi_block);
  687. int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *maxvec)
  688. {
  689. int ret, nvec;
  690. u16 msgctl;
  691. if (!dev->msi_cap)
  692. return -EINVAL;
  693. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
  694. ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  695. if (maxvec)
  696. *maxvec = ret;
  697. do {
  698. nvec = ret;
  699. ret = pci_enable_msi_block(dev, nvec);
  700. } while (ret > 0);
  701. if (ret < 0)
  702. return ret;
  703. return nvec;
  704. }
  705. EXPORT_SYMBOL(pci_enable_msi_block_auto);
  706. void pci_msi_shutdown(struct pci_dev *dev)
  707. {
  708. struct msi_desc *desc;
  709. u32 mask;
  710. u16 ctrl;
  711. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  712. return;
  713. BUG_ON(list_empty(&dev->msi_list));
  714. desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
  715. msi_set_enable(dev, 0);
  716. pci_intx_for_msi(dev, 1);
  717. dev->msi_enabled = 0;
  718. /* Return the device with MSI unmasked as initial states */
  719. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
  720. mask = msi_capable_mask(ctrl);
  721. /* Keep cached state to be restored */
  722. __msi_mask_irq(desc, mask, ~mask);
  723. /* Restore dev->irq to its default pin-assertion irq */
  724. dev->irq = desc->msi_attrib.default_irq;
  725. }
  726. void pci_disable_msi(struct pci_dev *dev)
  727. {
  728. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  729. return;
  730. pci_msi_shutdown(dev);
  731. free_msi_irqs(dev);
  732. kset_unregister(dev->msi_kset);
  733. dev->msi_kset = NULL;
  734. }
  735. EXPORT_SYMBOL(pci_disable_msi);
  736. /**
  737. * pci_msix_table_size - return the number of device's MSI-X table entries
  738. * @dev: pointer to the pci_dev data structure of MSI-X device function
  739. */
  740. int pci_msix_table_size(struct pci_dev *dev)
  741. {
  742. u16 control;
  743. if (!dev->msix_cap)
  744. return 0;
  745. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  746. return msix_table_size(control);
  747. }
  748. /**
  749. * pci_enable_msix - configure device's MSI-X capability structure
  750. * @dev: pointer to the pci_dev data structure of MSI-X device function
  751. * @entries: pointer to an array of MSI-X entries
  752. * @nvec: number of MSI-X irqs requested for allocation by device driver
  753. *
  754. * Setup the MSI-X capability structure of device function with the number
  755. * of requested irqs upon its software driver call to request for
  756. * MSI-X mode enabled on its hardware device function. A return of zero
  757. * indicates the successful configuration of MSI-X capability structure
  758. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  759. * Or a return of > 0 indicates that driver request is exceeding the number
  760. * of irqs or MSI-X vectors available. Driver should use the returned value to
  761. * re-send its request.
  762. **/
  763. int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
  764. {
  765. int status, nr_entries;
  766. int i, j;
  767. if (!entries || !dev->msix_cap)
  768. return -EINVAL;
  769. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
  770. if (status)
  771. return status;
  772. nr_entries = pci_msix_table_size(dev);
  773. if (nvec > nr_entries)
  774. return nr_entries;
  775. /* Check for any invalid entries */
  776. for (i = 0; i < nvec; i++) {
  777. if (entries[i].entry >= nr_entries)
  778. return -EINVAL; /* invalid entry */
  779. for (j = i + 1; j < nvec; j++) {
  780. if (entries[i].entry == entries[j].entry)
  781. return -EINVAL; /* duplicate entry */
  782. }
  783. }
  784. WARN_ON(!!dev->msix_enabled);
  785. /* Check whether driver already requested for MSI irq */
  786. if (dev->msi_enabled) {
  787. dev_info(&dev->dev, "can't enable MSI-X "
  788. "(MSI IRQ already assigned)\n");
  789. return -EINVAL;
  790. }
  791. status = msix_capability_init(dev, entries, nvec);
  792. return status;
  793. }
  794. EXPORT_SYMBOL(pci_enable_msix);
  795. void pci_msix_shutdown(struct pci_dev *dev)
  796. {
  797. struct msi_desc *entry;
  798. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  799. return;
  800. /* Return the device with MSI-X masked as initial states */
  801. list_for_each_entry(entry, &dev->msi_list, list) {
  802. /* Keep cached states to be restored */
  803. __msix_mask_irq(entry, 1);
  804. }
  805. msix_set_enable(dev, 0);
  806. pci_intx_for_msi(dev, 1);
  807. dev->msix_enabled = 0;
  808. }
  809. void pci_disable_msix(struct pci_dev *dev)
  810. {
  811. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  812. return;
  813. pci_msix_shutdown(dev);
  814. free_msi_irqs(dev);
  815. kset_unregister(dev->msi_kset);
  816. dev->msi_kset = NULL;
  817. }
  818. EXPORT_SYMBOL(pci_disable_msix);
  819. /**
  820. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  821. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  822. *
  823. * Being called during hotplug remove, from which the device function
  824. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  825. * allocated for this device function, are reclaimed to unused state,
  826. * which may be used later on.
  827. **/
  828. void msi_remove_pci_irq_vectors(struct pci_dev *dev)
  829. {
  830. if (!pci_msi_enable || !dev)
  831. return;
  832. if (dev->msi_enabled || dev->msix_enabled)
  833. free_msi_irqs(dev);
  834. }
  835. void pci_no_msi(void)
  836. {
  837. pci_msi_enable = 0;
  838. }
  839. /**
  840. * pci_msi_enabled - is MSI enabled?
  841. *
  842. * Returns true if MSI has not been disabled by the command-line option
  843. * pci=nomsi.
  844. **/
  845. int pci_msi_enabled(void)
  846. {
  847. return pci_msi_enable;
  848. }
  849. EXPORT_SYMBOL(pci_msi_enabled);
  850. void pci_msi_init_pci_dev(struct pci_dev *dev)
  851. {
  852. INIT_LIST_HEAD(&dev->msi_list);
  853. /* Disable the msi hardware to avoid screaming interrupts
  854. * during boot. This is the power on reset default so
  855. * usually this should be a noop.
  856. */
  857. dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
  858. if (dev->msi_cap)
  859. msi_set_enable(dev, 0);
  860. dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  861. if (dev->msix_cap)
  862. msix_set_enable(dev, 0);
  863. }