htirq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. int node;
  89. pos = pci_find_ht_capability(dev, HT_CAPTYPE_IRQ);
  90. if (!pos)
  91. return -EINVAL;
  92. /* Verify the idx I want to use is in range */
  93. spin_lock_irqsave(&ht_irq_lock, flags);
  94. pci_write_config_byte(dev, pos + 2, 1);
  95. pci_read_config_dword(dev, pos + 4, &data);
  96. spin_unlock_irqrestore(&ht_irq_lock, flags);
  97. max_irq = (data >> 16) & 0xff;
  98. if ( idx > max_irq)
  99. return -EINVAL;
  100. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  101. if (!cfg)
  102. return -ENOMEM;
  103. cfg->dev = dev;
  104. cfg->update = update;
  105. cfg->pos = pos;
  106. cfg->idx = 0x10 + (idx * 2);
  107. /* Initialize msg to a value that will never match the first write. */
  108. cfg->msg.address_lo = 0xffffffff;
  109. cfg->msg.address_hi = 0xffffffff;
  110. node = dev_to_node(&dev->dev);
  111. irq = create_irq_nr(0, node);
  112. if (irq <= 0) {
  113. kfree(cfg);
  114. return -EBUSY;
  115. }
  116. set_irq_data(irq, cfg);
  117. if (arch_setup_ht_irq(irq, dev) < 0) {
  118. ht_destroy_irq(irq);
  119. return -EBUSY;
  120. }
  121. return irq;
  122. }
  123. /**
  124. * ht_create_irq - create an irq and attach it to a device.
  125. * @dev: The hypertransport device to find the irq capability on.
  126. * @idx: Which of the possible irqs to attach to.
  127. *
  128. * ht_create_irq needs to be called for all hypertransport devices
  129. * that generate irqs.
  130. *
  131. * The irq number of the new irq or a negative error value is returned.
  132. */
  133. int ht_create_irq(struct pci_dev *dev, int idx)
  134. {
  135. return __ht_create_irq(dev, idx, NULL);
  136. }
  137. /**
  138. * ht_destroy_irq - destroy an irq created with ht_create_irq
  139. * @irq: irq to be destroyed
  140. *
  141. * This reverses ht_create_irq removing the specified irq from
  142. * existence. The irq should be free before this happens.
  143. */
  144. void ht_destroy_irq(unsigned int irq)
  145. {
  146. struct ht_irq_cfg *cfg;
  147. cfg = get_irq_data(irq);
  148. set_irq_chip(irq, NULL);
  149. set_irq_data(irq, NULL);
  150. destroy_irq(irq);
  151. kfree(cfg);
  152. }
  153. EXPORT_SYMBOL(__ht_create_irq);
  154. EXPORT_SYMBOL(ht_create_irq);
  155. EXPORT_SYMBOL(ht_destroy_irq);