tcm.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * tcm.h
  3. *
  4. * TILER container manager specification and support functions for TI
  5. * TILER driver.
  6. *
  7. * Author: Lajos Molnar <molnar@ti.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. *
  22. * * Neither the name of Texas Instruments Incorporated nor the names of
  23. * its contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  33. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  35. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  36. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #ifndef TCM_H
  39. #define TCM_H
  40. struct tcm;
  41. /* point */
  42. struct tcm_pt {
  43. u16 x;
  44. u16 y;
  45. };
  46. /* 1d or 2d area */
  47. struct tcm_area {
  48. bool is2d; /* whether area is 1d or 2d */
  49. struct tcm *tcm; /* parent */
  50. struct tcm_pt p0;
  51. struct tcm_pt p1;
  52. };
  53. struct tcm {
  54. u16 width, height; /* container dimensions */
  55. int lut_id; /* Lookup table identifier */
  56. unsigned int y_offset; /* offset to use for y coordinates */
  57. /* 'pvt' structure shall contain any tcm details (attr) along with
  58. linked list of allocated areas and mutex for mutually exclusive access
  59. to the list. It may also contain copies of width and height to notice
  60. any changes to the publicly available width and height fields. */
  61. void *pvt;
  62. /* function table */
  63. s32 (*reserve_2d)(struct tcm *tcm, u16 height, u16 width, u8 align,
  64. struct tcm_area *area);
  65. s32 (*reserve_1d)(struct tcm *tcm, u32 slots, struct tcm_area *area);
  66. s32 (*free) (struct tcm *tcm, struct tcm_area *area);
  67. void (*deinit) (struct tcm *tcm);
  68. };
  69. /*=============================================================================
  70. BASIC TILER CONTAINER MANAGER INTERFACE
  71. =============================================================================*/
  72. /*
  73. * NOTE:
  74. *
  75. * Since some basic parameter checking is done outside the TCM algorithms,
  76. * TCM implementation do NOT have to check the following:
  77. *
  78. * area pointer is NULL
  79. * width and height fits within container
  80. * number of pages is more than the size of the container
  81. *
  82. */
  83. struct tcm *sita_init(u16 width, u16 height, struct tcm_pt *attr);
  84. /**
  85. * Deinitialize tiler container manager.
  86. *
  87. * @param tcm Pointer to container manager.
  88. *
  89. * @return 0 on success, non-0 error value on error. The call
  90. * should free as much memory as possible and meaningful
  91. * even on failure. Some error codes: -ENODEV: invalid
  92. * manager.
  93. */
  94. static inline void tcm_deinit(struct tcm *tcm)
  95. {
  96. if (tcm)
  97. tcm->deinit(tcm);
  98. }
  99. /**
  100. * Reserves a 2D area in the container.
  101. *
  102. * @param tcm Pointer to container manager.
  103. * @param height Height(in pages) of area to be reserved.
  104. * @param width Width(in pages) of area to be reserved.
  105. * @param align Alignment requirement for top-left corner of area. Not
  106. * all values may be supported by the container manager,
  107. * but it must support 0 (1), 32 and 64.
  108. * 0 value is equivalent to 1.
  109. * @param area Pointer to where the reserved area should be stored.
  110. *
  111. * @return 0 on success. Non-0 error code on failure. Also,
  112. * the tcm field of the area will be set to NULL on
  113. * failure. Some error codes: -ENODEV: invalid manager,
  114. * -EINVAL: invalid area, -ENOMEM: not enough space for
  115. * allocation.
  116. */
  117. static inline s32 tcm_reserve_2d(struct tcm *tcm, u16 width, u16 height,
  118. u16 align, struct tcm_area *area)
  119. {
  120. /* perform rudimentary error checking */
  121. s32 res = tcm == NULL ? -ENODEV :
  122. (area == NULL || width == 0 || height == 0 ||
  123. /* align must be a 2 power */
  124. (align & (align - 1))) ? -EINVAL :
  125. (height > tcm->height || width > tcm->width) ? -ENOMEM : 0;
  126. if (!res) {
  127. area->is2d = true;
  128. res = tcm->reserve_2d(tcm, height, width, align, area);
  129. area->tcm = res ? NULL : tcm;
  130. }
  131. return res;
  132. }
  133. /**
  134. * Reserves a 1D area in the container.
  135. *
  136. * @param tcm Pointer to container manager.
  137. * @param slots Number of (contiguous) slots to reserve.
  138. * @param area Pointer to where the reserved area should be stored.
  139. *
  140. * @return 0 on success. Non-0 error code on failure. Also,
  141. * the tcm field of the area will be set to NULL on
  142. * failure. Some error codes: -ENODEV: invalid manager,
  143. * -EINVAL: invalid area, -ENOMEM: not enough space for
  144. * allocation.
  145. */
  146. static inline s32 tcm_reserve_1d(struct tcm *tcm, u32 slots,
  147. struct tcm_area *area)
  148. {
  149. /* perform rudimentary error checking */
  150. s32 res = tcm == NULL ? -ENODEV :
  151. (area == NULL || slots == 0) ? -EINVAL :
  152. slots > (tcm->width * (u32) tcm->height) ? -ENOMEM : 0;
  153. if (!res) {
  154. area->is2d = false;
  155. res = tcm->reserve_1d(tcm, slots, area);
  156. area->tcm = res ? NULL : tcm;
  157. }
  158. return res;
  159. }
  160. /**
  161. * Free a previously reserved area from the container.
  162. *
  163. * @param area Pointer to area reserved by a prior call to
  164. * tcm_reserve_1d or tcm_reserve_2d call, whether
  165. * it was successful or not. (Note: all fields of
  166. * the structure must match.)
  167. *
  168. * @return 0 on success. Non-0 error code on failure. Also, the tcm
  169. * field of the area is set to NULL on success to avoid subsequent
  170. * freeing. This call will succeed even if supplying
  171. * the area from a failed reserved call.
  172. */
  173. static inline s32 tcm_free(struct tcm_area *area)
  174. {
  175. s32 res = 0; /* free succeeds by default */
  176. if (area && area->tcm) {
  177. res = area->tcm->free(area->tcm, area);
  178. if (res == 0)
  179. area->tcm = NULL;
  180. }
  181. return res;
  182. }
  183. /*=============================================================================
  184. HELPER FUNCTION FOR ANY TILER CONTAINER MANAGER
  185. =============================================================================*/
  186. /**
  187. * This method slices off the topmost 2D slice from the parent area, and stores
  188. * it in the 'slice' parameter. The 'parent' parameter will get modified to
  189. * contain the remaining portion of the area. If the whole parent area can
  190. * fit in a 2D slice, its tcm pointer is set to NULL to mark that it is no
  191. * longer a valid area.
  192. *
  193. * @param parent Pointer to a VALID parent area that will get modified
  194. * @param slice Pointer to the slice area that will get modified
  195. */
  196. static inline void tcm_slice(struct tcm_area *parent, struct tcm_area *slice)
  197. {
  198. *slice = *parent;
  199. /* check if we need to slice */
  200. if (slice->tcm && !slice->is2d &&
  201. slice->p0.y != slice->p1.y &&
  202. (slice->p0.x || (slice->p1.x != slice->tcm->width - 1))) {
  203. /* set end point of slice (start always remains) */
  204. slice->p1.x = slice->tcm->width - 1;
  205. slice->p1.y = (slice->p0.x) ? slice->p0.y : slice->p1.y - 1;
  206. /* adjust remaining area */
  207. parent->p0.x = 0;
  208. parent->p0.y = slice->p1.y + 1;
  209. } else {
  210. /* mark this as the last slice */
  211. parent->tcm = NULL;
  212. }
  213. }
  214. /* Verify if a tcm area is logically valid */
  215. static inline bool tcm_area_is_valid(struct tcm_area *area)
  216. {
  217. return area && area->tcm &&
  218. /* coordinate bounds */
  219. area->p1.x < area->tcm->width &&
  220. area->p1.y < area->tcm->height &&
  221. area->p0.y <= area->p1.y &&
  222. /* 1D coordinate relationship + p0.x check */
  223. ((!area->is2d &&
  224. area->p0.x < area->tcm->width &&
  225. area->p0.x + area->p0.y * area->tcm->width <=
  226. area->p1.x + area->p1.y * area->tcm->width) ||
  227. /* 2D coordinate relationship */
  228. (area->is2d &&
  229. area->p0.x <= area->p1.x));
  230. }
  231. /* see if a coordinate is within an area */
  232. static inline bool __tcm_is_in(struct tcm_pt *p, struct tcm_area *a)
  233. {
  234. u16 i;
  235. if (a->is2d) {
  236. return p->x >= a->p0.x && p->x <= a->p1.x &&
  237. p->y >= a->p0.y && p->y <= a->p1.y;
  238. } else {
  239. i = p->x + p->y * a->tcm->width;
  240. return i >= a->p0.x + a->p0.y * a->tcm->width &&
  241. i <= a->p1.x + a->p1.y * a->tcm->width;
  242. }
  243. }
  244. /* calculate area width */
  245. static inline u16 __tcm_area_width(struct tcm_area *area)
  246. {
  247. return area->p1.x - area->p0.x + 1;
  248. }
  249. /* calculate area height */
  250. static inline u16 __tcm_area_height(struct tcm_area *area)
  251. {
  252. return area->p1.y - area->p0.y + 1;
  253. }
  254. /* calculate number of slots in an area */
  255. static inline u16 __tcm_sizeof(struct tcm_area *area)
  256. {
  257. return area->is2d ?
  258. __tcm_area_width(area) * __tcm_area_height(area) :
  259. (area->p1.x - area->p0.x + 1) + (area->p1.y - area->p0.y) *
  260. area->tcm->width;
  261. }
  262. #define tcm_sizeof(area) __tcm_sizeof(&(area))
  263. #define tcm_awidth(area) __tcm_area_width(&(area))
  264. #define tcm_aheight(area) __tcm_area_height(&(area))
  265. #define tcm_is_in(pt, area) __tcm_is_in(&(pt), &(area))
  266. /* limit a 1D area to the first N pages */
  267. static inline s32 tcm_1d_limit(struct tcm_area *a, u32 num_pg)
  268. {
  269. if (__tcm_sizeof(a) < num_pg)
  270. return -ENOMEM;
  271. if (!num_pg)
  272. return -EINVAL;
  273. a->p1.x = (a->p0.x + num_pg - 1) % a->tcm->width;
  274. a->p1.y = a->p0.y + ((a->p0.x + num_pg - 1) / a->tcm->width);
  275. return 0;
  276. }
  277. /**
  278. * Iterate through 2D slices of a valid area. Behaves
  279. * syntactically as a for(;;) statement.
  280. *
  281. * @param var Name of a local variable of type 'struct
  282. * tcm_area *' that will get modified to
  283. * contain each slice.
  284. * @param area Pointer to the VALID parent area. This
  285. * structure will not get modified
  286. * throughout the loop.
  287. *
  288. */
  289. #define tcm_for_each_slice(var, area, safe) \
  290. for (safe = area, \
  291. tcm_slice(&safe, &var); \
  292. var.tcm; tcm_slice(&safe, &var))
  293. #endif