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