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