Merge branch 'work.get_user_pages_fast' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / staging / vc04_services / interface / vchiq_arm / vchiq_2835_arm.c
1 /**
2  * Copyright (c) 2010-2012 Broadcom. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions, and the following disclaimer,
9  *    without modification.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The names of the above-listed copyright holders may not be used
14  *    to endorse or promote products derived from this software without
15  *    specific prior written permission.
16  *
17  * ALTERNATIVELY, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2, as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/interrupt.h>
38 #include <linux/pagemap.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/io.h>
41 #include <linux/platform_device.h>
42 #include <linux/uaccess.h>
43 #include <linux/mm.h>
44 #include <linux/of.h>
45 #include <soc/bcm2835/raspberrypi-firmware.h>
46
47 #define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
48
49 #include "vchiq_arm.h"
50 #include "vchiq_connected.h"
51 #include "vchiq_killable.h"
52 #include "vchiq_pagelist.h"
53
54 #define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
55
56 #define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
57 #define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
58
59 #define BELL0   0x00
60 #define BELL2   0x08
61
62 struct vchiq_2835_state {
63         int inited;
64         VCHIQ_ARM_STATE_T arm_state;
65 };
66
67 struct vchiq_pagelist_info {
68         PAGELIST_T *pagelist;
69         size_t pagelist_buffer_size;
70         dma_addr_t dma_addr;
71         enum dma_data_direction dma_dir;
72         unsigned int num_pages;
73         unsigned int pages_need_release;
74         struct page **pages;
75         struct scatterlist *scatterlist;
76         unsigned int scatterlist_mapped;
77 };
78
79 static void __iomem *g_regs;
80 static unsigned int g_cache_line_size = sizeof(CACHE_LINE_SIZE);
81 static unsigned int g_fragments_size;
82 static char *g_fragments_base;
83 static char *g_free_fragments;
84 static struct semaphore g_free_fragments_sema;
85 static struct device *g_dev;
86
87 static DEFINE_SEMAPHORE(g_free_fragments_mutex);
88
89 static irqreturn_t
90 vchiq_doorbell_irq(int irq, void *dev_id);
91
92 static struct vchiq_pagelist_info *
93 create_pagelist(char __user *buf, size_t count, unsigned short type);
94
95 static void
96 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
97               int actual);
98
99 int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state)
100 {
101         struct device *dev = &pdev->dev;
102         struct rpi_firmware *fw = platform_get_drvdata(pdev);
103         VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
104         struct resource *res;
105         void *slot_mem;
106         dma_addr_t slot_phys;
107         u32 channelbase;
108         int slot_mem_size, frag_mem_size;
109         int err, irq, i;
110
111         /*
112          * VCHI messages between the CPU and firmware use
113          * 32-bit bus addresses.
114          */
115         err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
116
117         if (err < 0)
118                 return err;
119
120         err = of_property_read_u32(dev->of_node, "cache-line-size",
121                                    &g_cache_line_size);
122
123         if (err) {
124                 dev_err(dev, "Missing cache-line-size property\n");
125                 return -ENODEV;
126         }
127
128         g_fragments_size = 2 * g_cache_line_size;
129
130         /* Allocate space for the channels in coherent memory */
131         slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
132         frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
133
134         slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
135                                        &slot_phys, GFP_KERNEL);
136         if (!slot_mem) {
137                 dev_err(dev, "could not allocate DMA memory\n");
138                 return -ENOMEM;
139         }
140
141         WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
142
143         vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
144         if (!vchiq_slot_zero)
145                 return -EINVAL;
146
147         vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
148                 (int)slot_phys + slot_mem_size;
149         vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
150                 MAX_FRAGMENTS;
151
152         g_fragments_base = (char *)slot_mem + slot_mem_size;
153
154         g_free_fragments = g_fragments_base;
155         for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
156                 *(char **)&g_fragments_base[i*g_fragments_size] =
157                         &g_fragments_base[(i + 1)*g_fragments_size];
158         }
159         *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
160         sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
161
162         if (vchiq_init_state(state, vchiq_slot_zero, 0) != VCHIQ_SUCCESS)
163                 return -EINVAL;
164
165         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
166         g_regs = devm_ioremap_resource(&pdev->dev, res);
167         if (IS_ERR(g_regs))
168                 return PTR_ERR(g_regs);
169
170         irq = platform_get_irq(pdev, 0);
171         if (irq <= 0) {
172                 dev_err(dev, "failed to get IRQ\n");
173                 return irq;
174         }
175
176         err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
177                                "VCHIQ doorbell", state);
178         if (err) {
179                 dev_err(dev, "failed to register irq=%d\n", irq);
180                 return err;
181         }
182
183         /* Send the base address of the slots to VideoCore */
184         channelbase = slot_phys;
185         err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
186                                     &channelbase, sizeof(channelbase));
187         if (err || channelbase) {
188                 dev_err(dev, "failed to set channelbase\n");
189                 return err ? : -ENXIO;
190         }
191
192         g_dev = dev;
193         vchiq_log_info(vchiq_arm_log_level,
194                 "vchiq_init - done (slots %pK, phys %pad)",
195                 vchiq_slot_zero, &slot_phys);
196
197         vchiq_call_connected_callbacks();
198
199         return 0;
200 }
201
202 VCHIQ_STATUS_T
203 vchiq_platform_init_state(VCHIQ_STATE_T *state)
204 {
205         VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
206         struct vchiq_2835_state *platform_state;
207
208         state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
209         platform_state = (struct vchiq_2835_state *)state->platform_state;
210
211         platform_state->inited = 1;
212         status = vchiq_arm_init_state(state, &platform_state->arm_state);
213
214         if (status != VCHIQ_SUCCESS)
215                 platform_state->inited = 0;
216
217         return status;
218 }
219
220 VCHIQ_ARM_STATE_T*
221 vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
222 {
223         struct vchiq_2835_state *platform_state;
224
225         platform_state   = (struct vchiq_2835_state *)state->platform_state;
226
227         if (!platform_state->inited)
228                 BUG();
229
230         return &platform_state->arm_state;
231 }
232
233 void
234 remote_event_signal(REMOTE_EVENT_T *event)
235 {
236         wmb();
237
238         event->fired = 1;
239
240         dsb(sy);         /* data barrier operation */
241
242         if (event->armed)
243                 writel(0, g_regs + BELL2); /* trigger vc interrupt */
244 }
245
246 VCHIQ_STATUS_T
247 vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
248         void *offset, int size, int dir)
249 {
250         struct vchiq_pagelist_info *pagelistinfo;
251
252         WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
253
254         pagelistinfo = create_pagelist((char __user *)offset, size,
255                                        (dir == VCHIQ_BULK_RECEIVE)
256                                        ? PAGELIST_READ
257                                        : PAGELIST_WRITE);
258
259         if (!pagelistinfo)
260                 return VCHIQ_ERROR;
261
262         bulk->handle = memhandle;
263         bulk->data = (void *)(unsigned long)pagelistinfo->dma_addr;
264
265         /*
266          * Store the pagelistinfo address in remote_data,
267          * which isn't used by the slave.
268          */
269         bulk->remote_data = pagelistinfo;
270
271         return VCHIQ_SUCCESS;
272 }
273
274 void
275 vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
276 {
277         if (bulk && bulk->remote_data && bulk->actual)
278                 free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
279                               bulk->actual);
280 }
281
282 void
283 vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
284 {
285         /*
286          * This should only be called on the master (VideoCore) side, but
287          * provide an implementation to avoid the need for ifdefery.
288          */
289         BUG();
290 }
291
292 void
293 vchiq_dump_platform_state(void *dump_context)
294 {
295         char buf[80];
296         int len;
297
298         len = snprintf(buf, sizeof(buf),
299                 "  Platform: 2835 (VC master)");
300         vchiq_dump(dump_context, buf, len + 1);
301 }
302
303 VCHIQ_STATUS_T
304 vchiq_platform_suspend(VCHIQ_STATE_T *state)
305 {
306         return VCHIQ_ERROR;
307 }
308
309 VCHIQ_STATUS_T
310 vchiq_platform_resume(VCHIQ_STATE_T *state)
311 {
312         return VCHIQ_SUCCESS;
313 }
314
315 void
316 vchiq_platform_paused(VCHIQ_STATE_T *state)
317 {
318 }
319
320 void
321 vchiq_platform_resumed(VCHIQ_STATE_T *state)
322 {
323 }
324
325 int
326 vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state)
327 {
328         return 1; // autosuspend not supported - videocore always wanted
329 }
330
331 int
332 vchiq_platform_use_suspend_timer(void)
333 {
334         return 0;
335 }
336 void
337 vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
338 {
339         vchiq_log_info(vchiq_arm_log_level, "Suspend timer not in use");
340 }
341 void
342 vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
343 {
344         (void)state;
345 }
346 /*
347  * Local functions
348  */
349
350 static irqreturn_t
351 vchiq_doorbell_irq(int irq, void *dev_id)
352 {
353         VCHIQ_STATE_T *state = dev_id;
354         irqreturn_t ret = IRQ_NONE;
355         unsigned int status;
356
357         /* Read (and clear) the doorbell */
358         status = readl(g_regs + BELL0);
359
360         if (status & 0x4) {  /* Was the doorbell rung? */
361                 remote_event_pollall(state);
362                 ret = IRQ_HANDLED;
363         }
364
365         return ret;
366 }
367
368 static void
369 cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
370 {
371         if (pagelistinfo->scatterlist_mapped) {
372                 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
373                              pagelistinfo->num_pages, pagelistinfo->dma_dir);
374         }
375
376         if (pagelistinfo->pages_need_release) {
377                 unsigned int i;
378
379                 for (i = 0; i < pagelistinfo->num_pages; i++)
380                         put_page(pagelistinfo->pages[i]);
381         }
382
383         dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
384                           pagelistinfo->pagelist, pagelistinfo->dma_addr);
385 }
386
387 /* There is a potential problem with partial cache lines (pages?)
388  * at the ends of the block when reading. If the CPU accessed anything in
389  * the same line (page?) then it may have pulled old data into the cache,
390  * obscuring the new data underneath. We can solve this by transferring the
391  * partial cache lines separately, and allowing the ARM to copy into the
392  * cached area.
393  */
394
395 static struct vchiq_pagelist_info *
396 create_pagelist(char __user *buf, size_t count, unsigned short type)
397 {
398         PAGELIST_T *pagelist;
399         struct vchiq_pagelist_info *pagelistinfo;
400         struct page **pages;
401         u32 *addrs;
402         unsigned int num_pages, offset, i, k;
403         int actual_pages;
404         size_t pagelist_size;
405         struct scatterlist *scatterlist, *sg;
406         int dma_buffers;
407         dma_addr_t dma_addr;
408
409         offset = ((unsigned int)(unsigned long)buf & (PAGE_SIZE - 1));
410         num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
411
412         pagelist_size = sizeof(PAGELIST_T) +
413                         (num_pages * sizeof(u32)) +
414                         (num_pages * sizeof(pages[0]) +
415                         (num_pages * sizeof(struct scatterlist))) +
416                         sizeof(struct vchiq_pagelist_info);
417
418         /* Allocate enough storage to hold the page pointers and the page
419          * list
420          */
421         pagelist = dma_zalloc_coherent(g_dev,
422                                        pagelist_size,
423                                        &dma_addr,
424                                        GFP_KERNEL);
425
426         vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
427
428         if (!pagelist)
429                 return NULL;
430
431         addrs           = pagelist->addrs;
432         pages           = (struct page **)(addrs + num_pages);
433         scatterlist     = (struct scatterlist *)(pages + num_pages);
434         pagelistinfo    = (struct vchiq_pagelist_info *)
435                           (scatterlist + num_pages);
436
437         pagelist->length = count;
438         pagelist->type = type;
439         pagelist->offset = offset;
440
441         /* Populate the fields of the pagelistinfo structure */
442         pagelistinfo->pagelist = pagelist;
443         pagelistinfo->pagelist_buffer_size = pagelist_size;
444         pagelistinfo->dma_addr = dma_addr;
445         pagelistinfo->dma_dir =  (type == PAGELIST_WRITE) ?
446                                   DMA_TO_DEVICE : DMA_FROM_DEVICE;
447         pagelistinfo->num_pages = num_pages;
448         pagelistinfo->pages_need_release = 0;
449         pagelistinfo->pages = pages;
450         pagelistinfo->scatterlist = scatterlist;
451         pagelistinfo->scatterlist_mapped = 0;
452
453         if (is_vmalloc_addr(buf)) {
454                 unsigned long length = count;
455                 unsigned int off = offset;
456
457                 for (actual_pages = 0; actual_pages < num_pages;
458                      actual_pages++) {
459                         struct page *pg = vmalloc_to_page(buf + (actual_pages *
460                                                                  PAGE_SIZE));
461                         size_t bytes = PAGE_SIZE - off;
462
463                         if (!pg) {
464                                 cleanup_pagelistinfo(pagelistinfo);
465                                 return NULL;
466                         }
467
468                         if (bytes > length)
469                                 bytes = length;
470                         pages[actual_pages] = pg;
471                         length -= bytes;
472                         off = 0;
473                 }
474                 /* do not try and release vmalloc pages */
475         } else {
476                 actual_pages = get_user_pages_fast(
477                                           (unsigned long)buf & PAGE_MASK,
478                                           num_pages,
479                                           type == PAGELIST_READ,
480                                           pages);
481
482                 if (actual_pages != num_pages) {
483                         vchiq_log_info(vchiq_arm_log_level,
484                                        "%s - only %d/%d pages locked",
485                                        __func__, actual_pages, num_pages);
486
487                         /* This is probably due to the process being killed */
488                         while (actual_pages > 0)
489                         {
490                                 actual_pages--;
491                                 put_page(pages[actual_pages]);
492                         }
493                         cleanup_pagelistinfo(pagelistinfo);
494                         return NULL;
495                 }
496                  /* release user pages */
497                 pagelistinfo->pages_need_release = 1;
498         }
499
500         /*
501          * Initialize the scatterlist so that the magic cookie
502          *  is filled if debugging is enabled
503          */
504         sg_init_table(scatterlist, num_pages);
505         /* Now set the pages for each scatterlist */
506         for (i = 0; i < num_pages; i++) {
507                 unsigned int len = PAGE_SIZE - offset;
508
509                 if (len > count)
510                         len = count;
511                 sg_set_page(scatterlist + i, pages[i], len, offset);
512                 offset = 0;
513                 count -= len;
514         }
515
516         dma_buffers = dma_map_sg(g_dev,
517                                  scatterlist,
518                                  num_pages,
519                                  pagelistinfo->dma_dir);
520
521         if (dma_buffers == 0) {
522                 cleanup_pagelistinfo(pagelistinfo);
523                 return NULL;
524         }
525
526         pagelistinfo->scatterlist_mapped = 1;
527
528         /* Combine adjacent blocks for performance */
529         k = 0;
530         for_each_sg(scatterlist, sg, dma_buffers, i) {
531                 u32 len = sg_dma_len(sg);
532                 u32 addr = sg_dma_address(sg);
533
534                 /* Note: addrs is the address + page_count - 1
535                  * The firmware expects blocks after the first to be page-
536                  * aligned and a multiple of the page size
537                  */
538                 WARN_ON(len == 0);
539                 WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
540                 WARN_ON(i && (addr & ~PAGE_MASK));
541                 if (k > 0 &&
542                     ((addrs[k - 1] & PAGE_MASK) +
543                      (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
544                     == (addr & PAGE_MASK))
545                         addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
546                 else
547                         addrs[k++] = (addr & PAGE_MASK) |
548                                 (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
549         }
550
551         /* Partial cache lines (fragments) require special measures */
552         if ((type == PAGELIST_READ) &&
553                 ((pagelist->offset & (g_cache_line_size - 1)) ||
554                 ((pagelist->offset + pagelist->length) &
555                 (g_cache_line_size - 1)))) {
556                 char *fragments;
557
558                 if (down_interruptible(&g_free_fragments_sema) != 0) {
559                         cleanup_pagelistinfo(pagelistinfo);
560                         return NULL;
561                 }
562
563                 WARN_ON(g_free_fragments == NULL);
564
565                 down(&g_free_fragments_mutex);
566                 fragments = g_free_fragments;
567                 WARN_ON(fragments == NULL);
568                 g_free_fragments = *(char **) g_free_fragments;
569                 up(&g_free_fragments_mutex);
570                 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
571                         (fragments - g_fragments_base) / g_fragments_size;
572         }
573
574         return pagelistinfo;
575 }
576
577 static void
578 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
579               int actual)
580 {
581         PAGELIST_T *pagelist   = pagelistinfo->pagelist;
582         struct page **pages    = pagelistinfo->pages;
583         unsigned int num_pages = pagelistinfo->num_pages;
584
585         vchiq_log_trace(vchiq_arm_log_level, "free_pagelist - %pK, %d",
586                         pagelistinfo->pagelist, actual);
587
588         /*
589          * NOTE: dma_unmap_sg must be called before the
590          * cpu can touch any of the data/pages.
591          */
592         dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
593                      pagelistinfo->num_pages, pagelistinfo->dma_dir);
594         pagelistinfo->scatterlist_mapped = 0;
595
596         /* Deal with any partial cache lines (fragments) */
597         if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
598                 char *fragments = g_fragments_base +
599                         (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
600                         g_fragments_size;
601                 int head_bytes, tail_bytes;
602
603                 head_bytes = (g_cache_line_size - pagelist->offset) &
604                         (g_cache_line_size - 1);
605                 tail_bytes = (pagelist->offset + actual) &
606                         (g_cache_line_size - 1);
607
608                 if ((actual >= 0) && (head_bytes != 0)) {
609                         if (head_bytes > actual)
610                                 head_bytes = actual;
611
612                         memcpy((char *)kmap(pages[0]) +
613                                 pagelist->offset,
614                                 fragments,
615                                 head_bytes);
616                         kunmap(pages[0]);
617                 }
618                 if ((actual >= 0) && (head_bytes < actual) &&
619                         (tail_bytes != 0)) {
620                         memcpy((char *)kmap(pages[num_pages - 1]) +
621                                 ((pagelist->offset + actual) &
622                                 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
623                                 fragments + g_cache_line_size,
624                                 tail_bytes);
625                         kunmap(pages[num_pages - 1]);
626                 }
627
628                 down(&g_free_fragments_mutex);
629                 *(char **)fragments = g_free_fragments;
630                 g_free_fragments = fragments;
631                 up(&g_free_fragments_mutex);
632                 up(&g_free_fragments_sema);
633         }
634
635         /* Need to mark all the pages dirty. */
636         if (pagelist->type != PAGELIST_WRITE &&
637             pagelistinfo->pages_need_release) {
638                 unsigned int i;
639
640                 for (i = 0; i < num_pages; i++)
641                         set_page_dirty(pages[i]);
642         }
643
644         cleanup_pagelistinfo(pagelistinfo);
645 }