atm: iphase: Fix Spectre v1 vulnerability
[linux/fpc-iii.git] / drivers / s390 / cio / qdio_thinint.c
blobdebe69adfc705a8d4ec8ed56c8f26de1599a29c1
1 /*
2 * Copyright IBM Corp. 2000, 2009
3 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
4 * Cornelia Huck <cornelia.huck@de.ibm.com>
5 * Jan Glauber <jang@linux.vnet.ibm.com>
6 */
7 #include <linux/io.h>
8 #include <linux/slab.h>
9 #include <linux/kernel_stat.h>
10 #include <linux/atomic.h>
11 #include <asm/debug.h>
12 #include <asm/qdio.h>
13 #include <asm/airq.h>
14 #include <asm/isc.h>
16 #include "cio.h"
17 #include "ioasm.h"
18 #include "qdio.h"
19 #include "qdio_debug.h"
22 * Restriction: only 63 iqdio subchannels would have its own indicator,
23 * after that, subsequent subchannels share one indicator
25 #define TIQDIO_NR_NONSHARED_IND 63
26 #define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1)
27 #define TIQDIO_SHARED_IND 63
29 /* device state change indicators */
30 struct indicator_t {
31 u32 ind; /* u32 because of compare-and-swap performance */
32 atomic_t count; /* use count, 0 or 1 for non-shared indicators */
35 /* list of thin interrupt input queues */
36 static LIST_HEAD(tiq_list);
37 static DEFINE_MUTEX(tiq_list_lock);
39 /* Adapter interrupt definitions */
40 static void tiqdio_thinint_handler(struct airq_struct *airq);
42 static struct airq_struct tiqdio_airq = {
43 .handler = tiqdio_thinint_handler,
44 .isc = QDIO_AIRQ_ISC,
47 static struct indicator_t *q_indicators;
49 u64 last_ai_time;
51 /* returns addr for the device state change indicator */
52 static u32 *get_indicator(void)
54 int i;
56 for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++)
57 if (!atomic_read(&q_indicators[i].count)) {
58 atomic_set(&q_indicators[i].count, 1);
59 return &q_indicators[i].ind;
62 /* use the shared indicator */
63 atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count);
64 return &q_indicators[TIQDIO_SHARED_IND].ind;
67 static void put_indicator(u32 *addr)
69 int i;
71 if (!addr)
72 return;
73 i = ((unsigned long)addr - (unsigned long)q_indicators) /
74 sizeof(struct indicator_t);
75 atomic_dec(&q_indicators[i].count);
78 void tiqdio_add_input_queues(struct qdio_irq *irq_ptr)
80 mutex_lock(&tiq_list_lock);
81 list_add_rcu(&irq_ptr->input_qs[0]->entry, &tiq_list);
82 mutex_unlock(&tiq_list_lock);
85 void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr)
87 struct qdio_q *q;
89 q = irq_ptr->input_qs[0];
90 if (!q)
91 return;
93 mutex_lock(&tiq_list_lock);
94 list_del_rcu(&q->entry);
95 mutex_unlock(&tiq_list_lock);
96 synchronize_rcu();
97 INIT_LIST_HEAD(&q->entry);
100 static inline int has_multiple_inq_on_dsci(struct qdio_irq *irq_ptr)
102 return irq_ptr->nr_input_qs > 1;
105 static inline int references_shared_dsci(struct qdio_irq *irq_ptr)
107 return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind;
110 static inline int shared_ind(struct qdio_irq *irq_ptr)
112 return references_shared_dsci(irq_ptr) ||
113 has_multiple_inq_on_dsci(irq_ptr);
116 void clear_nonshared_ind(struct qdio_irq *irq_ptr)
118 if (!is_thinint_irq(irq_ptr))
119 return;
120 if (shared_ind(irq_ptr))
121 return;
122 xchg(irq_ptr->dsci, 0);
125 int test_nonshared_ind(struct qdio_irq *irq_ptr)
127 if (!is_thinint_irq(irq_ptr))
128 return 0;
129 if (shared_ind(irq_ptr))
130 return 0;
131 if (*irq_ptr->dsci)
132 return 1;
133 else
134 return 0;
137 static inline u32 clear_shared_ind(void)
139 if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count))
140 return 0;
141 return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0);
144 static inline void tiqdio_call_inq_handlers(struct qdio_irq *irq)
146 struct qdio_q *q;
147 int i;
149 if (!references_shared_dsci(irq) &&
150 has_multiple_inq_on_dsci(irq))
151 xchg(irq->dsci, 0);
153 for_each_input_queue(irq, q, i) {
154 if (q->u.in.queue_start_poll) {
155 /* skip if polling is enabled or already in work */
156 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
157 &q->u.in.queue_irq_state)) {
158 qperf_inc(q, int_discarded);
159 continue;
162 /* avoid dsci clear here, done after processing */
163 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
164 q->irq_ptr->int_parm);
165 } else {
166 if (!shared_ind(q->irq_ptr))
167 xchg(q->irq_ptr->dsci, 0);
170 * Call inbound processing but not directly
171 * since that could starve other thinint queues.
173 tasklet_schedule(&q->tasklet);
179 * tiqdio_thinint_handler - thin interrupt handler for qdio
180 * @alsi: pointer to adapter local summary indicator
181 * @data: NULL
183 static void tiqdio_thinint_handler(struct airq_struct *airq)
185 u32 si_used = clear_shared_ind();
186 struct qdio_q *q;
188 last_ai_time = S390_lowcore.int_clock;
189 inc_irq_stat(IRQIO_QAI);
191 /* protect tiq_list entries, only changed in activate or shutdown */
192 rcu_read_lock();
194 /* check for work on all inbound thinint queues */
195 list_for_each_entry_rcu(q, &tiq_list, entry) {
196 struct qdio_irq *irq;
198 /* only process queues from changed sets */
199 irq = q->irq_ptr;
200 if (unlikely(references_shared_dsci(irq))) {
201 if (!si_used)
202 continue;
203 } else if (!*irq->dsci)
204 continue;
206 tiqdio_call_inq_handlers(irq);
208 qperf_inc(q, adapter_int);
210 rcu_read_unlock();
213 static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset)
215 struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page;
216 u64 summary_indicator_addr, subchannel_indicator_addr;
217 int rc;
219 if (reset) {
220 summary_indicator_addr = 0;
221 subchannel_indicator_addr = 0;
222 } else {
223 summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr);
224 subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci);
227 rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr,
228 subchannel_indicator_addr);
229 if (rc) {
230 DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no,
231 scssc->response.code);
232 goto out;
235 DBF_EVENT("setscind");
236 DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr));
237 DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr));
238 out:
239 return rc;
242 /* allocate non-shared indicators and shared indicator */
243 int __init tiqdio_allocate_memory(void)
245 q_indicators = kzalloc(sizeof(struct indicator_t) * TIQDIO_NR_INDICATORS,
246 GFP_KERNEL);
247 if (!q_indicators)
248 return -ENOMEM;
249 return 0;
252 void tiqdio_free_memory(void)
254 kfree(q_indicators);
257 int __init tiqdio_register_thinints(void)
259 int rc;
261 rc = register_adapter_interrupt(&tiqdio_airq);
262 if (rc) {
263 DBF_EVENT("RTI:%x", rc);
264 return rc;
266 return 0;
269 int qdio_establish_thinint(struct qdio_irq *irq_ptr)
271 if (!is_thinint_irq(irq_ptr))
272 return 0;
273 return set_subchannel_ind(irq_ptr, 0);
276 void qdio_setup_thinint(struct qdio_irq *irq_ptr)
278 if (!is_thinint_irq(irq_ptr))
279 return;
280 irq_ptr->dsci = get_indicator();
281 DBF_HEX(&irq_ptr->dsci, sizeof(void *));
284 void qdio_shutdown_thinint(struct qdio_irq *irq_ptr)
286 if (!is_thinint_irq(irq_ptr))
287 return;
289 /* reset adapter interrupt indicators */
290 set_subchannel_ind(irq_ptr, 1);
291 put_indicator(irq_ptr->dsci);
294 void __exit tiqdio_unregister_thinints(void)
296 WARN_ON(!list_empty(&tiq_list));
297 unregister_adapter_interrupt(&tiqdio_airq);