0092-dwc_otg-introduce-fiq_fsm_spin-un-lock.patch 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. From 424f79f35a94611f73182f19a7711174b756b052 Mon Sep 17 00:00:00 2001
  2. From: P33M <P33M@github.com>
  3. Date: Fri, 26 Sep 2014 11:32:09 +0100
  4. Subject: [PATCH 092/114] dwc_otg: introduce fiq_fsm_spin(un|)lock()
  5. SMP safety for the FIQ relies on register read-modify write cycles being
  6. completed in the correct order. Several places in the DWC code modify
  7. registers also touched by the FIQ. Protect these by a bare-bones lock
  8. mechanism.
  9. This also makes it possible to run the FIQ and IRQ handlers on different
  10. cores.
  11. ---
  12. .../usb/host/dwc_common_port/dwc_common_linux.c | 6 ---
  13. drivers/usb/host/dwc_otg/dwc_otg_cil.c | 10 -----
  14. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 46 +++++++++++++++++++++-
  15. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 16 +++++++-
  16. drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 23 ++++++++++-
  17. drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 9 ++++-
  18. 6 files changed, 88 insertions(+), 22 deletions(-)
  19. --- a/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  20. +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  21. @@ -580,13 +580,7 @@ void DWC_WRITE_REG64(uint64_t volatile *
  22. void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  23. {
  24. - unsigned long flags;
  25. -
  26. - local_irq_save(flags);
  27. - local_fiq_disable();
  28. writel((readl(reg) & ~clear_mask) | set_mask, reg);
  29. - local_fiq_enable();
  30. - local_irq_restore(flags);
  31. }
  32. #if 0
  33. --- a/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  34. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  35. @@ -2244,9 +2244,7 @@ void dwc_otg_core_host_init(dwc_otg_core
  36. */
  37. void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  38. {
  39. - uint32_t intr_enable;
  40. hcintmsk_data_t hc_intr_mask;
  41. - gintmsk_data_t gintmsk = {.d32 = 0 };
  42. hcchar_data_t hcchar;
  43. hcsplt_data_t hcsplt;
  44. @@ -2348,14 +2346,6 @@ void dwc_otg_hc_init(dwc_otg_core_if_t *
  45. }
  46. DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  47. - /* Enable the top level host channel interrupt. */
  48. - intr_enable = (1 << hc_num);
  49. - DWC_MODIFY_REG32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
  50. -
  51. - /* Make sure host channel interrupts are enabled. */
  52. - gintmsk.b.hcintr = 1;
  53. - DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  54. -
  55. /*
  56. * Program the HCCHARn register with the endpoint characteristics for
  57. * the current transfer.
  58. --- a/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  59. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  60. @@ -75,6 +75,46 @@ void notrace _fiq_print(enum fiq_debug_l
  61. }
  62. /**
  63. + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
  64. + * Must be called with local interrupts and FIQ disabled.
  65. + */
  66. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
  67. +{
  68. + unsigned long tmp;
  69. + uint32_t newval;
  70. + fiq_lock_t lockval;
  71. + smp_mb__before_spinlock();
  72. + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
  73. + * will be sufficient. If we are on a different CPU, then the lock protects us. */
  74. + prefetchw(&lock->slock);
  75. + asm volatile (
  76. + "1: ldrex %0, [%3]\n"
  77. + " add %1, %0, %4\n"
  78. + " strex %2, %1, [%3]\n"
  79. + " teq %2, #0\n"
  80. + " bne 1b"
  81. + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  82. + : "r" (&lock->slock), "I" (1 << 16)
  83. + : "cc");
  84. +
  85. + while (lockval.tickets.next != lockval.tickets.owner) {
  86. + wfe();
  87. + lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
  88. + }
  89. + smp_mb();
  90. +}
  91. +
  92. +/**
  93. + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
  94. + */
  95. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
  96. +{
  97. + smp_mb();
  98. + lock->tickets.owner++;
  99. + dsb_sev();
  100. +}
  101. +
  102. +/**
  103. * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  104. * @channel: channel to re-enable
  105. */
  106. @@ -1142,6 +1182,7 @@ void notrace dwc_otg_fiq_fsm(struct fiq_
  107. gintsts_handled.d32 = 0;
  108. haint_handled.d32 = 0;
  109. + fiq_fsm_spin_lock(&state->lock);
  110. gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  111. gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  112. gintsts.d32 &= gintmsk.d32;
  113. @@ -1231,7 +1272,7 @@ void notrace dwc_otg_fiq_fsm(struct fiq_
  114. }
  115. state->fiq_done++;
  116. - mb();
  117. + fiq_fsm_spin_unlock(&state->lock);
  118. }
  119. @@ -1253,6 +1294,7 @@ void notrace dwc_otg_fiq_nop(struct fiq_
  120. gintmsk_data_t gintmsk;
  121. hfnum_data_t hfnum;
  122. + fiq_fsm_spin_lock(&state->lock);
  123. hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  124. gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  125. gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  126. @@ -1290,5 +1332,5 @@ void notrace dwc_otg_fiq_nop(struct fiq_
  127. }
  128. state->fiq_done++;
  129. - mb();
  130. + fiq_fsm_spin_unlock(&state->lock);
  131. }
  132. --- a/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  133. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  134. @@ -120,7 +120,6 @@ typedef struct {
  135. volatile void* intstat;
  136. } mphi_regs_t;
  137. -
  138. enum fiq_debug_level {
  139. FIQDBG_SCHED = (1 << 0),
  140. FIQDBG_INT = (1 << 1),
  141. @@ -128,6 +127,16 @@ enum fiq_debug_level {
  142. FIQDBG_PORTHUB = (1 << 3),
  143. };
  144. +typedef struct {
  145. + union {
  146. + uint32_t slock;
  147. + struct _tickets {
  148. + uint16_t owner;
  149. + uint16_t next;
  150. + } tickets;
  151. + };
  152. +} fiq_lock_t;
  153. +
  154. struct fiq_state;
  155. extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  156. @@ -324,6 +333,7 @@ struct fiq_channel_state {
  157. * It contains top-level state information.
  158. */
  159. struct fiq_state {
  160. + fiq_lock_t lock;
  161. mphi_regs_t mphi_regs;
  162. void *dwc_regs_base;
  163. dma_addr_t dma_base;
  164. @@ -342,6 +352,10 @@ struct fiq_state {
  165. struct fiq_channel_state channel[0];
  166. };
  167. +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
  168. +
  169. +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
  170. +
  171. extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  172. extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  173. --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  174. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  175. @@ -1184,6 +1184,9 @@ static void assign_and_init_hc(dwc_otg_h
  176. dwc_otg_qtd_t *qtd;
  177. dwc_otg_hcd_urb_t *urb;
  178. void* ptr = NULL;
  179. + uint32_t intr_enable;
  180. + unsigned long flags;
  181. + gintmsk_data_t gintmsk = { .d32 = 0, };
  182. qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  183. @@ -1409,6 +1412,20 @@ static void assign_and_init_hc(dwc_otg_h
  184. hc->desc_list_addr = qh->desc_list_dma;
  185. dwc_otg_hc_init(hcd->core_if, hc);
  186. +
  187. + local_irq_save(flags);
  188. + local_fiq_disable();
  189. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  190. + /* Enable the top level host channel interrupt. */
  191. + intr_enable = (1 << hc->hc_num);
  192. + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
  193. +
  194. + /* Make sure host channel interrupts are enabled. */
  195. + gintmsk.b.hcintr = 1;
  196. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  197. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  198. + local_fiq_enable();
  199. + local_irq_restore(flags);
  200. hc->qh = qh;
  201. }
  202. @@ -1659,6 +1676,7 @@ int fiq_fsm_queue_isoc_transaction(dwc_o
  203. fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  204. hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  205. local_fiq_disable();
  206. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  207. DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  208. DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  209. DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  210. @@ -1676,6 +1694,7 @@ int fiq_fsm_queue_isoc_transaction(dwc_o
  211. }
  212. mb();
  213. st->hcchar_copy.b.chen = 0;
  214. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  215. local_fiq_enable();
  216. return 0;
  217. }
  218. @@ -1811,7 +1830,7 @@ int fiq_fsm_queue_split_transaction(dwc_
  219. DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  220. local_fiq_disable();
  221. - mb();
  222. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  223. if (hc->ep_type & 0x1) {
  224. hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  225. @@ -1909,7 +1928,7 @@ int fiq_fsm_queue_split_transaction(dwc_
  226. st->hcchar_copy.b.chen = 1;
  227. DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  228. }
  229. - mb();
  230. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  231. local_fiq_enable();
  232. return 0;
  233. }
  234. --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  235. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  236. @@ -101,6 +101,7 @@ int32_t dwc_otg_hcd_handle_intr(dwc_otg_
  237. if (dwc_otg_is_host_mode(core_if)) {
  238. if (fiq_enable) {
  239. local_fiq_disable();
  240. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  241. /* Pull in from the FIQ's disabled mask */
  242. gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  243. dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  244. @@ -116,8 +117,10 @@ int32_t dwc_otg_hcd_handle_intr(dwc_otg_
  245. }
  246. gintsts.d32 &= gintmsk.d32;
  247. - if (fiq_enable)
  248. + if (fiq_enable) {
  249. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  250. local_fiq_enable();
  251. + }
  252. if (!gintsts.d32) {
  253. goto exit_handler_routine;
  254. @@ -200,6 +203,7 @@ exit_handler_routine:
  255. gintmsk_data_t gintmsk_new;
  256. haintmsk_data_t haintmsk_new;
  257. local_fiq_disable();
  258. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  259. gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  260. if(fiq_fsm_enable)
  261. haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  262. @@ -222,6 +226,7 @@ exit_handler_routine:
  263. haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  264. /* Re-enable interrupts that the FIQ masked (first time round) */
  265. FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  266. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  267. local_fiq_enable();
  268. if ((jiffies / HZ) > last_time) {
  269. @@ -633,8 +638,10 @@ int32_t dwc_otg_hcd_handle_hc_intr(dwc_o
  270. {
  271. /* check the mask? */
  272. local_fiq_disable();
  273. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  274. haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  275. dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  276. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  277. local_fiq_enable();
  278. }