]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/mmc/core/sdio_irq.c
mmc: core: Clarify sdio_irq_pending flag for MMC_CAP2_SDIO_IRQ_NOTHREAD
[mirror_ubuntu-eoan-kernel.git] / drivers / mmc / core / sdio_irq.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/drivers/mmc/core/sdio_irq.c
4 *
5 * Author: Nicolas Pitre
6 * Created: June 18, 2007
7 * Copyright: MontaVista Software Inc.
8 *
9 * Copyright 2008 Pierre Ossman
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <uapi/linux/sched/types.h>
15 #include <linux/kthread.h>
16 #include <linux/export.h>
17 #include <linux/wait.h>
18 #include <linux/delay.h>
19
20 #include <linux/mmc/core.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/mmc/sdio_func.h>
25
26 #include "sdio_ops.h"
27 #include "core.h"
28 #include "card.h"
29
30 static int process_sdio_pending_irqs(struct mmc_host *host)
31 {
32 struct mmc_card *card = host->card;
33 int i, ret, count;
34 bool sdio_irq_pending = host->sdio_irq_pending;
35 unsigned char pending;
36 struct sdio_func *func;
37
38 /* Don't process SDIO IRQs if the card is suspended. */
39 if (mmc_card_suspended(card))
40 return 0;
41
42 /* Clear the flag to indicate that we have processed the IRQ. */
43 host->sdio_irq_pending = false;
44
45 /*
46 * Optimization, if there is only 1 function interrupt registered
47 * and we know an IRQ was signaled then call irq handler directly.
48 * Otherwise do the full probe.
49 */
50 func = card->sdio_single_irq;
51 if (func && sdio_irq_pending) {
52 func->irq_handler(func);
53 return 1;
54 }
55
56 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
57 if (ret) {
58 pr_debug("%s: error %d reading SDIO_CCCR_INTx\n",
59 mmc_card_id(card), ret);
60 return ret;
61 }
62
63 if (pending && mmc_card_broken_irq_polling(card) &&
64 !(host->caps & MMC_CAP_SDIO_IRQ)) {
65 unsigned char dummy;
66
67 /* A fake interrupt could be created when we poll SDIO_CCCR_INTx
68 * register with a Marvell SD8797 card. A dummy CMD52 read to
69 * function 0 register 0xff can avoid this.
70 */
71 mmc_io_rw_direct(card, 0, 0, 0xff, 0, &dummy);
72 }
73
74 count = 0;
75 for (i = 1; i <= 7; i++) {
76 if (pending & (1 << i)) {
77 func = card->sdio_func[i - 1];
78 if (!func) {
79 pr_warn("%s: pending IRQ for non-existent function\n",
80 mmc_card_id(card));
81 ret = -EINVAL;
82 } else if (func->irq_handler) {
83 func->irq_handler(func);
84 count++;
85 } else {
86 pr_warn("%s: pending IRQ with no handler\n",
87 sdio_func_id(func));
88 ret = -EINVAL;
89 }
90 }
91 }
92
93 if (count)
94 return count;
95
96 return ret;
97 }
98
99 static void sdio_run_irqs(struct mmc_host *host)
100 {
101 mmc_claim_host(host);
102 if (host->sdio_irqs) {
103 process_sdio_pending_irqs(host);
104 if (host->ops->ack_sdio_irq)
105 host->ops->ack_sdio_irq(host);
106 }
107 mmc_release_host(host);
108 }
109
110 void sdio_irq_work(struct work_struct *work)
111 {
112 struct mmc_host *host =
113 container_of(work, struct mmc_host, sdio_irq_work.work);
114
115 sdio_run_irqs(host);
116 }
117
118 void sdio_signal_irq(struct mmc_host *host)
119 {
120 host->sdio_irq_pending = true;
121 queue_delayed_work(system_wq, &host->sdio_irq_work, 0);
122 }
123 EXPORT_SYMBOL_GPL(sdio_signal_irq);
124
125 static int sdio_irq_thread(void *_host)
126 {
127 struct mmc_host *host = _host;
128 struct sched_param param = { .sched_priority = 1 };
129 unsigned long period, idle_period;
130 int ret;
131
132 sched_setscheduler(current, SCHED_FIFO, &param);
133
134 /*
135 * We want to allow for SDIO cards to work even on non SDIO
136 * aware hosts. One thing that non SDIO host cannot do is
137 * asynchronous notification of pending SDIO card interrupts
138 * hence we poll for them in that case.
139 */
140 idle_period = msecs_to_jiffies(10);
141 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
142 MAX_SCHEDULE_TIMEOUT : idle_period;
143
144 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
145 mmc_hostname(host), period);
146
147 do {
148 /*
149 * We claim the host here on drivers behalf for a couple
150 * reasons:
151 *
152 * 1) it is already needed to retrieve the CCCR_INTx;
153 * 2) we want the driver(s) to clear the IRQ condition ASAP;
154 * 3) we need to control the abort condition locally.
155 *
156 * Just like traditional hard IRQ handlers, we expect SDIO
157 * IRQ handlers to be quick and to the point, so that the
158 * holding of the host lock does not cover too much work
159 * that doesn't require that lock to be held.
160 */
161 ret = __mmc_claim_host(host, NULL,
162 &host->sdio_irq_thread_abort);
163 if (ret)
164 break;
165 ret = process_sdio_pending_irqs(host);
166 mmc_release_host(host);
167
168 /*
169 * Give other threads a chance to run in the presence of
170 * errors.
171 */
172 if (ret < 0) {
173 set_current_state(TASK_INTERRUPTIBLE);
174 if (!kthread_should_stop())
175 schedule_timeout(HZ);
176 set_current_state(TASK_RUNNING);
177 }
178
179 /*
180 * Adaptive polling frequency based on the assumption
181 * that an interrupt will be closely followed by more.
182 * This has a substantial benefit for network devices.
183 */
184 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
185 if (ret > 0)
186 period /= 2;
187 else {
188 period++;
189 if (period > idle_period)
190 period = idle_period;
191 }
192 }
193
194 set_current_state(TASK_INTERRUPTIBLE);
195 if (host->caps & MMC_CAP_SDIO_IRQ)
196 host->ops->enable_sdio_irq(host, 1);
197 if (!kthread_should_stop())
198 schedule_timeout(period);
199 set_current_state(TASK_RUNNING);
200 } while (!kthread_should_stop());
201
202 if (host->caps & MMC_CAP_SDIO_IRQ)
203 host->ops->enable_sdio_irq(host, 0);
204
205 pr_debug("%s: IRQ thread exiting with code %d\n",
206 mmc_hostname(host), ret);
207
208 return ret;
209 }
210
211 static int sdio_card_irq_get(struct mmc_card *card)
212 {
213 struct mmc_host *host = card->host;
214
215 WARN_ON(!host->claimed);
216
217 if (!host->sdio_irqs++) {
218 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
219 atomic_set(&host->sdio_irq_thread_abort, 0);
220 host->sdio_irq_thread =
221 kthread_run(sdio_irq_thread, host,
222 "ksdioirqd/%s", mmc_hostname(host));
223 if (IS_ERR(host->sdio_irq_thread)) {
224 int err = PTR_ERR(host->sdio_irq_thread);
225 host->sdio_irqs--;
226 return err;
227 }
228 } else if (host->caps & MMC_CAP_SDIO_IRQ) {
229 host->ops->enable_sdio_irq(host, 1);
230 }
231 }
232
233 return 0;
234 }
235
236 static int sdio_card_irq_put(struct mmc_card *card)
237 {
238 struct mmc_host *host = card->host;
239
240 WARN_ON(!host->claimed);
241
242 if (host->sdio_irqs < 1)
243 return -EINVAL;
244
245 if (!--host->sdio_irqs) {
246 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
247 atomic_set(&host->sdio_irq_thread_abort, 1);
248 kthread_stop(host->sdio_irq_thread);
249 } else if (host->caps & MMC_CAP_SDIO_IRQ) {
250 host->ops->enable_sdio_irq(host, 0);
251 }
252 }
253
254 return 0;
255 }
256
257 /* If there is only 1 function registered set sdio_single_irq */
258 static void sdio_single_irq_set(struct mmc_card *card)
259 {
260 struct sdio_func *func;
261 int i;
262
263 card->sdio_single_irq = NULL;
264 if ((card->host->caps & MMC_CAP_SDIO_IRQ) &&
265 card->host->sdio_irqs == 1)
266 for (i = 0; i < card->sdio_funcs; i++) {
267 func = card->sdio_func[i];
268 if (func && func->irq_handler) {
269 card->sdio_single_irq = func;
270 break;
271 }
272 }
273 }
274
275 /**
276 * sdio_claim_irq - claim the IRQ for a SDIO function
277 * @func: SDIO function
278 * @handler: IRQ handler callback
279 *
280 * Claim and activate the IRQ for the given SDIO function. The provided
281 * handler will be called when that IRQ is asserted. The host is always
282 * claimed already when the handler is called so the handler should not
283 * call sdio_claim_host() or sdio_release_host().
284 */
285 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
286 {
287 int ret;
288 unsigned char reg;
289
290 if (!func)
291 return -EINVAL;
292
293 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
294
295 if (func->irq_handler) {
296 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
297 return -EBUSY;
298 }
299
300 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
301 if (ret)
302 return ret;
303
304 reg |= 1 << func->num;
305
306 reg |= 1; /* Master interrupt enable */
307
308 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
309 if (ret)
310 return ret;
311
312 func->irq_handler = handler;
313 ret = sdio_card_irq_get(func->card);
314 if (ret)
315 func->irq_handler = NULL;
316 sdio_single_irq_set(func->card);
317
318 return ret;
319 }
320 EXPORT_SYMBOL_GPL(sdio_claim_irq);
321
322 /**
323 * sdio_release_irq - release the IRQ for a SDIO function
324 * @func: SDIO function
325 *
326 * Disable and release the IRQ for the given SDIO function.
327 */
328 int sdio_release_irq(struct sdio_func *func)
329 {
330 int ret;
331 unsigned char reg;
332
333 if (!func)
334 return -EINVAL;
335
336 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
337
338 if (func->irq_handler) {
339 func->irq_handler = NULL;
340 sdio_card_irq_put(func->card);
341 sdio_single_irq_set(func->card);
342 }
343
344 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
345 if (ret)
346 return ret;
347
348 reg &= ~(1 << func->num);
349
350 /* Disable master interrupt with the last function interrupt */
351 if (!(reg & 0xFE))
352 reg = 0;
353
354 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
355 if (ret)
356 return ret;
357
358 return 0;
359 }
360 EXPORT_SYMBOL_GPL(sdio_release_irq);
361