1 /*
2  * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <drm/drm_util.h>
20 
21 #include "mdp5_kms.h"
22 #include "mdp5_smp.h"
23 
24 
25 struct mdp5_smp {
26 	struct drm_device *dev;
27 
28 	uint8_t reserved[MAX_CLIENTS]; /* fixed MMBs allocation per client */
29 
30 	int blk_cnt;
31 	int blk_size;
32 
33 	/* register cache */
34 	u32 alloc_w[22];
35 	u32 alloc_r[22];
36 	u32 pipe_reqprio_fifo_wm0[SSPP_MAX];
37 	u32 pipe_reqprio_fifo_wm1[SSPP_MAX];
38 	u32 pipe_reqprio_fifo_wm2[SSPP_MAX];
39 };
40 
41 static inline
42 struct mdp5_kms *get_kms(struct mdp5_smp *smp)
43 {
44 	struct msm_drm_private *priv = smp->dev->dev_private;
45 
46 	return to_mdp5_kms(to_mdp_kms(priv->kms));
47 }
48 
49 static inline u32 pipe2client(enum mdp5_pipe pipe, int plane)
50 {
51 #define CID_UNUSED	0
52 
53 	if (WARN_ON(plane >= pipe2nclients(pipe)))
54 		return CID_UNUSED;
55 
56 	/*
57 	 * Note on SMP clients:
58 	 * For ViG pipes, fetch Y/Cr/Cb-components clients are always
59 	 * consecutive, and in that order.
60 	 *
61 	 * e.g.:
62 	 * if mdp5_cfg->smp.clients[SSPP_VIG0] = N,
63 	 *	Y  plane's client ID is N
64 	 *	Cr plane's client ID is N + 1
65 	 *	Cb plane's client ID is N + 2
66 	 */
67 
68 	return mdp5_cfg->smp.clients[pipe] + plane;
69 }
70 
71 /* allocate blocks for the specified request: */
72 static int smp_request_block(struct mdp5_smp *smp,
73 		struct mdp5_smp_state *state,
74 		u32 cid, int nblks)
75 {
76 	void *cs = state->client_state[cid];
77 	int i, avail, cnt = smp->blk_cnt;
78 	uint8_t reserved;
79 
80 	/* we shouldn't be requesting blocks for an in-use client: */
81 	WARN_ON(bitmap_weight(cs, cnt) > 0);
82 
83 	reserved = smp->reserved[cid];
84 
85 	if (reserved) {
86 		nblks = max(0, nblks - reserved);
87 		DBG("%d MMBs allocated (%d reserved)", nblks, reserved);
88 	}
89 
90 	avail = cnt - bitmap_weight(state->state, cnt);
91 	if (nblks > avail) {
92 		DRM_DEV_ERROR(smp->dev->dev, "out of blks (req=%d > avail=%d)\n",
93 				nblks, avail);
94 		return -ENOSPC;
95 	}
96 
97 	for (i = 0; i < nblks; i++) {
98 		int blk = find_first_zero_bit(state->state, cnt);
99 		set_bit(blk, cs);
100 		set_bit(blk, state->state);
101 	}
102 
103 	return 0;
104 }
105 
106 static void set_fifo_thresholds(struct mdp5_smp *smp,
107 		enum mdp5_pipe pipe, int nblks)
108 {
109 	u32 smp_entries_per_blk = smp->blk_size / (128 / BITS_PER_BYTE);
110 	u32 val;
111 
112 	/* 1/4 of SMP pool that is being fetched */
113 	val = (nblks * smp_entries_per_blk) / 4;
114 
115 	smp->pipe_reqprio_fifo_wm0[pipe] = val * 1;
116 	smp->pipe_reqprio_fifo_wm1[pipe] = val * 2;
117 	smp->pipe_reqprio_fifo_wm2[pipe] = val * 3;
118 }
119 
120 /*
121  * NOTE: looks like if horizontal decimation is used (if we supported that)
122  * then the width used to calculate SMP block requirements is the post-
123  * decimated width.  Ie. SMP buffering sits downstream of decimation (which
124  * presumably happens during the dma from scanout buffer).
125  */
126 uint32_t mdp5_smp_calculate(struct mdp5_smp *smp,
127 		const struct mdp_format *format,
128 		u32 width, bool hdecim)
129 {
130 	struct mdp5_kms *mdp5_kms = get_kms(smp);
131 	int rev = mdp5_cfg_get_hw_rev(mdp5_kms->cfg);
132 	int i, hsub, nplanes, nlines;
133 	u32 fmt = format->base.pixel_format;
134 	uint32_t blkcfg = 0;
135 
136 	nplanes = drm_format_num_planes(fmt);
137 	hsub = drm_format_horz_chroma_subsampling(fmt);
138 
139 	/* different if BWC (compressed framebuffer?) enabled: */
140 	nlines = 2;
141 
142 	/* Newer MDPs have split/packing logic, which fetches sub-sampled
143 	 * U and V components (splits them from Y if necessary) and packs
144 	 * them together, writes to SMP using a single client.
145 	 */
146 	if ((rev > 0) && (format->chroma_sample > CHROMA_FULL)) {
147 		fmt = DRM_FORMAT_NV24;
148 		nplanes = 2;
149 
150 		/* if decimation is enabled, HW decimates less on the
151 		 * sub sampled chroma components
152 		 */
153 		if (hdecim && (hsub > 1))
154 			hsub = 1;
155 	}
156 
157 	for (i = 0; i < nplanes; i++) {
158 		int n, fetch_stride, cpp;
159 
160 		cpp = drm_format_plane_cpp(fmt, i);
161 		fetch_stride = width * cpp / (i ? hsub : 1);
162 
163 		n = DIV_ROUND_UP(fetch_stride * nlines, smp->blk_size);
164 
165 		/* for hw rev v1.00 */
166 		if (rev == 0)
167 			n = roundup_pow_of_two(n);
168 
169 		blkcfg |= (n << (8 * i));
170 	}
171 
172 	return blkcfg;
173 }
174 
175 int mdp5_smp_assign(struct mdp5_smp *smp, struct mdp5_smp_state *state,
176 		enum mdp5_pipe pipe, uint32_t blkcfg)
177 {
178 	struct mdp5_kms *mdp5_kms = get_kms(smp);
179 	struct drm_device *dev = mdp5_kms->dev;
180 	int i, ret;
181 
182 	for (i = 0; i < pipe2nclients(pipe); i++) {
183 		u32 cid = pipe2client(pipe, i);
184 		int n = blkcfg & 0xff;
185 
186 		if (!n)
187 			continue;
188 
189 		DBG("%s[%d]: request %d SMP blocks", pipe2name(pipe), i, n);
190 		ret = smp_request_block(smp, state, cid, n);
191 		if (ret) {
192 			DRM_DEV_ERROR(dev->dev, "Cannot allocate %d SMP blocks: %d\n",
193 					n, ret);
194 			return ret;
195 		}
196 
197 		blkcfg >>= 8;
198 	}
199 
200 	state->assigned |= (1 << pipe);
201 
202 	return 0;
203 }
204 
205 /* Release SMP blocks for all clients of the pipe */
206 void mdp5_smp_release(struct mdp5_smp *smp, struct mdp5_smp_state *state,
207 		enum mdp5_pipe pipe)
208 {
209 	int i;
210 	int cnt = smp->blk_cnt;
211 
212 	for (i = 0; i < pipe2nclients(pipe); i++) {
213 		u32 cid = pipe2client(pipe, i);
214 		void *cs = state->client_state[cid];
215 
216 		/* update global state: */
217 		bitmap_andnot(state->state, state->state, cs, cnt);
218 
219 		/* clear client's state */
220 		bitmap_zero(cs, cnt);
221 	}
222 
223 	state->released |= (1 << pipe);
224 }
225 
226 /* NOTE: SMP_ALLOC_* regs are *not* double buffered, so release has to
227  * happen after scanout completes.
228  */
229 static unsigned update_smp_state(struct mdp5_smp *smp,
230 		u32 cid, mdp5_smp_state_t *assigned)
231 {
232 	int cnt = smp->blk_cnt;
233 	unsigned nblks = 0;
234 	u32 blk, val;
235 
236 	for_each_set_bit(blk, *assigned, cnt) {
237 		int idx = blk / 3;
238 		int fld = blk % 3;
239 
240 		val = smp->alloc_w[idx];
241 
242 		switch (fld) {
243 		case 0:
244 			val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT0__MASK;
245 			val |= MDP5_SMP_ALLOC_W_REG_CLIENT0(cid);
246 			break;
247 		case 1:
248 			val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT1__MASK;
249 			val |= MDP5_SMP_ALLOC_W_REG_CLIENT1(cid);
250 			break;
251 		case 2:
252 			val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT2__MASK;
253 			val |= MDP5_SMP_ALLOC_W_REG_CLIENT2(cid);
254 			break;
255 		}
256 
257 		smp->alloc_w[idx] = val;
258 		smp->alloc_r[idx] = val;
259 
260 		nblks++;
261 	}
262 
263 	return nblks;
264 }
265 
266 static void write_smp_alloc_regs(struct mdp5_smp *smp)
267 {
268 	struct mdp5_kms *mdp5_kms = get_kms(smp);
269 	int i, num_regs;
270 
271 	num_regs = smp->blk_cnt / 3 + 1;
272 
273 	for (i = 0; i < num_regs; i++) {
274 		mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_W_REG(i),
275 			   smp->alloc_w[i]);
276 		mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_R_REG(i),
277 			   smp->alloc_r[i]);
278 	}
279 }
280 
281 static void write_smp_fifo_regs(struct mdp5_smp *smp)
282 {
283 	struct mdp5_kms *mdp5_kms = get_kms(smp);
284 	int i;
285 
286 	for (i = 0; i < mdp5_kms->num_hwpipes; i++) {
287 		struct mdp5_hw_pipe *hwpipe = mdp5_kms->hwpipes[i];
288 		enum mdp5_pipe pipe = hwpipe->pipe;
289 
290 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_0(pipe),
291 			   smp->pipe_reqprio_fifo_wm0[pipe]);
292 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_1(pipe),
293 			   smp->pipe_reqprio_fifo_wm1[pipe]);
294 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_2(pipe),
295 			   smp->pipe_reqprio_fifo_wm2[pipe]);
296 	}
297 }
298 
299 void mdp5_smp_prepare_commit(struct mdp5_smp *smp, struct mdp5_smp_state *state)
300 {
301 	enum mdp5_pipe pipe;
302 
303 	for_each_set_bit(pipe, &state->assigned, sizeof(state->assigned) * 8) {
304 		unsigned i, nblks = 0;
305 
306 		for (i = 0; i < pipe2nclients(pipe); i++) {
307 			u32 cid = pipe2client(pipe, i);
308 			void *cs = state->client_state[cid];
309 
310 			nblks += update_smp_state(smp, cid, cs);
311 
312 			DBG("assign %s:%u, %u blks",
313 				pipe2name(pipe), i, nblks);
314 		}
315 
316 		set_fifo_thresholds(smp, pipe, nblks);
317 	}
318 
319 	write_smp_alloc_regs(smp);
320 	write_smp_fifo_regs(smp);
321 
322 	state->assigned = 0;
323 }
324 
325 void mdp5_smp_complete_commit(struct mdp5_smp *smp, struct mdp5_smp_state *state)
326 {
327 	enum mdp5_pipe pipe;
328 
329 	for_each_set_bit(pipe, &state->released, sizeof(state->released) * 8) {
330 		DBG("release %s", pipe2name(pipe));
331 		set_fifo_thresholds(smp, pipe, 0);
332 	}
333 
334 	write_smp_fifo_regs(smp);
335 
336 	state->released = 0;
337 }
338 
339 void mdp5_smp_dump(struct mdp5_smp *smp, struct drm_printer *p)
340 {
341 	struct mdp5_kms *mdp5_kms = get_kms(smp);
342 	struct mdp5_hw_pipe_state *hwpstate;
343 	struct mdp5_smp_state *state;
344 	struct mdp5_global_state *global_state;
345 	int total = 0, i, j;
346 
347 	drm_printf(p, "name\tinuse\tplane\n");
348 	drm_printf(p, "----\t-----\t-----\n");
349 
350 	if (drm_can_sleep())
351 		drm_modeset_lock(&mdp5_kms->glob_state_lock, NULL);
352 
353 	global_state = mdp5_get_existing_global_state(mdp5_kms);
354 
355 	/* grab these *after* we hold the state_lock */
356 	hwpstate = &global_state->hwpipe;
357 	state = &global_state->smp;
358 
359 	for (i = 0; i < mdp5_kms->num_hwpipes; i++) {
360 		struct mdp5_hw_pipe *hwpipe = mdp5_kms->hwpipes[i];
361 		struct drm_plane *plane = hwpstate->hwpipe_to_plane[hwpipe->idx];
362 		enum mdp5_pipe pipe = hwpipe->pipe;
363 		for (j = 0; j < pipe2nclients(pipe); j++) {
364 			u32 cid = pipe2client(pipe, j);
365 			void *cs = state->client_state[cid];
366 			int inuse = bitmap_weight(cs, smp->blk_cnt);
367 
368 			drm_printf(p, "%s:%d\t%d\t%s\n",
369 				pipe2name(pipe), j, inuse,
370 				plane ? plane->name : NULL);
371 
372 			total += inuse;
373 		}
374 	}
375 
376 	drm_printf(p, "TOTAL:\t%d\t(of %d)\n", total, smp->blk_cnt);
377 	drm_printf(p, "AVAIL:\t%d\n", smp->blk_cnt -
378 			bitmap_weight(state->state, smp->blk_cnt));
379 
380 	if (drm_can_sleep())
381 		drm_modeset_unlock(&mdp5_kms->glob_state_lock);
382 }
383 
384 void mdp5_smp_destroy(struct mdp5_smp *smp)
385 {
386 	kfree(smp);
387 }
388 
389 struct mdp5_smp *mdp5_smp_init(struct mdp5_kms *mdp5_kms, const struct mdp5_smp_block *cfg)
390 {
391 	struct mdp5_smp_state *state;
392 	struct mdp5_global_state *global_state;
393 	struct mdp5_smp *smp = NULL;
394 	int ret;
395 
396 	smp = kzalloc(sizeof(*smp), GFP_KERNEL);
397 	if (unlikely(!smp)) {
398 		ret = -ENOMEM;
399 		goto fail;
400 	}
401 
402 	smp->dev = mdp5_kms->dev;
403 	smp->blk_cnt = cfg->mmb_count;
404 	smp->blk_size = cfg->mmb_size;
405 
406 	global_state = mdp5_get_existing_global_state(mdp5_kms);
407 	state = &global_state->smp;
408 
409 	/* statically tied MMBs cannot be re-allocated: */
410 	bitmap_copy(state->state, cfg->reserved_state, smp->blk_cnt);
411 	memcpy(smp->reserved, cfg->reserved, sizeof(smp->reserved));
412 
413 	return smp;
414 fail:
415 	if (smp)
416 		mdp5_smp_destroy(smp);
417 
418 	return ERR_PTR(ret);
419 }
420