v3d_mmu.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2017-2018 Broadcom */
  3. /**
  4. * DOC: Broadcom V3D MMU
  5. *
  6. * The V3D 3.x hardware (compared to VC4) now includes an MMU. It has
  7. * a single level of page tables for the V3D's 4GB address space to
  8. * map to AXI bus addresses, thus it could need up to 4MB of
  9. * physically contiguous memory to store the PTEs.
  10. *
  11. * Because the 4MB of contiguous memory for page tables is precious,
  12. * and switching between them is expensive, we load all BOs into the
  13. * same 4GB address space.
  14. *
  15. * To protect clients from each other, we should use the GMP to
  16. * quickly mask out (at 128kb granularity) what pages are available to
  17. * each client. This is not yet implemented.
  18. */
  19. #include "v3d_drv.h"
  20. #include "v3d_regs.h"
  21. #define V3D_MMU_PAGE_SHIFT 12
  22. /* Note: All PTEs for the 1MB superpage must be filled with the
  23. * superpage bit set.
  24. */
  25. #define V3D_PTE_SUPERPAGE BIT(31)
  26. #define V3D_PTE_WRITEABLE BIT(29)
  27. #define V3D_PTE_VALID BIT(28)
  28. static int v3d_mmu_flush_all(struct v3d_dev *v3d)
  29. {
  30. int ret;
  31. /* Make sure that another flush isn't already running when we
  32. * start this one.
  33. */
  34. ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
  35. V3D_MMU_CTL_TLB_CLEARING), 100);
  36. if (ret)
  37. dev_err(v3d->dev, "TLB clear wait idle pre-wait failed\n");
  38. V3D_WRITE(V3D_MMU_CTL, V3D_READ(V3D_MMU_CTL) |
  39. V3D_MMU_CTL_TLB_CLEAR);
  40. V3D_WRITE(V3D_MMUC_CONTROL,
  41. V3D_MMUC_CONTROL_FLUSH |
  42. V3D_MMUC_CONTROL_ENABLE);
  43. ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
  44. V3D_MMU_CTL_TLB_CLEARING), 100);
  45. if (ret) {
  46. dev_err(v3d->dev, "TLB clear wait idle failed\n");
  47. return ret;
  48. }
  49. ret = wait_for(!(V3D_READ(V3D_MMUC_CONTROL) &
  50. V3D_MMUC_CONTROL_FLUSHING), 100);
  51. if (ret)
  52. dev_err(v3d->dev, "MMUC flush wait idle failed\n");
  53. return ret;
  54. }
  55. int v3d_mmu_set_page_table(struct v3d_dev *v3d)
  56. {
  57. V3D_WRITE(V3D_MMU_PT_PA_BASE, v3d->pt_paddr >> V3D_MMU_PAGE_SHIFT);
  58. V3D_WRITE(V3D_MMU_CTL,
  59. V3D_MMU_CTL_ENABLE |
  60. V3D_MMU_CTL_PT_INVALID |
  61. V3D_MMU_CTL_PT_INVALID_ABORT |
  62. V3D_MMU_CTL_WRITE_VIOLATION_ABORT |
  63. V3D_MMU_CTL_CAP_EXCEEDED_ABORT);
  64. V3D_WRITE(V3D_MMU_ILLEGAL_ADDR,
  65. (v3d->mmu_scratch_paddr >> V3D_MMU_PAGE_SHIFT) |
  66. V3D_MMU_ILLEGAL_ADDR_ENABLE);
  67. V3D_WRITE(V3D_MMUC_CONTROL, V3D_MMUC_CONTROL_ENABLE);
  68. return v3d_mmu_flush_all(v3d);
  69. }
  70. void v3d_mmu_insert_ptes(struct v3d_bo *bo)
  71. {
  72. struct v3d_dev *v3d = to_v3d_dev(bo->base.dev);
  73. u32 page = bo->node.start;
  74. u32 page_prot = V3D_PTE_WRITEABLE | V3D_PTE_VALID;
  75. unsigned int count;
  76. struct scatterlist *sgl;
  77. for_each_sg(bo->sgt->sgl, sgl, bo->sgt->nents, count) {
  78. u32 page_address = sg_dma_address(sgl) >> V3D_MMU_PAGE_SHIFT;
  79. u32 pte = page_prot | page_address;
  80. u32 i;
  81. BUG_ON(page_address + (sg_dma_len(sgl) >> V3D_MMU_PAGE_SHIFT) >=
  82. BIT(24));
  83. for (i = 0; i < sg_dma_len(sgl) >> V3D_MMU_PAGE_SHIFT; i++)
  84. v3d->pt[page++] = pte + i;
  85. }
  86. WARN_ON_ONCE(page - bo->node.start !=
  87. bo->base.size >> V3D_MMU_PAGE_SHIFT);
  88. if (v3d_mmu_flush_all(v3d))
  89. dev_err(v3d->dev, "MMU flush timeout\n");
  90. }
  91. void v3d_mmu_remove_ptes(struct v3d_bo *bo)
  92. {
  93. struct v3d_dev *v3d = to_v3d_dev(bo->base.dev);
  94. u32 npages = bo->base.size >> V3D_MMU_PAGE_SHIFT;
  95. u32 page;
  96. for (page = bo->node.start; page < bo->node.start + npages; page++)
  97. v3d->pt[page] = 0;
  98. if (v3d_mmu_flush_all(v3d))
  99. dev_err(v3d->dev, "MMU flush timeout\n");
  100. }