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/>.
24 /* SMP - Shared Memory Pool
26 * These are shared between all the clients, where each plane in a
27 * scanout buffer is a SMP client. Ie. scanout of 3 plane I420 on
28 * pipe VIG0 => 3 clients: VIG0_Y, VIG0_CB, VIG0_CR.
30 * Based on the size of the attached scanout buffer, a certain # of
31 * blocks must be allocated to that client out of the shared pool.
33 * In some hw, some blocks are statically allocated for certain pipes
34 * and CANNOT be re-allocated (eg: MMB0 and MMB1 both tied to RGB0).
36 * For each block that can be dynamically allocated, it can be either
37 * free, or pending/in-use by a client. The updates happen in three steps:
39 * 1) mdp5_smp_request():
40 * When plane scanout is setup, calculate required number of
41 * blocks needed per client, and request. Blocks not inuse or
42 * pending by any other client are added to client's pending
45 * 2) mdp5_smp_configure():
46 * As hw is programmed, before FLUSH, MDP5_SMP_ALLOC registers
47 * are configured for the union(pending, inuse)
49 * 3) mdp5_smp_commit():
50 * After next vblank, copy pending -> inuse. Optionally update
51 * MDP5_SMP_ALLOC registers if there are newly unused blocks
53 * On the next vblank after changes have been committed to hw, the
54 * client's pending blocks become it's in-use blocks (and no-longer
55 * in-use blocks become available to other clients).
57 * btw, hurray for confusing overloaded acronyms! :-/
59 * NOTE: for atomic modeset/pageflip NONBLOCK operations, step #1
60 * should happen at (or before)? atomic->check(). And we'd need
61 * an API to discard previous requests if update is aborted or
64 * TODO would perhaps be nice to have debugfs to dump out kernel
65 * inuse and pending state of all clients..
69 struct drm_device *dev;
74 spinlock_t state_lock;
75 mdp5_smp_state_t state; /* to track smp allocation amongst pipes: */
77 struct mdp5_client_smp_state client_state[CID_MAX];
81 struct mdp5_kms *get_kms(struct mdp5_smp *smp)
83 struct msm_drm_private *priv = smp->dev->dev_private;
85 return to_mdp5_kms(to_mdp_kms(priv->kms));
88 static inline enum mdp5_client_id pipe2client(enum mdp5_pipe pipe, int plane)
90 WARN_ON(plane >= pipe2nclients(pipe));
92 case SSPP_VIG0: return CID_VIG0_Y + plane;
93 case SSPP_VIG1: return CID_VIG1_Y + plane;
94 case SSPP_VIG2: return CID_VIG2_Y + plane;
95 case SSPP_RGB0: return CID_RGB0;
96 case SSPP_RGB1: return CID_RGB1;
97 case SSPP_RGB2: return CID_RGB2;
98 case SSPP_DMA0: return CID_DMA0_Y + plane;
99 case SSPP_DMA1: return CID_DMA1_Y + plane;
100 case SSPP_VIG3: return CID_VIG3_Y + plane;
101 case SSPP_RGB3: return CID_RGB3;
102 default: return CID_UNUSED;
106 /* step #1: update # of blocks pending for the client: */
107 static int smp_request_block(struct mdp5_smp *smp,
108 enum mdp5_client_id cid, int nblks)
110 struct mdp5_kms *mdp5_kms = get_kms(smp);
111 const struct mdp5_cfg_hw *hw_cfg;
112 struct mdp5_client_smp_state *ps = &smp->client_state[cid];
113 int i, ret, avail, cur_nblks, cnt = smp->blk_cnt;
117 hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
118 reserved = hw_cfg->smp.reserved[cid];
120 spin_lock_irqsave(&smp->state_lock, flags);
124 DBG("%d MMBs allocated (%d reserved)", nblks, reserved);
126 avail = cnt - bitmap_weight(smp->state, cnt);
128 dev_err(mdp5_kms->dev->dev, "out of blks (req=%d > avail=%d)\n",
134 cur_nblks = bitmap_weight(ps->pending, cnt);
135 if (nblks > cur_nblks) {
136 /* grow the existing pending reservation: */
137 for (i = cur_nblks; i < nblks; i++) {
138 int blk = find_first_zero_bit(smp->state, cnt);
139 set_bit(blk, ps->pending);
140 set_bit(blk, smp->state);
143 /* shrink the existing pending reservation: */
144 for (i = cur_nblks; i > nblks; i--) {
145 int blk = find_first_bit(ps->pending, cnt);
146 clear_bit(blk, ps->pending);
147 /* don't clear in global smp_state until _commit() */
152 spin_unlock_irqrestore(&smp->state_lock, flags);
156 static void set_fifo_thresholds(struct mdp5_smp *smp,
157 enum mdp5_pipe pipe, int nblks)
159 struct mdp5_kms *mdp5_kms = get_kms(smp);
160 u32 smp_entries_per_blk = smp->blk_size / (128 / BITS_PER_BYTE);
163 /* 1/4 of SMP pool that is being fetched */
164 val = (nblks * smp_entries_per_blk) / 4;
166 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_0(pipe), val * 1);
167 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_1(pipe), val * 2);
168 mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_2(pipe), val * 3);
172 * NOTE: looks like if horizontal decimation is used (if we supported that)
173 * then the width used to calculate SMP block requirements is the post-
174 * decimated width. Ie. SMP buffering sits downstream of decimation (which
175 * presumably happens during the dma from scanout buffer).
177 int mdp5_smp_request(struct mdp5_smp *smp, enum mdp5_pipe pipe, u32 fmt, u32 width)
179 struct mdp5_kms *mdp5_kms = get_kms(smp);
180 struct drm_device *dev = mdp5_kms->dev;
181 int rev = mdp5_cfg_get_hw_rev(mdp5_kms->cfg);
182 int i, hsub, nplanes, nlines, nblks, ret;
184 nplanes = drm_format_num_planes(fmt);
185 hsub = drm_format_horz_chroma_subsampling(fmt);
187 /* different if BWC (compressed framebuffer?) enabled: */
190 for (i = 0, nblks = 0; i < nplanes; i++) {
191 int n, fetch_stride, cpp;
193 cpp = drm_format_plane_cpp(fmt, i);
194 fetch_stride = width * cpp / (i ? hsub : 1);
196 n = DIV_ROUND_UP(fetch_stride * nlines, smp->blk_size);
198 /* for hw rev v1.00 */
200 n = roundup_pow_of_two(n);
202 DBG("%s[%d]: request %d SMP blocks", pipe2name(pipe), i, n);
203 ret = smp_request_block(smp, pipe2client(pipe, i), n);
205 dev_err(dev->dev, "Cannot allocate %d SMP blocks: %d\n",
213 set_fifo_thresholds(smp, pipe, nblks);
218 /* Release SMP blocks for all clients of the pipe */
219 void mdp5_smp_release(struct mdp5_smp *smp, enum mdp5_pipe pipe)
223 for (i = 0, nblks = 0; i < pipe2nclients(pipe); i++)
224 smp_request_block(smp, pipe2client(pipe, i), 0);
225 set_fifo_thresholds(smp, pipe, 0);
228 static void update_smp_state(struct mdp5_smp *smp,
229 enum mdp5_client_id cid, mdp5_smp_state_t *assigned)
231 struct mdp5_kms *mdp5_kms = get_kms(smp);
232 int cnt = smp->blk_cnt;
235 for_each_set_bit(blk, *assigned, cnt) {
239 val = mdp5_read(mdp5_kms, REG_MDP5_SMP_ALLOC_W_REG(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 mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_W_REG(idx), val);
257 mdp5_write(mdp5_kms, REG_MDP5_SMP_ALLOC_R_REG(idx), val);
261 /* step #2: configure hw for union(pending, inuse): */
262 void mdp5_smp_configure(struct mdp5_smp *smp, enum mdp5_pipe pipe)
264 int cnt = smp->blk_cnt;
265 mdp5_smp_state_t assigned;
268 for (i = 0; i < pipe2nclients(pipe); i++) {
269 enum mdp5_client_id cid = pipe2client(pipe, i);
270 struct mdp5_client_smp_state *ps = &smp->client_state[cid];
272 bitmap_or(assigned, ps->inuse, ps->pending, cnt);
273 update_smp_state(smp, cid, &assigned);
277 /* step #3: after vblank, copy pending -> inuse: */
278 void mdp5_smp_commit(struct mdp5_smp *smp, enum mdp5_pipe pipe)
280 int cnt = smp->blk_cnt;
281 mdp5_smp_state_t released;
284 for (i = 0; i < pipe2nclients(pipe); i++) {
285 enum mdp5_client_id cid = pipe2client(pipe, i);
286 struct mdp5_client_smp_state *ps = &smp->client_state[cid];
289 * Figure out if there are any blocks we where previously
290 * using, which can be released and made available to other
293 if (bitmap_andnot(released, ps->inuse, ps->pending, cnt)) {
296 spin_lock_irqsave(&smp->state_lock, flags);
297 /* clear released blocks: */
298 bitmap_andnot(smp->state, smp->state, released, cnt);
299 spin_unlock_irqrestore(&smp->state_lock, flags);
301 update_smp_state(smp, CID_UNUSED, &released);
304 bitmap_copy(ps->inuse, ps->pending, cnt);
308 void mdp5_smp_destroy(struct mdp5_smp *smp)
313 struct mdp5_smp *mdp5_smp_init(struct drm_device *dev, const struct mdp5_smp_block *cfg)
315 struct mdp5_smp *smp = NULL;
318 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
319 if (unlikely(!smp)) {
325 smp->blk_cnt = cfg->mmb_count;
326 smp->blk_size = cfg->mmb_size;
328 /* statically tied MMBs cannot be re-allocated: */
329 bitmap_copy(smp->state, cfg->reserved_state, smp->blk_cnt);
330 spin_lock_init(&smp->state_lock);
335 mdp5_smp_destroy(smp);