msi.c 26 KB

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