htirq.c 3.5 KB

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