msi.c 25 KB

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