summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/omapdrm/tcm.h
blob: ef7df7d6fc84c7faa2b92b94e93be1853a8c4c55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * tcm.h
 *
 * TILER container manager specification and support functions for TI
 * TILER driver.
 *
 * Author: Lajos Molnar <molnar@ti.com>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Texas Instruments Incorporated nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef TCM_H
#define TCM_H

struct tcm;

/* point */
struct tcm_pt {
	u16 x;
	u16 y;
};

/* 1d or 2d area */
struct tcm_area {
	bool is2d;		/* whether area is 1d or 2d */
	struct tcm    *tcm;	/* parent */
	struct tcm_pt  p0;
	struct tcm_pt  p1;
};

struct tcm {
	u16 width, height;	/* container dimensions */
	int lut_id;		/* Lookup table identifier */

	unsigned int y_offset;	/* offset to use for y coordinates */

	spinlock_t lock;
	unsigned long *bitmap;
	size_t map_size;

	/* function table */
	s32 (*reserve_2d)(struct tcm *tcm, u16 height, u16 width, u16 align,
			  int16_t offset, uint16_t slot_bytes,
			  struct tcm_area *area);
	s32 (*reserve_1d)(struct tcm *tcm, u32 slots, struct tcm_area *area);
	s32 (*free)(struct tcm *tcm, struct tcm_area *area);
	void (*deinit)(struct tcm *tcm);
};

/*=============================================================================
    BASIC TILER CONTAINER MANAGER INTERFACE
=============================================================================*/

/*
 * NOTE:
 *
 * Since some basic parameter checking is done outside the TCM algorithms,
 * TCM implementation do NOT have to check the following:
 *
 *   area pointer is NULL
 *   width and height fits within container
 *   number of pages is more than the size of the container
 *
 */

struct tcm *sita_init(u16 width, u16 height);


/**
 * Deinitialize tiler container manager.
 *
 * @param tcm	Pointer to container manager.
 *
 * @return 0 on success, non-0 error value on error.  The call
 *	   should free as much memory as possible and meaningful
 *	   even on failure.  Some error codes: -ENODEV: invalid
 *	   manager.
 */
static inline void tcm_deinit(struct tcm *tcm)
{
	if (tcm)
		tcm->deinit(tcm);
}

/**
 * Reserves a 2D area in the container.
 *
 * @param tcm		Pointer to container manager.
 * @param height	Height(in pages) of area to be reserved.
 * @param width		Width(in pages) of area to be reserved.
 * @param align		Alignment requirement for top-left corner of area. Not
 *			all values may be supported by the container manager,
 *			but it must support 0 (1), 32 and 64.
 *			0 value is equivalent to 1.
 * @param offset	Offset requirement, in bytes.  This is the offset
 *			from a 4KiB aligned virtual address.
 * @param slot_bytes	Width of slot in bytes
 * @param area		Pointer to where the reserved area should be stored.
 *
 * @return 0 on success.  Non-0 error code on failure.  Also,
 *	   the tcm field of the area will be set to NULL on
 *	   failure.  Some error codes: -ENODEV: invalid manager,
 *	   -EINVAL: invalid area, -ENOMEM: not enough space for
 *	    allocation.
 */
static inline s32 tcm_reserve_2d(struct tcm *tcm, u16 width, u16 height,
				u16 align, int16_t offset, uint16_t slot_bytes,
				struct tcm_area *area)
{
	/* perform rudimentary error checking */
	s32 res = tcm  == NULL ? -ENODEV :
		(area == NULL || width == 0 || height == 0 ||
		 /* align must be a 2 power */
		 (align & (align - 1))) ? -EINVAL :
		(height > tcm->height || width > tcm->width) ? -ENOMEM : 0;

	if (!res) {
		area->is2d = true;
		res = tcm->reserve_2d(tcm, height, width, align, offset,
					slot_bytes, area);
		area->tcm = res ? NULL : tcm;
	}

	return res;
}

/**
 * Reserves a 1D area in the container.
 *
 * @param tcm		Pointer to container manager.
 * @param slots		Number of (contiguous) slots to reserve.
 * @param area		Pointer to where the reserved area should be stored.
 *
 * @return 0 on success.  Non-0 error code on failure.  Also,
 *	   the tcm field of the area will be set to NULL on
 *	   failure.  Some error codes: -ENODEV: invalid manager,
 *	   -EINVAL: invalid area, -ENOMEM: not enough space for
 *	    allocation.
 */
static inline s32 tcm_reserve_1d(struct tcm *tcm, u32 slots,
				 struct tcm_area *area)
{
	/* perform rudimentary error checking */
	s32 res = tcm  == NULL ? -ENODEV :
		(area == NULL || slots == 0) ? -EINVAL :
		slots > (tcm->width * (u32) tcm->height) ? -ENOMEM : 0;

	if (!res) {
		area->is2d = false;
		res = tcm->reserve_1d(tcm, slots, area);
		area->tcm = res ? NULL : tcm;
	}

	return res;
}

