ceph: use get_user_pages_fast()
[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 typedef struct vchiq_2835_state_struct {
63         int inited;
64         VCHIQ_ARM_STATE_T arm_state;
65 } VCHIQ_2835_ARM_STATE_T;
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 extern int vchiq_arm_log_level;
88
89 static DEFINE_SEMAPHORE(g_free_fragments_mutex);
90
91 static irqreturn_t
92 vchiq_doorbell_irq(int irq, void *dev_id);
93
94 static struct vchiq_pagelist_info *
95 create_pagelist(char __user *buf, size_t count, unsigned short type);
96
97 static void
98 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
99               int actual);
100
101 int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state)
102 {
103         struct device *dev = &pdev->dev;
104         struct rpi_firmware *fw = platform_get_drvdata(pdev);
105         VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
106         struct resource *res;
107         void *slot_mem;
108         dma_addr_t slot_phys;
109         u32 channelbase;
110         int slot_mem_size, frag_mem_size;
111         int err, irq, i;
112
113         /*
114          * VCHI messages between the CPU and firmware use
115          * 32-bit bus addresses.
116          */
117         err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
118
119         if (err < 0)
120                 return err;
121
122         err = of_property_read_u32(dev->of_node, "cache-line-size",
123                                    &g_cache_line_size);
124
125         if (err) {
126                 dev_err(dev, "Missing cache-line-size property\n");
127                 return -ENODEV;
128         }
129
130         g_fragments_size = 2 * g_cache_line_size;
131
132         /* Allocate space for the channels in coherent memory */
133         slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
134         frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
135
136         slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
137                                        &slot_phys, GFP_KERNEL);
138         if (!slot_mem) {
139                 dev_err(dev, "could not allocate DMA memory\n");
140                 return -ENOMEM;
141         }
142
143         WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
144
145         vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
146         if (!vchiq_slot_zero)
147                 return -EINVAL;
148
149         vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
150                 (int)slot_phys + slot_mem_size;
151         vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
152                 MAX_FRAGMENTS;
153
154         g_fragments_base = (char *)slot_mem + slot_mem_size;
155
156         g_free_fragments = g_fragments_base;
157         for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
158                 *(char **)&g_fragments_base[i*g_fragments_size] =
159                         &g_fragments_base[(i + 1)*g_fragments_size];
160         }
161         *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
162         sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
163
164         if (vchiq_init_state(state, vchiq_slot_zero, 0) != VCHIQ_SUCCESS)
165                 return -EINVAL;
166
167         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
168         g_regs = devm_ioremap_resource(&pdev->dev, res);
169         if (IS_ERR(g_regs))
170                 return PTR_ERR(g_regs);
171
172         irq = platform_get_irq(pdev, 0);
173         if (irq <= 0) {
174                 dev_err(dev, "failed to get IRQ\n");
175                 return irq;
176         }
177
178         err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
179                                "VCHIQ doorbell", state);
180         if (err) {
181                 dev_err(dev, "failed to register irq=%d\n", irq);
182                 return err;
183         }
184
185         /* Send the base address of the slots to VideoCore */
186         channelbase = slot_phys;
187         err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
188                                     &channelbase, sizeof(channelbase));
189         if (err || channelbase) {
190                 dev_err(dev, "failed to set channelbase\n");
191                 return err ? : -ENXIO;
192         }
193
194         g_dev = dev;
195         vchiq_log_info(vchiq_arm_log_level,
196                 "vchiq_init - done (slots %pK, phys %pad)",
197                 vchiq_slot_zero, &slot_phys);
198
199         vchiq_call_connected_callbacks();
200
201         return 0;
202 }
203
204 VCHIQ_STATUS_T
205 vchiq_platform_init_state(VCHIQ_STATE_T *state)
206 {
207         VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
208
209         state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
210         ((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->inited = 1;
211         status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->arm_state);
212         if (status != VCHIQ_SUCCESS)
213         {
214                 ((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->inited = 0;
215         }
216         return status;
217 }
218
219 VCHIQ_ARM_STATE_T*
220 vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
221 {
222         if (!((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->inited)
223         {
224                 BUG();
225         }
226         return &((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->arm_state;
227 }
228
229 void
230 remote_event_signal(REMOTE_EVENT_T *event)
231 {
232         wmb();
233
234         event->fired = 1;
235
236         dsb(sy);         /* data barrier operation */
237
238         if (event->armed)
239                 writel(0, g_regs + BELL2); /* trigger vc interrupt */
240 }
241
242 VCHIQ_STATUS_T
243 vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
244         void *offset, int size, int dir)
245 {
246         struct vchiq_pagelist_info *pagelistinfo;
247
248         WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
249
250         pagelistinfo = create_pagelist((char __user *)offset, size,
251                                        (dir == VCHIQ_BULK_RECEIVE)
252                                        ? PAGELIST_READ
253                                        : PAGELIST_WRITE);
254
255         if (!pagelistinfo)
256                 return VCHIQ_ERROR;
257
258         bulk->handle = memhandle;
259         bulk->data = (void *)(unsigned long)pagelistinfo->dma_addr;
260
261         /*
262          * Store the pagelistinfo address in remote_data,
263          * which isn't used by the slave.
264          */
265         bulk->remote_data = pagelistinfo;
266
267         return VCHIQ_SUCCESS;
268 }
269
270 void
271 vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
272 {
273         if (bulk && bulk->remote_data && bulk->actual)
274                 free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
275                               bulk->actual);
276 }
277
278 void
279 vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
280 {
281         /*
282          * This should only be called on the master (VideoCore) side, but
283          * provide an implementation to avoid the need for ifdefery.
284          */
285         BUG();
286 }
287
288 void
289 vchiq_dump_platform_state(void *dump_context)
290 {
291         char buf[80];
292         int len;
293
294         len = snprintf(buf, sizeof(buf),
295                 "  Platform: 2835 (VC master)");
296         vchiq_dump(dump_context, buf, len + 1);
297 }
298
299 VCHIQ_STATUS_T
300 vchiq_platform_suspend(VCHIQ_STATE_T *state)
301 {
302         return VCHIQ_ERROR;
303 }
304
305 VCHIQ_STATUS_T
306 vchiq_platform_resume(VCHIQ_STATE_T *state)
307 {
308         return VCHIQ_SUCCESS;
309 }
310
311 void
312 vchiq_platform_paused(VCHIQ_STATE_T *state)
313 {
314 }
315
316 void
317 vchiq_platform_resumed(VCHIQ_STATE_T *state)
318 {
319 }
320
321 int
322 vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state)
323 {
324         return 1; // autosuspend not supported - videocore always wanted
325 }
326
327 int
328 vchiq_platform_use_suspend_timer(void)
329 {
330         return 0;
331 }
332 void
333 vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
334 {
335         vchiq_log_info(vchiq_arm_log_level, "Suspend timer not in use");
336 }
337 void
338 vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
339 {
340         (void)state;
341 }
342 /*
343  * Local functions
344  */
345
346 static irqreturn_t
347 vchiq_doorbell_irq(int irq, void *dev_id)
348 {
349         VCHIQ_STATE_T *state = dev_id;
350         irqreturn_t ret = IRQ_NONE;
351         unsigned int status;
352
353         /* Read (and clear) the doorbell */
354         status = readl(g_regs + BELL0);
355
356         if (status & 0x4) {  /* Was the doorbell rung? */
357                 remote_event_pollall(state);
358                 ret = IRQ_HANDLED;
359         }
360
361         return ret;
362 }
363
364 static void
365 cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
366 {
367         if (pagelistinfo->scatterlist_mapped) {
368                 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
369                              pagelistinfo->num_pages, pagelistinfo->dma_dir);
370         }
371
372         if (pagelistinfo->pages_need_release) {
373                 unsigned int i;
374
375                 for (i = 0; i < pagelistinfo->num_pages; i++)
376                         put_page(pagelistinfo->pages[i]);
377         }
378
379         dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
380                           pagelistinfo->pagelist, pagelistinfo->dma_addr);
381 }
382
383 /* There is a potential problem with partial cache lines (pages?)
384 ** at the ends of the block when reading. If the CPU accessed anything in
385 ** the same line (page?) then it may have pulled old data into the cache,
386 ** obscuring the new data underneath. We can solve this by transferring the
387 ** partial cache lines separately, and allowing the ARM to copy into the
388 ** cached area.
389 */
390
391 static struct vchiq_pagelist_info *
392 create_pagelist(char __user *buf, size_t count, unsigned short type)
393 {
394         PAGELIST_T *pagelist;
395         struct vchiq_pagelist_info *pagelistinfo;
396         struct page **pages;
397         u32 *addrs;
398         unsigned int num_pages, offset, i, k;
399         int actual_pages;
400         size_t pagelist_size;
401         struct scatterlist *scatterlist, *sg;
402         int dma_buffers;
403         dma_addr_t dma_addr;
404
405         offset = ((unsigned int)(unsigned long)buf & (PAGE_SIZE - 1));
406         num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
407
408         pagelist_size = sizeof(PAGELIST_T) +
409                         (num_pages * sizeof(u32)) +
410                         (num_pages * sizeof(pages[0]) +
411                         (num_pages * sizeof(struct scatterlist))) +
412                         sizeof(struct vchiq_pagelist_info);
413
414         /* Allocate enough storage to hold the page pointers and the page
415         ** list
416         */
417         pagelist = dma_zalloc_coherent(g_dev,
418                                        pagelist_size,
419                                        &dma_addr,
420                                        GFP_KERNEL);
421
422         vchiq_log_trace(vchiq_arm_log_level, "create_pagelist - %pK",
423                         pagelist);
424         if (!pagelist)
425                 return NULL;
426
427         addrs           = pagelist->addrs;
428         pages           = (struct page **)(addrs + num_pages);
429         scatterlist     = (struct scatterlist *)(pages + num_pages);
430         pagelistinfo    = (struct vchiq_pagelist_info *)
431                           (scatterlist + num_pages);
432
433         pagelist->length = count;
434         pagelist->type = type;
435         pagelist->offset = offset;
436
437         /* Populate the fields of the pagelistinfo structure */
438         pagelistinfo->pagelist = pagelist;
439         pagelistinfo->pagelist_buffer_size = pagelist_size;
440         pagelistinfo->dma_addr = dma_addr;
441         pagelistinfo->dma_dir =  (type == PAGELIST_WRITE) ?
442                                   DMA_TO_DEVICE : DMA_FROM_DEVICE;
443         pagelistinfo->num_pages = num_pages;
444         pagelistinfo->pages_need_release = 0;
445         pagelistinfo->pages = pages;
446         pagelistinfo->scatterlist = scatterlist;
447         pagelistinfo->scatterlist_mapped = 0;
448
449         if (is_vmalloc_addr(buf)) {
450                 unsigned long length = count;
451                 unsigned int off = offset;
452
453                 for (actual_pages = 0; actual_pages < num_pages;
454                      actual_pages++) {
455                         struct page *pg = vmalloc_to_page(buf + (actual_pages *
456                                                                  PAGE_SIZE));
457                         size_t bytes = PAGE_SIZE - off;
458
459                         if (!pg) {
460                                 cleanup_pagelistinfo(pagelistinfo);
461                                 return NULL;
462                         }
463
464                         if (bytes > length)
465                                 bytes = length;
466                         pages[actual_pages] = pg;
467                         length -= bytes;
468                         off = 0;
469                 }
470                 /* do not try and release vmalloc pages */
471         } else {
472                 actual_pages = get_user_pages_fast(
473                                           (unsigned long)buf & PAGE_MASK,
474                                           num_pages,
475                                           type == PAGELIST_READ,
476                                           pages);
477
478                 if (actual_pages != num_pages) {
479                         vchiq_log_info(vchiq_arm_log_level,
480                                        "create_pagelist - only %d/%d pages locked",
481                                        actual_pages,
482                                        num_pages);
483
484                         /* This is probably due to the process being killed */
485                         while (actual_pages > 0)
486                         {
487                                 actual_pages--;
488                                 put_page(pages[actual_pages]);
489                         }
490                         cleanup_pagelistinfo(pagelistinfo);
491                         return NULL;
492                 }
493                  /* release user pages */
494                 pagelistinfo->pages_need_release = 1;
495         }
496
497         /*
498          * Initialize the scatterlist so that the magic cookie
499          *  is filled if debugging is enabled
500          */
501         sg_init_table(scatterlist, num_pages);
502         /* Now set the pages for each scatterlist */
503         for (i = 0; i < num_pages; i++) {
504                 unsigned int len = PAGE_SIZE - offset;
505
506                 if (len > count)
507                         len = count;
508                 sg_set_page(scatterlist + i, pages[i], len, offset);
509                 offset = 0;
510                 count -= len;
511         }
512
513         dma_buffers = dma_map_sg(g_dev,
514                                  scatterlist,
515                                  num_pages,
516                                  pagelistinfo->dma_dir);
517
518         if (dma_buffers == 0) {
519                 cleanup_pagelistinfo(pagelistinfo);
520                 return NULL;
521         }
522
523         pagelistinfo->scatterlist_mapped = 1;
524
525         /* Combine adjacent blocks for performance */
526         k = 0;
527         for_each_sg(scatterlist, sg, dma_buffers, i) {
528                 u32 len = sg_dma_len(sg);
529                 u32 addr = sg_dma_address(sg);
530
531                 /* Note: addrs is the address + page_count - 1
532                  * The firmware expects blocks after the first to be page-
533                  * aligned and a multiple of the page size
534                  */
535                 WARN_ON(len == 0);
536                 WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
537                 WARN_ON(i && (addr & ~PAGE_MASK));
538                 if (k > 0 &&
539                     ((addrs[k - 1] & PAGE_MASK) +
540                      (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
541                     == (addr & PAGE_MASK))
542                         addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
543                 else
544                         addrs[k++] = (addr & PAGE_MASK) |
545                                 (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
546         }
547
548         /* Partial cache lines (fragments) require special measures */
549         if ((type == PAGELIST_READ) &&
550                 ((pagelist->offset & (g_cache_line_size - 1)) ||
551                 ((pagelist->offset + pagelist->length) &
552                 (g_cache_line_size - 1)))) {
553                 char *fragments;
554
555                 if (down_interruptible(&g_free_fragments_sema) != 0) {
556                         cleanup_pagelistinfo(pagelistinfo);
557                         return NULL;
558                 }
559
560                 WARN_ON(g_free_fragments == NULL);
561
562                 down(&g_free_fragments_mutex);
563                 fragments = g_free_fragments;
564                 WARN_ON(fragments == NULL);
565                 g_free_fragments = *(char **) g_free_fragments;
566                 up(&g_free_fragments_mutex);
567                 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
568                         (fragments - g_fragments_base) / g_fragments_size;
569         }
570
571         return pagelistinfo;
572 }
573
574 static void
575 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
576               int actual)
577 {
578         PAGELIST_T *pagelist   = pagelistinfo->pagelist;
579         struct page **pages    = pagelistinfo->pages;
580         unsigned int num_pages = pagelistinfo->num_pages;
581
582         vchiq_log_trace(vchiq_arm_log_level, "free_pagelist - %pK, %d",
583                         pagelistinfo->pagelist, actual);
584
585         /*
586          * NOTE: dma_unmap_sg must be called before the
587          * cpu can touch any of the data/pages.
588          */
589         dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
590                      pagelistinfo->num_pages, pagelistinfo->dma_dir);
591         pagelistinfo->scatterlist_mapped = 0;
592
593         /* Deal with any partial cache lines (fragments) */
594         if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
595                 char *fragments = g_fragments_base +
596                         (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
597                         g_fragments_size;
598                 int head_bytes, tail_bytes;
599
600                 head_bytes = (g_cache_line_size - pagelist->offset) &
601                         (g_cache_line_size - 1);
602                 tail_bytes = (pagelist->offset + actual) &
603                         (g_cache_line_size - 1);
604
605                 if ((actual >= 0) && (head_bytes != 0)) {
606                         if (head_bytes > actual)
607                                 head_bytes = actual;
608
609                         memcpy((char *)page_address(pages[0]) +
610                                 pagelist->offset,
611                                 fragments,
612                                 head_bytes);
613                 }
614                 if ((actual >= 0) && (head_bytes < actual) &&
615                         (tail_bytes != 0)) {
616                         memcpy((char *)page_address(pages[num_pages - 1]) +
617                                 ((pagelist->offset + actual) &
618                                 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
619                                 fragments + g_cache_line_size,
620                                 tail_bytes);
621                 }
622
623                 down(&g_free_fragments_mutex);
624                 *(char **)fragments = g_free_fragments;
625                 g_free_fragments = fragments;
626                 up(&g_free_fragments_mutex);
627                 up(&g_free_fragments_sema);
628         }
629
630         /* Need to mark all the pages dirty. */
631         if (pagelist->type != PAGELIST_WRITE &&
632             pagelistinfo->pages_need_release) {
633                 unsigned int i;
634
635                 for (i = 0; i < num_pages; i++)
636                         set_page_dirty(pages[i]);
637         }
638
639         cleanup_pagelistinfo(pagelistinfo);
640 }