gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / s390 / cio / qdio_thinint.c
blobae50373617cd4954bbe2211680fe0ef5b031acd6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2000, 2009
4 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
5 * Cornelia Huck <cornelia.huck@de.ibm.com>
6 * Jan Glauber <jang@linux.vnet.ibm.com>
7 */
8 #include <linux/io.h>
9 #include <linux/slab.h>
10 #include <linux/kernel_stat.h>
11 #include <linux/atomic.h>
12 #include <linux/rculist.h>
14 #include <asm/debug.h>
15 #include <asm/qdio.h>
16 #include <asm/airq.h>
17 #include <asm/isc.h>
19 #include "cio.h"
20 #include "ioasm.h"
21 #include "qdio.h"
22 #include "qdio_debug.h"
25 * Restriction: only 63 iqdio subchannels would have its own indicator,
26 * after that, subsequent subchannels share one indicator
28 #define TIQDIO_NR_NONSHARED_IND 63
29 #define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1)
30 #define TIQDIO_SHARED_IND 63
32 /* device state change indicators */
33 struct indicator_t {
34 u32 ind; /* u32 because of compare-and-swap performance */
35 atomic_t count; /* use count, 0 or 1 for non-shared indicators */
38 /* list of thin interrupt input queues */
39 static LIST_HEAD(tiq_list);
40 static DEFINE_MUTEX(tiq_list_lock);
42 static struct indicator_t *q_indicators;
44 u64 last_ai_time;
46 /* returns addr for the device state change indicator */
47 static u32 *get_indicator(void)
49 int i;
51 for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++)
52 if (!atomic_cmpxchg(&q_indicators[i].count, 0, 1))
53 return &q_indicators[i].ind;
55 /* use the shared indicator */
56 atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count);
57 return &q_indicators[TIQDIO_SHARED_IND].ind;
60 static void put_indicator(u32 *addr)
62 struct indicator_t *ind = container_of(addr, struct indicator_t, ind);
64 if (!addr)
65 return;
66 atomic_dec(&ind->count);
69 void tiqdio_add_device(struct qdio_irq *irq_ptr)
71 mutex_lock(&tiq_list_lock);
72 list_add_rcu(&irq_ptr->entry, &tiq_list);
73 mutex_unlock(&tiq_list_lock);
76 void tiqdio_remove_device(struct qdio_irq *irq_ptr)
78 mutex_lock(&tiq_list_lock);
79 list_del_rcu(&irq_ptr->entry);
80 mutex_unlock(&tiq_list_lock);
81 synchronize_rcu();
82 INIT_LIST_HEAD(&irq_ptr->entry);
85 static inline int references_shared_dsci(struct qdio_irq *irq_ptr)
87 return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind;
90 int test_nonshared_ind(struct qdio_irq *irq_ptr)
92 if (!is_thinint_irq(irq_ptr))
93 return 0;
94 if (references_shared_dsci(irq_ptr))
95 return 0;
96 if (*irq_ptr->dsci)
97 return 1;
98 else
99 return 0;
102 static inline u32 clear_shared_ind(void)
104 if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count))
105 return 0;
106 return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0);
109 static inline void tiqdio_call_inq_handlers(struct qdio_irq *irq)
111 struct qdio_q *q;
112 int i;
114 if (!references_shared_dsci(irq))
115 xchg(irq->dsci, 0);
117 if (irq->irq_poll) {
118 if (!test_and_set_bit(QDIO_IRQ_DISABLED, &irq->poll_state))
119 irq->irq_poll(irq->cdev, irq->int_parm);
120 else
121 QDIO_PERF_STAT_INC(irq, int_discarded);
123 return;
126 for_each_input_queue(irq, q, i) {
128 * Call inbound processing but not directly
129 * since that could starve other thinint queues.
131 tasklet_schedule(&q->tasklet);
136 * tiqdio_thinint_handler - thin interrupt handler for qdio
137 * @airq: pointer to adapter interrupt descriptor
138 * @floating: flag to recognize floating vs. directed interrupts (unused)
140 static void tiqdio_thinint_handler(struct airq_struct *airq, bool floating)
142 u32 si_used = clear_shared_ind();
143 struct qdio_irq *irq;
145 last_ai_time = S390_lowcore.int_clock;
146 inc_irq_stat(IRQIO_QAI);
148 /* protect tiq_list entries, only changed in activate or shutdown */
149 rcu_read_lock();
151 list_for_each_entry_rcu(irq, &tiq_list, entry) {
152 /* only process queues from changed sets */
153 if (unlikely(references_shared_dsci(irq))) {
154 if (!si_used)
155 continue;
156 } else if (!*irq->dsci)
157 continue;
159 tiqdio_call_inq_handlers(irq);
161 QDIO_PERF_STAT_INC(irq, adapter_int);
163 rcu_read_unlock();
166 static struct airq_struct tiqdio_airq = {
167 .handler = tiqdio_thinint_handler,
168 .isc = QDIO_AIRQ_ISC,
171 static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset)
173 struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page;
174 u64 summary_indicator_addr, subchannel_indicator_addr;
175 int rc;
177 if (reset) {
178 summary_indicator_addr = 0;
179 subchannel_indicator_addr = 0;
180 } else {
181 summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr);
182 subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci);
185 rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr,
186 subchannel_indicator_addr, tiqdio_airq.isc);
187 if (rc) {
188 DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no,
189 scssc->response.code);
190 goto out;
193 DBF_EVENT("setscind");
194 DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr));
195 DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr));
196 out:
197 return rc;
200 /* allocate non-shared indicators and shared indicator */
201 int __init tiqdio_allocate_memory(void)
203 q_indicators = kcalloc(TIQDIO_NR_INDICATORS,
204 sizeof(struct indicator_t),
205 GFP_KERNEL);
206 if (!q_indicators)
207 return -ENOMEM;
208 return 0;
211 void tiqdio_free_memory(void)
213 kfree(q_indicators);
216 int __init tiqdio_register_thinints(void)
218 int rc;
220 rc = register_adapter_interrupt(&tiqdio_airq);
221 if (rc) {
222 DBF_EVENT("RTI:%x", rc);
223 return rc;
225 return 0;
228 int qdio_establish_thinint(struct qdio_irq *irq_ptr)
230 if (!is_thinint_irq(irq_ptr))
231 return 0;
232 return set_subchannel_ind(irq_ptr, 0);
235 void qdio_setup_thinint(struct qdio_irq *irq_ptr)
237 if (!is_thinint_irq(irq_ptr))
238 return;
239 irq_ptr->dsci = get_indicator();
240 DBF_HEX(&irq_ptr->dsci, sizeof(void *));
243 void qdio_shutdown_thinint(struct qdio_irq *irq_ptr)
245 if (!is_thinint_irq(irq_ptr))
246 return;
248 /* reset adapter interrupt indicators */
249 set_subchannel_ind(irq_ptr, 1);
250 put_indicator(irq_ptr->dsci);
253 void __exit tiqdio_unregister_thinints(void)
255 WARN_ON(!list_empty(&tiq_list));
256 unregister_adapter_interrupt(&tiqdio_airq);