msi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. #include "msi.h"
  24. static int pci_msi_enable = 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 = entry->msi_attrib.pos;
  204. u16 data;
  205. pci_read_config_dword(dev, msi_lower_address_reg(pos),
  206. &msg->address_lo);
  207. if (entry->msi_attrib.is_64) {
  208. pci_read_config_dword(dev, msi_upper_address_reg(pos),
  209. &msg->address_hi);
  210. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  211. } else {
  212. msg->address_hi = 0;
  213. pci_read_config_word(dev, msi_data_reg(pos, 0), &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 = entry->msi_attrib.pos;
  250. u16 msgctl;
  251. pci_read_config_word(dev, msi_control_reg(pos), &msgctl);
  252. msgctl &= ~PCI_MSI_FLAGS_QSIZE;
  253. msgctl |= entry->msi_attrib.multiple << 4;
  254. pci_write_config_word(dev, msi_control_reg(pos), msgctl);
  255. pci_write_config_dword(dev, msi_lower_address_reg(pos),
  256. msg->address_lo);
  257. if (entry->msi_attrib.is_64) {
  258. pci_write_config_dword(dev, msi_upper_address_reg(pos),
  259. msg->address_hi);
  260. pci_write_config_word(dev, msi_data_reg(pos, 1),
  261. msg->data);
  262. } else {
  263. pci_write_config_word(dev, msi_data_reg(pos, 0),
  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. int pos;
  324. u16 control;
  325. struct msi_desc *entry;
  326. if (!dev->msi_enabled)
  327. return;
  328. entry = irq_get_msi_desc(dev->irq);
  329. pos = entry->msi_attrib.pos;
  330. pci_intx_for_msi(dev, 0);
  331. msi_set_enable(dev, 0);
  332. arch_restore_msi_irqs(dev, dev->irq);
  333. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  334. msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
  335. control &= ~PCI_MSI_FLAGS_QSIZE;
  336. control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
  337. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  338. }
  339. static void __pci_restore_msix_state(struct pci_dev *dev)
  340. {
  341. int pos;
  342. struct msi_desc *entry;
  343. u16 control;
  344. if (!dev->msix_enabled)
  345. return;
  346. BUG_ON(list_empty(&dev->msi_list));
  347. entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
  348. pos = entry->msi_attrib.pos;
  349. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  350. /* route the table */
  351. pci_intx_for_msi(dev, 0);
  352. control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL;
  353. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  354. list_for_each_entry(entry, &dev->msi_list, list) {
  355. arch_restore_msi_irqs(dev, entry->irq);
  356. msix_mask_irq(entry, entry->masked);
  357. }
  358. control &= ~PCI_MSIX_FLAGS_MASKALL;
  359. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  360. }
  361. void pci_restore_msi_state(struct pci_dev *dev)
  362. {
  363. __pci_restore_msi_state(dev);
  364. __pci_restore_msix_state(dev);
  365. }
  366. EXPORT_SYMBOL_GPL(pci_restore_msi_state);
  367. #define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
  368. #define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
  369. struct msi_attribute {
  370. struct attribute attr;
  371. ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr,
  372. char *buf);
  373. ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr,
  374. const char *buf, size_t count);
  375. };
  376. static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr,
  377. char *buf)
  378. {
  379. return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi");
  380. }
  381. static ssize_t msi_irq_attr_show(struct kobject *kobj,
  382. struct attribute *attr, char *buf)
  383. {
  384. struct msi_attribute *attribute = to_msi_attr(attr);
  385. struct msi_desc *entry = to_msi_desc(kobj);
  386. if (!attribute->show)
  387. return -EIO;
  388. return attribute->show(entry, attribute, buf);
  389. }
  390. static const struct sysfs_ops msi_irq_sysfs_ops = {
  391. .show = msi_irq_attr_show,
  392. };
  393. static struct msi_attribute mode_attribute =
  394. __ATTR(mode, S_IRUGO, show_msi_mode, NULL);
  395. struct attribute *msi_irq_default_attrs[] = {
  396. &mode_attribute.attr,
  397. NULL
  398. };
  399. void msi_kobj_release(struct kobject *kobj)
  400. {
  401. struct msi_desc *entry = to_msi_desc(kobj);
  402. pci_dev_put(entry->dev);
  403. }
  404. static struct kobj_type msi_irq_ktype = {
  405. .release = msi_kobj_release,
  406. .sysfs_ops = &msi_irq_sysfs_ops,
  407. .default_attrs = msi_irq_default_attrs,
  408. };
  409. static int populate_msi_sysfs(struct pci_dev *pdev)
  410. {
  411. struct msi_desc *entry;
  412. struct kobject *kobj;
  413. int ret;
  414. int count = 0;
  415. pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj);
  416. if (!pdev->msi_kset)
  417. return -ENOMEM;
  418. list_for_each_entry(entry, &pdev->msi_list, list) {
  419. kobj = &entry->kobj;
  420. kobj->kset = pdev->msi_kset;
  421. pci_dev_get(pdev);
  422. ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL,
  423. "%u", entry->irq);
  424. if (ret)
  425. goto out_unroll;
  426. count++;
  427. }
  428. return 0;
  429. out_unroll:
  430. list_for_each_entry(entry, &pdev->msi_list, list) {
  431. if (!count)
  432. break;
  433. kobject_del(&entry->kobj);
  434. kobject_put(&entry->kobj);
  435. count--;
  436. }
  437. return ret;
  438. }
  439. /**
  440. * msi_capability_init - configure device's MSI capability structure
  441. * @dev: pointer to the pci_dev data structure of MSI device function
  442. * @nvec: number of interrupts to allocate
  443. *
  444. * Setup the MSI capability structure of the device with the requested
  445. * number of interrupts. A return value of zero indicates the successful
  446. * setup of an entry with the new MSI irq. A negative return value indicates
  447. * an error, and a positive return value indicates the number of interrupts
  448. * which could have been allocated.
  449. */
  450. static int msi_capability_init(struct pci_dev *dev, int nvec)
  451. {
  452. struct msi_desc *entry;
  453. int pos, ret;
  454. u16 control;
  455. unsigned mask;
  456. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  457. msi_set_enable(dev, 0); /* Disable MSI during set up */
  458. pci_read_config_word(dev, msi_control_reg(pos), &control);
  459. /* MSI Entry Initialization */
  460. entry = alloc_msi_entry(dev);
  461. if (!entry)
  462. return -ENOMEM;
  463. entry->msi_attrib.is_msix = 0;
  464. entry->msi_attrib.is_64 = is_64bit_address(control);
  465. entry->msi_attrib.entry_nr = 0;
  466. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  467. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  468. entry->msi_attrib.pos = pos;
  469. entry->mask_pos = msi_mask_reg(pos, entry->msi_attrib.is_64);
  470. /* All MSIs are unmasked by default, Mask them all */
  471. if (entry->msi_attrib.maskbit)
  472. pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
  473. mask = msi_capable_mask(control);
  474. msi_mask_irq(entry, mask, mask);
  475. list_add_tail(&entry->list, &dev->msi_list);
  476. /* Configure MSI capability structure */
  477. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
  478. if (ret) {
  479. msi_mask_irq(entry, mask, ~mask);
  480. free_msi_irqs(dev);
  481. return ret;
  482. }
  483. ret = populate_msi_sysfs(dev);
  484. if (ret) {
  485. msi_mask_irq(entry, mask, ~mask);
  486. free_msi_irqs(dev);
  487. return ret;
  488. }
  489. /* Set MSI enabled bits */
  490. pci_intx_for_msi(dev, 0);
  491. msi_set_enable(dev, 1);
  492. dev->msi_enabled = 1;
  493. dev->irq = entry->irq;
  494. return 0;
  495. }
  496. static void __iomem *msix_map_region(struct pci_dev *dev, unsigned pos,
  497. unsigned nr_entries)
  498. {
  499. resource_size_t phys_addr;
  500. u32 table_offset;
  501. u8 bir;
  502. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  503. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  504. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  505. phys_addr = pci_resource_start(dev, bir) + table_offset;
  506. return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  507. }
  508. static int msix_setup_entries(struct pci_dev *dev, unsigned pos,
  509. void __iomem *base, struct msix_entry *entries,
  510. int nvec)
  511. {
  512. struct msi_desc *entry;
  513. int i;
  514. for (i = 0; i < nvec; i++) {
  515. entry = alloc_msi_entry(dev);
  516. if (!entry) {
  517. if (!i)
  518. iounmap(base);
  519. else
  520. free_msi_irqs(dev);
  521. /* No enough memory. Don't try again */
  522. return -ENOMEM;
  523. }
  524. entry->msi_attrib.is_msix = 1;
  525. entry->msi_attrib.is_64 = 1;
  526. entry->msi_attrib.entry_nr = entries[i].entry;
  527. entry->msi_attrib.default_irq = dev->irq;
  528. entry->msi_attrib.pos = pos;
  529. entry->mask_base = base;
  530. list_add_tail(&entry->list, &dev->msi_list);
  531. }
  532. return 0;
  533. }
  534. static void msix_program_entries(struct pci_dev *dev,
  535. struct msix_entry *entries)
  536. {
  537. struct msi_desc *entry;
  538. int i = 0;
  539. list_for_each_entry(entry, &dev->msi_list, list) {
  540. int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
  541. PCI_MSIX_ENTRY_VECTOR_CTRL;
  542. entries[i].vector = entry->irq;
  543. irq_set_msi_desc(entry->irq, entry);
  544. entry->masked = readl(entry->mask_base + offset);
  545. msix_mask_irq(entry, 1);
  546. i++;
  547. }
  548. }
  549. /**
  550. * msix_capability_init - configure device's MSI-X capability
  551. * @dev: pointer to the pci_dev data structure of MSI-X device function
  552. * @entries: pointer to an array of struct msix_entry entries
  553. * @nvec: number of @entries
  554. *
  555. * Setup the MSI-X capability structure of device function with a
  556. * single MSI-X irq. A return of zero indicates the successful setup of
  557. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  558. **/
  559. static int msix_capability_init(struct pci_dev *dev,
  560. struct msix_entry *entries, int nvec)
  561. {
  562. int pos, ret;
  563. u16 control;
  564. void __iomem *base;
  565. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  566. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  567. /* Ensure MSI-X is disabled while it is set up */
  568. control &= ~PCI_MSIX_FLAGS_ENABLE;
  569. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  570. /* Request & Map MSI-X table region */
  571. base = msix_map_region(dev, pos, multi_msix_capable(control));
  572. if (!base)
  573. return -ENOMEM;
  574. ret = msix_setup_entries(dev, pos, base, entries, nvec);
  575. if (ret)
  576. return ret;
  577. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  578. if (ret)
  579. goto error;
  580. /*
  581. * Some devices require MSI-X to be enabled before we can touch the
  582. * MSI-X registers. We need to mask all the vectors to prevent
  583. * interrupts coming in before they're fully set up.
  584. */
  585. control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
  586. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  587. msix_program_entries(dev, entries);
  588. ret = populate_msi_sysfs(dev);
  589. if (ret) {
  590. ret = 0;
  591. goto error;
  592. }
  593. /* Set MSI-X enabled bits and unmask the function */
  594. pci_intx_for_msi(dev, 0);
  595. dev->msix_enabled = 1;
  596. control &= ~PCI_MSIX_FLAGS_MASKALL;
  597. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  598. return 0;
  599. error:
  600. if (ret < 0) {
  601. /*
  602. * If we had some success, report the number of irqs
  603. * we succeeded in setting up.
  604. */
  605. struct msi_desc *entry;
  606. int avail = 0;
  607. list_for_each_entry(entry, &dev->msi_list, list) {
  608. if (entry->irq != 0)
  609. avail++;
  610. }
  611. if (avail != 0)
  612. ret = avail;
  613. }
  614. free_msi_irqs(dev);
  615. return ret;
  616. }
  617. /**
  618. * pci_msi_check_device - check whether MSI may be enabled on a device
  619. * @dev: pointer to the pci_dev data structure of MSI device function
  620. * @nvec: how many MSIs have been requested ?
  621. * @type: are we checking for MSI or MSI-X ?
  622. *
  623. * Look at global flags, the device itself, and its parent busses
  624. * to determine if MSI/-X are supported for the device. If MSI/-X is
  625. * supported return 0, else return an error code.
  626. **/
  627. static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
  628. {
  629. struct pci_bus *bus;
  630. int ret;
  631. /* MSI must be globally enabled and supported by the device */
  632. if (!pci_msi_enable || !dev || dev->no_msi)
  633. return -EINVAL;
  634. /*
  635. * You can't ask to have 0 or less MSIs configured.
  636. * a) it's stupid ..
  637. * b) the list manipulation code assumes nvec >= 1.
  638. */
  639. if (nvec < 1)
  640. return -ERANGE;
  641. /*
  642. * Any bridge which does NOT route MSI transactions from its
  643. * secondary bus to its primary bus must set NO_MSI flag on
  644. * the secondary pci_bus.
  645. * We expect only arch-specific PCI host bus controller driver
  646. * or quirks for specific PCI bridges to be setting NO_MSI.
  647. */
  648. for (bus = dev->bus; bus; bus = bus->parent)
  649. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  650. return -EINVAL;
  651. ret = arch_msi_check_device(dev, nvec, type);
  652. if (ret)
  653. return ret;
  654. if (!pci_find_capability(dev, type))
  655. return -EINVAL;
  656. return 0;
  657. }
  658. /**
  659. * pci_enable_msi_block - configure device's MSI capability structure
  660. * @dev: device to configure
  661. * @nvec: number of interrupts to configure
  662. *
  663. * Allocate IRQs for a device with the MSI capability.
  664. * This function returns a negative errno if an error occurs. If it
  665. * is unable to allocate the number of interrupts requested, it returns
  666. * the number of interrupts it might be able to allocate. If it successfully
  667. * allocates at least the number of interrupts requested, it returns 0 and
  668. * updates the @dev's irq member to the lowest new interrupt number; the
  669. * other interrupt numbers allocated to this device are consecutive.
  670. */
  671. int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
  672. {
  673. int status, pos, maxvec;
  674. u16 msgctl;
  675. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  676. if (!pos)
  677. return -EINVAL;
  678. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
  679. maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  680. if (nvec > maxvec)
  681. return maxvec;
  682. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
  683. if (status)
  684. return status;
  685. WARN_ON(!!dev->msi_enabled);
  686. /* Check whether driver already requested MSI-X irqs */
  687. if (dev->msix_enabled) {
  688. dev_info(&dev->dev, "can't enable MSI "
  689. "(MSI-X already enabled)\n");
  690. return -EINVAL;
  691. }
  692. status = msi_capability_init(dev, nvec);
  693. return status;
  694. }
  695. EXPORT_SYMBOL(pci_enable_msi_block);
  696. int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *maxvec)
  697. {
  698. int ret, pos, nvec;
  699. u16 msgctl;
  700. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  701. if (!pos)
  702. return -EINVAL;
  703. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
  704. ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  705. if (maxvec)
  706. *maxvec = ret;
  707. do {
  708. nvec = ret;
  709. ret = pci_enable_msi_block(dev, nvec);
  710. } while (ret > 0);
  711. if (ret < 0)
  712. return ret;
  713. return nvec;
  714. }
  715. EXPORT_SYMBOL(pci_enable_msi_block_auto);
  716. void pci_msi_shutdown(struct pci_dev *dev)
  717. {
  718. struct msi_desc *desc;
  719. u32 mask;
  720. u16 ctrl;
  721. unsigned pos;
  722. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  723. return;
  724. BUG_ON(list_empty(&dev->msi_list));
  725. desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
  726. pos = desc->msi_attrib.pos;
  727. msi_set_enable(dev, 0);
  728. pci_intx_for_msi(dev, 1);
  729. dev->msi_enabled = 0;
  730. /* Return the device with MSI unmasked as initial states */
  731. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &ctrl);
  732. mask = msi_capable_mask(ctrl);
  733. /* Keep cached state to be restored */
  734. __msi_mask_irq(desc, mask, ~mask);
  735. /* Restore dev->irq to its default pin-assertion irq */
  736. dev->irq = desc->msi_attrib.default_irq;
  737. }
  738. void pci_disable_msi(struct pci_dev *dev)
  739. {
  740. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  741. return;
  742. pci_msi_shutdown(dev);
  743. free_msi_irqs(dev);
  744. kset_unregister(dev->msi_kset);
  745. dev->msi_kset = NULL;
  746. }
  747. EXPORT_SYMBOL(pci_disable_msi);
  748. /**
  749. * pci_msix_table_size - return the number of device's MSI-X table entries
  750. * @dev: pointer to the pci_dev data structure of MSI-X device function
  751. */
  752. int pci_msix_table_size(struct pci_dev *dev)
  753. {
  754. int pos;
  755. u16 control;
  756. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  757. if (!pos)
  758. return 0;
  759. pci_read_config_word(dev, msi_control_reg(pos), &control);
  760. return multi_msix_capable(control);
  761. }
  762. /**
  763. * pci_enable_msix - configure device's MSI-X capability structure
  764. * @dev: pointer to the pci_dev data structure of MSI-X device function
  765. * @entries: pointer to an array of MSI-X entries
  766. * @nvec: number of MSI-X irqs requested for allocation by device driver
  767. *
  768. * Setup the MSI-X capability structure of device function with the number
  769. * of requested irqs upon its software driver call to request for
  770. * MSI-X mode enabled on its hardware device function. A return of zero
  771. * indicates the successful configuration of MSI-X capability structure
  772. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  773. * Or a return of > 0 indicates that driver request is exceeding the number
  774. * of irqs or MSI-X vectors available. Driver should use the returned value to
  775. * re-send its request.
  776. **/
  777. int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
  778. {
  779. int status, nr_entries;
  780. int i, j;
  781. if (!entries)
  782. return -EINVAL;
  783. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
  784. if (status)
  785. return status;
  786. nr_entries = pci_msix_table_size(dev);
  787. if (nvec > nr_entries)
  788. return nr_entries;
  789. /* Check for any invalid entries */
  790. for (i = 0; i < nvec; i++) {
  791. if (entries[i].entry >= nr_entries)
  792. return -EINVAL; /* invalid entry */
  793. for (j = i + 1; j < nvec; j++) {
  794. if (entries[i].entry == entries[j].entry)
  795. return -EINVAL; /* duplicate entry */
  796. }
  797. }
  798. WARN_ON(!!dev->msix_enabled);
  799. /* Check whether driver already requested for MSI irq */
  800. if (dev->msi_enabled) {
  801. dev_info(&dev->dev, "can't enable MSI-X "
  802. "(MSI IRQ already assigned)\n");
  803. return -EINVAL;
  804. }
  805. status = msix_capability_init(dev, entries, nvec);
  806. return status;
  807. }
  808. EXPORT_SYMBOL(pci_enable_msix);
  809. void pci_msix_shutdown(struct pci_dev *dev)
  810. {
  811. struct msi_desc *entry;
  812. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  813. return;
  814. /* Return the device with MSI-X masked as initial states */
  815. list_for_each_entry(entry, &dev->msi_list, list) {
  816. /* Keep cached states to be restored */
  817. __msix_mask_irq(entry, 1);
  818. }
  819. msix_set_enable(dev, 0);
  820. pci_intx_for_msi(dev, 1);
  821. dev->msix_enabled = 0;
  822. }
  823. void pci_disable_msix(struct pci_dev *dev)
  824. {
  825. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  826. return;
  827. pci_msix_shutdown(dev);
  828. free_msi_irqs(dev);
  829. kset_unregister(dev->msi_kset);
  830. dev->msi_kset = NULL;
  831. }
  832. EXPORT_SYMBOL(pci_disable_msix);
  833. /**
  834. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  835. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  836. *
  837. * Being called during hotplug remove, from which the device function
  838. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  839. * allocated for this device function, are reclaimed to unused state,
  840. * which may be used later on.
  841. **/
  842. void msi_remove_pci_irq_vectors(struct pci_dev *dev)
  843. {
  844. if (!pci_msi_enable || !dev)
  845. return;
  846. if (dev->msi_enabled || dev->msix_enabled)
  847. free_msi_irqs(dev);
  848. }
  849. void pci_no_msi(void)
  850. {
  851. pci_msi_enable = 0;
  852. }
  853. /**
  854. * pci_msi_enabled - is MSI enabled?
  855. *
  856. * Returns true if MSI has not been disabled by the command-line option
  857. * pci=nomsi.
  858. **/
  859. int pci_msi_enabled(void)
  860. {
  861. return pci_msi_enable;
  862. }
  863. EXPORT_SYMBOL(pci_msi_enabled);
  864. void pci_msi_init_pci_dev(struct pci_dev *dev)
  865. {
  866. INIT_LIST_HEAD(&dev->msi_list);
  867. /* Disable the msi hardware to avoid screaming interrupts
  868. * during boot. This is the power on reset default so
  869. * usually this should be a noop.
  870. */
  871. dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
  872. if (dev->msi_cap)
  873. msi_set_enable(dev, 0);
  874. dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  875. if (dev->msix_cap)
  876. msix_set_enable(dev, 0);
  877. }