htirq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/htirq.h>
  13. /* Global ht irq lock.
  14. *
  15. * This is needed to serialize access to the data port in hypertransport
  16. * irq capability.
  17. *
  18. * With multiple simultaneous hypertransport irq devices it might pay
  19. * to make this more fine grained. But start with simple, stupid, and correct.
  20. */
  21. static DEFINE_SPINLOCK(ht_irq_lock);
  22. struct ht_irq_cfg {
  23. struct pci_dev *dev;
  24. /* Update callback used to cope with buggy hardware */
  25. ht_irq_update_t *update;
  26. unsigned pos;
  27. unsigned idx;
  28. struct ht_irq_msg msg;
  29. };
  30. void write_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
  31. {
  32. struct ht_irq_cfg *cfg = get_irq_data(irq);
  33. unsigned long flags;
  34. spin_lock_irqsave(&ht_irq_lock, flags);
  35. if (cfg->msg.address_lo != msg->address_lo) {
  36. pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx);
  37. pci_write_config_dword(cfg->dev, cfg->pos + 4, msg->address_lo);
  38. }
  39. if (cfg->msg.address_hi != msg->address_hi) {
  40. pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx + 1);
  41. pci_write_config_dword(cfg->dev, cfg->pos + 4, msg->address_hi);
  42. }
  43. if (cfg->update)
  44. cfg->update(cfg->dev, irq, msg);
  45. spin_unlock_irqrestore(&ht_irq_lock, flags);
  46. cfg->msg = *msg;
  47. }
  48. void fetch_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
  49. {
  50. struct ht_irq_cfg *cfg = get_irq_data(irq);
  51. *msg = cfg->msg;
  52. }
  53. void mask_ht_irq(unsigned int irq)
  54. {
  55. struct ht_irq_cfg *cfg;
  56. struct ht_irq_msg msg;
  57. cfg = get_irq_data(irq);
  58. msg = cfg->msg;
  59. msg.address_lo |= 1;
  60. write_ht_irq_msg(irq, &msg);
  61. }
  62. void unmask_ht_irq(unsigned int irq)
  63. {
  64. struct ht_irq_cfg *cfg;
  65. struct ht_irq_msg msg;
  66. cfg = get_irq_data(irq);
  67. msg = cfg->msg;
  68. msg.address_lo &= ~1;
  69. write_ht_irq_msg(irq, &msg);
  70. }
  71. /**
  72. * __ht_create_irq - create an irq and attach it to a device.
  73. * @dev: The hypertransport device to find the irq capability on.
  74. * @idx: Which of the possible irqs to attach to.
  75. * @update: Function to be called when changing the htirq message
  76. *
  77. * The irq number of the new irq or a negative error value is returned.
  78. */
  79. int __ht_create_irq(struct pci_dev *dev, int idx, ht_irq_update_t *update)
  80. {
  81. struct ht_irq_cfg *cfg;
  82. unsigned long flags;
  83. u32 data;
  84. int max_irq;
  85. int pos;
  86. int irq;
  87. int node;
  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. node = dev_to_node(&dev->dev);
  110. irq = create_irq_nr(0, node);
  111. if (irq <= 0) {
  112. kfree(cfg);
  113. return -EBUSY;
  114. }
  115. set_irq_data(irq, cfg);
  116. if (arch_setup_ht_irq(irq, dev) < 0) {
  117. ht_destroy_irq(irq);
  118. return -EBUSY;
  119. }
  120. return irq;
  121. }
  122. /**
  123. * ht_create_irq - create an irq and attach it to a device.
  124. * @dev: The hypertransport device to find the irq capability on.
  125. * @idx: Which of the possible irqs to attach to.
  126. *
  127. * ht_create_irq needs to be called for all hypertransport devices
  128. * that generate irqs.
  129. *
  130. * The irq number of the new irq or a negative error value is returned.
  131. */
  132. int ht_create_irq(struct pci_dev *dev, int idx)
  133. {
  134. return __ht_create_irq(dev, idx, NULL);
  135. }
  136. /**
  137. * ht_destroy_irq - destroy an irq created with ht_create_irq
  138. * @irq: irq to be destroyed
  139. *
  140. * This reverses ht_create_irq removing the specified irq from
  141. * existence. The irq should be free before this happens.
  142. */
  143. void ht_destroy_irq(unsigned int irq)
  144. {
  145. struct ht_irq_cfg *cfg;
  146. cfg = get_irq_data(irq);
  147. set_irq_chip(irq, NULL);
  148. set_irq_data(irq, NULL);
  149. destroy_irq(irq);
  150. kfree(cfg);
  151. }
  152. EXPORT_SYMBOL(__ht_create_irq);
  153. EXPORT_SYMBOL(ht_create_irq);
  154. EXPORT_SYMBOL(ht_destroy_irq);