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