1 From 15b3faf99f1b16184620bc89be78a5f205acbe92 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 3 Jul 2013 00:31:47 +0100
4 Subject: [PATCH 006/196] cma: Add vc_cma driver to enable use of CMA
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 drivers/char/Kconfig | 2 +
9 drivers/char/Makefile | 3 +
10 drivers/char/broadcom/Kconfig | 16 +
11 drivers/char/broadcom/Makefile | 2 +
12 drivers/char/broadcom/vc_cma/Makefile | 15 +
13 drivers/char/broadcom/vc_cma/vc_cma.c | 1143 +++++++++++++++++++++++++++++++++
14 drivers/misc/Makefile | 2 +-
15 include/linux/broadcom/vc_cma.h | 30 +
16 8 files changed, 1212 insertions(+), 1 deletion(-)
17 create mode 100644 drivers/char/broadcom/Kconfig
18 create mode 100644 drivers/char/broadcom/Makefile
19 create mode 100644 drivers/char/broadcom/vc_cma/Makefile
20 create mode 100644 drivers/char/broadcom/vc_cma/vc_cma.c
21 create mode 100644 include/linux/broadcom/vc_cma.h
23 diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
24 index 3bb6fa3..022b91c 100644
25 --- a/drivers/char/Kconfig
26 +++ b/drivers/char/Kconfig
27 @@ -586,6 +586,8 @@ config DEVPORT
29 source "drivers/s390/char/Kconfig"
31 +source "drivers/char/broadcom/Kconfig"
34 bool "Enable device interface for some SMD packet ports"
36 diff --git a/drivers/char/Makefile b/drivers/char/Makefile
37 index 7ff1d0d..c594502 100644
38 --- a/drivers/char/Makefile
39 +++ b/drivers/char/Makefile
40 @@ -62,3 +62,6 @@ obj-$(CONFIG_JS_RTC) += js-rtc.o
43 obj-$(CONFIG_TILE_SROM) += tile-srom.o
45 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
47 diff --git a/drivers/char/broadcom/Kconfig b/drivers/char/broadcom/Kconfig
49 index 0000000..e23b440
51 +++ b/drivers/char/broadcom/Kconfig
54 +# Broadcom char driver config
57 +menuconfig BRCM_CHAR_DRIVERS
58 + bool "Broadcom Char Drivers"
60 + Broadcom's char drivers
63 + bool "Videocore CMA"
64 + depends on CMA && BRCM_CHAR_DRIVERS
67 + Helper for videocore CMA access.
69 diff --git a/drivers/char/broadcom/Makefile b/drivers/char/broadcom/Makefile
71 index 0000000..c8574c8
73 +++ b/drivers/char/broadcom/Makefile
75 +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
77 diff --git a/drivers/char/broadcom/vc_cma/Makefile b/drivers/char/broadcom/vc_cma/Makefile
79 index 0000000..ae71918
81 +++ b/drivers/char/broadcom/vc_cma/Makefile
83 +EXTRA_CFLAGS += -Wall -Wstrict-prototypes -Wno-trigraphs
84 +EXTRA_CFLAGS += -Werror
85 +EXTRA_CFLAGS += -I"include/linux/broadcom"
86 +EXTRA_CFLAGS += -I"drivers/misc/vc04_services"
87 +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchi"
88 +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchiq_arm"
90 +EXTRA_CFLAGS += -D__KERNEL__
91 +EXTRA_CFLAGS += -D__linux__
92 +EXTRA_CFLAGS += -Werror
94 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
96 +vc-cma-objs := vc_cma.o
98 diff --git a/drivers/char/broadcom/vc_cma/vc_cma.c b/drivers/char/broadcom/vc_cma/vc_cma.c
100 index 0000000..fe80887
102 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
105 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
107 + * Redistribution and use in source and binary forms, with or without
108 + * modification, are permitted provided that the following conditions
110 + * 1. Redistributions of source code must retain the above copyright
111 + * notice, this list of conditions, and the following disclaimer,
112 + * without modification.
113 + * 2. Redistributions in binary form must reproduce the above copyright
114 + * notice, this list of conditions and the following disclaimer in the
115 + * documentation and/or other materials provided with the distribution.
116 + * 3. The names of the above-listed copyright holders may not be used
117 + * to endorse or promote products derived from this software without
118 + * specific prior written permission.
120 + * ALTERNATIVELY, this software may be distributed under the terms of the
121 + * GNU General Public License ("GPL") version 2, as published by the Free
122 + * Software Foundation.
124 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
125 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
126 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
127 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
128 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
129 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
130 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
131 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
132 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
133 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
134 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
137 +#include <linux/kernel.h>
138 +#include <linux/module.h>
139 +#include <linux/kthread.h>
140 +#include <linux/fs.h>
141 +#include <linux/device.h>
142 +#include <linux/cdev.h>
143 +#include <linux/mm.h>
144 +#include <linux/proc_fs.h>
145 +#include <linux/seq_file.h>
146 +#include <linux/dma-mapping.h>
147 +#include <linux/dma-contiguous.h>
148 +#include <linux/platform_device.h>
149 +#include <linux/uaccess.h>
150 +#include <asm/cacheflush.h>
154 +#include "vchiq_util.h"
155 +#include "vchiq_connected.h"
156 +//#include "debug_sym.h"
157 +//#include "vc_mem.h"
159 +#define DRIVER_NAME "vc-cma"
161 +#define LOG_DBG(fmt, ...) \
162 + if (vc_cma_debug) \
163 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
164 +#define LOG_ERR(fmt, ...) \
165 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
167 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
168 +#define VC_CMA_VERSION 2
170 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
171 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
172 +#define VC_CMA_MAX_PARAMS_PER_MSG \
173 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
174 +#define VC_CMA_RESERVE_COUNT_MAX 16
176 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
178 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
180 +#define loud_error(...) \
181 + LOG_ERR("===== " __VA_ARGS__)
187 + VC_CMA_MSG_ALLOC, /* chunk count */
188 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
189 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
190 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
191 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
192 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
193 + VC_CMA_MSG_UPDATE_RESERVE,
198 + unsigned short type;
199 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
202 +struct vc_cma_reserve_user {
204 + unsigned int reserve;
207 +/* Device (/dev) related variables */
208 +static dev_t vc_cma_devnum;
209 +static struct class *vc_cma_class;
210 +static struct cdev vc_cma_cdev;
211 +static int vc_cma_inited;
212 +static int vc_cma_debug;
215 +static struct proc_dir_entry *vc_cma_proc_entry;
217 +phys_addr_t vc_cma_base;
218 +struct page *vc_cma_base_page;
219 +unsigned int vc_cma_size;
220 +EXPORT_SYMBOL(vc_cma_size);
221 +unsigned int vc_cma_initial;
222 +unsigned int vc_cma_chunks;
223 +unsigned int vc_cma_chunks_used;
224 +unsigned int vc_cma_chunks_reserved;
226 +static int in_loud_error;
228 +unsigned int vc_cma_reserve_total;
229 +unsigned int vc_cma_reserve_count;
230 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
231 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
232 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
234 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
235 +static struct platform_device vc_cma_device = {
239 + .dma_mask = &vc_cma_dma_mask,
240 + .coherent_dma_mask = DMA_BIT_MASK(32),
244 +static VCHIQ_INSTANCE_T cma_instance;
245 +static VCHIQ_SERVICE_HANDLE_T cma_service;
246 +static VCHIU_QUEUE_T cma_msg_queue;
247 +static struct task_struct *cma_worker;
249 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
250 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
251 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
252 + VCHIQ_HEADER_T * header,
253 + VCHIQ_SERVICE_HANDLE_T service,
254 + void *bulk_userdata);
255 +static void send_vc_msg(unsigned short type,
256 + unsigned short param1, unsigned short param2);
257 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
259 +static int early_vc_cma_mem(char *p)
261 + unsigned int new_size;
262 + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
263 + vc_cma_size = memparse(p, &p);
264 + vc_cma_initial = vc_cma_size;
266 + vc_cma_size = memparse(p + 1, &p);
268 + vc_cma_base = memparse(p + 1, &p);
270 + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
271 + & ~(VC_CMA_CHUNK_SIZE - 1);
272 + if (new_size > vc_cma_size)
274 + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
275 + & ~(VC_CMA_CHUNK_SIZE - 1);
276 + if (vc_cma_initial > vc_cma_size)
277 + vc_cma_initial = vc_cma_size;
278 + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
279 + & ~(VC_CMA_CHUNK_SIZE - 1);
281 + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
282 + vc_cma_size, (unsigned int)vc_cma_base);
287 +early_param("vc-cma-mem", early_vc_cma_mem);
289 +void vc_cma_early_init(void)
291 + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
293 + int rc = platform_device_register(&vc_cma_device);
294 + LOG_DBG("platform_device_register -> %d", rc);
298 +void vc_cma_reserve(void)
300 + /* if vc_cma_size is set, then declare vc CMA area of the same
301 + * size from the end of memory
304 + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
305 + vc_cma_base, 0) == 0) {
307 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
308 + vc_cma_size, (unsigned int)vc_cma_base);
312 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
315 +/****************************************************************************
319 +***************************************************************************/
321 +static int vc_cma_open(struct inode *inode, struct file *file)
329 +/****************************************************************************
333 +***************************************************************************/
335 +static int vc_cma_release(struct inode *inode, struct file *file)
340 + vc_cma_set_reserve(0, current->tgid);
345 +/****************************************************************************
349 +***************************************************************************/
351 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
359 + case VC_CMA_IOC_RESERVE:
360 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
365 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
372 +/****************************************************************************
374 +* File Operations for the driver.
376 +***************************************************************************/
378 +static const struct file_operations vc_cma_fops = {
379 + .owner = THIS_MODULE,
380 + .open = vc_cma_open,
381 + .release = vc_cma_release,
382 + .unlocked_ioctl = vc_cma_ioctl,
385 +/****************************************************************************
389 +***************************************************************************/
391 +static int vc_cma_show_info(struct seq_file *m, void *v)
395 + seq_printf(m, "Videocore CMA:\n");
396 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
397 + seq_printf(m, " Length : %08x\n", vc_cma_size);
398 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
399 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
400 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
401 + (int)vc_cma_chunks,
402 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
403 + seq_printf(m, " Used : %4d (%d bytes)\n",
404 + (int)vc_cma_chunks_used,
405 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
406 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
407 + (unsigned int)vc_cma_chunks_reserved,
408 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
410 + for (i = 0; i < vc_cma_reserve_count; i++) {
411 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
412 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
416 + seq_printf(m, "\n");
421 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
423 + return single_open(file, vc_cma_show_info, NULL);
426 +/****************************************************************************
430 +***************************************************************************/
432 +static int vc_cma_proc_write(struct file *file,
433 + const char __user *buffer,
434 + size_t size, loff_t *ppos)
437 + char input_str[20];
439 + memset(input_str, 0, sizeof(input_str));
441 + if (size > sizeof(input_str)) {
442 + LOG_ERR("%s: input string length too long", __func__);
446 + if (copy_from_user(input_str, buffer, size - 1)) {
447 + LOG_ERR("%s: failed to get input string", __func__);
450 +#define ALLOC_STR "alloc"
451 +#define FREE_STR "free"
452 +#define DEBUG_STR "debug"
453 +#define RESERVE_STR "reserve"
454 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
456 + char *p = input_str + strlen(ALLOC_STR);
460 + size = memparse(p, NULL);
461 + LOG_ERR("/proc/vc-cma: alloc %d", size);
463 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
464 + size / VC_CMA_CHUNK_SIZE, 0);
466 + LOG_ERR("invalid size '%s'", p);
468 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
470 + char *p = input_str + strlen(FREE_STR);
474 + size = memparse(p, NULL);
475 + LOG_ERR("/proc/vc-cma: free %d", size);
477 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
478 + size / VC_CMA_CHUNK_SIZE, 0);
480 + LOG_ERR("invalid size '%s'", p);
482 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
483 + char *p = input_str + strlen(DEBUG_STR);
486 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
488 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
490 + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
492 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
495 + char *p = input_str + strlen(RESERVE_STR);
498 + size = memparse(p, NULL);
500 + reserved = vc_cma_set_reserve(size, current->tgid);
501 + rc = (reserved >= 0) ? size : reserved;
508 +/****************************************************************************
510 +* File Operations for /proc interface.
512 +***************************************************************************/
514 +static const struct file_operations vc_cma_proc_fops = {
515 + .open = vc_cma_proc_open,
517 + .write = vc_cma_proc_write,
518 + .llseek = seq_lseek,
519 + .release = single_release
522 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
524 + struct vc_cma_reserve_user *user = NULL;
528 + if (down_interruptible(&vc_cma_reserve_mutex))
529 + return -ERESTARTSYS;
531 + for (i = 0; i < vc_cma_reserve_count; i++) {
532 + if (pid == vc_cma_reserve_users[i].pid) {
533 + user = &vc_cma_reserve_users[i];
534 + delta = reserve - user->reserve;
536 + user->reserve = reserve;
538 + /* Remove this entry by copying downwards */
539 + while ((i + 1) < vc_cma_reserve_count) {
540 + user[0].pid = user[1].pid;
541 + user[0].reserve = user[1].reserve;
545 + vc_cma_reserve_count--;
552 + if (reserve && !user) {
553 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
554 + LOG_ERR("vc-cma: Too many reservations - "
555 + "increase CMA_RESERVE_COUNT_MAX");
556 + up(&vc_cma_reserve_mutex);
559 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
561 + user->reserve = reserve;
563 + vc_cma_reserve_count++;
566 + vc_cma_reserve_total += delta;
568 + send_vc_msg(VC_CMA_MSG_RESERVE,
569 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
571 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
573 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
574 + reserve, pid, vc_cma_reserve_total);
576 + up(&vc_cma_reserve_mutex);
578 + return vc_cma_reserve_total;
581 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
582 + VCHIQ_HEADER_T * header,
583 + VCHIQ_SERVICE_HANDLE_T service,
584 + void *bulk_userdata)
587 + case VCHIQ_MESSAGE_AVAILABLE:
588 + if (!send_worker_msg(header))
589 + return VCHIQ_RETRY;
591 + case VCHIQ_SERVICE_CLOSED:
592 + LOG_DBG("CMA service closed");
595 + LOG_ERR("Unexpected CMA callback reason %d", reason);
598 + return VCHIQ_SUCCESS;
601 +static void send_vc_msg(unsigned short type,
602 + unsigned short param1, unsigned short param2)
604 + unsigned short msg[] = { type, param1, param2 };
605 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
606 + VCHIQ_STATUS_T ret;
607 + vchiq_use_service(cma_service);
608 + ret = vchiq_queue_message(cma_service, &elem, 1);
609 + vchiq_release_service(cma_service);
610 + if (ret != VCHIQ_SUCCESS)
611 + LOG_ERR("vchiq_queue_message returned %x", ret);
614 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
616 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
618 + vchiu_queue_push(&cma_msg_queue, msg);
619 + up(&vc_cma_worker_queue_push_mutex);
623 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
626 + for (i = 0; i < num_chunks; i++) {
627 + struct page *chunk;
628 + unsigned int chunk_num;
629 + uint8_t *chunk_addr;
630 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
632 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
634 + VC_CMA_CHUNK_ORDER);
638 + chunk_addr = page_address(chunk);
639 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
640 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
644 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
645 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
646 + VC_CMA_CHUNK_SIZE) != 0);
647 + if (chunk_num >= vc_cma_chunks) {
648 + LOG_ERR("%s: ===============================",
650 + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
651 + "bad SPARSEMEM configuration?",
652 + __func__, (unsigned int)page_to_phys(chunk),
653 + vc_cma_base, vc_cma_base + vc_cma_size - 1);
654 + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
655 + vc_cma_device.dev.cma_area);
656 + LOG_ERR("%s: ===============================",
660 + reply->params[i] = chunk_num;
661 + vc_cma_chunks_used++;
664 + if (i < num_chunks) {
665 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
666 + "for %x bytes (alloc %d of %d, %d free)",
667 + __func__, VC_CMA_CHUNK_SIZE, i,
668 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
672 + LOG_DBG("CMA allocated %d chunks -> %d used",
673 + num_chunks, vc_cma_chunks_used);
674 + reply->type = VC_CMA_MSG_ALLOCATED;
677 + VCHIQ_ELEMENT_T elem = {
679 + offsetof(struct cma_msg, params[0]) +
680 + num_chunks * sizeof(reply->params[0])
682 + VCHIQ_STATUS_T ret;
683 + vchiq_use_service(cma_service);
684 + ret = vchiq_queue_message(cma_service, &elem, 1);
685 + vchiq_release_service(cma_service);
686 + if (ret != VCHIQ_SUCCESS)
687 + LOG_ERR("vchiq_queue_message return " "%x", ret);
693 +static int cma_worker_proc(void *param)
695 + static struct cma_msg reply;
699 + VCHIQ_HEADER_T *msg;
700 + static struct cma_msg msg_copy;
701 + struct cma_msg *cma_msg = &msg_copy;
702 + int type, msg_size;
704 + msg = vchiu_queue_pop(&cma_msg_queue);
705 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
706 + msg_size = msg->size;
707 + memcpy(&msg_copy, msg->data, msg_size);
708 + type = cma_msg->type;
709 + vchiq_release_message(cma_service, msg);
713 + if (type == VC_CMA_MSG_QUIT)
715 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
725 + case VC_CMA_MSG_ALLOC:{
726 + int num_chunks, free_chunks;
727 + num_chunks = cma_msg->params[0];
729 + vc_cma_chunks - vc_cma_chunks_used;
730 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
731 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
733 + ("CMA_MSG_ALLOC - chunk count (%d) "
734 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
736 + VC_CMA_MAX_PARAMS_PER_MSG);
737 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
740 + if (num_chunks > free_chunks) {
742 + ("CMA_MSG_ALLOC - chunk count (%d) "
743 + "exceeds free chunks (%d)",
744 + num_chunks, free_chunks);
745 + num_chunks = free_chunks;
748 + vc_cma_alloc_chunks(num_chunks, &reply);
752 + case VC_CMA_MSG_FREE:{
755 + offsetof(struct cma_msg,
757 + sizeof(cma_msg->params[0]);
759 + BUG_ON(chunk_count <= 0);
761 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
762 + chunk_count, cma_msg->params[0]);
763 + for (i = 0; i < chunk_count; i++) {
764 + int chunk_num = cma_msg->params[i];
765 + struct page *page = vc_cma_base_page +
766 + chunk_num * PAGES_PER_CHUNK;
767 + if (chunk_num >= vc_cma_chunks) {
769 + ("CMA_MSG_FREE - chunk %d of %d"
770 + " (value %x) exceeds maximum "
771 + "(%x)", i, chunk_count,
773 + vc_cma_chunks - 1);
777 + if (!dma_release_from_contiguous
778 + (NULL /*&vc_cma_device.dev*/, page,
779 + PAGES_PER_CHUNK)) {
781 + ("CMA_MSG_FREE - failed to "
782 + "release chunk %d (phys %x, "
783 + "page %x)", chunk_num,
784 + page_to_phys(page),
785 + (unsigned int)page);
787 + vc_cma_chunks_used--;
789 + LOG_DBG("CMA released %d chunks -> %d used",
790 + i, vc_cma_chunks_used);
794 + case VC_CMA_MSG_UPDATE_RESERVE:{
795 + int chunks_needed =
796 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
798 + / VC_CMA_CHUNK_SIZE) -
799 + vc_cma_chunks_reserved;
802 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
805 + /* Cap the reservations to what is available */
806 + if (chunks_needed > 0) {
807 + if (chunks_needed >
809 + vc_cma_chunks_used))
812 + vc_cma_chunks_used);
815 + vc_cma_alloc_chunks(chunks_needed,
820 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
822 + vc_cma_chunks_reserved += chunks_needed;
827 + LOG_ERR("unexpected msg type %d", type);
832 + LOG_DBG("quitting...");
836 +/****************************************************************************
838 +* vc_cma_connected_init
840 +* This function is called once the videocore has been connected.
842 +***************************************************************************/
844 +static void vc_cma_connected_init(void)
846 + VCHIQ_SERVICE_PARAMS_T service_params;
848 + LOG_DBG("vc_cma_connected_init");
850 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
851 + LOG_ERR("could not create CMA msg queue");
855 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
856 + goto fail_vchiq_init;
858 + vchiq_connect(cma_instance);
860 + service_params.fourcc = VC_CMA_FOURCC;
861 + service_params.callback = cma_service_callback;
862 + service_params.userdata = NULL;
863 + service_params.version = VC_CMA_VERSION;
864 + service_params.version_min = VC_CMA_VERSION;
866 + if (vchiq_open_service(cma_instance, &service_params,
867 + &cma_service) != VCHIQ_SUCCESS) {
868 + LOG_ERR("failed to open service - already in use?");
869 + goto fail_vchiq_open;
872 + vchiq_release_service(cma_service);
874 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
876 + LOG_ERR("could not create CMA worker thread");
879 + set_user_nice(cma_worker, -20);
880 + wake_up_process(cma_worker);
885 + vchiq_close_service(cma_service);
887 + vchiq_shutdown(cma_instance);
889 + vchiu_queue_delete(&cma_msg_queue);
895 +loud_error_header(void)
900 + LOG_ERR("============================================================"
901 + "================");
902 + LOG_ERR("============================================================"
903 + "================");
910 +loud_error_footer(void)
912 + if (!in_loud_error)
916 + LOG_ERR("============================================================"
917 + "================");
918 + LOG_ERR("============================================================"
919 + "================");
925 +static int check_cma_config(void) { return 1; }
928 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
929 + const char *symbol,
930 + void *buf, size_t bufsize)
932 + VC_MEM_ADDR_T vcMemAddr;
937 + if (!LookupVideoCoreSymbol(handle, symbol,
940 + loud_error_header();
942 + "failed to find VC symbol \"%s\".",
944 + loud_error_footer();
948 + if (vcMemSize != bufsize) {
949 + loud_error_header();
951 + "VC symbol \"%s\" is the wrong size.",
953 + loud_error_footer();
957 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
958 + vcMapAddr += mm_vc_mem_phys_addr;
959 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
960 + if (mapAddr == 0) {
961 + loud_error_header();
963 + "failed to ioremap \"%s\" @ 0x%x "
964 + "(phys: 0x%x, size: %u).",
966 + (unsigned int)vcMapAddr,
967 + (unsigned int)vcMemAddr,
968 + (unsigned int)vcMemSize);
969 + loud_error_footer();
973 + memcpy(buf, mapAddr, bufsize);
981 +check_cma_config(void)
983 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
984 + VC_MEM_ADDR_T mempool_start;
985 + VC_MEM_ADDR_T mempool_end;
986 + VC_MEM_ADDR_T mempool_offline_start;
987 + VC_MEM_ADDR_T mempool_offline_end;
988 + VC_MEM_ADDR_T cam_alloc_base;
989 + VC_MEM_ADDR_T cam_alloc_size;
990 + VC_MEM_ADDR_T cam_alloc_end;
993 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
996 + /* Read the relevant VideoCore variables */
997 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
999 + sizeof(mempool_start)))
1002 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
1004 + sizeof(mempool_end)))
1007 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
1008 + &mempool_offline_start,
1009 + sizeof(mempool_offline_start)))
1012 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
1013 + &mempool_offline_end,
1014 + sizeof(mempool_offline_end)))
1017 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1019 + sizeof(cam_alloc_base)))
1022 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1024 + sizeof(cam_alloc_size)))
1027 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1031 + /* Now the sanity checks */
1032 + if (!mempool_offline_start)
1033 + mempool_offline_start = mempool_start;
1034 + if (!mempool_offline_end)
1035 + mempool_offline_end = mempool_end;
1037 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1038 + loud_error_header();
1040 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1041 + "vc_cma_base(%x)",
1042 + mempool_offline_start,
1043 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1048 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1049 + (vc_cma_base + vc_cma_size)) {
1050 + loud_error_header();
1052 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1053 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1054 + mempool_offline_start,
1055 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1056 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1060 + if (mempool_end < mempool_start) {
1061 + loud_error_header();
1063 + "__MEMPOOL_END(%x) must not be before "
1064 + "__MEMPOOL_START(%x)",
1070 + if (mempool_offline_end < mempool_offline_start) {
1071 + loud_error_header();
1073 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1074 + "__MEMPOOL_OFFLINE_START(%x)",
1075 + mempool_offline_end,
1076 + mempool_offline_start);
1080 + if (mempool_offline_start < mempool_start) {
1081 + loud_error_header();
1083 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1084 + "__MEMPOOL_START(%x)",
1085 + mempool_offline_start,
1090 + if (mempool_offline_end > mempool_end) {
1091 + loud_error_header();
1093 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1094 + "__MEMPOOL_END(%x)",
1095 + mempool_offline_end,
1100 + if ((cam_alloc_base < mempool_end) &&
1101 + (cam_alloc_end > mempool_start)) {
1102 + loud_error_header();
1104 + "cam_alloc pool(%x-%x) overlaps "
1106 + cam_alloc_base, cam_alloc_end,
1107 + mempool_start, mempool_end);
1111 + loud_error_footer();
1114 + CloseVideoCoreMemory(mem_hndl);
1121 +static int vc_cma_init(void)
1124 + struct device *dev;
1126 + if (!check_cma_config())
1129 + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
1130 + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
1131 + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
1132 + vc_cma_size, vc_cma_size / (1024 * 1024));
1133 + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
1134 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1136 + vc_cma_base_page = phys_to_page(vc_cma_base);
1138 + if (vc_cma_chunks) {
1139 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1141 + for (vc_cma_chunks_used = 0;
1142 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1143 + struct page *chunk;
1144 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
1146 + VC_CMA_CHUNK_ORDER);
1149 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1150 + VC_CMA_CHUNK_SIZE) != 0);
1152 + if (vc_cma_chunks_used != chunks_needed) {
1153 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1154 + "bytes, allocation %d of %d)",
1155 + __func__, VC_CMA_CHUNK_SIZE,
1156 + vc_cma_chunks_used, chunks_needed);
1160 + vchiq_add_connected_callback(vc_cma_connected_init);
1163 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1165 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1169 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1170 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1172 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1173 + goto out_unregister;
1176 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1177 + if (IS_ERR(vc_cma_class)) {
1178 + rc = PTR_ERR(vc_cma_class);
1179 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1180 + goto out_cdev_del;
1183 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1185 + if (IS_ERR(dev)) {
1186 + rc = PTR_ERR(dev);
1187 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1188 + goto out_class_destroy;
1191 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1192 + if (vc_cma_proc_entry == NULL) {
1194 + LOG_ERR("%s: proc_create failed", __func__);
1195 + goto out_device_destroy;
1198 + vc_cma_inited = 1;
1201 +out_device_destroy:
1202 + device_destroy(vc_cma_class, vc_cma_devnum);
1205 + class_destroy(vc_cma_class);
1206 + vc_cma_class = NULL;
1209 + cdev_del(&vc_cma_cdev);
1212 + unregister_chrdev_region(vc_cma_devnum, 1);
1215 + /* It is tempting to try to clean up by calling
1216 + dma_release_from_contiguous for all allocated chunks, but it isn't
1217 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1218 + VideoCore is already using that memory, so giving it back to Linux
1219 + is likely to be fatal.
1224 +/****************************************************************************
1228 +***************************************************************************/
1230 +static void __exit vc_cma_exit(void)
1232 + LOG_DBG("%s: called", __func__);
1234 + if (vc_cma_inited) {
1235 + remove_proc_entry(DRIVER_NAME, NULL);
1236 + device_destroy(vc_cma_class, vc_cma_devnum);
1237 + class_destroy(vc_cma_class);
1238 + cdev_del(&vc_cma_cdev);
1239 + unregister_chrdev_region(vc_cma_devnum, 1);
1243 +module_init(vc_cma_init);
1244 +module_exit(vc_cma_exit);
1245 +MODULE_LICENSE("GPL");
1246 +MODULE_AUTHOR("Broadcom Corporation");
1247 diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
1248 index d72aaf2..24b40c3 100644
1249 --- a/drivers/misc/Makefile
1250 +++ b/drivers/misc/Makefile
1251 @@ -53,4 +53,4 @@ obj-$(CONFIG_INTEL_MEI) += mei/
1252 obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
1253 obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
1254 obj-$(CONFIG_SRAM) += sram.o
1255 -obj-y += vc04_services/
1256 +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
1257 diff --git a/include/linux/broadcom/vc_cma.h b/include/linux/broadcom/vc_cma.h
1258 new file mode 100644
1259 index 0000000..bdc9d79
1261 +++ b/include/linux/broadcom/vc_cma.h
1263 +/*****************************************************************************
1264 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1266 +* Unless you and Broadcom execute a separate written software license
1267 +* agreement governing use of this software, this software is licensed to you
1268 +* under the terms of the GNU General Public License version 2, available at
1269 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1271 +* Notwithstanding the above, under no circumstances may you combine this
1272 +* software in any way with any other Broadcom software provided under a
1273 +* license other than the GPL, without Broadcom's express prior written
1275 +*****************************************************************************/
1277 +#if !defined( VC_CMA_H )
1280 +#include <linux/ioctl.h>
1282 +#define VC_CMA_IOC_MAGIC 0xc5
1284 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1287 +extern void __init vc_cma_early_init(void);
1288 +extern void __init vc_cma_reserve(void);
1291 +#endif /* VC_CMA_H */