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