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