aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTony Lindgren <tony@atomide.com>2016-11-16 14:21:23 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-11-17 10:25:39 -0500
commitea2f35c01d5ea72b43b9b4fb4c5b9417a9eb2fb8 (patch)
tree8dfa4ef390e0738db2c7d0f2c179a5c7b7a1ae7d
parentc723bd6ec2b50e7c8b3424d9cb8febd8ffa3da1f (diff)
usb: musb: Fix sleeping function called from invalid context for hdrc glue
Commit 65b3f50ed6fa ("usb: musb: Add PM runtime support for MUSB DSPS glue layer") wrongly added a call for pm_runtime_get_sync to otg_timer that runs in softirq context. That causes a "BUG: sleeping function called from invalid context" every time when polling the cable status: [<c015ebb4>] (__might_sleep) from [<c0413d60>] (__pm_runtime_resume+0x9c/0xa0) [<c0413d60>] (__pm_runtime_resume) from [<c04d0bc4>] (otg_timer+0x3c/0x254) [<c04d0bc4>] (otg_timer) from [<c0191180>] (call_timer_fn+0xfc/0x41c) [<c0191180>] (call_timer_fn) from [<c01915c0>] (expire_timers+0x120/0x210) [<c01915c0>] (expire_timers) from [<c0191acc>] (run_timer_softirq+0xa4/0xdc) [<c0191acc>] (run_timer_softirq) from [<c010168c>] (__do_softirq+0x12c/0x594) I did not notice that as I did not have CONFIG_DEBUG_ATOMIC_SLEEP enabled. And looks like also musb_gadget_queue() suffers from the same problem. Let's fix the issue by using a list of delayed work then call it on resume. Note that we want to do this only when musb core and it's parent devices are awake, and we need to make sure the DSPS glue timer is stopped as noted by Johan Hovold <johan@kernel.org>. Note that we already are re-enabling the timer with mod_timer() in dsps_musb_enable(). Later on we may be able to remove other delayed work in the musb driver and just do it from pending_resume_work. But this should be done only for delayed work that does not have other timing requirements beyond just being run on resume. Fixes: 65b3f50ed6fa ("usb: musb: Add PM runtime support for MUSB DSPS glue layer") Reported-by: Johan Hovold <johan@kernel.org> Reviewed-by: Johan Hovold <johan@kernel.org> Tested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Tony Lindgren <tony@atomide.com> Signed-off-by: Bin Liu <b-liu@ti.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/usb/musb/musb_core.c109
-rw-r--r--drivers/usb/musb/musb_core.h7
-rw-r--r--drivers/usb/musb/musb_dsps.c36
-rw-r--r--drivers/usb/musb/musb_gadget.c33
4 files changed, 167 insertions, 18 deletions
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index f1ea4494dcb2..384de6cd26f5 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -1969,6 +1969,7 @@ static struct musb *allocate_instance(struct device *dev,
1969 INIT_LIST_HEAD(&musb->control); 1969 INIT_LIST_HEAD(&musb->control);
1970 INIT_LIST_HEAD(&musb->in_bulk); 1970 INIT_LIST_HEAD(&musb->in_bulk);
1971 INIT_LIST_HEAD(&musb->out_bulk); 1971 INIT_LIST_HEAD(&musb->out_bulk);
1972 INIT_LIST_HEAD(&musb->pending_list);
1972 1973
1973 musb->vbuserr_retry = VBUSERR_RETRY_COUNT; 1974 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
1974 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON; 1975 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
@@ -2018,6 +2019,84 @@ static void musb_free(struct musb *musb)
2018 musb_host_free(musb); 2019 musb_host_free(musb);
2019} 2020}
2020 2021
2022struct musb_pending_work {
2023 int (*callback)(struct musb *musb, void *data);
2024 void *data;
2025 struct list_head node;
2026};
2027
2028/*
2029 * Called from musb_runtime_resume(), musb_resume(), and
2030 * musb_queue_resume_work(). Callers must take musb->lock.
2031 */
2032static int musb_run_resume_work(struct musb *musb)
2033{
2034 struct musb_pending_work *w, *_w;
2035 unsigned long flags;
2036 int error = 0;
2037
2038 spin_lock_irqsave(&musb->list_lock, flags);
2039 list_for_each_entry_safe(w, _w, &musb->pending_list, node) {
2040 if (w->callback) {
2041 error = w->callback(musb, w->data);
2042 if (error < 0) {
2043 dev_err(musb->controller,
2044 "resume callback %p failed: %i\n",
2045 w->callback, error);
2046 }
2047 }
2048 list_del(&w->node);
2049 devm_kfree(musb->controller, w);
2050 }
2051 spin_unlock_irqrestore(&musb->list_lock, flags);
2052
2053 return error;
2054}
2055
2056/*
2057 * Called to run work if device is active or else queue the work to happen
2058 * on resume. Caller must take musb->lock and must hold an RPM reference.
2059 *
2060 * Note that we cowardly refuse queuing work after musb PM runtime
2061 * resume is done calling musb_run_resume_work() and return -EINPROGRESS
2062 * instead.
2063 */
2064int musb_queue_resume_work(struct musb *musb,
2065 int (*callback)(struct musb *musb, void *data),
2066 void *data)
2067{
2068 struct musb_pending_work *w;
2069 unsigned long flags;
2070 int error;
2071
2072 if (WARN_ON(!callback))
2073 return -EINVAL;
2074
2075 if (pm_runtime_active(musb->controller))
2076 return callback(musb, data);
2077
2078 w = devm_kzalloc(musb->controller, sizeof(*w), GFP_ATOMIC);
2079 if (!w)
2080 return -ENOMEM;
2081
2082 w->callback = callback;
2083 w->data = data;
2084 spin_lock_irqsave(&musb->list_lock, flags);
2085 if (musb->is_runtime_suspended) {
2086 list_add_tail(&w->node, &musb->pending_list);
2087 error = 0;
2088 } else {
2089 dev_err(musb->controller, "could not add resume work %p\n",
2090 callback);
2091 devm_kfree(musb->controller, w);
2092 error = -EINPROGRESS;
2093 }
2094 spin_unlock_irqrestore(&musb->list_lock, flags);
2095
2096 return error;
2097}
2098EXPORT_SYMBOL_GPL(musb_queue_resume_work);
2099
2021static void musb_deassert_reset(struct work_struct *work) 2100static void musb_deassert_reset(struct work_struct *work)
2022{ 2101{
2023 struct musb *musb; 2102 struct musb *musb;
@@ -2065,6 +2144,7 @@ musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
2065 } 2144 }
2066 2145
2067 spin_lock_init(&musb->lock); 2146 spin_lock_init(&musb->lock);
2147 spin_lock_init(&musb->list_lock);
2068 musb->board_set_power = plat->set_power; 2148 musb->board_set_power = plat->set_power;
2069 musb->min_power = plat->min_power; 2149 musb->min_power = plat->min_power;
2070 musb->ops = plat->platform_ops; 2150 musb->ops = plat->platform_ops;
@@ -2558,6 +2638,7 @@ static int musb_suspend(struct device *dev)
2558 2638
2559 musb_platform_disable(musb); 2639 musb_platform_disable(musb);
2560 musb_generic_disable(musb); 2640 musb_generic_disable(musb);
2641 WARN_ON(!list_empty(&musb->pending_list));
2561 2642
2562 spin_lock_irqsave(&musb->lock, flags); 2643 spin_lock_irqsave(&musb->lock, flags);
2563 2644
@@ -2579,9 +2660,11 @@ static int musb_suspend(struct device *dev)
2579 2660
2580static int musb_resume(struct device *dev) 2661static int musb_resume(struct device *dev)
2581{ 2662{
2582 struct musb *musb = dev_to_musb(dev); 2663 struct musb *musb = dev_to_musb(dev);
2583 u8 devctl; 2664 unsigned long flags;
2584 u8 mask; 2665 int error;
2666 u8 devctl;
2667 u8 mask;
2585 2668
2586 /* 2669 /*
2587 * For static cmos like DaVinci, register values were preserved 2670 * For static cmos like DaVinci, register values were preserved
@@ -2615,6 +2698,13 @@ static int musb_resume(struct device *dev)
2615 2698
2616 musb_start(musb); 2699 musb_start(musb);
2617 2700
2701 spin_lock_irqsave(&musb->lock, flags);
2702 error = musb_run_resume_work(musb);
2703 if (error)
2704 dev_err(musb->controller, "resume work failed with %i\n",
2705 error);
2706 spin_unlock_irqrestore(&musb->lock, flags);
2707
2618 return 0; 2708 return 0;
2619} 2709}
2620 2710
@@ -2623,13 +2713,16 @@ static int musb_runtime_suspend(struct device *dev)
2623 struct musb *musb = dev_to_musb(dev); 2713 struct musb *musb = dev_to_musb(dev);
2624 2714
2625 musb_save_context(musb); 2715 musb_save_context(musb);
2716 musb->is_runtime_suspended = 1;
2626 2717
2627 return 0; 2718 return 0;
2628} 2719}
2629 2720
2630static int musb_runtime_resume(struct device *dev) 2721static int musb_runtime_resume(struct device *dev)
2631{ 2722{
2632 struct musb *musb = dev_to_musb(dev); 2723 struct musb *musb = dev_to_musb(dev);
2724 unsigned long flags;
2725 int error;
2633 2726
2634 /* 2727 /*
2635 * When pm_runtime_get_sync called for the first time in driver 2728 * When pm_runtime_get_sync called for the first time in driver
@@ -2651,6 +2744,14 @@ static int musb_runtime_resume(struct device *dev)
2651 msecs_to_jiffies(USB_RESUME_TIMEOUT)); 2744 msecs_to_jiffies(USB_RESUME_TIMEOUT));
2652 } 2745 }
2653 2746
2747 spin_lock_irqsave(&musb->lock, flags);
2748 error = musb_run_resume_work(musb);
2749 if (error)
2750 dev_err(musb->controller, "resume work failed with %i\n",
2751 error);
2752 musb->is_runtime_suspended = 0;
2753 spin_unlock_irqrestore(&musb->lock, flags);
2754
2654 return 0; 2755 return 0;
2655} 2756}
2656 2757
diff --git a/drivers/usb/musb/musb_core.h b/drivers/usb/musb/musb_core.h
index c04abf424c5c..15b1f93c7037 100644
--- a/drivers/usb/musb/musb_core.h
+++ b/drivers/usb/musb/musb_core.h
@@ -303,6 +303,7 @@ struct musb_context_registers {
303struct musb { 303struct musb {
304 /* device lock */ 304 /* device lock */
305 spinlock_t lock; 305 spinlock_t lock;
306 spinlock_t list_lock; /* resume work list lock */
306 307
307 struct musb_io io; 308 struct musb_io io;
308 const struct musb_platform_ops *ops; 309 const struct musb_platform_ops *ops;
@@ -337,6 +338,7 @@ struct musb {
337 struct list_head control; /* of musb_qh */ 338 struct list_head control; /* of musb_qh */
338 struct list_head in_bulk; /* of musb_qh */ 339 struct list_head in_bulk; /* of musb_qh */
339 struct list_head out_bulk; /* of musb_qh */ 340 struct list_head out_bulk; /* of musb_qh */
341 struct list_head pending_list; /* pending work list */
340 342
341 struct timer_list otg_timer; 343 struct timer_list otg_timer;
342 struct notifier_block nb; 344 struct notifier_block nb;
@@ -386,6 +388,7 @@ struct musb {
386 unsigned long idle_timeout; /* Next timeout in jiffies */ 388 unsigned long idle_timeout; /* Next timeout in jiffies */
387 389
388 unsigned is_initialized:1; 390 unsigned is_initialized:1;
391 unsigned is_runtime_suspended:1;
389 392
390 /* active means connected and not suspended */ 393 /* active means connected and not suspended */
391 unsigned is_active:1; 394 unsigned is_active:1;
@@ -542,6 +545,10 @@ extern irqreturn_t musb_interrupt(struct musb *);
542 545
543extern void musb_hnp_stop(struct musb *musb); 546extern void musb_hnp_stop(struct musb *musb);
544 547
548int musb_queue_resume_work(struct musb *musb,
549 int (*callback)(struct musb *musb, void *data),
550 void *data);
551
545static inline void musb_platform_set_vbus(struct musb *musb, int is_on) 552static inline void musb_platform_set_vbus(struct musb *musb, int is_on)
546{ 553{
547 if (musb->ops->set_vbus) 554 if (musb->ops->set_vbus)
diff --git a/drivers/usb/musb/musb_dsps.c b/drivers/usb/musb/musb_dsps.c
index 0f17d2140db6..6096c84ab67a 100644
--- a/drivers/usb/musb/musb_dsps.c
+++ b/drivers/usb/musb/musb_dsps.c
@@ -185,24 +185,19 @@ static void dsps_musb_disable(struct musb *musb)
185 musb_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap); 185 musb_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap);
186 musb_writel(reg_base, wrp->epintr_clear, 186 musb_writel(reg_base, wrp->epintr_clear,
187 wrp->txep_bitmap | wrp->rxep_bitmap); 187 wrp->txep_bitmap | wrp->rxep_bitmap);
188 del_timer_sync(&glue->timer);
188 musb_writeb(musb->mregs, MUSB_DEVCTL, 0); 189 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
189} 190}
190 191
191static void otg_timer(unsigned long _musb) 192/* Caller must take musb->lock */
193static int dsps_check_status(struct musb *musb, void *unused)
192{ 194{
193 struct musb *musb = (void *)_musb;
194 void __iomem *mregs = musb->mregs; 195 void __iomem *mregs = musb->mregs;
195 struct device *dev = musb->controller; 196 struct device *dev = musb->controller;
196 struct dsps_glue *glue = dev_get_drvdata(dev->parent); 197 struct dsps_glue *glue = dev_get_drvdata(dev->parent);
197 const struct dsps_musb_wrapper *wrp = glue->wrp; 198 const struct dsps_musb_wrapper *wrp = glue->wrp;
198 u8 devctl; 199 u8 devctl;
199 unsigned long flags;
200 int skip_session = 0; 200 int skip_session = 0;
201 int err;
202
203 err = pm_runtime_get_sync(dev);
204 if (err < 0)
205 dev_err(dev, "Poll could not pm_runtime_get: %i\n", err);
206 201
207 /* 202 /*
208 * We poll because DSPS IP's won't expose several OTG-critical 203 * We poll because DSPS IP's won't expose several OTG-critical
@@ -212,7 +207,6 @@ static void otg_timer(unsigned long _musb)
212 dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl, 207 dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
213 usb_otg_state_string(musb->xceiv->otg->state)); 208 usb_otg_state_string(musb->xceiv->otg->state));
214 209
215 spin_lock_irqsave(&musb->lock, flags);
216 switch (musb->xceiv->otg->state) { 210 switch (musb->xceiv->otg->state) {
217 case OTG_STATE_A_WAIT_VRISE: 211 case OTG_STATE_A_WAIT_VRISE:
218 mod_timer(&glue->timer, jiffies + 212 mod_timer(&glue->timer, jiffies +
@@ -245,8 +239,30 @@ static void otg_timer(unsigned long _musb)
245 default: 239 default:
246 break; 240 break;
247 } 241 }
248 spin_unlock_irqrestore(&musb->lock, flags);
249 242
243 return 0;
244}
245
246static void otg_timer(unsigned long _musb)
247{
248 struct musb *musb = (void *)_musb;
249 struct device *dev = musb->controller;
250 unsigned long flags;
251 int err;
252
253 err = pm_runtime_get(dev);
254 if ((err != -EINPROGRESS) && err < 0) {
255 dev_err(dev, "Poll could not pm_runtime_get: %i\n", err);
256 pm_runtime_put_noidle(dev);
257
258 return;
259 }
260
261 spin_lock_irqsave(&musb->lock, flags);
262 err = musb_queue_resume_work(musb, dsps_check_status, NULL);
263 if (err < 0)
264 dev_err(dev, "%s resume work: %i\n", __func__, err);
265 spin_unlock_irqrestore(&musb->lock, flags);
250 pm_runtime_mark_last_busy(dev); 266 pm_runtime_mark_last_busy(dev);
251 pm_runtime_put_autosuspend(dev); 267 pm_runtime_put_autosuspend(dev);
252} 268}
diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
index 4042ea017985..910f50967627 100644
--- a/drivers/usb/musb/musb_gadget.c
+++ b/drivers/usb/musb/musb_gadget.c
@@ -1222,13 +1222,22 @@ void musb_ep_restart(struct musb *musb, struct musb_request *req)
1222 rxstate(musb, req); 1222 rxstate(musb, req);
1223} 1223}
1224 1224
1225static int musb_ep_restart_resume_work(struct musb *musb, void *data)
1226{
1227 struct musb_request *req = data;
1228
1229 musb_ep_restart(musb, req);
1230
1231 return 0;
1232}
1233
1225static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, 1234static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1226 gfp_t gfp_flags) 1235 gfp_t gfp_flags)
1227{ 1236{
1228 struct musb_ep *musb_ep; 1237 struct musb_ep *musb_ep;
1229 struct musb_request *request; 1238 struct musb_request *request;
1230 struct musb *musb; 1239 struct musb *musb;
1231 int status = 0; 1240 int status;
1232 unsigned long lockflags; 1241 unsigned long lockflags;
1233 1242
1234 if (!ep || !req) 1243 if (!ep || !req)
@@ -1245,6 +1254,17 @@ static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1245 if (request->ep != musb_ep) 1254 if (request->ep != musb_ep)
1246 return -EINVAL; 1255 return -EINVAL;
1247 1256
1257 status = pm_runtime_get(musb->controller);
1258 if ((status != -EINPROGRESS) && status < 0) {
1259 dev_err(musb->controller,
1260 "pm runtime get failed in %s\n",
1261 __func__);
1262 pm_runtime_put_noidle(musb->controller);
1263
1264 return status;
1265 }
1266 status = 0;
1267
1248 trace_musb_req_enq(request); 1268 trace_musb_req_enq(request);
1249 1269
1250 /* request is mine now... */ 1270 /* request is mine now... */
@@ -1255,7 +1275,6 @@ static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1255 1275
1256 map_dma_buffer(request, musb, musb_ep); 1276 map_dma_buffer(request, musb, musb_ep);
1257 1277
1258 pm_runtime_get_sync(musb->controller);
1259 spin_lock_irqsave(&musb->lock, lockflags); 1278 spin_lock_irqsave(&musb->lock, lockflags);
1260 1279
1261 /* don't queue if the ep is down */ 1280 /* don't queue if the ep is down */
@@ -1271,8 +1290,14 @@ static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1271 list_add_tail(&request->list, &musb_ep->req_list); 1290 list_add_tail(&request->list, &musb_ep->req_list);
1272 1291
1273 /* it this is the head of the queue, start i/o ... */ 1292 /* it this is the head of the queue, start i/o ... */
1274 if (!musb_ep->busy && &request->list == musb_ep->req_list.next) 1293 if (!musb_ep->busy && &request->list == musb_ep->req_list.next) {
1275 musb_ep_restart(musb, request); 1294 status = musb_queue_resume_work(musb,
1295 musb_ep_restart_resume_work,
1296 request);
1297 if (status < 0)
1298 dev_err(musb->controller, "%s resume work: %i\n",
1299 __func__, status);
1300 }
1276 1301
1277unlock: 1302unlock:
1278 spin_unlock_irqrestore(&musb->lock, lockflags); 1303 spin_unlock_irqrestore(&musb->lock, lockflags);