msi.c 28 KB

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