htirq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * File: htirq.c
  4. * Purpose: Hypertransport Interrupt Capability
  5. *
  6. * Copyright (C) 2006 Linux Networx
  7. * Copyright (C) Eric Biederman <ebiederman@lnxi.com>
  8. */
  9. #include <linux/irq.h>
  10. #include <linux/pci.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/export.h>
  13. #include <linux/slab.h>
  14. #include <linux/htirq.h>
  15. /* Global ht irq lock.
  16. *
  17. * This is needed to serialize access to the data port in hypertransport
  18. * irq capability.
  19. *
  20. * With multiple simultaneous hypertransport irq devices it might pay
  21. * to make this more fine grained. But start with simple, stupid, and correct.
  22. */
  23. static DEFINE_SPINLOCK(ht_irq_lock);
  24. void write_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
  25. {
  26. struct ht_irq_cfg *cfg = irq_get_handler_data(irq);
  27. unsigned long flags;
  28. spin_lock_irqsave(&ht_irq_lock, flags);
  29. if (cfg->msg.address_lo != msg->address_lo) {
  30. pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx);
  31. pci_write_config_dword(cfg->dev, cfg->pos + 4, msg->address_lo);
  32. }
  33. if (cfg->msg.address_hi != msg->address_hi) {
  34. pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx + 1);
  35. pci_write_config_dword(cfg->dev, cfg->pos + 4, msg->address_hi);
  36. }
  37. if (cfg->update)
  38. cfg->update(cfg->dev, irq, msg);
  39. spin_unlock_irqrestore(&ht_irq_lock, flags);
  40. cfg->msg = *msg;
  41. }
  42. void fetch_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
  43. {
  44. struct ht_irq_cfg *cfg = irq_get_handler_data(irq);
  45. *msg = cfg->msg;
  46. }
  47. void mask_ht_irq(struct irq_data *data)
  48. {
  49. struct ht_irq_cfg *cfg = irq_data_get_irq_handler_data(data);
  50. struct ht_irq_msg msg = cfg->msg;
  51. msg.address_lo |= 1;
  52. write_ht_irq_msg(data->irq, &msg);
  53. }
  54. void unmask_ht_irq(struct irq_data *data)
  55. {
  56. struct ht_irq_cfg *cfg = irq_data_get_irq_handler_data(data);
  57. struct ht_irq_msg msg = cfg->msg;
  58. msg.address_lo &= ~1;
  59. write_ht_irq_msg(data->irq, &msg);
  60. }
  61. /**
  62. * __ht_create_irq - create an irq and attach it to a device.
  63. * @dev: The hypertransport device to find the irq capability on.
  64. * @idx: Which of the possible irqs to attach to.
  65. * @update: Function to be called when changing the htirq message
  66. *
  67. * The irq number of the new irq or a negative error value is returned.
  68. */
  69. int __ht_create_irq(struct pci_dev *dev, int idx, ht_irq_update_t *update)
  70. {
  71. int max_irq, pos, irq;
  72. unsigned long flags;
  73. u32 data;
  74. pos = pci_find_ht_capability(dev, HT_CAPTYPE_IRQ);
  75. if (!pos)
  76. return -EINVAL;
  77. /* Verify the idx I want to use is in range */
  78. spin_lock_irqsave(&ht_irq_lock, flags);
  79. pci_write_config_byte(dev, pos + 2, 1);
  80. pci_read_config_dword(dev, pos + 4, &data);
  81. spin_unlock_irqrestore(&ht_irq_lock, flags);
  82. max_irq = (data >> 16) & 0xff;
  83. if (idx > max_irq)
  84. return -EINVAL;
  85. irq = arch_setup_ht_irq(idx, pos, dev, update);
  86. if (irq > 0)
  87. dev_dbg(&dev->dev, "irq %d for HT\n", irq);
  88. return irq;
  89. }
  90. EXPORT_SYMBOL(__ht_create_irq);
  91. /**
  92. * ht_create_irq - create an irq and attach it to a device.
  93. * @dev: The hypertransport device to find the irq capability on.
  94. * @idx: Which of the possible irqs to attach to.
  95. *
  96. * ht_create_irq needs to be called for all hypertransport devices
  97. * that generate irqs.
  98. *
  99. * The irq number of the new irq or a negative error value is returned.
  100. */
  101. int ht_create_irq(struct pci_dev *dev, int idx)
  102. {
  103. return __ht_create_irq(dev, idx, NULL);
  104. }
  105. EXPORT_SYMBOL(ht_create_irq);
  106. /**
  107. * ht_destroy_irq - destroy an irq created with ht_create_irq
  108. * @irq: irq to be destroyed
  109. *
  110. * This reverses ht_create_irq removing the specified irq from
  111. * existence. The irq should be free before this happens.
  112. */
  113. void ht_destroy_irq(unsigned int irq)
  114. {
  115. arch_teardown_ht_irq(irq);
  116. }
  117. EXPORT_SYMBOL(ht_destroy_irq);