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