htirq.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = irq_get_handler_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 = irq_get_handler_data(irq);
  51. *msg = cfg->msg;
  52. }
  53. void mask_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. void unmask_ht_irq(struct irq_data *data)
  61. {
  62. struct ht_irq_cfg *cfg = irq_data_get_irq_handler_data(data);
  63. struct ht_irq_msg msg = cfg->msg;
  64. msg.address_lo &= ~1;
  65. write_ht_irq_msg(data->irq, &msg);
  66. }
  67. /**
  68. * __ht_create_irq - create an irq and attach it to a device.
  69. * @dev: The hypertransport device to find the irq capability on.
  70. * @idx: Which of the possible irqs to attach to.
  71. * @update: Function to be called when changing the htirq message
  72. *
  73. * The irq number of the new irq or a negative error value is returned.
  74. */
  75. int __ht_create_irq(struct pci_dev *dev, int idx, ht_irq_update_t *update)
  76. {
  77. struct ht_irq_cfg *cfg;
  78. unsigned long flags;
  79. u32 data;
  80. int max_irq;
  81. int pos;
  82. int irq;
  83. int node;
  84. pos = pci_find_ht_capability(dev, HT_CAPTYPE_IRQ);
  85. if (!pos)
  86. return -EINVAL;
  87. /* Verify the idx I want to use is in range */
  88. spin_lock_irqsave(&ht_irq_lock, flags);
  89. pci_write_config_byte(dev, pos + 2, 1);
  90. pci_read_config_dword(dev, pos + 4, &data);
  91. spin_unlock_irqrestore(&ht_irq_lock, flags);
  92. max_irq = (data >> 16) & 0xff;
  93. if ( idx > max_irq)
  94. return -EINVAL;
  95. cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  96. if (!cfg)
  97. return -ENOMEM;
  98. cfg->dev = dev;
  99. cfg->update = update;
  100. cfg->pos = pos;
  101. cfg->idx = 0x10 + (idx * 2);
  102. /* Initialize msg to a value that will never match the first write. */
  103. cfg->msg.address_lo = 0xffffffff;
  104. cfg->msg.address_hi = 0xffffffff;
  105. node = dev_to_node(&dev->dev);
  106. irq = create_irq_nr(0, node);
  107. if (irq <= 0) {
  108. kfree(cfg);
  109. return -EBUSY;
  110. }
  111. irq_set_handler_data(irq, cfg);
  112. if (arch_setup_ht_irq(irq, dev) < 0) {
  113. ht_destroy_irq(irq);
  114. return -EBUSY;
  115. }
  116. return irq;
  117. }
  118. /**
  119. * ht_create_irq - create an irq and attach it to a device.
  120. * @dev: The hypertransport device to find the irq capability on.
  121. * @idx: Which of the possible irqs to attach to.
  122. *
  123. * ht_create_irq needs to be called for all hypertransport devices
  124. * that generate irqs.
  125. *
  126. * The irq number of the new irq or a negative error value is returned.
  127. */
  128. int ht_create_irq(struct pci_dev *dev, int idx)
  129. {
  130. return __ht_create_irq(dev, idx, NULL);
  131. }
  132. /**
  133. * ht_destroy_irq - destroy an irq created with ht_create_irq
  134. * @irq: irq to be destroyed
  135. *
  136. * This reverses ht_create_irq removing the specified irq from
  137. * existence. The irq should be free before this happens.
  138. */
  139. void ht_destroy_irq(unsigned int irq)
  140. {
  141. struct ht_irq_cfg *cfg;
  142. cfg = irq_get_handler_data(irq);
  143. irq_set_chip(irq, NULL);
  144. irq_set_handler_data(irq, NULL);
  145. destroy_irq(irq);
  146. kfree(cfg);
  147. }
  148. EXPORT_SYMBOL(__ht_create_irq);
  149. EXPORT_SYMBOL(ht_create_irq);
  150. EXPORT_SYMBOL(ht_destroy_irq);