1 // SPDX-License-Identifier: GPL-2.0
4 * Purpose: Hypertransport Interrupt Capability
6 * Copyright (C) 2006 Linux Networx
7 * Copyright (C) Eric Biederman <ebiederman@lnxi.com>
10 #include <linux/irq.h>
11 #include <linux/pci.h>
12 #include <linux/spinlock.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/htirq.h>
17 /* Global ht irq lock.
19 * This is needed to serialize access to the data port in hypertransport
22 * With multiple simultaneous hypertransport irq devices it might pay
23 * to make this more fine grained. But start with simple, stupid, and correct.
25 static DEFINE_SPINLOCK(ht_irq_lock
);
27 void write_ht_irq_msg(unsigned int irq
, struct ht_irq_msg
*msg
)
29 struct ht_irq_cfg
*cfg
= irq_get_handler_data(irq
);
32 spin_lock_irqsave(&ht_irq_lock
, flags
);
33 if (cfg
->msg
.address_lo
!= msg
->address_lo
) {
34 pci_write_config_byte(cfg
->dev
, cfg
->pos
+ 2, cfg
->idx
);
35 pci_write_config_dword(cfg
->dev
, cfg
->pos
+ 4, msg
->address_lo
);
37 if (cfg
->msg
.address_hi
!= msg
->address_hi
) {
38 pci_write_config_byte(cfg
->dev
, cfg
->pos
+ 2, cfg
->idx
+ 1);
39 pci_write_config_dword(cfg
->dev
, cfg
->pos
+ 4, msg
->address_hi
);
42 cfg
->update(cfg
->dev
, irq
, msg
);
43 spin_unlock_irqrestore(&ht_irq_lock
, flags
);
47 void fetch_ht_irq_msg(unsigned int irq
, struct ht_irq_msg
*msg
)
49 struct ht_irq_cfg
*cfg
= irq_get_handler_data(irq
);
54 void mask_ht_irq(struct irq_data
*data
)
56 struct ht_irq_cfg
*cfg
= irq_data_get_irq_handler_data(data
);
57 struct ht_irq_msg msg
= cfg
->msg
;
60 write_ht_irq_msg(data
->irq
, &msg
);
63 void unmask_ht_irq(struct irq_data
*data
)
65 struct ht_irq_cfg
*cfg
= irq_data_get_irq_handler_data(data
);
66 struct ht_irq_msg msg
= cfg
->msg
;
69 write_ht_irq_msg(data
->irq
, &msg
);
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
78 * The irq number of the new irq or a negative error value is returned.
80 int __ht_create_irq(struct pci_dev
*dev
, int idx
, ht_irq_update_t
*update
)
82 int max_irq
, pos
, irq
;
86 pos
= pci_find_ht_capability(dev
, HT_CAPTYPE_IRQ
);
90 /* Verify the idx I want to use is in range */
91 spin_lock_irqsave(&ht_irq_lock
, flags
);
92 pci_write_config_byte(dev
, pos
+ 2, 1);
93 pci_read_config_dword(dev
, pos
+ 4, &data
);
94 spin_unlock_irqrestore(&ht_irq_lock
, flags
);
96 max_irq
= (data
>> 16) & 0xff;
100 irq
= arch_setup_ht_irq(idx
, pos
, dev
, update
);
102 dev_dbg(&dev
->dev
, "irq %d for HT\n", irq
);
106 EXPORT_SYMBOL(__ht_create_irq
);
109 * ht_create_irq - create an irq and attach it to a device.
110 * @dev: The hypertransport device to find the irq capability on.
111 * @idx: Which of the possible irqs to attach to.
113 * ht_create_irq needs to be called for all hypertransport devices
114 * that generate irqs.
116 * The irq number of the new irq or a negative error value is returned.
118 int ht_create_irq(struct pci_dev
*dev
, int idx
)
120 return __ht_create_irq(dev
, idx
, NULL
);
122 EXPORT_SYMBOL(ht_create_irq
);
125 * ht_destroy_irq - destroy an irq created with ht_create_irq
126 * @irq: irq to be destroyed
128 * This reverses ht_create_irq removing the specified irq from
129 * existence. The irq should be free before this happens.
131 void ht_destroy_irq(unsigned int irq
)
133 arch_teardown_ht_irq(irq
);
135 EXPORT_SYMBOL(ht_destroy_irq
);