2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
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.
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
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/>.
25 struct drm_device *dev;
27 uint8_t reserved[MAX_CLIENTS]; /* fixed MMBs allocation per client */
35 u32 pipe_reqprio_fifo_wm0[SSPP_MAX];
36 u32 pipe_reqprio_fifo_wm1[SSPP_MAX];
37 u32 pipe_reqprio_fifo_wm2[SSPP_MAX];
41 struct mdp5_kms *get_kms(struct mdp5_smp *smp)
43 struct msm_drm_private *priv = smp->dev->dev_private;
45 return to_mdp5_kms(to_mdp_kms(priv->kms));
48 static inline u32 pipe2client(enum mdp5_pipe pipe, int plane)
52 if (WARN_ON(plane >= pipe2nclients(pipe)))
56 * Note on SMP clients:
57 * For ViG pipes, fetch Y/Cr/Cb-components clients are always
58 * consecutive, and in that order.
61 * if mdp5_cfg->smp.clients[SSPP_VIG0] = N,
62 * Y plane's client ID is N
63 * Cr plane's client ID is N + 1
64 * Cb plane's client ID is N + 2
67 return mdp5_cfg->smp.clients[pipe] + plane;
70 /* allocate blocks for the specified request: */
71 static int smp_request_block(struct mdp5_smp *smp,
72 struct mdp5_smp_state *state,
75 void *cs = state->client_state[cid];
76 int i, avail, cnt = smp->blk_cnt;
79 /* we shouldn't be requesting blocks for an in-use client: */
80 WARN_ON(bitmap_weight(cs, cnt) > 0);
82 reserved = smp->reserved[cid];
85 nblks = max(0, nblks - reserved);
86 DBG("%d MMBs allocated (%d reserved)", nblks, reserved);
89 avail = cnt - bitmap_weight(state->state, cnt);
91 dev_err(smp->dev->dev, "out of blks (req=%d > avail=%d)\n",
96 for (i = 0; i < nblks; i++) {
97 int blk = find_first_zero_bit(state->state, cnt);
99 set_bit(blk, state->state);
105 static void set_fifo_thresholds(struct mdp5_smp *smp,
106 enum mdp5_pipe pipe, int nblks)
108 u32 smp_entries_per_blk = smp->blk_size / (128 / BITS_PER_BYTE);
111 /* 1/4 of SMP pool that is being fetched */
112 val = (nblks * smp_entries_per_blk) / 4;
114 smp->pipe_reqprio_fifo_wm0[pipe] = val * 1;
115 smp->pipe_reqprio_fifo_wm1[pipe] = val * 2;
116 smp->pipe_reqprio_fifo_wm2[pipe] = val * 3;
120 * NOTE: looks like if horizontal decimation is used (if we supported that)
121 * then the width used to calculate SMP block requirements is the post-
122 * decimated width. Ie. SMP buffering sits downstream of decimation (which
123 * presumably happens during the dma from scanout buffer).
125 uint32_t mdp5_smp_calculate(struct mdp5_smp *smp,
126 const struct mdp_format *format,
127 u32 width, bool hdecim)
129 struct mdp5_kms *mdp5_kms = get_kms(smp);
130 int rev = mdp5_cfg_get_hw_rev(mdp5_kms->cfg);
131 int i, hsub, nplanes, nlines;
132 u32 fmt = format->base.pixel_format;
135 nplanes = drm_format_num_planes(fmt);
136 hsub = drm_format_horz_chroma_subsampling(fmt);
138 /* different if BWC (compressed framebuffer?) enabled: */
141 /* Newer MDPs have split/packing logic, which fetches sub-sampled
142 * U and V components (splits them from Y if necessary) and packs
143 * them together, writes to SMP using a single client.
145 if ((rev > 0) && (format->chroma_sample > CHROMA_FULL)) {
146 fmt = DRM_FORMAT_NV24;
149 /* if decimation is enabled, HW decimates less on the
150 * sub sampled chroma components
152 if (hdecim && (hsub > 1))
156 for (i = 0; i < nplanes; i++) {
157 int n, fetch_stride, cpp;
159 cpp = drm_format_plane_cpp(fmt, i);
160 fetch_stride = width * cpp / (i ? hsub : 1);
162 n = DIV_ROUND_UP(fetch_stride * nlines, smp->blk_size);
164 /* for hw rev v1.00 */
166 n = roundup_pow_of_two(n);
168 blkcfg |= (n << (8 * i));
174 int mdp5_smp_assign(struct mdp5_smp *smp, struct mdp5_smp_state *state,
175 enum mdp5_pipe pipe, uint32_t blkcfg)
177 struct mdp5_kms *mdp5_kms = get_kms(smp);
178 struct drm_device *dev = mdp5_kms->dev;
181 for (i = 0; i < pipe2nclients(pipe); i++) {
182 u32 cid = pipe2client(pipe, i);
183 int n = blkcfg & 0xff;
188 DBG("%s[%d]: request %d SMP blocks", pipe2name(pipe), i, n);
189 ret = smp_request_block(smp, state, cid, n);
191 dev_err(dev->dev, "Cannot allocate %d SMP blocks: %d\n",
199 state->assigned |= (1 << pipe);
204 /* Release SMP blocks for all clients of the pipe */
205 void mdp5_smp_release(struct mdp5_smp *smp, struct mdp5_smp_state *state,
209 int cnt = smp->blk_cnt;
211 for (i = 0; i < pipe2nclients(pipe); i++) {
212 u32 cid = pipe2client(pipe, i);
213 void *cs = state->client_state[cid];
215 /* update global state: */
216 bitmap_andnot(state->state, state->state, cs, cnt);
218 /* clear client's state */
219 bitmap_zero(cs, cnt);
222 state->released |= (1 << pipe);
225 /* NOTE: SMP_ALLOC_* regs are *not* double buffered, so release has to
226 * happen after scanout completes.
228 static unsigned update_smp_state(struct mdp5_smp *smp,
229 u32 cid, mdp5_smp_state_t *assigned)
231 int cnt = smp->blk_cnt;
235 for_each_set_bit(blk, *assigned, cnt) {
239 val = smp->alloc_w[idx];
243 val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT0__MASK;
244 val |= MDP5_SMP_ALLOC_W_REG_CLIENT0(cid);
247 val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT1__MASK;
248 val |= MDP5_SMP_ALLOC_W_REG_CLIENT1(cid);
251 val &= ~MDP5_SMP_ALLOC_W_REG_CLIENT2__MASK;
252 val |= MDP5_SMP_ALLOC_W_REG_CLIENT2(cid);
256 smp->alloc_w[idx] = val;
257 smp->alloc_r[idx] = val;
265 static void write_smp_alloc_regs(struct mdp5_smp *smp)
267 struct mdp5_kms *mdp5_kms = get_kms(smp);
270 num_regs = smp->blk_cnt / 3 + 1;
272 for (i = 0; i < num_regs; i++) {
273 mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_W_REG(i),
275 mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_R_REG(i),
280 static void write_smp_fifo_regs(struct mdp5_smp *smp)
282 struct mdp5_kms *mdp5_kms = get_kms(smp);
285 for (i = 0; i < mdp5_kms->num_hwpipes; i++) {
286 struct mdp5_hw_pipe *hwpipe = mdp5_kms->hwpipes[i];
287 enum mdp5_pipe pipe = hwpipe->pipe;
289 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_0(pipe),
290 smp->pipe_reqprio_fifo_wm0[pipe]);
291 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_1(pipe),
292 smp->pipe_reqprio_fifo_wm1[pipe]);
293 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_2(pipe),
294 smp->pipe_reqprio_fifo_wm2[pipe]);
298 void mdp5_smp_prepare_commit(struct mdp5_smp *smp, struct mdp5_smp_state *state)
302 for_each_set_bit(pipe, &state->assigned, sizeof(state->assigned) * 8) {
303 unsigned i, nblks = 0;
305 for (i = 0; i < pipe2nclients(pipe); i++) {
306 u32 cid = pipe2client(pipe, i);
307 void *cs = state->client_state[cid];
309 nblks += update_smp_state(smp, cid, cs);
311 DBG("assign %s:%u, %u blks",
312 pipe2name(pipe), i, nblks);
315 set_fifo_thresholds(smp, pipe, nblks);
318 write_smp_alloc_regs(smp);
319 write_smp_fifo_regs(smp);
324 void mdp5_smp_complete_commit(struct mdp5_smp *smp, struct mdp5_smp_state *state)
328 for_each_set_bit(pipe, &state->released, sizeof(state->released) * 8) {
329 DBG("release %s", pipe2name(pipe));
330 set_fifo_thresholds(smp, pipe, 0);
333 write_smp_fifo_regs(smp);
338 void mdp5_smp_dump(struct mdp5_smp *smp, struct drm_printer *p)
340 struct mdp5_kms *mdp5_kms = get_kms(smp);
341 struct mdp5_hw_pipe_state *hwpstate;
342 struct mdp5_smp_state *state;
345 drm_printf(p, "name\tinuse\tplane\n");
346 drm_printf(p, "----\t-----\t-----\n");
349 drm_modeset_lock(&mdp5_kms->state_lock, NULL);
351 /* grab these *after* we hold the state_lock */
352 hwpstate = &mdp5_kms->state->hwpipe;
353 state = &mdp5_kms->state->smp;
355 for (i = 0; i < mdp5_kms->num_hwpipes; i++) {
356 struct mdp5_hw_pipe *hwpipe = mdp5_kms->hwpipes[i];
357 struct drm_plane *plane = hwpstate->hwpipe_to_plane[hwpipe->idx];
358 enum mdp5_pipe pipe = hwpipe->pipe;
359 for (j = 0; j < pipe2nclients(pipe); j++) {
360 u32 cid = pipe2client(pipe, j);
361 void *cs = state->client_state[cid];
362 int inuse = bitmap_weight(cs, smp->blk_cnt);
364 drm_printf(p, "%s:%d\t%d\t%s\n",
365 pipe2name(pipe), j, inuse,
366 plane ? plane->name : NULL);
372 drm_printf(p, "TOTAL:\t%d\t(of %d)\n", total, smp->blk_cnt);
373 drm_printf(p, "AVAIL:\t%d\n", smp->blk_cnt -
374 bitmap_weight(state->state, smp->blk_cnt));
377 drm_modeset_unlock(&mdp5_kms->state_lock);
380 void mdp5_smp_destroy(struct mdp5_smp *smp)
385 struct mdp5_smp *mdp5_smp_init(struct mdp5_kms *mdp5_kms, const struct mdp5_smp_block *cfg)
387 struct mdp5_smp_state *state = &mdp5_kms->state->smp;
388 struct mdp5_smp *smp = NULL;
391 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
392 if (unlikely(!smp)) {
397 smp->dev = mdp5_kms->dev;
398 smp->blk_cnt = cfg->mmb_count;
399 smp->blk_size = cfg->mmb_size;
401 /* statically tied MMBs cannot be re-allocated: */
402 bitmap_copy(state->state, cfg->reserved_state, smp->blk_cnt);
403 memcpy(smp->reserved, cfg->reserved, sizeof(smp->reserved));
408 mdp5_smp_destroy(smp);