kgsl_iommu.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef __KGSL_IOMMU_H
  7. #define __KGSL_IOMMU_H
  8. #include <linux/adreno-smmu-priv.h>
  9. #include <linux/io-pgtable.h>
  10. /*
  11. * These defines control the address range for allocations that
  12. * are mapped into all pagetables.
  13. */
  14. #define KGSL_IOMMU_GLOBAL_MEM_SIZE (20 * SZ_1M)
  15. #define KGSL_IOMMU_GLOBAL_MEM_BASE32 0xf8000000
  16. #define KGSL_IOMMU_GLOBAL_MEM_BASE64 \
  17. (KGSL_MEMSTORE_TOKEN_ADDRESS - KGSL_IOMMU_GLOBAL_MEM_SIZE)
  18. /*
  19. * This is a dummy token address that we use to identify memstore when the user
  20. * wants to map it. mmap() uses a unsigned long for the offset so we need a 32
  21. * bit value that works with all sized apps. We chose a value that was purposely
  22. * unmapped so if you increase the global memory size make sure it doesn't
  23. * conflict
  24. */
  25. #define KGSL_MEMSTORE_TOKEN_ADDRESS (KGSL_IOMMU_SECURE_BASE32 - SZ_4K)
  26. #define KGSL_IOMMU_GLOBAL_MEM_BASE(__mmu) \
  27. (test_bit(KGSL_MMU_64BIT, &(__mmu)->features) ? \
  28. KGSL_IOMMU_GLOBAL_MEM_BASE64 : KGSL_IOMMU_GLOBAL_MEM_BASE32)
  29. #define KGSL_IOMMU_SVM_BASE32(__mmu) \
  30. (ADRENO_DEVICE(KGSL_MMU_DEVICE(__mmu))->uche_gmem_base + \
  31. ADRENO_DEVICE(KGSL_MMU_DEVICE(__mmu))->gpucore->gmem_size)
  32. #define KGSL_IOMMU_SVM_END32(__mmu) \
  33. (test_bit(KGSL_MMU_64BIT, &(__mmu)->features) ? \
  34. (test_bit(KGSL_MMU_IOPGTABLE, &(__mmu)->features) ? \
  35. KGSL_MEMSTORE_TOKEN_ADDRESS : \
  36. KGSL_IOMMU_GLOBAL_MEM_BASE64) : \
  37. (0xC0000000 - SZ_16M))
  38. /*
  39. * Limit secure size to 256MB for 32bit kernels.
  40. */
  41. #define KGSL_IOMMU_SECURE_SIZE32 SZ_256M
  42. #define KGSL_IOMMU_SECURE_BASE32 \
  43. (KGSL_IOMMU_SECURE_BASE64 - KGSL_IOMMU_SECURE_SIZE32)
  44. #define KGSL_IOMMU_SECURE_END32 KGSL_IOMMU_SECURE_BASE64
  45. #define KGSL_IOMMU_SECURE_BASE64 0x100000000ULL
  46. #define KGSL_IOMMU_SECURE_END64 \
  47. (KGSL_IOMMU_SECURE_BASE64 + KGSL_IOMMU_SECURE_SIZE64)
  48. #define KGSL_IOMMU_MAX_SECURE_SIZE 0xFFFFF000
  49. #define KGSL_IOMMU_SECURE_SIZE64 \
  50. (KGSL_IOMMU_MAX_SECURE_SIZE - KGSL_IOMMU_SECURE_SIZE32)
  51. #define KGSL_IOMMU_SECURE_BASE(_mmu) (test_bit(KGSL_MMU_64BIT, \
  52. &(_mmu)->features) ? KGSL_IOMMU_SECURE_BASE64 : \
  53. KGSL_IOMMU_SECURE_BASE32)
  54. #define KGSL_IOMMU_SECURE_END(_mmu) (test_bit(KGSL_MMU_64BIT, \
  55. &(_mmu)->features) ? KGSL_IOMMU_SECURE_END64 : \
  56. KGSL_IOMMU_SECURE_END32)
  57. #define KGSL_IOMMU_SECURE_SIZE(_mmu) (test_bit(KGSL_MMU_64BIT, \
  58. &(_mmu)->features) ? KGSL_IOMMU_MAX_SECURE_SIZE : \
  59. KGSL_IOMMU_SECURE_SIZE32)
  60. /* The CPU supports 39 bit addresses */
  61. #define KGSL_IOMMU_SVM_BASE64 0x1000000000ULL
  62. #define KGSL_IOMMU_SVM_END64 0x4000000000ULL
  63. #define KGSL_IOMMU_VA_BASE64 0x4000000000ULL
  64. #define KGSL_IOMMU_VA_END64 0x8000000000ULL
  65. #define CP_APERTURE_REG 0
  66. #define CP_SMMU_APERTURE_ID 0x1B
  67. /* Global SMMU register offsets */
  68. #define KGSL_IOMMU_PRR_CFG_LADDR 0x6008
  69. #define KGSL_IOMMU_PRR_CFG_UADDR 0x600c
  70. /* Register offsets */
  71. #define KGSL_IOMMU_CTX_SCTLR 0x0000
  72. #define KGSL_IOMMU_CTX_ACTLR 0x0004
  73. #define KGSL_IOMMU_CTX_TTBR0 0x0020
  74. #define KGSL_IOMMU_CTX_CONTEXTIDR 0x0034
  75. #define KGSL_IOMMU_CTX_FSR 0x0058
  76. #define KGSL_IOMMU_CTX_TLBIALL 0x0618
  77. #define KGSL_IOMMU_CTX_RESUME 0x0008
  78. #define KGSL_IOMMU_CTX_FSYNR0 0x0068
  79. #define KGSL_IOMMU_CTX_FSYNR1 0x006c
  80. #define KGSL_IOMMU_CTX_TLBSYNC 0x07f0
  81. #define KGSL_IOMMU_CTX_TLBSTATUS 0x07f4
  82. /* TLBSTATUS register fields */
  83. #define KGSL_IOMMU_CTX_TLBSTATUS_SACTIVE BIT(0)
  84. /* SCTLR fields */
  85. #define KGSL_IOMMU_SCTLR_HUPCF_SHIFT 8
  86. #define KGSL_IOMMU_SCTLR_CFCFG_SHIFT 7
  87. #define KGSL_IOMMU_SCTLR_CFIE_SHIFT 6
  88. #define KGSL_IOMMU_ACTLR_PRR_ENABLE BIT(5)
  89. /* FSR fields */
  90. #define KGSL_IOMMU_FSR_SS_SHIFT 30
  91. /* ASID field in TTBR register */
  92. #define KGSL_IOMMU_ASID_START_BIT 48
  93. /* offset at which a nop command is placed in setstate */
  94. #define KGSL_IOMMU_SETSTATE_NOP_OFFSET 1024
  95. /*
  96. * struct kgsl_iommu_context - Structure holding data about an iommu context
  97. * bank
  98. * @pdev: pointer to the iommu context's platform device
  99. * @name: context name
  100. * @id: The id of the context, used for deciding how it is used.
  101. * @cb_num: The hardware context bank number, used for calculating register
  102. * offsets.
  103. * @kgsldev: The kgsl device that uses this context.
  104. * @stalled_on_fault: Flag when set indicates that this iommu device is stalled
  105. * on a page fault
  106. */
  107. struct kgsl_iommu_context {
  108. struct platform_device *pdev;
  109. const char *name;
  110. int cb_num;
  111. struct kgsl_device *kgsldev;
  112. bool stalled_on_fault;
  113. /** ratelimit: Ratelimit state for the context */
  114. struct ratelimit_state ratelimit;
  115. struct iommu_domain *domain;
  116. struct adreno_smmu_priv adreno_smmu;
  117. };
  118. /*
  119. * struct kgsl_iommu - Structure holding iommu data for kgsl driver
  120. * @regbase: Virtual address of the IOMMU register base
  121. * @regstart: Physical address of the iommu registers
  122. * @regsize: Length of the iommu register region.
  123. * @setstate: Scratch GPU memory for IOMMU operations
  124. * @clk_enable_count: The ref count of clock enable calls
  125. * @clks: Array of pointers to IOMMU clocks
  126. * @smmu_info: smmu info used in a5xx preemption
  127. */
  128. struct kgsl_iommu {
  129. /** @user_context: Container for the user iommu context */
  130. struct kgsl_iommu_context user_context;
  131. /** @secure_context: Container for the secure iommu context */
  132. struct kgsl_iommu_context secure_context;
  133. /** @lpac_context: Container for the LPAC iommu context */
  134. struct kgsl_iommu_context lpac_context;
  135. void __iomem *regbase;
  136. struct kgsl_memdesc *setstate;
  137. atomic_t clk_enable_count;
  138. struct clk_bulk_data *clks;
  139. int num_clks;
  140. struct kgsl_memdesc *smmu_info;
  141. /** @pdev: Pointer to the platform device for the IOMMU device */
  142. struct platform_device *pdev;
  143. /**
  144. * @ppt_active: Set when the first per process pagetable is created.
  145. * This is used to warn when global buffers are created that might not
  146. * be mapped in all contexts
  147. */
  148. bool ppt_active;
  149. /** @cb0_offset: Offset of context bank 0 from iommu register base */
  150. u32 cb0_offset;
  151. /** @pagesize: Size of each context bank register space */
  152. u32 pagesize;
  153. /** @cx_gdsc: CX GDSC handle in case the IOMMU needs it */
  154. struct regulator *cx_gdsc;
  155. };
  156. /*
  157. * struct kgsl_iommu_pt - Iommu pagetable structure private to kgsl driver
  158. * @base: Container of the base kgsl pagetable
  159. * @ttbr0: register value to set when using this pagetable
  160. * @pgtbl_ops: Pagetable operations for mapping/unmapping buffers
  161. * @info: Pagetable info used to allocate pagetable operations
  162. */
  163. struct kgsl_iommu_pt {
  164. struct kgsl_pagetable base;
  165. u64 ttbr0;
  166. struct io_pgtable_ops *pgtbl_ops;
  167. struct qcom_io_pgtable_info info;
  168. };
  169. /**
  170. * kgsl_set_smmu_aperture - set SMMU Aperture for user context
  171. * @device: A GPU device handle
  172. *
  173. * Return: 0 on success or negative on failure.
  174. */
  175. int kgsl_set_smmu_aperture(struct kgsl_device *device,
  176. struct kgsl_iommu_context *context);
  177. /**
  178. * kgsl_set_smmu_lpac_aperture - set SMMU Aperture for LPAC context
  179. * @device: Pointer to the KGSL device
  180. * @context: Pointer to the LPAC iommu context
  181. *
  182. * Return: 0 on success or negative on failure.
  183. */
  184. int kgsl_set_smmu_lpac_aperture(struct kgsl_device *device,
  185. struct kgsl_iommu_context *context);
  186. #endif