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