msi.c 30 KB

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