kgsl_regmap.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef KGSL_REGMAP_H
  7. #define KGSL_REGMAP_H
  8. struct kgsl_regmap;
  9. struct kgsl_regmap_region;
  10. /**
  11. * @ksgl_regmap_ops - Helper functions to access registers in a regmap region
  12. */
  13. struct kgsl_regmap_ops {
  14. /**
  15. * @preaccess: called before accesses to the register. This is used by
  16. * adreno to call kgsl_pre_hwaccess()
  17. */
  18. void (*preaccess)(struct kgsl_regmap_region *region);
  19. };
  20. /**
  21. * struct kgsl_regmap_region - Defines a region of registers in a kgsl_regmap
  22. */
  23. struct kgsl_regmap_region {
  24. /** @virt: Kernel address for the re-mapped region */
  25. void __iomem *virt;
  26. /** @offset: Dword offset of the region from the regmap base */
  27. u32 offset;
  28. /** @size: Size of the region in dwords */
  29. u32 size;
  30. /** @ops: Helper functions to access registers in the region */
  31. const struct kgsl_regmap_ops *ops;
  32. /** @priv: Private data to send to the ops */
  33. void *priv;
  34. };
  35. /**
  36. * struct kgsl_regmap - Define a set of individual regions that are all indexed
  37. * from a commmon base. This is used to access GPU and GMU registers in
  38. * separate io-remmaped regions from a single set of function calls.
  39. */
  40. struct kgsl_regmap {
  41. /**
  42. * @base: Resource pointer for the "base" region (the region that all
  43. * other regions are indexed from)
  44. */
  45. struct resource *base;
  46. /** @region: Array of regions for this regmap */
  47. struct kgsl_regmap_region region[4];
  48. /** @count: Number of active regions in @region */
  49. int count;
  50. };
  51. /**
  52. * struct kgsl_regmap_list
  53. */
  54. struct kgsl_regmap_list {
  55. /** offset: Dword offset of the register to write */
  56. u32 offset;
  57. /** val: Value to write */
  58. u32 val;
  59. };
  60. /**
  61. * kgsl_regmap_init - Initialize a regmap
  62. * @pdev: Pointer to the platform device that owns @name
  63. * @regmap: Pointer to the regmap to initialize
  64. * @name: Name of the resource to map
  65. * @ops: Pointer to the regmap ops for this region
  66. * @priv: Private data to pass to the regmap ops
  67. *
  68. * Initialize a regmap and set the resource @name as the base region in the
  69. * regmap. All other regions will be indexed from the start of this region.
  70. * This will nominally be the start of the GPU register region.
  71. *
  72. * Return: 0 on success or negative error on failure.
  73. */
  74. int kgsl_regmap_init(struct platform_device *pdev, struct kgsl_regmap *regmap,
  75. const char *name, const struct kgsl_regmap_ops *ops,
  76. void *priv);
  77. /**
  78. * kgsl_regmap_add_region - Add a region to an existing regmap
  79. * @regmap: The regmap to add the region to
  80. * @pdev: Pointer to the platform device that owns @name
  81. * @name: Name of the resource to map
  82. * @ops: Pointer to the regmap ops for this region
  83. * @priv: Private data to pass to the regmap ops
  84. *
  85. * Add a new region to the regmap. It will be indexed against the base
  86. * address already defined when the regmap was initialized. For example,
  87. * if the base GPU address is at physical address 0x3d000000 and the new
  88. * region is at physical address 0x3d010000 this region will be added at
  89. * (0x3d010000 - 0x3d000000) or dword offset 0x4000.
  90. *
  91. * Return: 0 on success or negative error on failure.
  92. */
  93. int kgsl_regmap_add_region(struct kgsl_regmap *regmap, struct platform_device *pdev,
  94. const char *name, const struct kgsl_regmap_ops *ops, void *priv);
  95. /**
  96. * kgsl_regmap_valid_offset - return true if the offset is valid
  97. * @regmap: The regmap to query
  98. * @offset: The dword offset to query
  99. *
  100. * Return: True if @offset is a valid register in the regmap
  101. */
  102. bool kgsl_regmap_valid_offset(struct kgsl_regmap *regmap, u32 offset);
  103. /**
  104. * kgsl_regmap_read - Read a register from the regmap
  105. * @regmap: The regmap to read from
  106. * @offset: The dword offset to read
  107. *
  108. * Read the register at the specified offset indexed against the base address in
  109. * the regmap. An offset that falls out of mapped regions will WARN and return
  110. * 0.
  111. *
  112. * Return: The value of the register at @offset
  113. */
  114. u32 kgsl_regmap_read(struct kgsl_regmap *regmap, u32 offset);
  115. /**
  116. * kgsl_regmap_write - Write a register to the regmap
  117. * @regmap: The regmap to write to
  118. * @data: The value to write to @offset
  119. * @offset: The dword offset to write
  120. *
  121. * Write @data to the register at the specified offset indexed against the base
  122. * address in he regmap. An offset that falls out of mapped regions will WARN
  123. * and skip the write.
  124. */
  125. void kgsl_regmap_write(struct kgsl_regmap *regmap, u32 value, u32 offset);
  126. /**
  127. * kgsl_regmap_multi_write - Write a list of registers
  128. * @regmap: The regmap to write to
  129. * @list: A pointer to an array of &strut kgsl_regmap_list items
  130. * @count: NUmber of items in @list
  131. *
  132. * Write all the registers in @list to the regmap.
  133. */
  134. void kgsl_regmap_multi_write(struct kgsl_regmap *regmap,
  135. const struct kgsl_regmap_list *list, int count);
  136. /**
  137. * kgsl_regmap_rmw - read-modify-write a register in the regmap
  138. * @regmap: The regmap to write to
  139. * @offset: The dword offset to write
  140. * @mask: Mask the register contents against this mask
  141. * @or: OR these bits into the register before writing it back again
  142. *
  143. * Read the register at @offset, mask it against @mask, OR the bits in @or and
  144. * write it back to @offset. @offset will be indexed against the base
  145. * address in the regmap. An offset that falls out of mapped regions will WARN
  146. * and skip the operation.
  147. */
  148. void kgsl_regmap_rmw(struct kgsl_regmap *regmap, u32 offset, u32 mask,
  149. u32 or);
  150. /**
  151. * kgsl_regmap_bulk_write - Write an array of values to a I/O region
  152. * @regmap: The regmap to write to
  153. * @offset: The dword offset to start writing to
  154. * @data: The data to write
  155. * @dwords: Number of dwords to write
  156. *
  157. * Bulk write @data to the I/O region starting at @offset for @dwords.
  158. * The write operation must fit fully inside a single region (no crossing the
  159. * boundaries). @offset will be indexed against the base
  160. * address in he regmap. An offset that falls out of mapped regions will WARN
  161. * and skip the operation.
  162. */
  163. void kgsl_regmap_bulk_write(struct kgsl_regmap *regmap, u32 offset,
  164. const void *data, int dwords);
  165. /**
  166. * kgsl_regmap_bulk_read - Read an array of values to a I/O region
  167. * @regmap: The regmap to read from
  168. * @offset: The dword offset to start reading from
  169. * @data: The data pointer to read into
  170. * @dwords: Number of dwords to read
  171. *
  172. * Bulk read into @data the I/O region starting at @offset for @dwords.
  173. * The read operation must fit fully inside a single region (no crossing the
  174. * boundaries). @offset will be indexed against the base
  175. * address in the regmap. An offset that falls out of mapped regions will WARN
  176. * and skip the operation.
  177. */
  178. void kgsl_regmap_bulk_read(struct kgsl_regmap *regmap, u32 offset,
  179. const void *data, int dwords);
  180. /**
  181. * kgsl_regmap_virt - Return the kernel address for a offset
  182. * @regmap: The regmap to write to
  183. * @offset: The dword offset to map to a kernel address
  184. *
  185. * Return: The kernel address for @offset or NULL if out of range.
  186. */
  187. void __iomem *kgsl_regmap_virt(struct kgsl_regmap *regmap, u32 offset);
  188. /**
  189. * kgsl_regmap_read_indexed - Read a indexed pair of registers
  190. * @regmap: The regmap to read from
  191. * @addr: The offset of the address register for the index pair
  192. * @data: The offset of the data register for the index pair
  193. * @dest: An array to put the values
  194. * @count: Number of dwords to read from @data
  195. *
  196. * This function configures the address register once and then
  197. * reads from the data register in a loop.
  198. */
  199. void kgsl_regmap_read_indexed(struct kgsl_regmap *regmap, u32 addr,
  200. u32 data, u32 *dest, int count);
  201. /**
  202. * kgsl_regmap_read_indexed_interleaved - Dump an indexed pair of registers
  203. * @regmap: The regmap to read from
  204. * @addr: The offset of the address register for the index pair
  205. * @data: The offset of the data register for the index pair
  206. * @dest: An array to put the values
  207. * @start: Starting value to be programmed in the address register
  208. * @count: Number of dwords to read from @data
  209. *
  210. * This function is slightly different than kgsl_regmap_read_indexed()
  211. * in that it takes as argument a start value that is to be programmed
  212. * in the address register and secondly, the address register is to be
  213. * configured before every read of the data register.
  214. */
  215. void kgsl_regmap_read_indexed_interleaved(struct kgsl_regmap *regmap, u32 addr,
  216. u32 data, u32 *dest, u32 start, int count);
  217. /**
  218. * kgsl_regmap_get_region - Return the region for the given offset
  219. * @regmap: The regmap to query
  220. * @offset: The offset to query
  221. *
  222. * Return: The &struct kgsl_regmap_region that owns the offset or NULL
  223. */
  224. struct kgsl_regmap_region *kgsl_regmap_get_region(struct kgsl_regmap *regmap,
  225. u32 offset);
  226. /**
  227. * kgsl_regmap_poll_read - A helper function for kgsl_regmap_read_poll_timeout
  228. * @region: Pointer to a &struct kgsl_regmap_region
  229. * @offset: Offset to read
  230. * @val: Pointer for the result
  231. *
  232. * This is a special helper function to be called only from
  233. * kgsl_regmap_read_poll_timeout.
  234. *
  235. * Return: 0 on success or -ENODEV if the region is NULL.
  236. */
  237. int kgsl_regmap_poll_read(struct kgsl_regmap_region *region, u32 offset,
  238. u32 *val);
  239. #define kgsl_regmap_read_poll_timeout(regmap, offset, val, cond, \
  240. sleep_us, timeout_us) \
  241. ({ \
  242. int __ret, __tmp; \
  243. struct kgsl_regmap_region *region = \
  244. kgsl_regmap_get_region(regmap, offset); \
  245. \
  246. if (region && region->ops && region->ops->preaccess) \
  247. region->ops->preaccess(region); \
  248. __tmp = read_poll_timeout(kgsl_regmap_poll_read, __ret, __ret || (cond),\
  249. sleep_us, timeout_us, false, region, offset, &(val)); \
  250. __ret ?: __tmp; \
  251. })
  252. #endif