1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
8 #include <drm/drm_fourcc.h>
9 #include <drm/drm_util.h>
16 struct drm_device *dev;
18 uint8_t reserved[MAX_CLIENTS]; /* fixed MMBs allocation per client */
26 u32 pipe_reqprio_fifo_wm0[SSPP_MAX];
27 u32 pipe_reqprio_fifo_wm1[SSPP_MAX];
28 u32 pipe_reqprio_fifo_wm2[SSPP_MAX];
32 struct mdp5_kms *get_kms(struct mdp5_smp *smp)
34 struct msm_drm_private *priv = smp->dev->dev_private;
36 return to_mdp5_kms(to_mdp_kms(priv->kms));
39 static inline u32 pipe2client(enum mdp5_pipe pipe, int plane)
43 if (WARN_ON(plane >= pipe2nclients(pipe)))
47 * Note on SMP clients:
48 * For ViG pipes, fetch Y/Cr/Cb-components clients are always
49 * consecutive, and in that order.
52 * if mdp5_cfg->smp.clients[SSPP_VIG0] = N,
53 * Y plane's client ID is N
54 * Cr plane's client ID is N + 1
55 * Cb plane's client ID is N + 2
58 return mdp5_cfg->smp.clients[pipe] + plane;
61 /* allocate blocks for the specified request: */
62 static int smp_request_block(struct mdp5_smp *smp,
63 struct mdp5_smp_state *state,
66 void *cs = state->client_state[cid];
67 int i, avail, cnt = smp->blk_cnt;
70 /* we shouldn't be requesting blocks for an in-use client: */
71 WARN_ON(bitmap_weight(cs, cnt) > 0);
73 reserved = smp->reserved[cid];
76 nblks = max(0, nblks - reserved);
77 DBG("%d MMBs allocated (%d reserved)", nblks, reserved);
80 avail = cnt - bitmap_weight(state->state, cnt);
82 DRM_DEV_ERROR(smp->dev->dev, "out of blks (req=%d > avail=%d)\n",
87 for (i = 0; i < nblks; i++) {
88 int blk = find_first_zero_bit(state->state, cnt);
90 set_bit(blk, state->state);
96 static void set_fifo_thresholds(struct mdp5_smp *smp,
97 enum mdp5_pipe pipe, int nblks)
99 u32 smp_entries_per_blk = smp->blk_size / (128 / BITS_PER_BYTE);
102 /* 1/4 of SMP pool that is being fetched */
103 val = (nblks * smp_entries_per_blk) / 4;
105 smp->pipe_reqprio_fifo_wm0[pipe] = val * 1;
106 smp->pipe_reqprio_fifo_wm1[pipe] = val * 2;
107 smp->pipe_reqprio_fifo_wm2[pipe] = val * 3;
111 * NOTE: looks like if horizontal decimation is used (if we supported that)
112 * then the width used to calculate SMP block requirements is the post-
113 * decimated width. Ie. SMP buffering sits downstream of decimation (which
114 * presumably happens during the dma from scanout buffer).
116 uint32_t mdp5_smp_calculate(struct mdp5_smp *smp,
117 const struct mdp_format *format,
118 u32 width, bool hdecim)
120 const struct drm_format_info *info = drm_format_info(format->base.pixel_format);
121 struct mdp5_kms *mdp5_kms = get_kms(smp);
122 int rev = mdp5_cfg_get_hw_rev(mdp5_kms->cfg);
123 int i, hsub, nplanes, nlines;
126 nplanes = info->num_planes;
129 /* different if BWC (compressed framebuffer?) enabled: */
132 /* Newer MDPs have split/packing logic, which fetches sub-sampled
133 * U and V components (splits them from Y if necessary) and packs
134 * them together, writes to SMP using a single client.
136 if ((rev > 0) && (format->chroma_sample > CHROMA_FULL)) {
139 /* if decimation is enabled, HW decimates less on the
140 * sub sampled chroma components
142 if (hdecim && (hsub > 1))
146 for (i = 0; i < nplanes; i++) {
147 int n, fetch_stride, cpp;
150 fetch_stride = width * cpp / (i ? hsub : 1);
152 n = DIV_ROUND_UP(fetch_stride * nlines, smp->blk_size);
154 /* for hw rev v1.00 */
156 n = roundup_pow_of_two(n);
158 blkcfg |= (n << (8 * i));
164 int mdp5_smp_assign(struct mdp5_smp *smp, struct mdp5_smp_state *state,
165 enum mdp5_pipe pipe, uint32_t blkcfg)
167 struct mdp5_kms *mdp5_kms = get_kms(smp);
168 struct drm_device *dev = mdp5_kms->dev;
171 for (i = 0; i < pipe2nclients(pipe); i++) {
172 u32 cid = pipe2client(pipe, i);
173 int n = blkcfg & 0xff;
178 DBG("%s[%d]: request %d SMP blocks", pipe2name(pipe), i, n);
179 ret = smp_request_block(smp, state, cid, n);
181 DRM_DEV_ERROR(dev->dev, "Cannot allocate %d SMP blocks: %d\n",
189 state->assigned |= (1 << pipe);
194 /* Release SMP blocks for all clients of the pipe */
195 void mdp5_smp_release(struct mdp5_smp *smp, struct mdp5_smp_state *state,
199 int cnt = smp->blk_cnt;
201 for (i = 0; i < pipe2nclients(pipe); i++) {
202 u32 cid = pipe2client(pipe, i);
203 void *cs = state->client_state[cid];
205 /* update global state: */
206 bitmap_andnot(state->state, state->state, cs, cnt);
208 /* clear client's state */
209 bitmap_zero(cs, cnt);
212 state->released |= (1 << pipe);
215 /* NOTE: SMP_ALLOC_* regs are *not* double buffered, so release has to
216 * happen after scanout completes.
218 static unsigned update_smp_state(struct mdp5_smp *smp,
219 u32 cid, mdp5_smp_state_t *assigned)
221 int cnt = smp->blk_cnt;
225 for_each_set_bit(blk, *assigned, cnt) {
229 val = smp->alloc_w[idx];
233 val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT0__MASK;
234 val |= MDP5_SMP_ALLOC_W_REG_CLIENT0(cid);
237 val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT1__MASK;
238 val |= MDP5_SMP_ALLOC_W_REG_CLIENT1(cid);
241 val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT2__MASK;
242 val |= MDP5_SMP_ALLOC_W_REG_CLIENT2(cid);
246 smp->alloc_w[idx] = val;
247 smp->alloc_r[idx] = val;
255 static void write_smp_alloc_regs(struct mdp5_smp *smp)
257 struct mdp5_kms *mdp5_kms = get_kms(smp);
260 num_regs = smp->blk_cnt / 3 + 1;
262 for (i = 0; i < num_regs; i++) {
263 mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_W_REG(i),
265 mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_R_REG(i),
270 static void write_smp_fifo_regs(struct mdp5_smp *smp)
272 struct mdp5_kms *mdp5_kms = get_kms(smp);
275 for (i = 0; i < mdp5_kms->num_hwpipes; i++) {
276 struct mdp5_hw_pipe *hwpipe = mdp5_kms->hwpipes[i];
277 enum mdp5_pipe pipe = hwpipe->pipe;
279 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_0(pipe),
280 smp->pipe_reqprio_fifo_wm0[pipe]);
281 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_1(pipe),
282 smp->pipe_reqprio_fifo_wm1[pipe]);
283 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_2(pipe),
284 smp->pipe_reqprio_fifo_wm2[pipe]);
288 void mdp5_smp_prepare_commit(struct mdp5_smp *smp, struct mdp5_smp_state *state)
292 for_each_set_bit(pipe, &state->assigned, sizeof(state->assigned) * 8) {
293 unsigned i, nblks = 0;
295 for (i = 0; i < pipe2nclients(pipe); i++) {
296 u32 cid = pipe2client(pipe, i);
297 void *cs = state->client_state[cid];
299 nblks += update_smp_state(smp, cid, cs);
301 DBG("assign %s:%u, %u blks",
302 pipe2name(pipe), i, nblks);
305 set_fifo_thresholds(smp, pipe, nblks);
308 write_smp_alloc_regs(smp);
309 write_smp_fifo_regs(smp);
314 void mdp5_smp_complete_commit(struct mdp5_smp *smp, struct mdp5_smp_state *state)
318 for_each_set_bit(pipe, &state->released, sizeof(state->released) * 8) {
319 DBG("release %s", pipe2name(pipe));
320 set_fifo_thresholds(smp, pipe, 0);
323 write_smp_fifo_regs(smp);
328 void mdp5_smp_dump(struct mdp5_smp *smp, struct drm_printer *p)
330 struct mdp5_kms *mdp5_kms = get_kms(smp);
331 struct mdp5_hw_pipe_state *hwpstate;
332 struct mdp5_smp_state *state;
333 struct mdp5_global_state *global_state;
336 drm_printf(p, "name\tinuse\tplane\n");
337 drm_printf(p, "----\t-----\t-----\n");
340 drm_modeset_lock(&mdp5_kms->glob_state_lock, NULL);
342 global_state = mdp5_get_existing_global_state(mdp5_kms);
344 /* grab these *after* we hold the state_lock */
345 hwpstate = &global_state->hwpipe;
346 state = &global_state->smp;
348 for (i = 0; i < mdp5_kms->num_hwpipes; i++) {
349 struct mdp5_hw_pipe *hwpipe = mdp5_kms->hwpipes[i];
350 struct drm_plane *plane = hwpstate->hwpipe_to_plane[hwpipe->idx];
351 enum mdp5_pipe pipe = hwpipe->pipe;
352 for (j = 0; j < pipe2nclients(pipe); j++) {
353 u32 cid = pipe2client(pipe, j);
354 void *cs = state->client_state[cid];
355 int inuse = bitmap_weight(cs, smp->blk_cnt);
357 drm_printf(p, "%s:%d\t%d\t%s\n",
358 pipe2name(pipe), j, inuse,
359 plane ? plane->name : NULL);
365 drm_printf(p, "TOTAL:\t%d\t(of %d)\n", total, smp->blk_cnt);
366 drm_printf(p, "AVAIL:\t%d\n", smp->blk_cnt -
367 bitmap_weight(state->state, smp->blk_cnt));
370 drm_modeset_unlock(&mdp5_kms->glob_state_lock);
373 void mdp5_smp_destroy(struct mdp5_smp *smp)
378 struct mdp5_smp *mdp5_smp_init(struct mdp5_kms *mdp5_kms, const struct mdp5_smp_block *cfg)
380 struct mdp5_smp_state *state;
381 struct mdp5_global_state *global_state;
382 struct mdp5_smp *smp = NULL;
385 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
386 if (unlikely(!smp)) {
391 smp->dev = mdp5_kms->dev;
392 smp->blk_cnt = cfg->mmb_count;
393 smp->blk_size = cfg->mmb_size;
395 global_state = mdp5_get_existing_global_state(mdp5_kms);
396 state = &global_state->smp;
398 /* statically tied MMBs cannot be re-allocated: */
399 bitmap_copy(state->state, cfg->reserved_state, smp->blk_cnt);
400 memcpy(smp->reserved, cfg->reserved, sizeof(smp->reserved));
405 mdp5_smp_destroy(smp);