brcm2708: add linux 4.1 support
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0010-cma-Add-vc_cma-driver-to-enable-use-of-CMA.patch
1 From 2bb9c01de4c90a8b7a9b52be95f504177abeaf45 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 3 Jul 2013 00:31:47 +0100
4 Subject: [PATCH 010/121] cma: Add vc_cma driver to enable use of CMA
5
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
7
8 vc_cma: Make the vc_cma area the default contiguous DMA area
9 ---
10  drivers/char/Kconfig                  |    2 +
11  drivers/char/Makefile                 |    1 +
12  drivers/char/broadcom/Kconfig         |   15 +
13  drivers/char/broadcom/Makefile        |    1 +
14  drivers/char/broadcom/vc_cma/Makefile |   14 +
15  drivers/char/broadcom/vc_cma/vc_cma.c | 1193 +++++++++++++++++++++++++++++++++
16  include/linux/broadcom/vc_cma.h       |   29 +
17  7 files changed, 1255 insertions(+)
18  create mode 100644 drivers/char/broadcom/Kconfig
19  create mode 100644 drivers/char/broadcom/Makefile
20  create mode 100644 drivers/char/broadcom/vc_cma/Makefile
21  create mode 100644 drivers/char/broadcom/vc_cma/vc_cma.c
22  create mode 100644 include/linux/broadcom/vc_cma.h
23
24 --- a/drivers/char/Kconfig
25 +++ b/drivers/char/Kconfig
26 @@ -590,6 +590,8 @@ config DEVPORT
27  
28  source "drivers/s390/char/Kconfig"
29  
30 +source "drivers/char/broadcom/Kconfig"
31 +
32  config MSM_SMD_PKT
33         bool "Enable device interface for some SMD packet ports"
34         default n
35 --- a/drivers/char/Makefile
36 +++ b/drivers/char/Makefile
37 @@ -62,3 +62,4 @@ js-rtc-y = rtc.o
38  
39  obj-$(CONFIG_TILE_SROM)                += tile-srom.o
40  obj-$(CONFIG_XILLYBUS)         += xillybus/
41 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
42 --- /dev/null
43 +++ b/drivers/char/broadcom/Kconfig
44 @@ -0,0 +1,15 @@
45 +#
46 +# Broadcom char driver config
47 +#
48 +
49 +menuconfig BRCM_CHAR_DRIVERS
50 +       bool "Broadcom Char Drivers"
51 +       help
52 +         Broadcom's char drivers
53 +
54 +config BCM_VC_CMA
55 +       bool "Videocore CMA"
56 +       depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
57 +       default n
58 +        help
59 +          Helper for videocore CMA access.
60 --- /dev/null
61 +++ b/drivers/char/broadcom/Makefile
62 @@ -0,0 +1 @@
63 +obj-$(CONFIG_BCM_VC_CMA)       += vc_cma/
64 --- /dev/null
65 +++ b/drivers/char/broadcom/vc_cma/Makefile
66 @@ -0,0 +1,14 @@
67 +ccflags-y  += -Wall -Wstrict-prototypes -Wno-trigraphs
68 +ccflags-y  += -Werror
69 +ccflags-y  += -Iinclude/linux/broadcom
70 +ccflags-y  += -Idrivers/misc/vc04_services
71 +ccflags-y  += -Idrivers/misc/vc04_services/interface/vchi
72 +ccflags-y  += -Idrivers/misc/vc04_services/interface/vchiq_arm
73 +
74 +ccflags-y  += -D__KERNEL__
75 +ccflags-y  += -D__linux__
76 +ccflags-y  += -Werror
77 +
78 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
79 +
80 +vc-cma-objs := vc_cma.o
81 --- /dev/null
82 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
83 @@ -0,0 +1,1193 @@
84 +/**
85 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
86 + *
87 + * Redistribution and use in source and binary forms, with or without
88 + * modification, are permitted provided that the following conditions
89 + * are met:
90 + * 1. Redistributions of source code must retain the above copyright
91 + *    notice, this list of conditions, and the following disclaimer,
92 + *    without modification.
93 + * 2. Redistributions in binary form must reproduce the above copyright
94 + *    notice, this list of conditions and the following disclaimer in the
95 + *    documentation and/or other materials provided with the distribution.
96 + * 3. The names of the above-listed copyright holders may not be used
97 + *    to endorse or promote products derived from this software without
98 + *    specific prior written permission.
99 + *
100 + * ALTERNATIVELY, this software may be distributed under the terms of the
101 + * GNU General Public License ("GPL") version 2, as published by the Free
102 + * Software Foundation.
103 + *
104 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
105 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
106 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
107 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
108 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
109 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
110 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
111 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
112 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
113 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
114 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
115 + */
116 +
117 +#include <linux/kernel.h>
118 +#include <linux/module.h>
119 +#include <linux/kthread.h>
120 +#include <linux/fs.h>
121 +#include <linux/device.h>
122 +#include <linux/cdev.h>
123 +#include <linux/mm.h>
124 +#include <linux/proc_fs.h>
125 +#include <linux/seq_file.h>
126 +#include <linux/dma-mapping.h>
127 +#include <linux/dma-contiguous.h>
128 +#include <linux/platform_device.h>
129 +#include <linux/uaccess.h>
130 +#include <asm/cacheflush.h>
131 +
132 +#include "vc_cma.h"
133 +
134 +#include "vchiq_util.h"
135 +#include "vchiq_connected.h"
136 +//#include "debug_sym.h"
137 +//#include "vc_mem.h"
138 +
139 +#define DRIVER_NAME  "vc-cma"
140 +
141 +#define LOG_DBG(fmt, ...) \
142 +       if (vc_cma_debug) \
143 +               printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
144 +#define LOG_INFO(fmt, ...) \
145 +       printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
146 +#define LOG_ERR(fmt, ...) \
147 +       printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
148 +
149 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
150 +#define VC_CMA_VERSION 2
151 +
152 +#define VC_CMA_CHUNK_ORDER 6   /* 256K */
153 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
154 +#define VC_CMA_MAX_PARAMS_PER_MSG \
155 +       ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
156 +#define VC_CMA_RESERVE_COUNT_MAX 16
157 +
158 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
159 +
160 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
161 +
162 +#define loud_error(...) \
163 +       LOG_ERR("===== " __VA_ARGS__)
164 +
165 +enum {
166 +       VC_CMA_MSG_QUIT,
167 +       VC_CMA_MSG_OPEN,
168 +       VC_CMA_MSG_TICK,
169 +       VC_CMA_MSG_ALLOC,       /* chunk count */
170 +       VC_CMA_MSG_FREE,        /* chunk, chunk, ... */
171 +       VC_CMA_MSG_ALLOCATED,   /* chunk, chunk, ... */
172 +       VC_CMA_MSG_REQUEST_ALLOC,       /* chunk count */
173 +       VC_CMA_MSG_REQUEST_FREE,        /* chunk count */
174 +       VC_CMA_MSG_RESERVE,     /* bytes lo, bytes hi */
175 +       VC_CMA_MSG_UPDATE_RESERVE,
176 +       VC_CMA_MSG_MAX
177 +};
178 +
179 +struct cma_msg {
180 +       unsigned short type;
181 +       unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
182 +};
183 +
184 +struct vc_cma_reserve_user {
185 +       unsigned int pid;
186 +       unsigned int reserve;
187 +};
188 +
189 +/* Device (/dev) related variables */
190 +static dev_t vc_cma_devnum;
191 +static struct class *vc_cma_class;
192 +static struct cdev vc_cma_cdev;
193 +static int vc_cma_inited;
194 +static int vc_cma_debug;
195 +
196 +/* Proc entry */
197 +static struct proc_dir_entry *vc_cma_proc_entry;
198 +
199 +phys_addr_t vc_cma_base;
200 +struct page *vc_cma_base_page;
201 +unsigned int vc_cma_size;
202 +EXPORT_SYMBOL(vc_cma_size);
203 +unsigned int vc_cma_initial;
204 +unsigned int vc_cma_chunks;
205 +unsigned int vc_cma_chunks_used;
206 +unsigned int vc_cma_chunks_reserved;
207 +
208 +
209 +void *vc_cma_dma_alloc;
210 +unsigned int vc_cma_dma_size;
211 +
212 +static int in_loud_error;
213 +
214 +unsigned int vc_cma_reserve_total;
215 +unsigned int vc_cma_reserve_count;
216 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
217 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
218 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
219 +
220 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
221 +static struct platform_device vc_cma_device = {
222 +       .name = "vc-cma",
223 +       .id = 0,
224 +       .dev = {
225 +               .dma_mask = &vc_cma_dma_mask,
226 +               .coherent_dma_mask = DMA_BIT_MASK(32),
227 +               },
228 +};
229 +
230 +static VCHIQ_INSTANCE_T cma_instance;
231 +static VCHIQ_SERVICE_HANDLE_T cma_service;
232 +static VCHIU_QUEUE_T cma_msg_queue;
233 +static struct task_struct *cma_worker;
234 +
235 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
236 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
237 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
238 +                                          VCHIQ_HEADER_T * header,
239 +                                          VCHIQ_SERVICE_HANDLE_T service,
240 +                                          void *bulk_userdata);
241 +static void send_vc_msg(unsigned short type,
242 +                       unsigned short param1, unsigned short param2);
243 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
244 +
245 +static int early_vc_cma_mem(char *p)
246 +{
247 +       unsigned int new_size;
248 +       printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
249 +       vc_cma_size = memparse(p, &p);
250 +       vc_cma_initial = vc_cma_size;
251 +       if (*p == '/')
252 +               vc_cma_size = memparse(p + 1, &p);
253 +       if (*p == '@')
254 +               vc_cma_base = memparse(p + 1, &p);
255 +
256 +       new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
257 +           & ~(VC_CMA_CHUNK_SIZE - 1);
258 +       if (new_size > vc_cma_size)
259 +               vc_cma_size = 0;
260 +       vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
261 +           & ~(VC_CMA_CHUNK_SIZE - 1);
262 +       if (vc_cma_initial > vc_cma_size)
263 +               vc_cma_initial = vc_cma_size;
264 +       vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
265 +           & ~(VC_CMA_CHUNK_SIZE - 1);
266 +
267 +       printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
268 +              vc_cma_size, (unsigned int)vc_cma_base);
269 +
270 +       return 0;
271 +}
272 +
273 +early_param("vc-cma-mem", early_vc_cma_mem);
274 +
275 +void vc_cma_early_init(void)
276 +{
277 +       LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
278 +       if (vc_cma_size) {
279 +               int rc = platform_device_register(&vc_cma_device);
280 +               LOG_DBG("platform_device_register -> %d", rc);
281 +       }
282 +}
283 +
284 +void vc_cma_reserve(void)
285 +{
286 +       /* if vc_cma_size is set, then declare vc CMA area of the same
287 +        * size from the end of memory
288 +        */
289 +       if (vc_cma_size) {
290 +               if (dma_declare_contiguous(&vc_cma_device.dev, vc_cma_size,
291 +                                          vc_cma_base, 0) == 0) {
292 +                       if (!dev_get_cma_area(NULL)) {
293 +                               /* There is no default CMA area - make this
294 +                                  the default */
295 +                               struct cma *vc_cma_area = dev_get_cma_area(
296 +                                       &vc_cma_device.dev);
297 +                               dma_contiguous_set_default(vc_cma_area);
298 +                               LOG_INFO("vc_cma_reserve - using vc_cma as "
299 +                                        "the default contiguous DMA area");
300 +                       }
301 +               } else {
302 +                       LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
303 +                               vc_cma_size, (unsigned int)vc_cma_base);
304 +                       vc_cma_size = 0;
305 +               }
306 +       }
307 +       vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
308 +}
309 +
310 +/****************************************************************************
311 +*
312 +*   vc_cma_open
313 +*
314 +***************************************************************************/
315 +
316 +static int vc_cma_open(struct inode *inode, struct file *file)
317 +{
318 +       (void)inode;
319 +       (void)file;
320 +
321 +       return 0;
322 +}
323 +
324 +/****************************************************************************
325 +*
326 +*   vc_cma_release
327 +*
328 +***************************************************************************/
329 +
330 +static int vc_cma_release(struct inode *inode, struct file *file)
331 +{
332 +       (void)inode;
333 +       (void)file;
334 +
335 +       vc_cma_set_reserve(0, current->tgid);
336 +
337 +       return 0;
338 +}
339 +
340 +/****************************************************************************
341 +*
342 +*   vc_cma_ioctl
343 +*
344 +***************************************************************************/
345 +
346 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
347 +{
348 +       int rc = 0;
349 +
350 +       (void)cmd;
351 +       (void)arg;
352 +
353 +       switch (cmd) {
354 +       case VC_CMA_IOC_RESERVE:
355 +               rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
356 +               if (rc >= 0)
357 +                       rc = 0;
358 +               break;
359 +       default:
360 +               LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
361 +               return -ENOTTY;
362 +       }
363 +
364 +       return rc;
365 +}
366 +
367 +/****************************************************************************
368 +*
369 +*   File Operations for the driver.
370 +*
371 +***************************************************************************/
372 +
373 +static const struct file_operations vc_cma_fops = {
374 +       .owner = THIS_MODULE,
375 +       .open = vc_cma_open,
376 +       .release = vc_cma_release,
377 +       .unlocked_ioctl = vc_cma_ioctl,
378 +};
379 +
380 +/****************************************************************************
381 +*
382 +*   vc_cma_proc_open
383 +*
384 +***************************************************************************/
385 +
386 +static int vc_cma_show_info(struct seq_file *m, void *v)
387 +{
388 +       int i;
389 +
390 +       seq_printf(m, "Videocore CMA:\n");
391 +       seq_printf(m, "   Base       : %08x\n", (unsigned int)vc_cma_base);
392 +       seq_printf(m, "   Length     : %08x\n", vc_cma_size);
393 +       seq_printf(m, "   Initial    : %08x\n", vc_cma_initial);
394 +       seq_printf(m, "   Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
395 +       seq_printf(m, "   Chunks     : %4d (%d bytes)\n",
396 +                  (int)vc_cma_chunks,
397 +                  (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
398 +       seq_printf(m, "   Used       : %4d (%d bytes)\n",
399 +                  (int)vc_cma_chunks_used,
400 +                  (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
401 +       seq_printf(m, "   Reserved   : %4d (%d bytes)\n",
402 +                  (unsigned int)vc_cma_chunks_reserved,
403 +                  (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
404 +
405 +       for (i = 0; i < vc_cma_reserve_count; i++) {
406 +               struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
407 +               seq_printf(m, "     PID %5d: %d bytes\n", user->pid,
408 +                          user->reserve);
409 +       }
410 +       seq_printf(m, "   dma_alloc  : %p (%d pages)\n",
411 +                  vc_cma_dma_alloc ? page_address(vc_cma_dma_alloc) : 0,
412 +                  vc_cma_dma_size);
413 +
414 +       seq_printf(m, "\n");
415 +
416 +       return 0;
417 +}
418 +
419 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
420 +{
421 +       return single_open(file, vc_cma_show_info, NULL);
422 +}
423 +
424 +/****************************************************************************
425 +*
426 +*   vc_cma_proc_write
427 +*
428 +***************************************************************************/
429 +
430 +static int vc_cma_proc_write(struct file *file,
431 +                            const char __user *buffer,
432 +                            size_t size, loff_t *ppos)
433 +{
434 +       int rc = -EFAULT;
435 +       char input_str[20];
436 +
437 +       memset(input_str, 0, sizeof(input_str));
438 +
439 +       if (size > sizeof(input_str)) {
440 +               LOG_ERR("%s: input string length too long", __func__);
441 +               goto out;
442 +       }
443 +
444 +       if (copy_from_user(input_str, buffer, size - 1)) {
445 +               LOG_ERR("%s: failed to get input string", __func__);
446 +               goto out;
447 +       }
448 +#define ALLOC_STR "alloc"
449 +#define FREE_STR "free"
450 +#define DEBUG_STR "debug"
451 +#define RESERVE_STR "reserve"
452 +#define DMA_ALLOC_STR "dma_alloc"
453 +#define DMA_FREE_STR "dma_free"
454 +       if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
455 +               int alloc_size;
456 +               char *p = input_str + strlen(ALLOC_STR);
457 +
458 +               while (*p == ' ')
459 +                       p++;
460 +               alloc_size = memparse(p, NULL);
461 +               LOG_INFO("/proc/vc-cma: alloc %d", alloc_size);
462 +               if (alloc_size)
463 +                       send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
464 +                                   alloc_size / VC_CMA_CHUNK_SIZE, 0);
465 +               else
466 +                       LOG_ERR("invalid size '%s'", p);
467 +               rc = size;
468 +       } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
469 +               int alloc_size;
470 +               char *p = input_str + strlen(FREE_STR);
471 +
472 +               while (*p == ' ')
473 +                       p++;
474 +               alloc_size = memparse(p, NULL);
475 +               LOG_INFO("/proc/vc-cma: free %d", alloc_size);
476 +               if (alloc_size)
477 +                       send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
478 +                                   alloc_size / VC_CMA_CHUNK_SIZE, 0);
479 +               else
480 +                       LOG_ERR("invalid size '%s'", p);
481 +               rc = size;
482 +       } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
483 +               char *p = input_str + strlen(DEBUG_STR);
484 +               while (*p == ' ')
485 +                       p++;
486 +               if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
487 +                       vc_cma_debug = 1;
488 +               else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
489 +                       vc_cma_debug = 0;
490 +               LOG_INFO("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
491 +               rc = size;
492 +       } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
493 +               int alloc_size;
494 +               int reserved;
495 +               char *p = input_str + strlen(RESERVE_STR);
496 +               while (*p == ' ')
497 +                       p++;
498 +               alloc_size = memparse(p, NULL);
499 +
500 +               reserved = vc_cma_set_reserve(alloc_size, current->tgid);
501 +               rc = (reserved >= 0) ? size : reserved;
502 +       } else if (strncmp(input_str, DMA_ALLOC_STR, strlen(DMA_ALLOC_STR)) == 0) {
503 +               int alloc_size;
504 +               char *p = input_str + strlen(DMA_ALLOC_STR);
505 +               while (*p == ' ')
506 +                       p++;
507 +               alloc_size = memparse(p, NULL);
508 +
509 +               if (vc_cma_dma_alloc) {
510 +                   dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
511 +                                               vc_cma_dma_size);
512 +                   vc_cma_dma_alloc = NULL;
513 +                   vc_cma_dma_size = 0;
514 +               }
515 +               vc_cma_dma_alloc = dma_alloc_from_contiguous(NULL, alloc_size, 0);
516 +               vc_cma_dma_size = (vc_cma_dma_alloc ? alloc_size : 0);
517 +               if (vc_cma_dma_alloc)
518 +                       LOG_INFO("dma_alloc(%d pages) -> %p", alloc_size, page_address(vc_cma_dma_alloc));
519 +               else
520 +                       LOG_ERR("dma_alloc(%d pages) failed", alloc_size);
521 +               rc = size;
522 +       } else if (strncmp(input_str, DMA_FREE_STR, strlen(DMA_FREE_STR)) == 0) {
523 +               if (vc_cma_dma_alloc) {
524 +                   dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
525 +                                               vc_cma_dma_size);
526 +                   vc_cma_dma_alloc = NULL;
527 +                   vc_cma_dma_size = 0;
528 +               }
529 +               rc = size;
530 +       }
531 +
532 +out:
533 +       return rc;
534 +}
535 +
536 +/****************************************************************************
537 +*
538 +*   File Operations for /proc interface.
539 +*
540 +***************************************************************************/
541 +
542 +static const struct file_operations vc_cma_proc_fops = {
543 +       .open = vc_cma_proc_open,
544 +       .read = seq_read,
545 +       .write = vc_cma_proc_write,
546 +       .llseek = seq_lseek,
547 +       .release = single_release
548 +};
549 +
550 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
551 +{
552 +       struct vc_cma_reserve_user *user = NULL;
553 +       int delta = 0;
554 +       int i;
555 +
556 +       if (down_interruptible(&vc_cma_reserve_mutex))
557 +               return -ERESTARTSYS;
558 +
559 +       for (i = 0; i < vc_cma_reserve_count; i++) {
560 +               if (pid == vc_cma_reserve_users[i].pid) {
561 +                       user = &vc_cma_reserve_users[i];
562 +                       delta = reserve - user->reserve;
563 +                       if (reserve)
564 +                               user->reserve = reserve;
565 +                       else {
566 +                               /* Remove this entry by copying downwards */
567 +                               while ((i + 1) < vc_cma_reserve_count) {
568 +                                       user[0].pid = user[1].pid;
569 +                                       user[0].reserve = user[1].reserve;
570 +                                       user++;
571 +                                       i++;
572 +                               }
573 +                               vc_cma_reserve_count--;
574 +                               user = NULL;
575 +                       }
576 +                       break;
577 +               }
578 +       }
579 +
580 +       if (reserve && !user) {
581 +               if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
582 +                       LOG_ERR("vc-cma: Too many reservations - "
583 +                               "increase CMA_RESERVE_COUNT_MAX");
584 +                       up(&vc_cma_reserve_mutex);
585 +                       return -EBUSY;
586 +               }
587 +               user = &vc_cma_reserve_users[vc_cma_reserve_count];
588 +               user->pid = pid;
589 +               user->reserve = reserve;
590 +               delta = reserve;
591 +               vc_cma_reserve_count++;
592 +       }
593 +
594 +       vc_cma_reserve_total += delta;
595 +
596 +       send_vc_msg(VC_CMA_MSG_RESERVE,
597 +                   vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
598 +
599 +       send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
600 +
601 +       LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
602 +               reserve, pid, vc_cma_reserve_total);
603 +
604 +       up(&vc_cma_reserve_mutex);
605 +
606 +       return vc_cma_reserve_total;
607 +}
608 +
609 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
610 +                                          VCHIQ_HEADER_T * header,
611 +                                          VCHIQ_SERVICE_HANDLE_T service,
612 +                                          void *bulk_userdata)
613 +{
614 +       switch (reason) {
615 +       case VCHIQ_MESSAGE_AVAILABLE:
616 +               if (!send_worker_msg(header))
617 +                       return VCHIQ_RETRY;
618 +               break;
619 +       case VCHIQ_SERVICE_CLOSED:
620 +               LOG_DBG("CMA service closed");
621 +               break;
622 +       default:
623 +               LOG_ERR("Unexpected CMA callback reason %d", reason);
624 +               break;
625 +       }
626 +       return VCHIQ_SUCCESS;
627 +}
628 +
629 +static void send_vc_msg(unsigned short type,
630 +                       unsigned short param1, unsigned short param2)
631 +{
632 +       unsigned short msg[] = { type, param1, param2 };
633 +       VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
634 +       VCHIQ_STATUS_T ret;
635 +       vchiq_use_service(cma_service);
636 +       ret = vchiq_queue_message(cma_service, &elem, 1);
637 +       vchiq_release_service(cma_service);
638 +       if (ret != VCHIQ_SUCCESS)
639 +               LOG_ERR("vchiq_queue_message returned %x", ret);
640 +}
641 +
642 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
643 +{
644 +       if (down_interruptible(&vc_cma_worker_queue_push_mutex))
645 +               return false;
646 +       vchiu_queue_push(&cma_msg_queue, msg);
647 +       up(&vc_cma_worker_queue_push_mutex);
648 +       return true;
649 +}
650 +
651 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
652 +{
653 +       int i;
654 +       for (i = 0; i < num_chunks; i++) {
655 +               struct page *chunk;
656 +               unsigned int chunk_num;
657 +               uint8_t *chunk_addr;
658 +               size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
659 +
660 +               chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
661 +                                                 PAGES_PER_CHUNK,
662 +                                                 VC_CMA_CHUNK_ORDER);
663 +               if (!chunk)
664 +                       break;
665 +
666 +               chunk_addr = page_address(chunk);
667 +               dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
668 +               outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
669 +                       chunk_size);
670 +
671 +               chunk_num =
672 +                   (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
673 +               BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
674 +                       VC_CMA_CHUNK_SIZE) != 0);
675 +               if (chunk_num >= vc_cma_chunks) {
676 +                       phys_addr_t _pa = vc_cma_base + vc_cma_size - 1;
677 +                       LOG_ERR("%s: ===============================",
678 +                               __func__);
679 +                       LOG_ERR("%s: chunk phys %x, vc_cma %pa-%pa - "
680 +                               "bad SPARSEMEM configuration?",
681 +                               __func__, (unsigned int)page_to_phys(chunk),
682 +                               &vc_cma_base, &_pa);
683 +                       LOG_ERR("%s: dev->cma_area = %p", __func__,
684 +                               (void*)0/*vc_cma_device.dev.cma_area*/);
685 +                       LOG_ERR("%s: ===============================",
686 +                               __func__);
687 +                       break;
688 +               }
689 +               reply->params[i] = chunk_num;
690 +               vc_cma_chunks_used++;
691 +       }
692 +
693 +       if (i < num_chunks) {
694 +               LOG_ERR("%s: dma_alloc_from_contiguous failed "
695 +                       "for %x bytes (alloc %d of %d, %d free)",
696 +                       __func__, VC_CMA_CHUNK_SIZE, i,
697 +                       num_chunks, vc_cma_chunks - vc_cma_chunks_used);
698 +               num_chunks = i;
699 +       }
700 +
701 +       LOG_DBG("CMA allocated %d chunks -> %d used",
702 +               num_chunks, vc_cma_chunks_used);
703 +       reply->type = VC_CMA_MSG_ALLOCATED;
704 +
705 +       {
706 +               VCHIQ_ELEMENT_T elem = {
707 +                       reply,
708 +                       offsetof(struct cma_msg, params[0]) +
709 +                           num_chunks * sizeof(reply->params[0])
710 +               };
711 +               VCHIQ_STATUS_T ret;
712 +               vchiq_use_service(cma_service);
713 +               ret = vchiq_queue_message(cma_service, &elem, 1);
714 +               vchiq_release_service(cma_service);
715 +               if (ret != VCHIQ_SUCCESS)
716 +                       LOG_ERR("vchiq_queue_message return " "%x", ret);
717 +       }
718 +
719 +       return num_chunks;
720 +}
721 +
722 +static int cma_worker_proc(void *param)
723 +{
724 +       static struct cma_msg reply;
725 +       (void)param;
726 +
727 +       while (1) {
728 +               VCHIQ_HEADER_T *msg;
729 +               static struct cma_msg msg_copy;
730 +               struct cma_msg *cma_msg = &msg_copy;
731 +               int type, msg_size;
732 +
733 +               msg = vchiu_queue_pop(&cma_msg_queue);
734 +               if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
735 +                       msg_size = msg->size;
736 +                       memcpy(&msg_copy, msg->data, msg_size);
737 +                       type = cma_msg->type;
738 +                       vchiq_release_message(cma_service, msg);
739 +               } else {
740 +                       msg_size = 0;
741 +                       type = (int)msg;
742 +                       if (type == VC_CMA_MSG_QUIT)
743 +                               break;
744 +                       else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
745 +                               msg = NULL;
746 +                               cma_msg = NULL;
747 +                       } else {
748 +                               BUG();
749 +                               continue;
750 +                       }
751 +               }
752 +
753 +               switch (type) {
754 +               case VC_CMA_MSG_ALLOC:{
755 +                               int num_chunks, free_chunks;
756 +                               num_chunks = cma_msg->params[0];
757 +                               free_chunks =
758 +                                   vc_cma_chunks - vc_cma_chunks_used;
759 +                               LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
760 +                               if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
761 +                                       LOG_ERR
762 +                                           ("CMA_MSG_ALLOC - chunk count (%d) "
763 +                                            "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
764 +                                            num_chunks,
765 +                                            VC_CMA_MAX_PARAMS_PER_MSG);
766 +                                       num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
767 +                               }
768 +
769 +                               if (num_chunks > free_chunks) {
770 +                                       LOG_ERR
771 +                                           ("CMA_MSG_ALLOC - chunk count (%d) "
772 +                                            "exceeds free chunks (%d)",
773 +                                            num_chunks, free_chunks);
774 +                                       num_chunks = free_chunks;
775 +                               }
776 +
777 +                               vc_cma_alloc_chunks(num_chunks, &reply);
778 +                       }
779 +                       break;
780 +
781 +               case VC_CMA_MSG_FREE:{
782 +                               int chunk_count =
783 +                                   (msg_size -
784 +                                    offsetof(struct cma_msg,
785 +                                             params)) /
786 +                                   sizeof(cma_msg->params[0]);
787 +                               int i;
788 +                               BUG_ON(chunk_count <= 0);
789 +
790 +                               LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
791 +                                       chunk_count, cma_msg->params[0]);
792 +                               for (i = 0; i < chunk_count; i++) {
793 +                                       int chunk_num = cma_msg->params[i];
794 +                                       struct page *page = vc_cma_base_page +
795 +                                           chunk_num * PAGES_PER_CHUNK;
796 +                                       if (chunk_num >= vc_cma_chunks) {
797 +                                               LOG_ERR
798 +                                                   ("CMA_MSG_FREE - chunk %d of %d"
799 +                                                    " (value %x) exceeds maximum "
800 +                                                    "(%x)", i, chunk_count,
801 +                                                    chunk_num,
802 +                                                    vc_cma_chunks - 1);
803 +                                               break;
804 +                                       }
805 +
806 +                                       if (!dma_release_from_contiguous
807 +                                           (&vc_cma_device.dev, page,
808 +                                            PAGES_PER_CHUNK)) {
809 +                                               phys_addr_t _pa = page_to_phys(page);
810 +                                               LOG_ERR
811 +                                                   ("CMA_MSG_FREE - failed to "
812 +                                                    "release chunk %d (phys %pa, "
813 +                                                    "page %x)", chunk_num,
814 +                                                    &_pa,
815 +                                                    (unsigned int)page);
816 +                                       }
817 +                                       vc_cma_chunks_used--;
818 +                               }
819 +                               LOG_DBG("CMA released %d chunks -> %d used",
820 +                                       i, vc_cma_chunks_used);
821 +                       }
822 +                       break;
823 +
824 +               case VC_CMA_MSG_UPDATE_RESERVE:{
825 +                               int chunks_needed =
826 +                                   ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
827 +                                     1)
828 +                                    / VC_CMA_CHUNK_SIZE) -
829 +                                   vc_cma_chunks_reserved;
830 +
831 +                               LOG_DBG
832 +                                   ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
833 +                                    chunks_needed);
834 +
835 +                               /* Cap the reservations to what is available */
836 +                               if (chunks_needed > 0) {
837 +                                       if (chunks_needed >
838 +                                           (vc_cma_chunks -
839 +                                            vc_cma_chunks_used))
840 +                                               chunks_needed =
841 +                                                   (vc_cma_chunks -
842 +                                                    vc_cma_chunks_used);
843 +
844 +                                       chunks_needed =
845 +                                           vc_cma_alloc_chunks(chunks_needed,
846 +                                                               &reply);
847 +                               }
848 +
849 +                               LOG_DBG
850 +                                   ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
851 +                                    chunks_needed);
852 +                               vc_cma_chunks_reserved += chunks_needed;
853 +                       }
854 +                       break;
855 +
856 +               default:
857 +                       LOG_ERR("unexpected msg type %d", type);
858 +                       break;
859 +               }
860 +       }
861 +
862 +       LOG_DBG("quitting...");
863 +       return 0;
864 +}
865 +
866 +/****************************************************************************
867 +*
868 +*   vc_cma_connected_init
869 +*
870 +*   This function is called once the videocore has been connected.
871 +*
872 +***************************************************************************/
873 +
874 +static void vc_cma_connected_init(void)
875 +{
876 +       VCHIQ_SERVICE_PARAMS_T service_params;
877 +
878 +       LOG_DBG("vc_cma_connected_init");
879 +
880 +       if (!vchiu_queue_init(&cma_msg_queue, 16)) {
881 +               LOG_ERR("could not create CMA msg queue");
882 +               goto fail_queue;
883 +       }
884 +
885 +       if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
886 +               goto fail_vchiq_init;
887 +
888 +       vchiq_connect(cma_instance);
889 +
890 +       service_params.fourcc = VC_CMA_FOURCC;
891 +       service_params.callback = cma_service_callback;
892 +       service_params.userdata = NULL;
893 +       service_params.version = VC_CMA_VERSION;
894 +       service_params.version_min = VC_CMA_VERSION;
895 +
896 +       if (vchiq_open_service(cma_instance, &service_params,
897 +                              &cma_service) != VCHIQ_SUCCESS) {
898 +               LOG_ERR("failed to open service - already in use?");
899 +               goto fail_vchiq_open;
900 +       }
901 +
902 +       vchiq_release_service(cma_service);
903 +
904 +       cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
905 +       if (!cma_worker) {
906 +               LOG_ERR("could not create CMA worker thread");
907 +               goto fail_worker;
908 +       }
909 +       set_user_nice(cma_worker, -20);
910 +       wake_up_process(cma_worker);
911 +
912 +       return;
913 +
914 +fail_worker:
915 +       vchiq_close_service(cma_service);
916 +fail_vchiq_open:
917 +       vchiq_shutdown(cma_instance);
918 +fail_vchiq_init:
919 +       vchiu_queue_delete(&cma_msg_queue);
920 +fail_queue:
921 +       return;
922 +}
923 +
924 +void
925 +loud_error_header(void)
926 +{
927 +       if (in_loud_error)
928 +               return;
929 +
930 +       LOG_ERR("============================================================"
931 +               "================");
932 +       LOG_ERR("============================================================"
933 +               "================");
934 +       LOG_ERR("=====");
935 +
936 +       in_loud_error = 1;
937 +}
938 +
939 +void
940 +loud_error_footer(void)
941 +{
942 +       if (!in_loud_error)
943 +               return;
944 +
945 +       LOG_ERR("=====");
946 +       LOG_ERR("============================================================"
947 +               "================");
948 +       LOG_ERR("============================================================"
949 +               "================");
950 +
951 +       in_loud_error = 0;
952 +}
953 +
954 +#if 1
955 +static int check_cma_config(void) { return 1; }
956 +#else
957 +static int
958 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
959 +       const char *symbol,
960 +       void *buf, size_t bufsize)
961 +{
962 +       VC_MEM_ADDR_T vcMemAddr;
963 +       size_t vcMemSize;
964 +       uint8_t *mapAddr;
965 +       off_t  vcMapAddr;
966 +
967 +       if (!LookupVideoCoreSymbol(handle, symbol,
968 +               &vcMemAddr,
969 +               &vcMemSize)) {
970 +               loud_error_header();
971 +               loud_error(
972 +                       "failed to find VC symbol \"%s\".",
973 +                       symbol);
974 +               loud_error_footer();
975 +               return 0;
976 +       }
977 +
978 +       if (vcMemSize != bufsize) {
979 +               loud_error_header();
980 +               loud_error(
981 +                       "VC symbol \"%s\" is the wrong size.",
982 +                       symbol);
983 +               loud_error_footer();
984 +               return 0;
985 +       }
986 +
987 +       vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
988 +       vcMapAddr += mm_vc_mem_phys_addr;
989 +       mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
990 +       if (mapAddr == 0) {
991 +               loud_error_header();
992 +               loud_error(
993 +                       "failed to ioremap \"%s\" @ 0x%x "
994 +                       "(phys: 0x%x, size: %u).",
995 +                       symbol,
996 +                       (unsigned int)vcMapAddr,
997 +                       (unsigned int)vcMemAddr,
998 +                       (unsigned int)vcMemSize);
999 +               loud_error_footer();
1000 +               return 0;
1001 +       }
1002 +
1003 +       memcpy(buf, mapAddr, bufsize);
1004 +       iounmap(mapAddr);
1005 +
1006 +       return 1;
1007 +}
1008 +
1009 +
1010 +static int
1011 +check_cma_config(void)
1012 +{
1013 +       VC_MEM_ACCESS_HANDLE_T mem_hndl;
1014 +       VC_MEM_ADDR_T mempool_start;
1015 +       VC_MEM_ADDR_T mempool_end;
1016 +       VC_MEM_ADDR_T mempool_offline_start;
1017 +       VC_MEM_ADDR_T mempool_offline_end;
1018 +       VC_MEM_ADDR_T cam_alloc_base;
1019 +       VC_MEM_ADDR_T cam_alloc_size;
1020 +       VC_MEM_ADDR_T cam_alloc_end;
1021 +       int success = 0;
1022 +
1023 +       if (OpenVideoCoreMemory(&mem_hndl) != 0)
1024 +               goto out;
1025 +
1026 +       /* Read the relevant VideoCore variables */
1027 +       if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
1028 +               &mempool_start,
1029 +               sizeof(mempool_start)))
1030 +               goto close;
1031 +
1032 +       if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
1033 +               &mempool_end,
1034 +               sizeof(mempool_end)))
1035 +               goto close;
1036 +
1037 +       if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
1038 +               &mempool_offline_start,
1039 +               sizeof(mempool_offline_start)))
1040 +               goto close;
1041 +
1042 +       if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
1043 +               &mempool_offline_end,
1044 +               sizeof(mempool_offline_end)))
1045 +               goto close;
1046 +
1047 +       if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1048 +               &cam_alloc_base,
1049 +               sizeof(cam_alloc_base)))
1050 +               goto close;
1051 +
1052 +       if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1053 +               &cam_alloc_size,
1054 +               sizeof(cam_alloc_size)))
1055 +               goto close;
1056 +
1057 +       cam_alloc_end = cam_alloc_base + cam_alloc_size;
1058 +
1059 +       success = 1;
1060 +
1061 +       /* Now the sanity checks */
1062 +       if (!mempool_offline_start)
1063 +               mempool_offline_start = mempool_start;
1064 +       if (!mempool_offline_end)
1065 +               mempool_offline_end = mempool_end;
1066 +
1067 +       if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1068 +               loud_error_header();
1069 +               loud_error(
1070 +                       "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1071 +                       "vc_cma_base(%x)",
1072 +                       mempool_offline_start,
1073 +                       VCADDR_TO_PHYSADDR(mempool_offline_start),
1074 +                       vc_cma_base);
1075 +               success = 0;
1076 +       }
1077 +
1078 +       if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1079 +               (vc_cma_base + vc_cma_size)) {
1080 +               loud_error_header();
1081 +               loud_error(
1082 +                       "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1083 +                       "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1084 +                       mempool_offline_start,
1085 +                       VCADDR_TO_PHYSADDR(mempool_offline_end),
1086 +                       vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1087 +               success = 0;
1088 +       }
1089 +
1090 +       if (mempool_end < mempool_start) {
1091 +               loud_error_header();
1092 +               loud_error(
1093 +                       "__MEMPOOL_END(%x) must not be before "
1094 +                       "__MEMPOOL_START(%x)",
1095 +                       mempool_end,
1096 +                       mempool_start);
1097 +               success = 0;
1098 +       }
1099 +
1100 +       if (mempool_offline_end < mempool_offline_start) {
1101 +               loud_error_header();
1102 +               loud_error(
1103 +                       "__MEMPOOL_OFFLINE_END(%x) must not be before "
1104 +                       "__MEMPOOL_OFFLINE_START(%x)",
1105 +                       mempool_offline_end,
1106 +                       mempool_offline_start);
1107 +               success = 0;
1108 +       }
1109 +
1110 +       if (mempool_offline_start < mempool_start) {
1111 +               loud_error_header();
1112 +               loud_error(
1113 +                       "__MEMPOOL_OFFLINE_START(%x) must not be before "
1114 +                       "__MEMPOOL_START(%x)",
1115 +                       mempool_offline_start,
1116 +                       mempool_start);
1117 +               success = 0;
1118 +       }
1119 +
1120 +       if (mempool_offline_end > mempool_end) {
1121 +               loud_error_header();
1122 +               loud_error(
1123 +                       "__MEMPOOL_OFFLINE_END(%x) must not be after "
1124 +                       "__MEMPOOL_END(%x)",
1125 +                       mempool_offline_end,
1126 +                       mempool_end);
1127 +               success = 0;
1128 +       }
1129 +
1130 +       if ((cam_alloc_base < mempool_end) &&
1131 +               (cam_alloc_end > mempool_start)) {
1132 +               loud_error_header();
1133 +               loud_error(
1134 +                       "cam_alloc pool(%x-%x) overlaps "
1135 +                       "mempool(%x-%x)",
1136 +                       cam_alloc_base, cam_alloc_end,
1137 +                       mempool_start, mempool_end);
1138 +               success = 0;
1139 +       }
1140 +
1141 +       loud_error_footer();
1142 +
1143 +close:
1144 +       CloseVideoCoreMemory(mem_hndl);
1145 +
1146 +out:
1147 +       return success;
1148 +}
1149 +#endif
1150 +
1151 +static int vc_cma_init(void)
1152 +{
1153 +       int rc = -EFAULT;
1154 +       struct device *dev;
1155 +
1156 +       if (!check_cma_config())
1157 +               goto out_release;
1158 +
1159 +       LOG_INFO("vc-cma: Videocore CMA driver");
1160 +       LOG_INFO("vc-cma: vc_cma_base      = %pa", &vc_cma_base);
1161 +       LOG_INFO("vc-cma: vc_cma_size      = 0x%08x (%u MiB)",
1162 +                vc_cma_size, vc_cma_size / (1024 * 1024));
1163 +       LOG_INFO("vc-cma: vc_cma_initial   = 0x%08x (%u MiB)",
1164 +                vc_cma_initial, vc_cma_initial / (1024 * 1024));
1165 +
1166 +       vc_cma_base_page = phys_to_page(vc_cma_base);
1167 +
1168 +       if (vc_cma_chunks) {
1169 +               int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1170 +
1171 +               for (vc_cma_chunks_used = 0;
1172 +                    vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1173 +                       struct page *chunk;
1174 +                       chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
1175 +                                                         PAGES_PER_CHUNK,
1176 +                                                         VC_CMA_CHUNK_ORDER);
1177 +                       if (!chunk)
1178 +                               break;
1179 +                       BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1180 +                               VC_CMA_CHUNK_SIZE) != 0);
1181 +               }
1182 +               if (vc_cma_chunks_used != chunks_needed) {
1183 +                       LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1184 +                               "bytes, allocation %d of %d)",
1185 +                               __func__, VC_CMA_CHUNK_SIZE,
1186 +                               vc_cma_chunks_used, chunks_needed);
1187 +                       goto out_release;
1188 +               }
1189 +
1190 +               vchiq_add_connected_callback(vc_cma_connected_init);
1191 +       }
1192 +
1193 +       rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1194 +       if (rc < 0) {
1195 +               LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1196 +               goto out_release;
1197 +       }
1198 +
1199 +       cdev_init(&vc_cma_cdev, &vc_cma_fops);
1200 +       rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1201 +       if (rc != 0) {
1202 +               LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1203 +               goto out_unregister;
1204 +       }
1205 +
1206 +       vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1207 +       if (IS_ERR(vc_cma_class)) {
1208 +               rc = PTR_ERR(vc_cma_class);
1209 +               LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1210 +               goto out_cdev_del;
1211 +       }
1212 +
1213 +       dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1214 +                           DRIVER_NAME);
1215 +       if (IS_ERR(dev)) {
1216 +               rc = PTR_ERR(dev);
1217 +               LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1218 +               goto out_class_destroy;
1219 +       }
1220 +
1221 +       vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1222 +       if (vc_cma_proc_entry == NULL) {
1223 +               rc = -EFAULT;
1224 +               LOG_ERR("%s: proc_create failed", __func__);
1225 +               goto out_device_destroy;
1226 +       }
1227 +
1228 +       vc_cma_inited = 1;
1229 +       return 0;
1230 +
1231 +out_device_destroy:
1232 +       device_destroy(vc_cma_class, vc_cma_devnum);
1233 +
1234 +out_class_destroy:
1235 +       class_destroy(vc_cma_class);
1236 +       vc_cma_class = NULL;
1237 +
1238 +out_cdev_del:
1239 +       cdev_del(&vc_cma_cdev);
1240 +
1241 +out_unregister:
1242 +       unregister_chrdev_region(vc_cma_devnum, 1);
1243 +
1244 +out_release:
1245 +       /* It is tempting to try to clean up by calling
1246 +          dma_release_from_contiguous for all allocated chunks, but it isn't
1247 +          a very safe thing to do. If vc_cma_initial is non-zero it is because
1248 +          VideoCore is already using that memory, so giving it back to Linux
1249 +          is likely to be fatal.
1250 +        */
1251 +       return -1;
1252 +}
1253 +
1254 +/****************************************************************************
1255 +*
1256 +*   vc_cma_exit
1257 +*
1258 +***************************************************************************/
1259 +
1260 +static void __exit vc_cma_exit(void)
1261 +{
1262 +       LOG_DBG("%s: called", __func__);
1263 +
1264 +       if (vc_cma_inited) {
1265 +               remove_proc_entry(DRIVER_NAME, NULL);
1266 +               device_destroy(vc_cma_class, vc_cma_devnum);
1267 +               class_destroy(vc_cma_class);
1268 +               cdev_del(&vc_cma_cdev);
1269 +               unregister_chrdev_region(vc_cma_devnum, 1);
1270 +       }
1271 +}
1272 +
1273 +module_init(vc_cma_init);
1274 +module_exit(vc_cma_exit);
1275 +MODULE_LICENSE("GPL");
1276 +MODULE_AUTHOR("Broadcom Corporation");
1277 --- /dev/null
1278 +++ b/include/linux/broadcom/vc_cma.h
1279 @@ -0,0 +1,29 @@
1280 +/*****************************************************************************
1281 +* Copyright 2012 Broadcom Corporation.  All rights reserved.
1282 +*
1283 +* Unless you and Broadcom execute a separate written software license
1284 +* agreement governing use of this software, this software is licensed to you
1285 +* under the terms of the GNU General Public License version 2, available at
1286 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1287 +*
1288 +* Notwithstanding the above, under no circumstances may you combine this
1289 +* software in any way with any other Broadcom software provided under a
1290 +* license other than the GPL, without Broadcom's express prior written
1291 +* consent.
1292 +*****************************************************************************/
1293 +
1294 +#if !defined( VC_CMA_H )
1295 +#define VC_CMA_H
1296 +
1297 +#include <linux/ioctl.h>
1298 +
1299 +#define VC_CMA_IOC_MAGIC 0xc5
1300 +
1301 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1302 +
1303 +#ifdef __KERNEL__
1304 +extern void __init vc_cma_early_init(void);
1305 +extern void __init vc_cma_reserve(void);
1306 +#endif
1307 +
1308 +#endif /* VC_CMA_H */