950-0362-usb-xhci-add-VLI_TRB_CACHE_BUG-quirk.patch 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. From 02b78815e793d3f778b3f9b199f17cdd7f7e074d Mon Sep 17 00:00:00 2001
  2. From: Jonathan Bell <[email protected]>
  3. Date: Mon, 13 Dec 2021 16:04:03 +0000
  4. Subject: [PATCH] usb: xhci: add VLI_TRB_CACHE_BUG quirk
  5. The VL805 fetches up to 4 transfer TRBs at a time. TRB reads don't cross
  6. a 64B boundary, and if a TRB is fetched and is not on a 64B boundary,
  7. the read is sized up to the next 64B boundary.
  8. However the VL805 implements a readahead prefetch for TRBs on a transfer
  9. ring. This fetches the next 64B after any TRB read has happened. Near
  10. the end of a ring segment, the prefetcher can read the first 64B of the
  11. next page in physical memory and this is where the behaviour causes a
  12. bug.
  13. The controller does not tag reads with which endpoint they are for, so
  14. if the start of the next page is a ring segment used by a victim
  15. endpoint, and the victim endpoint is about to fetch TRBs from the start
  16. of the segment, the victim endpoint will read from the prefetched data
  17. and not perform a read to main memory. If the data is stale, the ring
  18. cycle state bit may not be correct and the endpoint will silently halt.
  19. Adjust trbs_per_seg for transfer rings allocated for this controller.
  20. See https://github.com/raspberrypi/linux/issues/4685
  21. Signed-off-by: Jonathan Bell <[email protected]>
  22. ---
  23. drivers/usb/host/xhci-mem.c | 11 +++++++++++
  24. drivers/usb/host/xhci-pci.c | 1 +
  25. drivers/usb/host/xhci.h | 1 +
  26. 3 files changed, 13 insertions(+)
  27. --- a/drivers/usb/host/xhci-mem.c
  28. +++ b/drivers/usb/host/xhci-mem.c
  29. @@ -392,6 +392,17 @@ struct xhci_ring *xhci_ring_alloc(struct
  30. return ring;
  31. ring->trbs_per_seg = TRBS_PER_SEGMENT;
  32. + /*
  33. + * The Via VL805 has a bug where cache readahead will fetch off the end
  34. + * of a page if the Link TRB of a transfer ring is in the last 4 slots.
  35. + * Where there are consecutive physical pages containing ring segments,
  36. + * this can cause a desync between the controller's view of a ring
  37. + * and the host.
  38. + */
  39. + if (xhci->quirks & XHCI_VLI_TRB_CACHE_BUG &&
  40. + type != TYPE_EVENT && type != TYPE_COMMAND)
  41. + ring->trbs_per_seg -= 4;
  42. +
  43. ret = xhci_alloc_segments_for_ring(xhci, &ring->first_seg,
  44. &ring->last_seg, num_segs, ring->trbs_per_seg,
  45. cycle_state, type, max_packet, flags);
  46. --- a/drivers/usb/host/xhci-pci.c
  47. +++ b/drivers/usb/host/xhci-pci.c
  48. @@ -296,6 +296,7 @@ static void xhci_pci_quirks(struct devic
  49. if (pdev->vendor == PCI_VENDOR_ID_VIA && pdev->device == 0x3483) {
  50. xhci->quirks |= XHCI_LPM_SUPPORT;
  51. xhci->quirks |= XHCI_AVOID_DQ_ON_LINK;
  52. + xhci->quirks |= XHCI_VLI_TRB_CACHE_BUG;
  53. }
  54. if (pdev->vendor == PCI_VENDOR_ID_ASMEDIA &&
  55. --- a/drivers/usb/host/xhci.h
  56. +++ b/drivers/usb/host/xhci.h
  57. @@ -1904,6 +1904,7 @@ struct xhci_hcd {
  58. #define XHCI_ZHAOXIN_TRB_FETCH BIT_ULL(45)
  59. #define XHCI_ZHAOXIN_HOST BIT_ULL(46)
  60. #define XHCI_AVOID_DQ_ON_LINK BIT_ULL(47)
  61. +#define XHCI_VLI_TRB_CACHE_BUG BIT_ULL(48)
  62. unsigned int num_active_eps;
  63. unsigned int limit_active_eps;