msi.c 25 KB

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