2 * drivers/video/tegra/host/nvhost_cdma.c
4 * Tegra Graphics Host Command DMA
6 * Copyright (c) 2010, NVIDIA Corporation.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "nvhost_cdma.h"
25 #include <asm/cacheflush.h>
30 * - for figuring out what to optimize further
31 * resizable push buffer & sync queue
32 * - some channels hardly need any, some channels (3d) could use more
35 #define cdma_to_channel(cdma) container_of(cdma, struct nvhost_channel, cdma)
36 #define cdma_to_dev(cdma) ((cdma_to_channel(cdma))->dev)
37 #define cdma_to_nvmap(cdma) ((cdma_to_dev(cdma))->nvmap)
38 #define pb_to_cdma(pb) container_of(pb, struct nvhost_cdma, push_buffer)
43 * The push buffer is a circular array of words to be fetched by command DMA.
44 * Note that it works slightly differently to the sync queue; fence == cur
45 * means that the push buffer is full, not empty.
48 // 8 bytes per slot. (This number does not include the final RESTART.)
49 #define PUSH_BUFFER_SIZE (NVHOST_GATHER_QUEUE_SIZE * 8)
51 static void destroy_push_buffer(struct push_buffer *pb);
54 * Reset to empty push buffer
56 static void reset_push_buffer(struct push_buffer *pb)
58 pb->fence = PUSH_BUFFER_SIZE - 8;
63 * Init push buffer resources
65 static int init_push_buffer(struct push_buffer *pb)
67 struct nvhost_cdma *cdma = pb_to_cdma(pb);
68 struct nvmap_client *nvmap = cdma_to_nvmap(cdma);
72 reset_push_buffer(pb);
74 /* allocate and map pushbuffer memory */
75 pb->mem = nvmap_alloc(nvmap, PUSH_BUFFER_SIZE + 4, 32,
76 NVMAP_HANDLE_WRITE_COMBINE);
77 if (IS_ERR_OR_NULL(pb->mem)) {
81 pb->mapped = nvmap_mmap(pb->mem);
82 if (pb->mapped == NULL)
85 /* pin pushbuffer and get physical address */
86 pb->phys = nvmap_pin(nvmap, pb->mem);
87 if (pb->phys >= 0xfffff000) {
92 /* put the restart at the end of pushbuffer memory */
93 *(pb->mapped + (PUSH_BUFFER_SIZE >> 2)) = nvhost_opcode_restart(pb->phys);
98 destroy_push_buffer(pb);
103 * Clean up push buffer resources
105 static void destroy_push_buffer(struct push_buffer *pb)
107 struct nvhost_cdma *cdma = pb_to_cdma(pb);
108 struct nvmap_client *nvmap = cdma_to_nvmap(cdma);
110 nvmap_munmap(pb->mem, pb->mapped);
113 nvmap_unpin(nvmap, pb->mem);
116 nvmap_free(nvmap, pb->mem);
124 * Push two words to the push buffer
125 * Caller must ensure push buffer is not full
127 static void push_to_push_buffer(struct push_buffer *pb, u32 op1, u32 op2)
130 u32 *p = (u32*)((u32)pb->mapped + cur);
131 BUG_ON(cur == pb->fence);
134 pb->cur = (cur + 8) & (PUSH_BUFFER_SIZE - 1);
135 /* printk("push_to_push_buffer: op1=%08x; op2=%08x; cur=%x\n", op1, op2, pb->cur); */
139 * Pop a number of two word slots from the push buffer
140 * Caller must ensure push buffer is not empty
142 static void pop_from_push_buffer(struct push_buffer *pb, unsigned int slots)
144 pb->fence = (pb->fence + slots * 8) & (PUSH_BUFFER_SIZE - 1);
148 * Return the number of two word slots free in the push buffer
150 static u32 push_buffer_space(struct push_buffer *pb)
152 return ((pb->fence - pb->cur) & (PUSH_BUFFER_SIZE - 1)) / 8;
155 static u32 push_buffer_putptr(struct push_buffer *pb)
157 return pb->phys + pb->cur;
163 * The sync queue is a circular buffer of u32s interpreted as:
166 * 2: NumSlots (how many pushbuffer slots to free)
168 * 4: nvmap client which pinned the handles
169 * 5..: NumHandles * nvmemhandle to unpin
171 * There's always one word unused, so (accounting for wrap):
172 * - Write == Read => queue empty
173 * - Write + 1 == Read => queue full
174 * The queue must not be left with less than SYNC_QUEUE_MIN_ENTRY words
175 * of space at the end of the array.
177 * We want to pass contiguous arrays of handles to NrRmMemUnpin, so arrays
178 * that would wrap at the end of the buffer will be split into two (or more)
182 /* Number of words needed to store an entry containing one handle */
183 #define SYNC_QUEUE_MIN_ENTRY (4 + (2 * sizeof(void *) / sizeof(u32)))
186 * Reset to empty queue.
188 static void reset_sync_queue(struct sync_queue *queue)
195 * Find the number of handles that can be stashed in the sync queue without
197 * 0 -> queue is full, must update to wait for some entries to be freed.
199 static unsigned int sync_queue_space(struct sync_queue *queue)
201 unsigned int read = queue->read;
202 unsigned int write = queue->write;
205 BUG_ON(read > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
206 BUG_ON(write > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
209 * We can use all of the space up to the end of the buffer, unless the
210 * read position is within that space (the read position may advance
211 * asynchronously, but that can't take space away once we've seen it).
214 size = (read - 1) - write;
216 size = NVHOST_SYNC_QUEUE_SIZE - write;
219 * If the read position is zero, it gets complicated. We can't
220 * use the last word in the buffer, because that would leave
222 * But also if we use too much we would not leave enough space
223 * for a single handle packet, and would have to wrap in
224 * add_to_sync_queue - also leaving write == read == 0,
228 size -= SYNC_QUEUE_MIN_ENTRY;
232 * There must be room for an entry header and at least one handle,
233 * otherwise we report a full queue.
235 if (size < SYNC_QUEUE_MIN_ENTRY)
237 /* Minimum entry stores one handle */
238 return (size - SYNC_QUEUE_MIN_ENTRY) + 1;
242 * Add an entry to the sync queue.
244 #define entry_size(_cnt) ((1 + _cnt)*sizeof(void *)/sizeof(u32))
246 static void add_to_sync_queue(struct sync_queue *queue,
247 u32 sync_point_id, u32 sync_point_value,
248 u32 nr_slots, struct nvmap_client *user_nvmap,
249 struct nvmap_handle **handles, u32 nr_handles)
251 u32 write = queue->write;
252 u32 *p = queue->buffer + write;
253 u32 size = 4 + (entry_size(nr_handles));
255 BUG_ON(sync_point_id == NVSYNCPT_INVALID);
256 BUG_ON(sync_queue_space(queue) < nr_handles);
259 BUG_ON(write > NVHOST_SYNC_QUEUE_SIZE);
261 *p++ = sync_point_id;
262 *p++ = sync_point_value;
266 *(struct nvmap_client **)p = nvmap_client_get(user_nvmap);
268 p = (u32 *)((void *)p + sizeof(struct nvmap_client *));
271 memcpy(p, handles, nr_handles * sizeof(struct nvmap_handle *));
273 /* If there's not enough room for another entry, wrap to the start. */
274 if ((write + SYNC_QUEUE_MIN_ENTRY) > NVHOST_SYNC_QUEUE_SIZE) {
276 * It's an error for the read position to be zero, as that
277 * would mean we emptied the queue while adding something.
279 BUG_ON(queue->read == 0);
283 queue->write = write;
287 * Get a pointer to the next entry in the queue, or NULL if the queue is empty.
288 * Doesn't consume the entry.
290 static u32 *sync_queue_head(struct sync_queue *queue)
292 u32 read = queue->read;
293 u32 write = queue->write;
295 BUG_ON(read > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
296 BUG_ON(write > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
300 return queue->buffer + read;
304 * Advances to the next queue entry, if you want to consume it.
307 dequeue_sync_queue_head(struct sync_queue *queue)
309 u32 read = queue->read;
312 BUG_ON(read == queue->write);
314 size = 4 + entry_size(queue->buffer[read + 3]);
317 BUG_ON(read > NVHOST_SYNC_QUEUE_SIZE);
319 /* If there's not enough room for another entry, wrap to the start. */
320 if ((read + SYNC_QUEUE_MIN_ENTRY) > NVHOST_SYNC_QUEUE_SIZE)
327 /*** Cdma internal stuff ***/
330 * Kick channel DMA into action by writing its PUT offset (if it has changed)
332 static void kick_cdma(struct nvhost_cdma *cdma)
334 u32 put = push_buffer_putptr(&cdma->push_buffer);
335 if (put != cdma->last_put) {
336 void __iomem *chan_regs = cdma_to_channel(cdma)->aperture;
338 writel(put, chan_regs + HOST1X_CHANNEL_DMAPUT);
339 cdma->last_put = put;
344 * Return the status of the cdma's sync queue or push buffer for the given event
345 * - sq empty: returns 1 for empty, 0 for not empty (as in "1 empty queue" :-)
346 * - sq space: returns the number of handles that can be stored in the queue
347 * - pb space: returns the number of free slots in the channel's push buffer
348 * Must be called with the cdma lock held.
350 static unsigned int cdma_status(struct nvhost_cdma *cdma, enum cdma_event event)
353 case CDMA_EVENT_SYNC_QUEUE_EMPTY:
354 return sync_queue_head(&cdma->sync_queue) ? 0 : 1;
355 case CDMA_EVENT_SYNC_QUEUE_SPACE:
356 return sync_queue_space(&cdma->sync_queue);
357 case CDMA_EVENT_PUSH_BUFFER_SPACE:
358 return push_buffer_space(&cdma->push_buffer);
365 * Sleep (if necessary) until the requested event happens
366 * - CDMA_EVENT_SYNC_QUEUE_EMPTY : sync queue is completely empty.
368 * - CDMA_EVENT_SYNC_QUEUE_SPACE : there is space in the sync queue.
369 * - CDMA_EVENT_PUSH_BUFFER_SPACE : there is space in the push buffer
370 * - Return the amount of space (> 0)
371 * Must be called with the cdma lock held.
373 static unsigned int wait_cdma(struct nvhost_cdma *cdma, enum cdma_event event)
376 unsigned int space = cdma_status(cdma, event);
380 BUG_ON(cdma->event != CDMA_EVENT_NONE);
383 mutex_unlock(&cdma->lock);
385 mutex_lock(&cdma->lock);
390 * For all sync queue entries that have already finished according to the
391 * current sync point registers:
392 * - unpin & unref their mems
393 * - pop their push buffer slots
394 * - remove them from the sync queue
395 * This is normally called from the host code's worker thread, but can be
396 * called manually if necessary.
397 * Must be called with the cdma lock held.
399 static void update_cdma(struct nvhost_cdma *cdma)
402 struct nvhost_master *dev = cdma_to_dev(cdma);
404 BUG_ON(!cdma->running);
407 * Walk the sync queue, reading the sync point registers as necessary,
408 * to consume as many sync queue entries as possible without blocking
411 u32 syncpt_id, syncpt_val;
412 unsigned int nr_slots, nr_handles;
413 struct nvmap_handle **handles;
414 struct nvmap_client *nvmap;
417 sync = sync_queue_head(&cdma->sync_queue);
419 if (cdma->event == CDMA_EVENT_SYNC_QUEUE_EMPTY)
425 syncpt_val = *sync++;
427 BUG_ON(syncpt_id == NVSYNCPT_INVALID);
429 /* Check whether this syncpt has completed, and bail if not */
430 if (!nvhost_syncpt_min_cmp(&dev->syncpt, syncpt_id, syncpt_val))
434 nr_handles = *sync++;
435 nvmap = *(struct nvmap_client **)sync;
436 sync = ((void *)sync + sizeof(struct nvmap_client *));
437 handles = (struct nvmap_handle **)sync;
441 /* Unpin the memory */
442 nvmap_unpin_handles(nvmap, handles, nr_handles);
444 nvmap_client_put(nvmap);
446 /* Pop push buffer slots */
448 pop_from_push_buffer(&cdma->push_buffer, nr_slots);
449 if (cdma->event == CDMA_EVENT_PUSH_BUFFER_SPACE)
453 dequeue_sync_queue_head(&cdma->sync_queue);
454 if (cdma->event == CDMA_EVENT_SYNC_QUEUE_SPACE)
458 /* Wake up CdmaWait() if the requested event happened */
460 cdma->event = CDMA_EVENT_NONE;
468 int nvhost_cdma_init(struct nvhost_cdma *cdma)
472 mutex_init(&cdma->lock);
473 sema_init(&cdma->sem, 0);
474 cdma->event = CDMA_EVENT_NONE;
475 cdma->running = false;
476 err = init_push_buffer(&cdma->push_buffer);
479 reset_sync_queue(&cdma->sync_queue);
486 void nvhost_cdma_deinit(struct nvhost_cdma *cdma)
488 BUG_ON(cdma->running);
489 destroy_push_buffer(&cdma->push_buffer);
492 static void start_cdma(struct nvhost_cdma *cdma)
494 void __iomem *chan_regs = cdma_to_channel(cdma)->aperture;
499 cdma->last_put = push_buffer_putptr(&cdma->push_buffer);
501 writel(nvhost_channel_dmactrl(true, false, false),
502 chan_regs + HOST1X_CHANNEL_DMACTRL);
504 /* set base, put, end pointer (all of memory) */
505 writel(0, chan_regs + HOST1X_CHANNEL_DMASTART);
506 writel(cdma->last_put, chan_regs + HOST1X_CHANNEL_DMAPUT);
507 writel(0xFFFFFFFF, chan_regs + HOST1X_CHANNEL_DMAEND);
510 writel(nvhost_channel_dmactrl(true, true, true),
511 chan_regs + HOST1X_CHANNEL_DMACTRL);
513 /* start the command DMA */
514 writel(nvhost_channel_dmactrl(false, false, false),
515 chan_regs + HOST1X_CHANNEL_DMACTRL);
517 cdma->running = true;
521 void nvhost_cdma_stop(struct nvhost_cdma *cdma)
523 void __iomem *chan_regs = cdma_to_channel(cdma)->aperture;
525 mutex_lock(&cdma->lock);
527 wait_cdma(cdma, CDMA_EVENT_SYNC_QUEUE_EMPTY);
528 writel(nvhost_channel_dmactrl(true, false, false),
529 chan_regs + HOST1X_CHANNEL_DMACTRL);
530 cdma->running = false;
532 mutex_unlock(&cdma->lock);
536 * Begin a cdma submit
538 void nvhost_cdma_begin(struct nvhost_cdma *cdma)
540 mutex_lock(&cdma->lock);
543 cdma->slots_free = 0;
544 cdma->slots_used = 0;
548 * Push two words into a push buffer slot
549 * Blocks as necessary if the push buffer is full.
551 void nvhost_cdma_push(struct nvhost_cdma *cdma, u32 op1, u32 op2)
553 u32 slots_free = cdma->slots_free;
554 if (slots_free == 0) {
556 slots_free = wait_cdma(cdma, CDMA_EVENT_PUSH_BUFFER_SPACE);
558 cdma->slots_free = slots_free - 1;
560 push_to_push_buffer(&cdma->push_buffer, op1, op2);
565 * Kick off DMA, add a contiguous block of memory handles to the sync queue,
566 * and a number of slots to be freed from the pushbuffer.
567 * Blocks as necessary if the sync queue is full.
568 * The handles for a submit must all be pinned at the same time, but they
569 * can be unpinned in smaller chunks.
571 void nvhost_cdma_end(struct nvmap_client *user_nvmap, struct nvhost_cdma *cdma,
572 u32 sync_point_id, u32 sync_point_value,
573 struct nvmap_handle **handles, unsigned int nr_handles)
577 while (nr_handles || cdma->slots_used) {
580 * Wait until there's enough room in the
581 * sync queue to write something.
583 count = wait_cdma(cdma, CDMA_EVENT_SYNC_QUEUE_SPACE);
586 * Add reloc entries to sync queue (as many as will fit)
589 if (count > nr_handles)
591 add_to_sync_queue(&cdma->sync_queue, sync_point_id,
592 sync_point_value, cdma->slots_used,
593 user_nvmap, handles, count);
594 /* NumSlots only goes in the first packet */
595 cdma->slots_used = 0;
600 mutex_unlock(&cdma->lock);
604 * Update cdma state according to current sync point values
606 void nvhost_cdma_update(struct nvhost_cdma *cdma)
608 mutex_lock(&cdma->lock);
610 mutex_unlock(&cdma->lock);
614 * Manually spin until all CDMA has finished. Used if an async update
615 * cannot be scheduled for any reason.
617 void nvhost_cdma_flush(struct nvhost_cdma *cdma)
619 mutex_lock(&cdma->lock);
620 while (sync_queue_head(&cdma->sync_queue)) {
622 mutex_unlock(&cdma->lock);
624 mutex_lock(&cdma->lock);
626 mutex_unlock(&cdma->lock);
630 * Find the currently executing gather in the push buffer and return
631 * its physical address and size.
633 void nvhost_cdma_find_gather(struct nvhost_cdma *cdma, u32 dmaget, u32 *addr, u32 *size)
635 u32 offset = dmaget - cdma->push_buffer.phys;
639 if (offset >= 8 && offset < cdma->push_buffer.cur) {
640 u32 *p = cdma->push_buffer.mapped + (offset - 8) / 4;
642 /* Make sure we have a gather */
643 if ((p[0] >> 28) == 6) {
645 *size = p[0] & 0x3fff;