]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
c29040fdf9a7757b7c0d428616e244a6e4d5f697
[mirror_ubuntu-zesty-kernel.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/version.h>
41 #include <linux/io.h>
42 #include <linux/platform_device.h>
43 #include <linux/uaccess.h>
44 #include <linux/of.h>
45 #include <asm/pgtable.h>
46 #include <soc/bcm2835/raspberrypi-firmware.h>
47
48 #define dmac_map_area __glue(_CACHE,_dma_map_area)
49 #define dmac_unmap_area __glue(_CACHE,_dma_unmap_area)
50
51 extern void dmac_map_area(const void *, size_t, int);
52 extern void dmac_unmap_area(const void *, size_t, int);
53
54 #define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
55
56 #define VCHIQ_ARM_ADDRESS(x) ((void *)((char *)x + g_virt_to_bus_offset))
57
58 #include "vchiq_arm.h"
59 #include "vchiq_2835.h"
60 #include "vchiq_connected.h"
61 #include "vchiq_killable.h"
62
63 #define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
64
65 #define BELL0 0x00
66 #define BELL2 0x08
67
68 typedef struct vchiq_2835_state_struct {
69 int inited;
70 VCHIQ_ARM_STATE_T arm_state;
71 } VCHIQ_2835_ARM_STATE_T;
72
73 static void __iomem *g_regs;
74 static unsigned int g_cache_line_size = sizeof(CACHE_LINE_SIZE);
75 static unsigned int g_fragments_size;
76 static char *g_fragments_base;
77 static char *g_free_fragments;
78 static struct semaphore g_free_fragments_sema;
79 static unsigned long g_virt_to_bus_offset;
80
81 extern int vchiq_arm_log_level;
82
83 static DEFINE_SEMAPHORE(g_free_fragments_mutex);
84
85 static irqreturn_t
86 vchiq_doorbell_irq(int irq, void *dev_id);
87
88 static int
89 create_pagelist(char __user *buf, size_t count, unsigned short type,
90 struct task_struct *task, PAGELIST_T ** ppagelist);
91
92 static void
93 free_pagelist(PAGELIST_T *pagelist, int actual);
94
95 int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state)
96 {
97 struct device *dev = &pdev->dev;
98 struct rpi_firmware *fw = platform_get_drvdata(pdev);
99 VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
100 struct resource *res;
101 void *slot_mem;
102 dma_addr_t slot_phys;
103 u32 channelbase;
104 int slot_mem_size, frag_mem_size;
105 int err, irq, i;
106
107 g_virt_to_bus_offset = virt_to_dma(dev, (void *)0);
108
109 (void)of_property_read_u32(dev->of_node, "cache-line-size",
110 &g_cache_line_size);
111 g_fragments_size = 2 * g_cache_line_size;
112
113 /* Allocate space for the channels in coherent memory */
114 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
115 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
116
117 slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
118 &slot_phys, GFP_KERNEL);
119 if (!slot_mem) {
120 dev_err(dev, "could not allocate DMA memory\n");
121 return -ENOMEM;
122 }
123
124 WARN_ON(((int)slot_mem & (PAGE_SIZE - 1)) != 0);
125
126 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
127 if (!vchiq_slot_zero)
128 return -EINVAL;
129
130 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
131 (int)slot_phys + slot_mem_size;
132 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
133 MAX_FRAGMENTS;
134
135 g_fragments_base = (char *)slot_mem + slot_mem_size;
136 slot_mem_size += frag_mem_size;
137
138 g_free_fragments = g_fragments_base;
139 for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
140 *(char **)&g_fragments_base[i*g_fragments_size] =
141 &g_fragments_base[(i + 1)*g_fragments_size];
142 }
143 *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
144 sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
145
146 if (vchiq_init_state(state, vchiq_slot_zero, 0) != VCHIQ_SUCCESS)
147 return -EINVAL;
148
149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150 g_regs = devm_ioremap_resource(&pdev->dev, res);
151 if (IS_ERR(g_regs))
152 return PTR_ERR(g_regs);
153
154 irq = platform_get_irq(pdev, 0);
155 if (irq <= 0) {
156 dev_err(dev, "failed to get IRQ\n");
157 return irq;
158 }
159
160 err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
161 "VCHIQ doorbell", state);
162 if (err) {
163 dev_err(dev, "failed to register irq=%d\n", irq);
164 return err;
165 }
166
167 /* Send the base address of the slots to VideoCore */
168 channelbase = slot_phys;
169 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
170 &channelbase, sizeof(channelbase));
171 if (err || channelbase) {
172 dev_err(dev, "failed to set channelbase\n");
173 return err ? : -ENXIO;
174 }
175
176 vchiq_log_info(vchiq_arm_log_level,
177 "vchiq_init - done (slots %x, phys %pad)",
178 (unsigned int)vchiq_slot_zero, &slot_phys);
179
180 vchiq_call_connected_callbacks();
181
182 return 0;
183 }
184
185 VCHIQ_STATUS_T
186 vchiq_platform_init_state(VCHIQ_STATE_T *state)
187 {
188 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
189 state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
190 ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
191 status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
192 if(status != VCHIQ_SUCCESS)
193 {
194 ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
195 }
196 return status;
197 }
198
199 VCHIQ_ARM_STATE_T*
200 vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
201 {
202 if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
203 {
204 BUG();
205 }
206 return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
207 }
208
209 void
210 remote_event_signal(REMOTE_EVENT_T *event)
211 {
212 wmb();
213
214 event->fired = 1;
215
216 dsb(); /* data barrier operation */
217
218 if (event->armed)
219 writel(0, g_regs + BELL2); /* trigger vc interrupt */
220 }
221
222 int
223 vchiq_copy_from_user(void *dst, const void *src, int size)
224 {
225 if ((uint32_t)src < TASK_SIZE) {
226 return copy_from_user(dst, src, size);
227 } else {
228 memcpy(dst, src, size);
229 return 0;
230 }
231 }
232
233 VCHIQ_STATUS_T
234 vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
235 void *offset, int size, int dir)
236 {
237 PAGELIST_T *pagelist;
238 int ret;
239
240 WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
241
242 ret = create_pagelist((char __user *)offset, size,
243 (dir == VCHIQ_BULK_RECEIVE)
244 ? PAGELIST_READ
245 : PAGELIST_WRITE,
246 current,
247 &pagelist);
248 if (ret != 0)
249 return VCHIQ_ERROR;
250
251 bulk->handle = memhandle;
252 bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
253
254 /* Store the pagelist address in remote_data, which isn't used by the
255 slave. */
256 bulk->remote_data = pagelist;
257
258 return VCHIQ_SUCCESS;
259 }
260
261 void
262 vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
263 {
264 if (bulk && bulk->remote_data && bulk->actual)
265 free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
266 }
267
268 void
269 vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
270 {
271 /*
272 * This should only be called on the master (VideoCore) side, but
273 * provide an implementation to avoid the need for ifdefery.
274 */
275 BUG();
276 }
277
278 void
279 vchiq_dump_platform_state(void *dump_context)
280 {
281 char buf[80];
282 int len;
283 len = snprintf(buf, sizeof(buf),
284 " Platform: 2835 (VC master)");
285 vchiq_dump(dump_context, buf, len + 1);
286 }
287
288 VCHIQ_STATUS_T
289 vchiq_platform_suspend(VCHIQ_STATE_T *state)
290 {
291 return VCHIQ_ERROR;
292 }
293
294 VCHIQ_STATUS_T
295 vchiq_platform_resume(VCHIQ_STATE_T *state)
296 {
297 return VCHIQ_SUCCESS;
298 }
299
300 void
301 vchiq_platform_paused(VCHIQ_STATE_T *state)
302 {
303 }
304
305 void
306 vchiq_platform_resumed(VCHIQ_STATE_T *state)
307 {
308 }
309
310 int
311 vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
312 {
313 return 1; // autosuspend not supported - videocore always wanted
314 }
315
316 int
317 vchiq_platform_use_suspend_timer(void)
318 {
319 return 0;
320 }
321 void
322 vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
323 {
324 vchiq_log_info(vchiq_arm_log_level, "Suspend timer not in use");
325 }
326 void
327 vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
328 {
329 (void)state;
330 }
331 /*
332 * Local functions
333 */
334
335 static irqreturn_t
336 vchiq_doorbell_irq(int irq, void *dev_id)
337 {
338 VCHIQ_STATE_T *state = dev_id;
339 irqreturn_t ret = IRQ_NONE;
340 unsigned int status;
341
342 /* Read (and clear) the doorbell */
343 status = readl(g_regs + BELL0);
344
345 if (status & 0x4) { /* Was the doorbell rung? */
346 remote_event_pollall(state);
347 ret = IRQ_HANDLED;
348 }
349
350 return ret;
351 }
352
353 /* There is a potential problem with partial cache lines (pages?)
354 ** at the ends of the block when reading. If the CPU accessed anything in
355 ** the same line (page?) then it may have pulled old data into the cache,
356 ** obscuring the new data underneath. We can solve this by transferring the
357 ** partial cache lines separately, and allowing the ARM to copy into the
358 ** cached area.
359
360 ** N.B. This implementation plays slightly fast and loose with the Linux
361 ** driver programming rules, e.g. its use of dmac_map_area instead of
362 ** dma_map_single, but it isn't a multi-platform driver and it benefits
363 ** from increased speed as a result.
364 */
365
366 static int
367 create_pagelist(char __user *buf, size_t count, unsigned short type,
368 struct task_struct *task, PAGELIST_T ** ppagelist)
369 {
370 PAGELIST_T *pagelist;
371 struct page **pages;
372 unsigned long *addrs;
373 unsigned int num_pages, offset, i;
374 char *addr, *base_addr, *next_addr;
375 int run, addridx, actual_pages;
376 unsigned long *need_release;
377
378 offset = (unsigned int)buf & (PAGE_SIZE - 1);
379 num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
380
381 *ppagelist = NULL;
382
383 /* Allocate enough storage to hold the page pointers and the page
384 ** list
385 */
386 pagelist = kmalloc(sizeof(PAGELIST_T) +
387 (num_pages * sizeof(unsigned long)) +
388 sizeof(unsigned long) +
389 (num_pages * sizeof(pages[0])),
390 GFP_KERNEL);
391
392 vchiq_log_trace(vchiq_arm_log_level,
393 "create_pagelist - %x", (unsigned int)pagelist);
394 if (!pagelist)
395 return -ENOMEM;
396
397 addrs = pagelist->addrs;
398 need_release = (unsigned long *)(addrs + num_pages);
399 pages = (struct page **)(addrs + num_pages + 1);
400
401 if (is_vmalloc_addr(buf)) {
402 int dir = (type == PAGELIST_WRITE) ?
403 DMA_TO_DEVICE : DMA_FROM_DEVICE;
404 unsigned long length = count;
405 unsigned int off = offset;
406
407 for (actual_pages = 0; actual_pages < num_pages;
408 actual_pages++) {
409 struct page *pg = vmalloc_to_page(buf + (actual_pages *
410 PAGE_SIZE));
411 size_t bytes = PAGE_SIZE - off;
412
413 if (bytes > length)
414 bytes = length;
415 pages[actual_pages] = pg;
416 dmac_map_area(page_address(pg) + off, bytes, dir);
417 length -= bytes;
418 off = 0;
419 }
420 *need_release = 0; /* do not try and release vmalloc pages */
421 } else {
422 down_read(&task->mm->mmap_sem);
423 actual_pages = get_user_pages(task, task->mm,
424 (unsigned long)buf & ~(PAGE_SIZE - 1),
425 num_pages,
426 (type == PAGELIST_READ) /*Write */ ,
427 0 /*Force */ ,
428 pages,
429 NULL /*vmas */);
430 up_read(&task->mm->mmap_sem);
431
432 if (actual_pages != num_pages) {
433 vchiq_log_info(vchiq_arm_log_level,
434 "create_pagelist - only %d/%d pages locked",
435 actual_pages,
436 num_pages);
437
438 /* This is probably due to the process being killed */
439 while (actual_pages > 0)
440 {
441 actual_pages--;
442 page_cache_release(pages[actual_pages]);
443 }
444 kfree(pagelist);
445 if (actual_pages == 0)
446 actual_pages = -ENOMEM;
447 return actual_pages;
448 }
449 *need_release = 1; /* release user pages */
450 }
451
452 pagelist->length = count;
453 pagelist->type = type;
454 pagelist->offset = offset;
455
456 /* Group the pages into runs of contiguous pages */
457
458 base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
459 next_addr = base_addr + PAGE_SIZE;
460 addridx = 0;
461 run = 0;
462
463 for (i = 1; i < num_pages; i++) {
464 addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
465 if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
466 next_addr += PAGE_SIZE;
467 run++;
468 } else {
469 addrs[addridx] = (unsigned long)base_addr + run;
470 addridx++;
471 base_addr = addr;
472 next_addr = addr + PAGE_SIZE;
473 run = 0;
474 }
475 }
476
477 addrs[addridx] = (unsigned long)base_addr + run;
478 addridx++;
479
480 /* Partial cache lines (fragments) require special measures */
481 if ((type == PAGELIST_READ) &&
482 ((pagelist->offset & (g_cache_line_size - 1)) ||
483 ((pagelist->offset + pagelist->length) &
484 (g_cache_line_size - 1)))) {
485 char *fragments;
486
487 if (down_interruptible(&g_free_fragments_sema) != 0) {
488 kfree(pagelist);
489 return -EINTR;
490 }
491
492 WARN_ON(g_free_fragments == NULL);
493
494 down(&g_free_fragments_mutex);
495 fragments = g_free_fragments;
496 WARN_ON(fragments == NULL);
497 g_free_fragments = *(char **) g_free_fragments;
498 up(&g_free_fragments_mutex);
499 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
500 (fragments - g_fragments_base) / g_fragments_size;
501 }
502
503 dmac_flush_range(pagelist, addrs + num_pages);
504
505 *ppagelist = pagelist;
506
507 return 0;
508 }
509
510 static void
511 free_pagelist(PAGELIST_T *pagelist, int actual)
512 {
513 unsigned long *need_release;
514 struct page **pages;
515 unsigned int num_pages, i;
516
517 vchiq_log_trace(vchiq_arm_log_level,
518 "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
519
520 num_pages =
521 (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
522 PAGE_SIZE;
523
524 need_release = (unsigned long *)(pagelist->addrs + num_pages);
525 pages = (struct page **)(pagelist->addrs + num_pages + 1);
526
527 /* Deal with any partial cache lines (fragments) */
528 if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
529 char *fragments = g_fragments_base +
530 (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
531 g_fragments_size;
532 int head_bytes, tail_bytes;
533 head_bytes = (g_cache_line_size - pagelist->offset) &
534 (g_cache_line_size - 1);
535 tail_bytes = (pagelist->offset + actual) &
536 (g_cache_line_size - 1);
537
538 if ((actual >= 0) && (head_bytes != 0)) {
539 if (head_bytes > actual)
540 head_bytes = actual;
541
542 memcpy((char *)page_address(pages[0]) +
543 pagelist->offset,
544 fragments,
545 head_bytes);
546 }
547 if ((actual >= 0) && (head_bytes < actual) &&
548 (tail_bytes != 0)) {
549 memcpy((char *)page_address(pages[num_pages - 1]) +
550 ((pagelist->offset + actual) &
551 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
552 fragments + g_cache_line_size,
553 tail_bytes);
554 }
555
556 down(&g_free_fragments_mutex);
557 *(char **)fragments = g_free_fragments;
558 g_free_fragments = fragments;
559 up(&g_free_fragments_mutex);
560 up(&g_free_fragments_sema);
561 }
562
563 if (*need_release) {
564 unsigned int length = pagelist->length;
565 unsigned int offset = pagelist->offset;
566
567 for (i = 0; i < num_pages; i++) {
568 struct page *pg = pages[i];
569
570 if (pagelist->type != PAGELIST_WRITE) {
571 unsigned int bytes = PAGE_SIZE - offset;
572
573 if (bytes > length)
574 bytes = length;
575 dmac_unmap_area(page_address(pg) + offset,
576 bytes, DMA_FROM_DEVICE);
577 length -= bytes;
578 offset = 0;
579 set_page_dirty(pg);
580 }
581 page_cache_release(pg);
582 }
583 }
584
585 kfree(pagelist);
586 }