18bb0daffSRob Clark /*
28bb0daffSRob Clark * TILER container manager specification and support functions for TI
38bb0daffSRob Clark * TILER driver.
48bb0daffSRob Clark *
58bb0daffSRob Clark * Author: Lajos Molnar <molnar@ti.com>
68bb0daffSRob Clark *
78bb0daffSRob Clark * All rights reserved.
88bb0daffSRob Clark *
98bb0daffSRob Clark * Redistribution and use in source and binary forms, with or without
108bb0daffSRob Clark * modification, are permitted provided that the following conditions
118bb0daffSRob Clark * are met:
128bb0daffSRob Clark *
138bb0daffSRob Clark * * Redistributions of source code must retain the above copyright
148bb0daffSRob Clark * notice, this list of conditions and the following disclaimer.
158bb0daffSRob Clark *
168bb0daffSRob Clark * * Redistributions in binary form must reproduce the above copyright
178bb0daffSRob Clark * notice, this list of conditions and the following disclaimer in the
188bb0daffSRob Clark * documentation and/or other materials provided with the distribution.
198bb0daffSRob Clark *
208bb0daffSRob Clark * * Neither the name of Texas Instruments Incorporated nor the names of
218bb0daffSRob Clark * its contributors may be used to endorse or promote products derived
228bb0daffSRob Clark * from this software without specific prior written permission.
238bb0daffSRob Clark *
248bb0daffSRob Clark * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
258bb0daffSRob Clark * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
268bb0daffSRob Clark * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
278bb0daffSRob Clark * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
288bb0daffSRob Clark * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
298bb0daffSRob Clark * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
308bb0daffSRob Clark * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
318bb0daffSRob Clark * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
328bb0daffSRob Clark * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
338bb0daffSRob Clark * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
348bb0daffSRob Clark * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
358bb0daffSRob Clark */
368bb0daffSRob Clark
378bb0daffSRob Clark #ifndef TCM_H
388bb0daffSRob Clark #define TCM_H
398bb0daffSRob Clark
408bb0daffSRob Clark struct tcm;
418bb0daffSRob Clark
428bb0daffSRob Clark /* point */
438bb0daffSRob Clark struct tcm_pt {
448bb0daffSRob Clark u16 x;
458bb0daffSRob Clark u16 y;
468bb0daffSRob Clark };
478bb0daffSRob Clark
488bb0daffSRob Clark /* 1d or 2d area */
498bb0daffSRob Clark struct tcm_area {
508bb0daffSRob Clark bool is2d; /* whether area is 1d or 2d */
518bb0daffSRob Clark struct tcm *tcm; /* parent */
528bb0daffSRob Clark struct tcm_pt p0;
538bb0daffSRob Clark struct tcm_pt p1;
548bb0daffSRob Clark };
558bb0daffSRob Clark
568bb0daffSRob Clark struct tcm {
578bb0daffSRob Clark u16 width, height; /* container dimensions */
588bb0daffSRob Clark int lut_id; /* Lookup table identifier */
598bb0daffSRob Clark
608bb0daffSRob Clark unsigned int y_offset; /* offset to use for y coordinates */
618bb0daffSRob Clark
620d6fa53fSAndy Gross spinlock_t lock;
630d6fa53fSAndy Gross unsigned long *bitmap;
640d6fa53fSAndy Gross size_t map_size;
658bb0daffSRob Clark
668bb0daffSRob Clark /* function table */
670d6fa53fSAndy Gross s32 (*reserve_2d)(struct tcm *tcm, u16 height, u16 width, u16 align,
68*dfe9cfccSLaurent Pinchart s16 offset, u16 slot_bytes,
698bb0daffSRob Clark struct tcm_area *area);
708bb0daffSRob Clark s32 (*reserve_1d)(struct tcm *tcm, u32 slots, struct tcm_area *area);
718bb0daffSRob Clark s32 (*free)(struct tcm *tcm, struct tcm_area *area);
728bb0daffSRob Clark void (*deinit)(struct tcm *tcm);
738bb0daffSRob Clark };
748bb0daffSRob Clark
758bb0daffSRob Clark /*=============================================================================
768bb0daffSRob Clark BASIC TILER CONTAINER MANAGER INTERFACE
778bb0daffSRob Clark =============================================================================*/
788bb0daffSRob Clark
798bb0daffSRob Clark /*
808bb0daffSRob Clark * NOTE:
818bb0daffSRob Clark *
828bb0daffSRob Clark * Since some basic parameter checking is done outside the TCM algorithms,
838bb0daffSRob Clark * TCM implementation do NOT have to check the following:
848bb0daffSRob Clark *
858bb0daffSRob Clark * area pointer is NULL
868bb0daffSRob Clark * width and height fits within container
878bb0daffSRob Clark * number of pages is more than the size of the container
888bb0daffSRob Clark *
898bb0daffSRob Clark */
908bb0daffSRob Clark
910d6fa53fSAndy Gross struct tcm *sita_init(u16 width, u16 height);
928bb0daffSRob Clark
938bb0daffSRob Clark
948bb0daffSRob Clark /**
958bb0daffSRob Clark * Deinitialize tiler container manager.
968bb0daffSRob Clark *
978bb0daffSRob Clark * @param tcm Pointer to container manager.
988bb0daffSRob Clark *
998bb0daffSRob Clark * @return 0 on success, non-0 error value on error. The call
1008bb0daffSRob Clark * should free as much memory as possible and meaningful
1018bb0daffSRob Clark * even on failure. Some error codes: -ENODEV: invalid
1028bb0daffSRob Clark * manager.
1038bb0daffSRob Clark */
tcm_deinit(struct tcm * tcm)1048bb0daffSRob Clark static inline void tcm_deinit(struct tcm *tcm)
1058bb0daffSRob Clark {
1068bb0daffSRob Clark if (tcm)
1078bb0daffSRob Clark tcm->deinit(tcm);
1088bb0daffSRob Clark }
1098bb0daffSRob Clark
1108bb0daffSRob Clark /**
1118bb0daffSRob Clark * Reserves a 2D area in the container.
1128bb0daffSRob Clark *
1138bb0daffSRob Clark * @param tcm Pointer to container manager.
1148bb0daffSRob Clark * @param height Height(in pages) of area to be reserved.
1158bb0daffSRob Clark * @param width Width(in pages) of area to be reserved.
1168bb0daffSRob Clark * @param align Alignment requirement for top-left corner of area. Not
1178bb0daffSRob Clark * all values may be supported by the container manager,
1188bb0daffSRob Clark * but it must support 0 (1), 32 and 64.
1198bb0daffSRob Clark * 0 value is equivalent to 1.
1200d6fa53fSAndy Gross * @param offset Offset requirement, in bytes. This is the offset
1210d6fa53fSAndy Gross * from a 4KiB aligned virtual address.
1220d6fa53fSAndy Gross * @param slot_bytes Width of slot in bytes
1238bb0daffSRob Clark * @param area Pointer to where the reserved area should be stored.
1248bb0daffSRob Clark *
1258bb0daffSRob Clark * @return 0 on success. Non-0 error code on failure. Also,
1268bb0daffSRob Clark * the tcm field of the area will be set to NULL on
1278bb0daffSRob Clark * failure. Some error codes: -ENODEV: invalid manager,
1288bb0daffSRob Clark * -EINVAL: invalid area, -ENOMEM: not enough space for
1298bb0daffSRob Clark * allocation.
1308bb0daffSRob Clark */
tcm_reserve_2d(struct tcm * tcm,u16 width,u16 height,u16 align,s16 offset,u16 slot_bytes,struct tcm_area * area)1318bb0daffSRob Clark static inline s32 tcm_reserve_2d(struct tcm *tcm, u16 width, u16 height,
132*dfe9cfccSLaurent Pinchart u16 align, s16 offset, u16 slot_bytes,
1330d6fa53fSAndy Gross struct tcm_area *area)
1348bb0daffSRob Clark {
1358bb0daffSRob Clark /* perform rudimentary error checking */
1368bb0daffSRob Clark s32 res = tcm == NULL ? -ENODEV :
1378bb0daffSRob Clark (area == NULL || width == 0 || height == 0 ||
1388bb0daffSRob Clark /* align must be a 2 power */
1398bb0daffSRob Clark (align & (align - 1))) ? -EINVAL :
1408bb0daffSRob Clark (height > tcm->height || width > tcm->width) ? -ENOMEM : 0;
1418bb0daffSRob Clark
1428bb0daffSRob Clark if (!res) {
1438bb0daffSRob Clark area->is2d = true;
1440d6fa53fSAndy Gross res = tcm->reserve_2d(tcm, height, width, align, offset,
1450d6fa53fSAndy Gross slot_bytes, area);
1468bb0daffSRob Clark area->tcm = res ? NULL : tcm;
1478bb0daffSRob Clark }
1488bb0daffSRob Clark
1498bb0daffSRob Clark return res;
1508bb0daffSRob Clark }
1518bb0daffSRob Clark
1528bb0daffSRob Clark /**
1538bb0daffSRob Clark * Reserves a 1D area in the container.
1548bb0daffSRob Clark *
1558bb0daffSRob Clark * @param tcm Pointer to container manager.
1568bb0daffSRob Clark * @param slots Number of (contiguous) slots to reserve.
1578bb0daffSRob Clark * @param area Pointer to where the reserved area should be stored.
1588bb0daffSRob Clark *
1598bb0daffSRob Clark * @return 0 on success. Non-0 error code on failure. Also,
1608bb0daffSRob Clark * the tcm field of the area will be set to NULL on
1618bb0daffSRob Clark * failure. Some error codes: -ENODEV: invalid manager,
1628bb0daffSRob Clark * -EINVAL: invalid area, -ENOMEM: not enough space for
1638bb0daffSRob Clark * allocation.
1648bb0daffSRob Clark */
tcm_reserve_1d(struct tcm * tcm,u32 slots,struct tcm_area * area)1658bb0daffSRob Clark static inline s32 tcm_reserve_1d(struct tcm *tcm, u32 slots,
1668bb0daffSRob Clark struct tcm_area *area)
1678bb0daffSRob Clark {
1688bb0daffSRob Clark /* perform rudimentary error checking */
1698bb0daffSRob Clark s32 res = tcm == NULL ? -ENODEV :
1708bb0daffSRob Clark (area == NULL || slots == 0) ? -EINVAL :
1718bb0daffSRob Clark slots > (tcm->width * (u32) tcm->height) ? -ENOMEM : 0;
1728bb0daffSRob Clark
1738bb0daffSRob Clark if (!res) {
1748bb0daffSRob Clark area->is2d = false;
1758bb0daffSRob Clark res = tcm->reserve_1d(tcm, slots, area);
1768bb0daffSRob Clark area->tcm = res ? NULL : tcm;
1778bb0daffSRob Clark }
1788bb0daffSRob Clark
1798bb0daffSRob Clark return res;
1808bb0daffSRob Clark }
1818bb0daffSRob Clark
1828bb0daffSRob Clark /**
1838bb0daffSRob Clark * Free a previously reserved area from the container.
1848bb0daffSRob Clark *
1858bb0daffSRob Clark * @param area Pointer to area reserved by a prior call to
1868bb0daffSRob Clark * tcm_reserve_1d or tcm_reserve_2d call, whether
1878bb0daffSRob Clark * it was successful or not. (Note: all fields of
1888bb0daffSRob Clark * the structure must match.)
1898bb0daffSRob Clark *
1908bb0daffSRob Clark * @return 0 on success. Non-0 error code on failure. Also, the tcm
1918bb0daffSRob Clark * field of the area is set to NULL on success to avoid subsequent
1928bb0daffSRob Clark * freeing. This call will succeed even if supplying
1938bb0daffSRob Clark * the area from a failed reserved call.
1948bb0daffSRob Clark */
tcm_free(struct tcm_area * area)1958bb0daffSRob Clark static inline s32 tcm_free(struct tcm_area *area)
1968bb0daffSRob Clark {
1978bb0daffSRob Clark s32 res = 0; /* free succeeds by default */
1988bb0daffSRob Clark
1998bb0daffSRob Clark if (area && area->tcm) {
2008bb0daffSRob Clark res = area->tcm->free(area->tcm, area);
2018bb0daffSRob Clark if (res == 0)
2028bb0daffSRob Clark area->tcm = NULL;
2038bb0daffSRob Clark }
2048bb0daffSRob Clark
2058bb0daffSRob Clark return res;
2068bb0daffSRob Clark }
2078bb0daffSRob Clark
2088bb0daffSRob Clark /*=============================================================================
2098bb0daffSRob Clark HELPER FUNCTION FOR ANY TILER CONTAINER MANAGER
2108bb0daffSRob Clark =============================================================================*/
2118bb0daffSRob Clark
2128bb0daffSRob Clark /**
2138bb0daffSRob Clark * This method slices off the topmost 2D slice from the parent area, and stores
2148bb0daffSRob Clark * it in the 'slice' parameter. The 'parent' parameter will get modified to
2158bb0daffSRob Clark * contain the remaining portion of the area. If the whole parent area can
2168bb0daffSRob Clark * fit in a 2D slice, its tcm pointer is set to NULL to mark that it is no
2178bb0daffSRob Clark * longer a valid area.
2188bb0daffSRob Clark *
2198bb0daffSRob Clark * @param parent Pointer to a VALID parent area that will get modified
2208bb0daffSRob Clark * @param slice Pointer to the slice area that will get modified
2218bb0daffSRob Clark */
tcm_slice(struct tcm_area * parent,struct tcm_area * slice)2228bb0daffSRob Clark static inline void tcm_slice(struct tcm_area *parent, struct tcm_area *slice)
2238bb0daffSRob Clark {
2248bb0daffSRob Clark *slice = *parent;
2258bb0daffSRob Clark
2268bb0daffSRob Clark /* check if we need to slice */
2278bb0daffSRob Clark if (slice->tcm && !slice->is2d &&
2288bb0daffSRob Clark slice->p0.y != slice->p1.y &&
2298bb0daffSRob Clark (slice->p0.x || (slice->p1.x != slice->tcm->width - 1))) {
2308bb0daffSRob Clark /* set end point of slice (start always remains) */
2318bb0daffSRob Clark slice->p1.x = slice->tcm->width - 1;
2328bb0daffSRob Clark slice->p1.y = (slice->p0.x) ? slice->p0.y : slice->p1.y - 1;
2338bb0daffSRob Clark /* adjust remaining area */
2348bb0daffSRob Clark parent->p0.x = 0;
2358bb0daffSRob Clark parent->p0.y = slice->p1.y + 1;
2368bb0daffSRob Clark } else {
2378bb0daffSRob Clark /* mark this as the last slice */
2388bb0daffSRob Clark parent->tcm = NULL;
2398bb0daffSRob Clark }
2408bb0daffSRob Clark }
2418bb0daffSRob Clark
2428bb0daffSRob Clark /* Verify if a tcm area is logically valid */
tcm_area_is_valid(struct tcm_area * area)2438bb0daffSRob Clark static inline bool tcm_area_is_valid(struct tcm_area *area)
2448bb0daffSRob Clark {
2458bb0daffSRob Clark return area && area->tcm &&
2468bb0daffSRob Clark /* coordinate bounds */
2478bb0daffSRob Clark area->p1.x < area->tcm->width &&
2488bb0daffSRob Clark area->p1.y < area->tcm->height &&
2498bb0daffSRob Clark area->p0.y <= area->p1.y &&
2508bb0daffSRob Clark /* 1D coordinate relationship + p0.x check */
2518bb0daffSRob Clark ((!area->is2d &&
2528bb0daffSRob Clark area->p0.x < area->tcm->width &&
2538bb0daffSRob Clark area->p0.x + area->p0.y * area->tcm->width <=
2548bb0daffSRob Clark area->p1.x + area->p1.y * area->tcm->width) ||
2558bb0daffSRob Clark /* 2D coordinate relationship */
2568bb0daffSRob Clark (area->is2d &&
2578bb0daffSRob Clark area->p0.x <= area->p1.x));
2588bb0daffSRob Clark }
2598bb0daffSRob Clark
2608bb0daffSRob Clark /* see if a coordinate is within an area */
__tcm_is_in(struct tcm_pt * p,struct tcm_area * a)2618bb0daffSRob Clark static inline bool __tcm_is_in(struct tcm_pt *p, struct tcm_area *a)
2628bb0daffSRob Clark {
2638bb0daffSRob Clark u16 i;
2648bb0daffSRob Clark
2658bb0daffSRob Clark if (a->is2d) {
2668bb0daffSRob Clark return p->x >= a->p0.x && p->x <= a->p1.x &&
2678bb0daffSRob Clark p->y >= a->p0.y && p->y <= a->p1.y;
2688bb0daffSRob Clark } else {
2698bb0daffSRob Clark i = p->x + p->y * a->tcm->width;
2708bb0daffSRob Clark return i >= a->p0.x + a->p0.y * a->tcm->width &&
2718bb0daffSRob Clark i <= a->p1.x + a->p1.y * a->tcm->width;
2728bb0daffSRob Clark }
2738bb0daffSRob Clark }
2748bb0daffSRob Clark
2758bb0daffSRob Clark /* calculate area width */
__tcm_area_width(struct tcm_area * area)2768bb0daffSRob Clark static inline u16 __tcm_area_width(struct tcm_area *area)
2778bb0daffSRob Clark {
2788bb0daffSRob Clark return area->p1.x - area->p0.x + 1;
2798bb0daffSRob Clark }
2808bb0daffSRob Clark
2818bb0daffSRob Clark /* calculate area height */
__tcm_area_height(struct tcm_area * area)2828bb0daffSRob Clark static inline u16 __tcm_area_height(struct tcm_area *area)
2838bb0daffSRob Clark {
2848bb0daffSRob Clark return area->p1.y - area->p0.y + 1;
2858bb0daffSRob Clark }
2868bb0daffSRob Clark
2878bb0daffSRob Clark /* calculate number of slots in an area */
__tcm_sizeof(struct tcm_area * area)2888bb0daffSRob Clark static inline u16 __tcm_sizeof(struct tcm_area *area)
2898bb0daffSRob Clark {
2908bb0daffSRob Clark return area->is2d ?
2918bb0daffSRob Clark __tcm_area_width(area) * __tcm_area_height(area) :
2928bb0daffSRob Clark (area->p1.x - area->p0.x + 1) + (area->p1.y - area->p0.y) *
2938bb0daffSRob Clark area->tcm->width;
2948bb0daffSRob Clark }
2958bb0daffSRob Clark #define tcm_sizeof(area) __tcm_sizeof(&(area))
2968bb0daffSRob Clark #define tcm_awidth(area) __tcm_area_width(&(area))
2978bb0daffSRob Clark #define tcm_aheight(area) __tcm_area_height(&(area))
2988bb0daffSRob Clark #define tcm_is_in(pt, area) __tcm_is_in(&(pt), &(area))
2998bb0daffSRob Clark
3008bb0daffSRob Clark /* limit a 1D area to the first N pages */
tcm_1d_limit(struct tcm_area * a,u32 num_pg)3018bb0daffSRob Clark static inline s32 tcm_1d_limit(struct tcm_area *a, u32 num_pg)
3028bb0daffSRob Clark {
3038bb0daffSRob Clark if (__tcm_sizeof(a) < num_pg)
3048bb0daffSRob Clark return -ENOMEM;
3058bb0daffSRob Clark if (!num_pg)
3068bb0daffSRob Clark return -EINVAL;
3078bb0daffSRob Clark
3088bb0daffSRob Clark a->p1.x = (a->p0.x + num_pg - 1) % a->tcm->width;
3098bb0daffSRob Clark a->p1.y = a->p0.y + ((a->p0.x + num_pg - 1) / a->tcm->width);
3108bb0daffSRob Clark return 0;
3118bb0daffSRob Clark }
3128bb0daffSRob Clark
3138bb0daffSRob Clark /**
3148bb0daffSRob Clark * Iterate through 2D slices of a valid area. Behaves
3158bb0daffSRob Clark * syntactically as a for(;;) statement.
3168bb0daffSRob Clark *
3178bb0daffSRob Clark * @param var Name of a local variable of type 'struct
3188bb0daffSRob Clark * tcm_area *' that will get modified to
3198bb0daffSRob Clark * contain each slice.
3208bb0daffSRob Clark * @param area Pointer to the VALID parent area. This
3218bb0daffSRob Clark * structure will not get modified
3228bb0daffSRob Clark * throughout the loop.
3238bb0daffSRob Clark *
3248bb0daffSRob Clark */
3258bb0daffSRob Clark #define tcm_for_each_slice(var, area, safe) \
3268bb0daffSRob Clark for (safe = area, \
3278bb0daffSRob Clark tcm_slice(&safe, &var); \
3288bb0daffSRob Clark var.tcm; tcm_slice(&safe, &var))
3298bb0daffSRob Clark
3308bb0daffSRob Clark #endif
331