GNU Linux-libre 4.14.254-gnu1
[releases.git] / drivers / pci / htirq.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * File:        htirq.c
4  * Purpose:     Hypertransport Interrupt Capability
5  *
6  * Copyright (C) 2006 Linux Networx
7  * Copyright (C) Eric Biederman <ebiederman@lnxi.com>
8  */
9
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>
16
17 /* Global ht irq lock.
18  *
19  * This is needed to serialize access to the data port in hypertransport
20  * irq capability.
21  *
22  * With multiple simultaneous hypertransport irq devices it might pay
23  * to make this more fine grained.  But start with simple, stupid, and correct.
24  */
25 static DEFINE_SPINLOCK(ht_irq_lock);
26
27 void write_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
28 {
29         struct ht_irq_cfg *cfg = irq_get_handler_data(irq);
30         unsigned long flags;
31
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);
36         }
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);
40         }
41         if (cfg->update)
42                 cfg->update(cfg->dev, irq, msg);
43         spin_unlock_irqrestore(&ht_irq_lock, flags);
44         cfg->msg = *msg;
45 }
46
47 void fetch_ht_irq_msg(unsigned int irq, struct ht_irq_msg *msg)
48 {
49         struct ht_irq_cfg *cfg = irq_get_handler_data(irq);
50
51         *msg = cfg->msg;
52 }
53
54 void mask_ht_irq(struct irq_data *data)
55 {
56         struct ht_irq_cfg *cfg = irq_data_get_irq_handler_data(data);
57         struct ht_irq_msg msg = cfg->msg;
58
59         msg.address_lo |= 1;
60         write_ht_irq_msg(data->irq, &msg);
61 }
62
63 void unmask_ht_irq(struct irq_data *data)
64 {
65         struct ht_irq_cfg *cfg = irq_data_get_irq_handler_data(data);
66         struct ht_irq_msg msg = cfg->msg;
67
68         msg.address_lo &= ~1;
69         write_ht_irq_msg(data->irq, &msg);
70 }
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         int max_irq, pos, irq;
83         unsigned long flags;
84         u32 data;
85
86         pos = pci_find_ht_capability(dev, HT_CAPTYPE_IRQ);
87         if (!pos)
88                 return -EINVAL;
89
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);
95
96         max_irq = (data >> 16) & 0xff;
97         if (idx > max_irq)
98                 return -EINVAL;
99
100         irq = arch_setup_ht_irq(idx, pos, dev, update);
101         if (irq > 0)
102                 dev_dbg(&dev->dev, "irq %d for HT\n", irq);
103
104         return irq;
105 }
106 EXPORT_SYMBOL(__ht_create_irq);
107
108 /**
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.
112  *
113  * ht_create_irq needs to be called for all hypertransport devices
114  * that generate irqs.
115  *
116  * The irq number of the new irq or a negative error value is returned.
117  */
118 int ht_create_irq(struct pci_dev *dev, int idx)
119 {
120         return __ht_create_irq(dev, idx, NULL);
121 }
122 EXPORT_SYMBOL(ht_create_irq);
123
124 /**
125  * ht_destroy_irq - destroy an irq created with ht_create_irq
126  * @irq: irq to be destroyed
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         arch_teardown_ht_irq(irq);
134 }
135 EXPORT_SYMBOL(ht_destroy_irq);