temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / video / tegra / host / nvhost_cdma.c
1 /*
2  * drivers/video/tegra/host/nvhost_cdma.c
3  *
4  * Tegra Graphics Host Command DMA
5  *
6  * Copyright (c) 2010, NVIDIA Corporation.
7  *
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.
12  *
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
16  * more details.
17  *
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.
21  */
22
23 #include "nvhost_cdma.h"
24 #include "dev.h"
25 #include <asm/cacheflush.h>
26
27 /*
28  * TODO:
29  *   stats
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
33  */
34
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)
39
40 /*
41  * push_buffer
42  *
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.
46  */
47
48 // 8 bytes per slot. (This number does not include the final RESTART.)
49 #define PUSH_BUFFER_SIZE (NVHOST_GATHER_QUEUE_SIZE * 8)
50
51 static void destroy_push_buffer(struct push_buffer *pb);
52
53 /**
54  * Reset to empty push buffer
55  */
56 static void reset_push_buffer(struct push_buffer *pb)
57 {
58         pb->fence = PUSH_BUFFER_SIZE - 8;
59         pb->cur = 0;
60 }
61
62 /**
63  * Init push buffer resources
64  */
65 static int init_push_buffer(struct push_buffer *pb)
66 {
67         struct nvhost_cdma *cdma = pb_to_cdma(pb);
68         struct nvmap_client *nvmap = cdma_to_nvmap(cdma);
69         pb->mem = NULL;
70         pb->mapped = NULL;
71         pb->phys = 0;
72         reset_push_buffer(pb);
73
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)) {
78                 pb->mem = NULL;
79                 goto fail;
80         }
81         pb->mapped = nvmap_mmap(pb->mem);
82         if (pb->mapped == NULL)
83                 goto fail;
84
85         /* pin pushbuffer and get physical address */
86         pb->phys = nvmap_pin(nvmap, pb->mem);
87         if (pb->phys >= 0xfffff000) {
88                 pb->phys = 0;
89                 goto fail;
90         }
91
92         /* put the restart at the end of pushbuffer memory */
93         *(pb->mapped + (PUSH_BUFFER_SIZE >> 2)) = nvhost_opcode_restart(pb->phys);
94
95         return 0;
96
97 fail:
98         destroy_push_buffer(pb);
99         return -ENOMEM;
100 }
101
102 /**
103  * Clean up push buffer resources
104  */
105 static void destroy_push_buffer(struct push_buffer *pb)
106 {
107         struct nvhost_cdma *cdma = pb_to_cdma(pb);
108         struct nvmap_client *nvmap = cdma_to_nvmap(cdma);
109         if (pb->mapped)
110                 nvmap_munmap(pb->mem, pb->mapped);
111
112         if (pb->phys != 0)
113                 nvmap_unpin(nvmap, pb->mem);
114
115         if (pb->mem)
116                 nvmap_free(nvmap, pb->mem);
117
118         pb->mem = NULL;
119         pb->mapped = NULL;
120         pb->phys = 0;
121 }
122
123 /**
124  * Push two words to the push buffer
125  * Caller must ensure push buffer is not full
126  */
127 static void push_to_push_buffer(struct push_buffer *pb, u32 op1, u32 op2)
128 {
129         u32 cur = pb->cur;
130         u32 *p = (u32*)((u32)pb->mapped + cur);
131         BUG_ON(cur == pb->fence);
132         *(p++) = op1;
133         *(p++) = op2;
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); */
136 }
137
138 /**
139  * Pop a number of two word slots from the push buffer
140  * Caller must ensure push buffer is not empty
141  */
142 static void pop_from_push_buffer(struct push_buffer *pb, unsigned int slots)
143 {
144         pb->fence = (pb->fence + slots * 8) & (PUSH_BUFFER_SIZE - 1);
145 }
146
147 /**
148  * Return the number of two word slots free in the push buffer
149  */
150 static u32 push_buffer_space(struct push_buffer *pb)
151 {
152         return ((pb->fence - pb->cur) & (PUSH_BUFFER_SIZE - 1)) / 8;
153 }
154
155 static u32 push_buffer_putptr(struct push_buffer *pb)
156 {
157         return pb->phys + pb->cur;
158 }
159
160
161 /* Sync Queue
162  *
163  * The sync queue is a circular buffer of u32s interpreted as:
164  *   0: SyncPointID
165  *   1: SyncPointValue
166  *   2: NumSlots (how many pushbuffer slots to free)
167  *   3: NumHandles
168  *   4: nvmap client which pinned the handles
169  *   5..: NumHandles * nvmemhandle to unpin
170  *
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.
176  *
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)
179  * entries.
180  */
181
182 /* Number of words needed to store an entry containing one handle */
183 #define SYNC_QUEUE_MIN_ENTRY (4 + (2 * sizeof(void *) / sizeof(u32)))
184
185 /**
186  * Reset to empty queue.
187  */
188 static void reset_sync_queue(struct sync_queue *queue)
189 {
190         queue->read = 0;
191         queue->write = 0;
192 }
193
194 /**
195  *  Find the number of handles that can be stashed in the sync queue without
196  *  waiting.
197  *  0 -> queue is full, must update to wait for some entries to be freed.
198  */
199 static unsigned int sync_queue_space(struct sync_queue *queue)
200 {
201         unsigned int read = queue->read;
202         unsigned int write = queue->write;
203         u32 size;
204
205         BUG_ON(read  > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
206         BUG_ON(write > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
207
208         /*
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).
212          */
213         if (read > write) {
214                 size = (read - 1) - write;
215         } else {
216                 size = NVHOST_SYNC_QUEUE_SIZE - write;
217
218                 /*
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
221                  * the queue empty.
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,
225                  * an empty queue.
226                  */
227                 if (read == 0)
228                         size -= SYNC_QUEUE_MIN_ENTRY;
229         }
230
231         /*
232          * There must be room for an entry header and at least one handle,
233          * otherwise we report a full queue.
234          */
235         if (size < SYNC_QUEUE_MIN_ENTRY)
236                 return 0;
237         /* Minimum entry stores one handle */
238         return (size - SYNC_QUEUE_MIN_ENTRY) + 1;
239 }
240
241 /**
242  * Add an entry to the sync queue.
243  */
244 #define entry_size(_cnt)        ((1 + _cnt)*sizeof(void *)/sizeof(u32))
245
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)
250 {
251         u32 write = queue->write;
252         u32 *p = queue->buffer + write;
253         u32 size = 4 + (entry_size(nr_handles));
254
255         BUG_ON(sync_point_id == NVSYNCPT_INVALID);
256         BUG_ON(sync_queue_space(queue) < nr_handles);
257
258         write += size;
259         BUG_ON(write > NVHOST_SYNC_QUEUE_SIZE);
260
261         *p++ = sync_point_id;
262         *p++ = sync_point_value;
263         *p++ = nr_slots;
264         *p++ = nr_handles;
265         BUG_ON(!user_nvmap);
266         *(struct nvmap_client **)p = nvmap_client_get(user_nvmap);
267
268         p = (u32 *)((void *)p + sizeof(struct nvmap_client *));
269
270         if (nr_handles)
271                 memcpy(p, handles, nr_handles * sizeof(struct nvmap_handle *));
272
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) {
275                 /*
276                  * It's an error for the read position to be zero, as that
277                  * would mean we emptied the queue while adding something.
278                  */
279                 BUG_ON(queue->read == 0);
280                 write = 0;
281         }
282
283         queue->write = write;
284 }
285
286 /**
287  * Get a pointer to the next entry in the queue, or NULL if the queue is empty.
288  * Doesn't consume the entry.
289  */
290 static u32 *sync_queue_head(struct sync_queue *queue)
291 {
292         u32 read = queue->read;
293         u32 write = queue->write;
294
295         BUG_ON(read  > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
296         BUG_ON(write > (NVHOST_SYNC_QUEUE_SIZE - SYNC_QUEUE_MIN_ENTRY));
297
298         if (read == write)
299                 return NULL;
300         return queue->buffer + read;
301 }
302
303 /**
304  * Advances to the next queue entry, if you want to consume it.
305  */
306 static void
307 dequeue_sync_queue_head(struct sync_queue *queue)
308 {
309         u32 read = queue->read;
310         u32 size;
311
312         BUG_ON(read == queue->write);
313
314         size = 4 + entry_size(queue->buffer[read + 3]);
315
316         read += size;
317         BUG_ON(read > NVHOST_SYNC_QUEUE_SIZE);
318
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)
321                 read = 0;
322
323         queue->read = read;
324 }
325
326
327 /*** Cdma internal stuff ***/
328
329 /**
330  * Kick channel DMA into action by writing its PUT offset (if it has changed)
331  */
332 static void kick_cdma(struct nvhost_cdma *cdma)
333 {
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;
337                 wmb();
338                 writel(put, chan_regs + HOST1X_CHANNEL_DMAPUT);
339                 cdma->last_put = put;
340         }
341 }
342
343 /**
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.
349  */
350 static unsigned int cdma_status(struct nvhost_cdma *cdma, enum cdma_event event)
351 {
352         switch (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);
359         default:
360                 return 0;
361         }
362 }
363
364 /**
365  * Sleep (if necessary) until the requested event happens
366  *   - CDMA_EVENT_SYNC_QUEUE_EMPTY : sync queue is completely empty.
367  *     - Returns 1
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.
372  */
373 static unsigned int wait_cdma(struct nvhost_cdma *cdma, enum cdma_event event)
374 {
375         for (;;) {
376                 unsigned int space = cdma_status(cdma, event);
377                 if (space)
378                         return space;
379
380                 BUG_ON(cdma->event != CDMA_EVENT_NONE);
381                 cdma->event = event;
382
383                 mutex_unlock(&cdma->lock);
384                 down(&cdma->sem);
385                 mutex_lock(&cdma->lock);
386         }
387 }
388
389 /**
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.
398  */
399 static void update_cdma(struct nvhost_cdma *cdma)
400 {
401         bool signal = false;
402         struct nvhost_master *dev = cdma_to_dev(cdma);
403
404         BUG_ON(!cdma->running);
405
406         /*
407          * Walk the sync queue, reading the sync point registers as necessary,
408          * to consume as many sync queue entries as possible without blocking
409          */
410         for (;;) {
411                 u32 syncpt_id, syncpt_val;
412                 unsigned int nr_slots, nr_handles;
413                 struct nvmap_handle **handles;
414                 struct nvmap_client *nvmap;
415                 u32 *sync;
416
417                 sync = sync_queue_head(&cdma->sync_queue);
418                 if (!sync) {
419                         if (cdma->event == CDMA_EVENT_SYNC_QUEUE_EMPTY)
420                                 signal = true;
421                         break;
422                 }
423
424                 syncpt_id = *sync++;
425                 syncpt_val = *sync++;
426
427                 BUG_ON(syncpt_id == NVSYNCPT_INVALID);
428
429                 /* Check whether this syncpt has completed, and bail if not */
430                 if (!nvhost_syncpt_min_cmp(&dev->syncpt, syncpt_id, syncpt_val))
431                         break;
432
433                 nr_slots = *sync++;
434                 nr_handles = *sync++;
435                 nvmap = *(struct nvmap_client **)sync;
436                 sync = ((void *)sync + sizeof(struct nvmap_client *));
437                 handles = (struct nvmap_handle **)sync;
438
439                 BUG_ON(!nvmap);
440
441                 /* Unpin the memory */
442                 nvmap_unpin_handles(nvmap, handles, nr_handles);
443
444                 nvmap_client_put(nvmap);
445
446                 /* Pop push buffer slots */
447                 if (nr_slots) {
448                         pop_from_push_buffer(&cdma->push_buffer, nr_slots);
449                         if (cdma->event == CDMA_EVENT_PUSH_BUFFER_SPACE)
450                                 signal = true;
451                 }
452
453                 dequeue_sync_queue_head(&cdma->sync_queue);
454                 if (cdma->event == CDMA_EVENT_SYNC_QUEUE_SPACE)
455                         signal = true;
456         }
457
458         /* Wake up CdmaWait() if the requested event happened */
459         if (signal) {
460                 cdma->event = CDMA_EVENT_NONE;
461                 up(&cdma->sem);
462         }
463 }
464
465 /**
466  * Create a cdma
467  */
468 int nvhost_cdma_init(struct nvhost_cdma *cdma)
469 {
470         int err;
471
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);
477         if (err)
478                 return err;
479         reset_sync_queue(&cdma->sync_queue);
480         return 0;
481 }
482
483 /**
484  * Destroy a cdma
485  */
486 void nvhost_cdma_deinit(struct nvhost_cdma *cdma)
487 {
488         BUG_ON(cdma->running);
489         destroy_push_buffer(&cdma->push_buffer);
490 }
491
492 static void start_cdma(struct nvhost_cdma *cdma)
493 {
494         void __iomem *chan_regs = cdma_to_channel(cdma)->aperture;
495
496         if (cdma->running)
497                 return;
498
499         cdma->last_put = push_buffer_putptr(&cdma->push_buffer);
500
501         writel(nvhost_channel_dmactrl(true, false, false),
502                 chan_regs + HOST1X_CHANNEL_DMACTRL);
503
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);
508
509         /* reset GET */
510         writel(nvhost_channel_dmactrl(true, true, true),
511                 chan_regs + HOST1X_CHANNEL_DMACTRL);
512
513         /* start the command DMA */
514         writel(nvhost_channel_dmactrl(false, false, false),
515                 chan_regs + HOST1X_CHANNEL_DMACTRL);
516
517         cdma->running = true;
518
519 }
520
521 void nvhost_cdma_stop(struct nvhost_cdma *cdma)
522 {
523         void __iomem *chan_regs = cdma_to_channel(cdma)->aperture;
524
525         mutex_lock(&cdma->lock);
526         if (cdma->running) {
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;
531         }
532         mutex_unlock(&cdma->lock);
533 }
534
535 /**
536  * Begin a cdma submit
537  */
538 void nvhost_cdma_begin(struct nvhost_cdma *cdma)
539 {
540         mutex_lock(&cdma->lock);
541         if (!cdma->running)
542                 start_cdma(cdma);
543         cdma->slots_free = 0;
544         cdma->slots_used = 0;
545 }
546
547 /**
548  * Push two words into a push buffer slot
549  * Blocks as necessary if the push buffer is full.
550  */
551 void nvhost_cdma_push(struct nvhost_cdma *cdma, u32 op1, u32 op2)
552 {
553         u32 slots_free = cdma->slots_free;
554         if (slots_free == 0) {
555                 kick_cdma(cdma);
556                 slots_free = wait_cdma(cdma, CDMA_EVENT_PUSH_BUFFER_SPACE);
557         }
558         cdma->slots_free = slots_free - 1;
559         cdma->slots_used++;
560         push_to_push_buffer(&cdma->push_buffer, op1, op2);
561 }
562
563 /**
564  * End a cdma submit
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.
570  */
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)
574 {
575         kick_cdma(cdma);
576
577         while (nr_handles || cdma->slots_used) {
578                 unsigned int count;
579                 /*
580                  * Wait until there's enough room in the
581                  * sync queue to write something.
582                  */
583                 count = wait_cdma(cdma, CDMA_EVENT_SYNC_QUEUE_SPACE);
584
585                 /*
586                  * Add reloc entries to sync queue (as many as will fit)
587                  * and unlock it
588                  */
589                 if (count > nr_handles)
590                         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;
596                 handles += count;
597                 nr_handles -= count;
598         }
599
600         mutex_unlock(&cdma->lock);
601 }
602
603 /**
604  * Update cdma state according to current sync point values
605  */
606 void nvhost_cdma_update(struct nvhost_cdma *cdma)
607 {
608         mutex_lock(&cdma->lock);
609         update_cdma(cdma);
610         mutex_unlock(&cdma->lock);
611 }
612
613 /**
614  * Manually spin until all CDMA has finished. Used if an async update
615  * cannot be scheduled for any reason.
616  */
617 void nvhost_cdma_flush(struct nvhost_cdma *cdma)
618 {
619         mutex_lock(&cdma->lock);
620         while (sync_queue_head(&cdma->sync_queue)) {
621                 update_cdma(cdma);
622                 mutex_unlock(&cdma->lock);
623                 schedule();
624                 mutex_lock(&cdma->lock);
625         }
626         mutex_unlock(&cdma->lock);
627 }
628
629 /**
630  * Find the currently executing gather in the push buffer and return
631  * its physical address and size.
632  */
633 void nvhost_cdma_find_gather(struct nvhost_cdma *cdma, u32 dmaget, u32 *addr, u32 *size)
634 {
635         u32 offset = dmaget - cdma->push_buffer.phys;
636
637         *addr = *size = 0;
638
639         if (offset >= 8 && offset < cdma->push_buffer.cur) {
640                 u32 *p = cdma->push_buffer.mapped + (offset - 8) / 4;
641
642                 /* Make sure we have a gather */
643                 if ((p[0] >> 28) == 6) {
644                         *addr = p[1];
645                         *size = p[0] & 0x3fff;
646                 }
647         }
648 }