2 * Tegra host1x Command DMA
4 * Copyright (c) 2010-2013, NVIDIA Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope 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
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/slab.h>
20 #include <linux/scatterlist.h>
21 #include <linux/dma-mapping.h>
24 #include "../channel.h"
29 * Put the restart at the end of pushbuffer memory
31 static void push_buffer_init(struct push_buffer *pb)
33 *(u32 *)(pb->mapped + pb->size) = host1x_opcode_restart(0);
37 * Increment timedout buffer's syncpt via CPU.
39 static void cdma_timeout_cpu_incr(struct host1x_cdma *cdma, u32 getptr,
40 u32 syncpt_incrs, u32 syncval, u32 nr_slots)
42 struct host1x *host1x = cdma_to_host1x(cdma);
43 struct push_buffer *pb = &cdma->push_buffer;
46 for (i = 0; i < syncpt_incrs; i++)
47 host1x_syncpt_incr(cdma->timeout.syncpt);
49 /* after CPU incr, ensure shadow is up to date */
50 host1x_syncpt_load(cdma->timeout.syncpt);
52 /* NOP all the PB slots */
54 u32 *p = (u32 *)(pb->mapped + getptr);
55 *(p++) = HOST1X_OPCODE_NOP;
56 *(p++) = HOST1X_OPCODE_NOP;
57 dev_dbg(host1x->dev, "%s: NOP at %pad+%#x\n", __func__,
59 getptr = (getptr + 8) & (pb->size - 1);
68 static void cdma_start(struct host1x_cdma *cdma)
70 struct host1x_channel *ch = cdma_to_channel(cdma);
76 cdma->last_pos = cdma->push_buffer.pos;
77 start = cdma->push_buffer.dma;
78 end = cdma->push_buffer.size + 4;
80 host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
81 HOST1X_CHANNEL_DMACTRL);
83 /* set base, put and end pointer */
84 host1x_ch_writel(ch, lower_32_bits(start), HOST1X_CHANNEL_DMASTART);
86 host1x_ch_writel(ch, upper_32_bits(start), HOST1X_CHANNEL_DMASTART_HI);
88 host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
90 host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMAPUT_HI);
92 host1x_ch_writel(ch, lower_32_bits(end), HOST1X_CHANNEL_DMAEND);
94 host1x_ch_writel(ch, upper_32_bits(end), HOST1X_CHANNEL_DMAEND_HI);
98 host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
99 HOST1X_CHANNEL_DMACTRL_DMAGETRST |
100 HOST1X_CHANNEL_DMACTRL_DMAINITGET,
101 HOST1X_CHANNEL_DMACTRL);
103 /* start the command DMA */
104 host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
106 cdma->running = true;
110 * Similar to cdma_start(), but rather than starting from an idle
111 * state (where DMA GET is set to DMA PUT), on a timeout we restore
112 * DMA GET from an explicit value (so DMA may again be pending).
114 static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
116 struct host1x *host1x = cdma_to_host1x(cdma);
117 struct host1x_channel *ch = cdma_to_channel(cdma);
123 cdma->last_pos = cdma->push_buffer.pos;
125 host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
126 HOST1X_CHANNEL_DMACTRL);
128 start = cdma->push_buffer.dma;
129 end = cdma->push_buffer.size + 4;
131 /* set base, end pointer (all of memory) */
132 host1x_ch_writel(ch, lower_32_bits(start), HOST1X_CHANNEL_DMASTART);
134 host1x_ch_writel(ch, upper_32_bits(start), HOST1X_CHANNEL_DMASTART_HI);
136 host1x_ch_writel(ch, lower_32_bits(end), HOST1X_CHANNEL_DMAEND);
138 host1x_ch_writel(ch, upper_32_bits(end), HOST1X_CHANNEL_DMAEND_HI);
141 /* set GET, by loading the value in PUT (then reset GET) */
142 host1x_ch_writel(ch, getptr, HOST1X_CHANNEL_DMAPUT);
143 host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
144 HOST1X_CHANNEL_DMACTRL_DMAGETRST |
145 HOST1X_CHANNEL_DMACTRL_DMAINITGET,
146 HOST1X_CHANNEL_DMACTRL);
149 "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n", __func__,
150 host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
151 host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
154 /* deassert GET reset and set PUT */
155 host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
156 HOST1X_CHANNEL_DMACTRL);
157 host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
159 /* start the command DMA */
160 host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
162 cdma->running = true;
166 * Kick channel DMA into action by writing its PUT offset (if it has changed)
168 static void cdma_flush(struct host1x_cdma *cdma)
170 struct host1x_channel *ch = cdma_to_channel(cdma);
172 if (cdma->push_buffer.pos != cdma->last_pos) {
173 host1x_ch_writel(ch, cdma->push_buffer.pos,
174 HOST1X_CHANNEL_DMAPUT);
175 cdma->last_pos = cdma->push_buffer.pos;
179 static void cdma_stop(struct host1x_cdma *cdma)
181 struct host1x_channel *ch = cdma_to_channel(cdma);
183 mutex_lock(&cdma->lock);
186 host1x_cdma_wait_locked(cdma, CDMA_EVENT_SYNC_QUEUE_EMPTY);
187 host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
188 HOST1X_CHANNEL_DMACTRL);
189 cdma->running = false;
192 mutex_unlock(&cdma->lock);
195 static void cdma_hw_cmdproc_stop(struct host1x *host, struct host1x_channel *ch,
199 host1x_ch_writel(ch, stop ? 0x1 : 0x0, HOST1X_CHANNEL_CMDPROC_STOP);
201 u32 cmdproc_stop = host1x_sync_readl(host, HOST1X_SYNC_CMDPROC_STOP);
203 cmdproc_stop |= BIT(ch->id);
205 cmdproc_stop &= ~BIT(ch->id);
206 host1x_sync_writel(host, cmdproc_stop, HOST1X_SYNC_CMDPROC_STOP);
210 static void cdma_hw_teardown(struct host1x *host, struct host1x_channel *ch)
213 host1x_ch_writel(ch, 0x1, HOST1X_CHANNEL_TEARDOWN);
215 host1x_sync_writel(host, BIT(ch->id), HOST1X_SYNC_CH_TEARDOWN);
220 * Stops both channel's command processor and CDMA immediately.
221 * Also, tears down the channel and resets corresponding module.
223 static void cdma_freeze(struct host1x_cdma *cdma)
225 struct host1x *host = cdma_to_host1x(cdma);
226 struct host1x_channel *ch = cdma_to_channel(cdma);
228 if (cdma->torndown && !cdma->running) {
229 dev_warn(host->dev, "Already torn down\n");
233 dev_dbg(host->dev, "freezing channel (id %d)\n", ch->id);
235 cdma_hw_cmdproc_stop(host, ch, true);
237 dev_dbg(host->dev, "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n",
238 __func__, host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
239 host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
242 host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
243 HOST1X_CHANNEL_DMACTRL);
245 cdma_hw_teardown(host, ch);
247 cdma->running = false;
248 cdma->torndown = true;
251 static void cdma_resume(struct host1x_cdma *cdma, u32 getptr)
253 struct host1x *host1x = cdma_to_host1x(cdma);
254 struct host1x_channel *ch = cdma_to_channel(cdma);
257 "resuming channel (id %u, DMAGET restart = 0x%x)\n",
260 cdma_hw_cmdproc_stop(host1x, ch, false);
262 cdma->torndown = false;
263 cdma_timeout_restart(cdma, getptr);
267 * If this timeout fires, it indicates the current sync_queue entry has
268 * exceeded its TTL and the userctx should be timed out and remaining
269 * submits already issued cleaned up (future submits return an error).
271 static void cdma_timeout_handler(struct work_struct *work)
274 struct host1x_cdma *cdma;
275 struct host1x *host1x;
276 struct host1x_channel *ch;
278 cdma = container_of(to_delayed_work(work), struct host1x_cdma,
280 host1x = cdma_to_host1x(cdma);
281 ch = cdma_to_channel(cdma);
283 host1x_debug_dump(cdma_to_host1x(cdma));
285 mutex_lock(&cdma->lock);
287 if (!cdma->timeout.client) {
289 "cdma_timeout: expired, but has no clientid\n");
290 mutex_unlock(&cdma->lock);
294 /* stop processing to get a clean snapshot */
295 cdma_hw_cmdproc_stop(host1x, ch, true);
297 syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
299 /* has buffer actually completed? */
300 if ((s32)(syncpt_val - cdma->timeout.syncpt_val) >= 0) {
302 "cdma_timeout: expired, but buffer had completed\n");
304 cdma_hw_cmdproc_stop(host1x, ch, false);
305 mutex_unlock(&cdma->lock);
309 dev_warn(host1x->dev, "%s: timeout: %u (%s), HW thresh %d, done %d\n",
310 __func__, cdma->timeout.syncpt->id, cdma->timeout.syncpt->name,
311 syncpt_val, cdma->timeout.syncpt_val);
313 /* stop HW, resetting channel/module */
314 host1x_hw_cdma_freeze(host1x, cdma);
316 host1x_cdma_update_sync_queue(cdma, ch->dev);
317 mutex_unlock(&cdma->lock);
321 * Init timeout resources
323 static int cdma_timeout_init(struct host1x_cdma *cdma, unsigned int syncpt)
325 INIT_DELAYED_WORK(&cdma->timeout.wq, cdma_timeout_handler);
326 cdma->timeout.initialized = true;
332 * Clean up timeout resources
334 static void cdma_timeout_destroy(struct host1x_cdma *cdma)
336 if (cdma->timeout.initialized)
337 cancel_delayed_work(&cdma->timeout.wq);
339 cdma->timeout.initialized = false;
342 static const struct host1x_cdma_ops host1x_cdma_ops = {
347 .timeout_init = cdma_timeout_init,
348 .timeout_destroy = cdma_timeout_destroy,
349 .freeze = cdma_freeze,
350 .resume = cdma_resume,
351 .timeout_cpu_incr = cdma_timeout_cpu_incr,
354 static const struct host1x_pushbuffer_ops host1x_pushbuffer_ops = {
355 .init = push_buffer_init,