htirq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/slab.h>
  12. #include <linux/gfp.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. struct ht_irq_cfg {
  24. struct pci_dev *dev;
  25. /* Update callback used to cope with buggy hardware */
  26. ht_irq_update_t *update;
  27. unsigned pos;
  28. unsigned idx;
  29. struct ht_irq_msg msg;
  30. };
  31. void write_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
  32. {
  33. struct ht_irq_cfg *cfg = get_irq_data(irq);
  34. unsigned long flags;
  35. spin_lock_irqsave(&ht_irq_lock, flags);
  36. if (cfg->msg.address_lo != msg->address_lo) {
  37. pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx);
  38. pci_write_config_dword(cfg->dev, cfg->pos + 4, msg->address_lo);
  39. }
  40. if (cfg->msg.address_hi != msg->address_hi) {
  41. pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx + 1);
  42. pci_write_config_dword(cfg->dev, cfg->pos + 4, msg->address_hi);
  43. }
  44. if (cfg->update)
  45. cfg->update(cfg->dev, irq, msg);
  46. spin_unlock_irqrestore(&ht_irq_lock, flags);
  47. cfg->msg = *msg;
  48. }
  49. void fetch_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
  50. {
  51. struct ht_irq_cfg *cfg = get_irq_data(irq);
  52. *msg = cfg->msg;
  53. }
  54. void mask_ht_irq(unsigned int irq)
  55. {
  56. struct ht_irq_cfg *cfg;
  57. struct ht_irq_msg msg;
  58. cfg = get_irq_data(irq);
  59. msg = cfg->msg;
  60. msg.address_lo |= 1;
  61. write_ht_irq_msg(irq, &msg);
  62. }
  63. void unmask_ht_irq(unsigned int irq)
  64. {
  65. struct ht_irq_cfg *cfg;
  66. struct ht_irq_msg msg;
  67. cfg = get_irq_data(irq);
  68. msg = cfg->msg;
  69. msg.address_lo &= ~1;
  70. write_ht_irq_msg(irq, &msg);
  71. }
  72. /**
  73. * __ht_create_irq - create an irq and attach it to a device.
  74. * @dev: The hypertransport device to find the irq capability on.
  75. * @idx: Which of the possible irqs to attach to.
  76. * @update: Function to be called when changing the htirq message
  77. *
  78. * The irq number of the new irq or a negative error value is returned.
  79. */
  80. int __ht_create_irq(struct pci_dev *dev, int idx, ht_irq_update_t *update)
  81. {
  82. struct ht_irq_cfg *cfg;
  83. unsigned long flags;
  84. u32 data;
  85. int max_irq;
  86. int pos;
  87. int irq;
  88. pos = pci_find_ht_capability(dev, HT_CAPTYPE_IRQ);
  89. if (!pos)
  90. return -EINVAL;
  91. /* Verify the idx I want to use is in range */
  92. spin_lock_irqsave(&ht_irq_lock, flags);
  93. pci_write_config_byte(dev, pos + 2, 1);
  94. pci_read_config_dword(dev, pos + 4, &data);
  95. spin_unlock_irqrestore(&ht_irq_lock, flags);
  96. max_irq = (data >> 16) & 0xff;
  97. if ( idx > max_irq)
  98. return -EINVAL;
  99. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  100. if (!cfg)
  101. return -ENOMEM;
  102. cfg->dev = dev;
  103. cfg->update = update;
  104. cfg->pos = pos;
  105. cfg->idx = 0x10 + (idx * 2);
  106. /* Initialize msg to a value that will never match the first write. */
  107. cfg->msg.address_lo = 0xffffffff;
  108. cfg->msg.address_hi = 0xffffffff;
  109. irq = create_irq();
  110. if (irq < 0) {
  111. kfree(cfg);
  112. return -EBUSY;
  113. }
  114. set_irq_data(irq, cfg);
  115. if (arch_setup_ht_irq(irq, dev) < 0) {
  116. ht_destroy_irq(irq);
  117. return -EBUSY;
  118. }
  119. return irq;
  120. }
  121. /**
  122. * ht_create_irq - create an irq and attach it to a device.
  123. * @dev: The hypertransport device to find the irq capability on.
  124. * @idx: Which of the possible irqs to attach to.
  125. *
  126. * ht_create_irq needs to be called for all hypertransport devices
  127. * that generate irqs.
  128. *
  129. * The irq number of the new irq or a negative error value is returned.
  130. */
  131. int ht_create_irq(struct pci_dev *dev, int idx)
  132. {
  133. return __ht_create_irq(dev, idx, NULL);
  134. }
  135. /**
  136. * ht_destroy_irq - destroy an irq created with ht_create_irq
  137. *
  138. * This reverses ht_create_irq removing the specified irq from
  139. * existence. The irq should be free before this happens.
  140. */
  141. void ht_destroy_irq(unsigned int irq)
  142. {
  143. struct ht_irq_cfg *cfg;
  144. cfg = get_irq_data(irq);
  145. set_irq_chip(irq, NULL);
  146. set_irq_data(irq, NULL);
  147. destroy_irq(irq);
  148. kfree(cfg);
  149. }
  150. EXPORT_SYMBOL(__ht_create_irq);
  151. EXPORT_SYMBOL(ht_create_irq);
  152. EXPORT_SYMBOL(ht_destroy_irq);