/**
 * Free a previously reserved area from the container.
 *
 * @param area	Pointer to area reserved by a prior call to
 *		tcm_reserve_1d or tcm_reserve_2d call, whether
 *		it was successful or not. (Note: all fields of
 *		the structure must match.)
 *
 * @return 0 on success.  Non-0 error code on failure.  Also, the tcm
 *	   field of the area is set to NULL on success to avoid subsequent
 *	   freeing.  This call will succeed even if supplying
 *	   the area from a failed reserved call.
 */
static inline s32 tcm_free(struct tcm_area *area)
{
	s32 res = 0; /* free succeeds by default */

	if (area && area->tcm) {
		res = area->tcm->free(area->tcm, area);
		if (res == 0)
			area->tcm = NULL;
	}

	return res;
}

/*=============================================================================
    HELPER FUNCTION FOR ANY TILER CONTAINER MANAGER
=============================================================================*/

/**
 * This method slices off the topmost 2D slice from the parent area, and stores
 * it in the 'slice' parameter.  The 'parent' parameter will get modified to
 * contain the remaining portion of the area.  If the whole parent area can
 * fit in a 2D slice, its tcm pointer is set to NULL to mark that it is no
 * longer a valid area.
 *
 * @param parent	Pointer to a VALID parent area that will get modified
 * @param slice		Pointer to the slice area that will get modified
 */
static inline void tcm_slice(struct tcm_area *parent, struct tcm_area *slice)
{
	*slice = *parent;

	/* check if we need to slice */
	if (slice->tcm && !slice->is2d &&
		slice->p0.y != slice->p1.y &&
		(slice->p0.x || (slice->p1.x != slice->tcm->width - 1))) {
		/* set end point of slice (start always remains) */
		slice->p1.x = slice->tcm->width - 1;
		slice->p1.y = (slice->p0.x) ? slice->p0.y : slice->p1.y - 1;
		/* adjust remaining area */
		parent->p0.x = 0;
		parent->p0.y = slice->p1.y + 1;
	} else {
		/* mark this as the last slice */
		parent->tcm = NULL;
	}
}

/* Verify if a tcm area is logically valid */
static inline bool tcm_area_is_valid(struct tcm_area *area)
{
	return area && area->tcm &&
		/* coordinate bounds */
		area->p1.x < area->tcm->width &&
		area->p1.y < area->tcm->height &&
		area->p0.y <= area->p1.y &&
		/* 1D coordinate relationship + p0.x check */
		((!area->is2d &&
		  area->p0.x < area->tcm->width &&
		  area->p0.x + area->p0.y * area->tcm->width <=
		  area->p1.x + area->p1.y * area->tcm->width) ||
		 /* 2D coordinate relationship */
		 (area->is2d &&
		  area->p0.x <= area->p1.x));
}

/* see if a coordinate is within an area */
static inline bool __tcm_is_in(struct tcm_pt *p, struct tcm_area *a)
{
	u16 i;

	if (a->is2d) {
		return p->x >= a->p0.x && p->x <= a->p1.x &&
		       p->y >= a->p0.y && p->y <= a->p1.y;
	} else {
		i = p->x + p->y * a->tcm->width;
		return i >= a->p0.x + a->p0.y * a->tcm->width &&
		       i <= a->p1.x + a->p1.y * a->tcm->width;
	}
}

/* calculate area width */
static inline u16 __tcm_area_width(struct tcm_area *area)
{
	return area->p1.x - area->p0.x + 1;
}

/* calculate area height */
static inline u16 __tcm_area_height(struct tcm_area *area)
{
	return area->p1.y - area->p0.y + 1;
}

/* calculate number of slots in an area */
static inline u16 __tcm_sizeof(struct tcm_area *area)
{
	return area->is2d ?
		__tcm_area_width(area) * __tcm_area_height(area) :
		(area->p1.x - area->p0.x + 1) + (area->p1.y - area->p0.y) *
							area->tcm->width;
}
#define tcm_sizeof(area) __tcm_sizeof(&(area))
#define tcm_awidth(area) __tcm_area_width(&(area))
#define tcm_aheight(area) __tcm_area_height(&(area))
#define tcm_is_in(pt, area) __tcm_is_in(&(pt), &(area))

/* limit a 1D area to the first N pages */
static inline s32 tcm_1d_limit(struct tcm_area *a, u32 num_pg)
{
	if (__tcm_sizeof(a) < num_pg)
		return -ENOMEM;
	if (!num_pg)
		return -EINVAL;

	a->p1.x = (a->p0.x + num_pg - 1) % a->tcm->width;
	a->p1.y = a->p0.y + ((a->p0.x + num_pg - 1) / a->tcm->width);
	return 0;
}

/**
 * Iterate through 2D slices of a valid area. Behaves
 * syntactically as a for(;;) statement.
 *
 * @param var		Name of a local variable of type 'struct
 *			tcm_area *' that will get modified to
 *			contain each slice.
 * @param area		Pointer to the VALID parent area. This
 *			structure will not get modified
 *			throughout the loop.
 *
 */
#define tcm_for_each_slice(var, area, safe) \
	for (safe = area, \
	     tcm_slice(&safe, &var); \
	     var.tcm; tcm_slice(&safe, &var))

#endif
OpenPOWER on IntegriCloud