pci_msi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. */
  7. #define COMPONENT "zPCI"
  8. #define pr_fmt(fmt) COMPONENT ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/err.h>
  11. #include <linux/rculist.h>
  12. #include <linux/hash.h>
  13. #include <linux/pci.h>
  14. #include <linux/msi.h>
  15. #include <asm/hw_irq.h>
  16. /* mapping of irq numbers to msi_desc */
  17. static struct hlist_head *msi_hash;
  18. static unsigned int msihash_shift = 6;
  19. #define msi_hashfn(nr) hash_long(nr, msihash_shift)
  20. static DEFINE_SPINLOCK(msi_map_lock);
  21. struct msi_desc *__irq_get_msi_desc(unsigned int irq)
  22. {
  23. struct msi_map *map;
  24. hlist_for_each_entry_rcu(map,
  25. &msi_hash[msi_hashfn(irq)], msi_chain)
  26. if (map->irq == irq)
  27. return map->msi;
  28. return NULL;
  29. }
  30. int zpci_msi_set_mask_bits(struct msi_desc *msi, u32 mask, u32 flag)
  31. {
  32. if (msi->msi_attrib.is_msix) {
  33. int offset = msi->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  34. PCI_MSIX_ENTRY_VECTOR_CTRL;
  35. msi->masked = readl(msi->mask_base + offset);
  36. writel(flag, msi->mask_base + offset);
  37. } else {
  38. if (msi->msi_attrib.maskbit) {
  39. int pos;
  40. u32 mask_bits;
  41. pos = (long) msi->mask_base;
  42. pci_read_config_dword(msi->dev, pos, &mask_bits);
  43. mask_bits &= ~(mask);
  44. mask_bits |= flag & mask;
  45. pci_write_config_dword(msi->dev, pos, mask_bits);
  46. } else {
  47. return 0;
  48. }
  49. }
  50. msi->msi_attrib.maskbit = !!flag;
  51. return 1;
  52. }
  53. int zpci_setup_msi_irq(struct zpci_dev *zdev, struct msi_desc *msi,
  54. unsigned int nr, int offset)
  55. {
  56. struct msi_map *map;
  57. struct msi_msg msg;
  58. int rc;
  59. map = kmalloc(sizeof(*map), GFP_KERNEL);
  60. if (map == NULL)
  61. return -ENOMEM;
  62. map->irq = nr;
  63. map->msi = msi;
  64. zdev->msi_map[nr & ZPCI_MSI_MASK] = map;
  65. pr_debug("%s hashing irq: %u to bucket nr: %llu\n",
  66. __func__, nr, msi_hashfn(nr));
  67. hlist_add_head_rcu(&map->msi_chain, &msi_hash[msi_hashfn(nr)]);
  68. spin_lock(&msi_map_lock);
  69. rc = irq_set_msi_desc(nr, msi);
  70. if (rc) {
  71. spin_unlock(&msi_map_lock);
  72. hlist_del_rcu(&map->msi_chain);
  73. kfree(map);
  74. zdev->msi_map[nr & ZPCI_MSI_MASK] = NULL;
  75. return rc;
  76. }
  77. spin_unlock(&msi_map_lock);
  78. msg.data = nr - offset;
  79. msg.address_lo = zdev->msi_addr & 0xffffffff;
  80. msg.address_hi = zdev->msi_addr >> 32;
  81. write_msi_msg(nr, &msg);
  82. return 0;
  83. }
  84. void zpci_teardown_msi_irq(struct zpci_dev *zdev, struct msi_desc *msi)
  85. {
  86. int irq = msi->irq & ZPCI_MSI_MASK;
  87. struct msi_map *map;
  88. msi->msg.address_lo = 0;
  89. msi->msg.address_hi = 0;
  90. msi->msg.data = 0;
  91. msi->irq = 0;
  92. zpci_msi_set_mask_bits(msi, 1, 1);
  93. spin_lock(&msi_map_lock);
  94. map = zdev->msi_map[irq];
  95. hlist_del_rcu(&map->msi_chain);
  96. kfree(map);
  97. zdev->msi_map[irq] = NULL;
  98. spin_unlock(&msi_map_lock);
  99. }
  100. /*
  101. * The msi hash table has 256 entries which is good for 4..20
  102. * devices (a typical device allocates 10 + CPUs MSI's). Maybe make
  103. * the hash table size adjustable later.
  104. */
  105. int __init zpci_msihash_init(void)
  106. {
  107. unsigned int i;
  108. msi_hash = kmalloc(256 * sizeof(*msi_hash), GFP_KERNEL);
  109. if (!msi_hash)
  110. return -ENOMEM;
  111. for (i = 0; i < (1U << msihash_shift); i++)
  112. INIT_HLIST_HEAD(&msi_hash[i]);
  113. return 0;
  114. }
  115. void __init zpci_msihash_exit(void)
  116. {
  117. kfree(msi_hash);
  118. }