htirq.c 3.7 KB

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