1 From 725e07b7b70c601ebbe15436736addc01953dfe7 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 3 Jul 2013 00:31:47 +0100
4 Subject: [PATCH 012/114] cma: Add vc_cma driver to enable use of CMA
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 drivers/char/Kconfig | 2 +
9 drivers/char/Makefile | 1 +
10 drivers/char/broadcom/Kconfig | 15 +
11 drivers/char/broadcom/Makefile | 1 +
12 drivers/char/broadcom/vc_cma/Makefile | 14 +
13 drivers/char/broadcom/vc_cma/vc_cma.c | 1143 +++++++++++++++++++++++++++++++++
14 drivers/misc/Makefile | 2 +-
15 include/linux/broadcom/vc_cma.h | 29 +
16 8 files changed, 1206 insertions(+), 1 deletion(-)
17 create mode 100644 drivers/char/broadcom/Kconfig
18 create mode 100644 drivers/char/broadcom/Makefile
19 create mode 100644 drivers/char/broadcom/vc_cma/Makefile
20 create mode 100644 drivers/char/broadcom/vc_cma/vc_cma.c
21 create mode 100644 include/linux/broadcom/vc_cma.h
23 diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
24 index efefd12..aebd7ca 100644
25 --- a/drivers/char/Kconfig
26 +++ b/drivers/char/Kconfig
27 @@ -581,6 +581,8 @@ config DEVPORT
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 d06cde26..a9f9daf 100644
38 --- a/drivers/char/Makefile
39 +++ b/drivers/char/Makefile
40 @@ -62,3 +62,4 @@ js-rtc-y = rtc.o
42 obj-$(CONFIG_TILE_SROM) += tile-srom.o
43 obj-$(CONFIG_XILLYBUS) += xillybus/
44 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
45 diff --git a/drivers/char/broadcom/Kconfig b/drivers/char/broadcom/Kconfig
47 index 0000000..2d8bd6e
49 +++ b/drivers/char/broadcom/Kconfig
52 +# Broadcom char driver config
55 +menuconfig BRCM_CHAR_DRIVERS
56 + bool "Broadcom Char Drivers"
58 + Broadcom's char drivers
61 + bool "Videocore CMA"
62 + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
65 + Helper for videocore CMA access.
66 diff --git a/drivers/char/broadcom/Makefile b/drivers/char/broadcom/Makefile
68 index 0000000..13c5bca
70 +++ b/drivers/char/broadcom/Makefile
72 +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
73 diff --git a/drivers/char/broadcom/vc_cma/Makefile b/drivers/char/broadcom/vc_cma/Makefile
75 index 0000000..6a94de4
77 +++ b/drivers/char/broadcom/vc_cma/Makefile
79 +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
81 +ccflags-y += -Iinclude/linux/broadcom
82 +ccflags-y += -Idrivers/misc/vc04_services
83 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
84 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
86 +ccflags-y += -D__KERNEL__
87 +ccflags-y += -D__linux__
90 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
92 +vc-cma-objs := vc_cma.o
93 diff --git a/drivers/char/broadcom/vc_cma/vc_cma.c b/drivers/char/broadcom/vc_cma/vc_cma.c
95 index 0000000..a635f9f
97 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
100 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
102 + * Redistribution and use in source and binary forms, with or without
103 + * modification, are permitted provided that the following conditions
105 + * 1. Redistributions of source code must retain the above copyright
106 + * notice, this list of conditions, and the following disclaimer,
107 + * without modification.
108 + * 2. Redistributions in binary form must reproduce the above copyright
109 + * notice, this list of conditions and the following disclaimer in the
110 + * documentation and/or other materials provided with the distribution.
111 + * 3. The names of the above-listed copyright holders may not be used
112 + * to endorse or promote products derived from this software without
113 + * specific prior written permission.
115 + * ALTERNATIVELY, this software may be distributed under the terms of the
116 + * GNU General Public License ("GPL") version 2, as published by the Free
117 + * Software Foundation.
119 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
120 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
121 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
122 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
123 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
124 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
125 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
126 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
127 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
128 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
129 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
132 +#include <linux/kernel.h>
133 +#include <linux/module.h>
134 +#include <linux/kthread.h>
135 +#include <linux/fs.h>
136 +#include <linux/device.h>
137 +#include <linux/cdev.h>
138 +#include <linux/mm.h>
139 +#include <linux/proc_fs.h>
140 +#include <linux/seq_file.h>
141 +#include <linux/dma-mapping.h>
142 +#include <linux/dma-contiguous.h>
143 +#include <linux/platform_device.h>
144 +#include <linux/uaccess.h>
145 +#include <asm/cacheflush.h>
149 +#include "vchiq_util.h"
150 +#include "vchiq_connected.h"
151 +//#include "debug_sym.h"
152 +//#include "vc_mem.h"
154 +#define DRIVER_NAME "vc-cma"
156 +#define LOG_DBG(fmt, ...) \
157 + if (vc_cma_debug) \
158 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
159 +#define LOG_ERR(fmt, ...) \
160 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
162 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
163 +#define VC_CMA_VERSION 2
165 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
166 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
167 +#define VC_CMA_MAX_PARAMS_PER_MSG \
168 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
169 +#define VC_CMA_RESERVE_COUNT_MAX 16
171 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
173 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
175 +#define loud_error(...) \
176 + LOG_ERR("===== " __VA_ARGS__)
182 + VC_CMA_MSG_ALLOC, /* chunk count */
183 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
184 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
185 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
186 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
187 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
188 + VC_CMA_MSG_UPDATE_RESERVE,
193 + unsigned short type;
194 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
197 +struct vc_cma_reserve_user {
199 + unsigned int reserve;
202 +/* Device (/dev) related variables */
203 +static dev_t vc_cma_devnum;
204 +static struct class *vc_cma_class;
205 +static struct cdev vc_cma_cdev;
206 +static int vc_cma_inited;
207 +static int vc_cma_debug;
210 +static struct proc_dir_entry *vc_cma_proc_entry;
212 +phys_addr_t vc_cma_base;
213 +struct page *vc_cma_base_page;
214 +unsigned int vc_cma_size;
215 +EXPORT_SYMBOL(vc_cma_size);
216 +unsigned int vc_cma_initial;
217 +unsigned int vc_cma_chunks;
218 +unsigned int vc_cma_chunks_used;
219 +unsigned int vc_cma_chunks_reserved;
221 +static int in_loud_error;
223 +unsigned int vc_cma_reserve_total;
224 +unsigned int vc_cma_reserve_count;
225 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
226 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
227 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
229 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
230 +static struct platform_device vc_cma_device = {
234 + .dma_mask = &vc_cma_dma_mask,
235 + .coherent_dma_mask = DMA_BIT_MASK(32),
239 +static VCHIQ_INSTANCE_T cma_instance;
240 +static VCHIQ_SERVICE_HANDLE_T cma_service;
241 +static VCHIU_QUEUE_T cma_msg_queue;
242 +static struct task_struct *cma_worker;
244 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
245 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
246 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
247 + VCHIQ_HEADER_T * header,
248 + VCHIQ_SERVICE_HANDLE_T service,
249 + void *bulk_userdata);
250 +static void send_vc_msg(unsigned short type,
251 + unsigned short param1, unsigned short param2);
252 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
254 +static int early_vc_cma_mem(char *p)
256 + unsigned int new_size;
257 + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
258 + vc_cma_size = memparse(p, &p);
259 + vc_cma_initial = vc_cma_size;
261 + vc_cma_size = memparse(p + 1, &p);
263 + vc_cma_base = memparse(p + 1, &p);
265 + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
266 + & ~(VC_CMA_CHUNK_SIZE - 1);
267 + if (new_size > vc_cma_size)
269 + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
270 + & ~(VC_CMA_CHUNK_SIZE - 1);
271 + if (vc_cma_initial > vc_cma_size)
272 + vc_cma_initial = vc_cma_size;
273 + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
274 + & ~(VC_CMA_CHUNK_SIZE - 1);
276 + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
277 + vc_cma_size, (unsigned int)vc_cma_base);
282 +early_param("vc-cma-mem", early_vc_cma_mem);
284 +void vc_cma_early_init(void)
286 + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
288 + int rc = platform_device_register(&vc_cma_device);
289 + LOG_DBG("platform_device_register -> %d", rc);
293 +void vc_cma_reserve(void)
295 + /* if vc_cma_size is set, then declare vc CMA area of the same
296 + * size from the end of memory
299 + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
300 + vc_cma_base, 0) == 0) {
302 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
303 + vc_cma_size, (unsigned int)vc_cma_base);
307 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
310 +/****************************************************************************
314 +***************************************************************************/
316 +static int vc_cma_open(struct inode *inode, struct file *file)
324 +/****************************************************************************
328 +***************************************************************************/
330 +static int vc_cma_release(struct inode *inode, struct file *file)
335 + vc_cma_set_reserve(0, current->tgid);
340 +/****************************************************************************
344 +***************************************************************************/
346 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
354 + case VC_CMA_IOC_RESERVE:
355 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
360 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
367 +/****************************************************************************
369 +* File Operations for the driver.
371 +***************************************************************************/
373 +static const struct file_operations vc_cma_fops = {
374 + .owner = THIS_MODULE,
375 + .open = vc_cma_open,
376 + .release = vc_cma_release,
377 + .unlocked_ioctl = vc_cma_ioctl,
380 +/****************************************************************************
384 +***************************************************************************/
386 +static int vc_cma_show_info(struct seq_file *m, void *v)
390 + seq_printf(m, "Videocore CMA:\n");
391 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
392 + seq_printf(m, " Length : %08x\n", vc_cma_size);
393 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
394 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
395 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
396 + (int)vc_cma_chunks,
397 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
398 + seq_printf(m, " Used : %4d (%d bytes)\n",
399 + (int)vc_cma_chunks_used,
400 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
401 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
402 + (unsigned int)vc_cma_chunks_reserved,
403 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
405 + for (i = 0; i < vc_cma_reserve_count; i++) {
406 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
407 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
411 + seq_printf(m, "\n");
416 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
418 + return single_open(file, vc_cma_show_info, NULL);
421 +/****************************************************************************
425 +***************************************************************************/
427 +static int vc_cma_proc_write(struct file *file,
428 + const char __user *buffer,
429 + size_t size, loff_t *ppos)
432 + char input_str[20];
434 + memset(input_str, 0, sizeof(input_str));
436 + if (size > sizeof(input_str)) {
437 + LOG_ERR("%s: input string length too long", __func__);
441 + if (copy_from_user(input_str, buffer, size - 1)) {
442 + LOG_ERR("%s: failed to get input string", __func__);
445 +#define ALLOC_STR "alloc"
446 +#define FREE_STR "free"
447 +#define DEBUG_STR "debug"
448 +#define RESERVE_STR "reserve"
449 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
451 + char *p = input_str + strlen(ALLOC_STR);
455 + size = memparse(p, NULL);
456 + LOG_ERR("/proc/vc-cma: alloc %d", size);
458 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
459 + size / VC_CMA_CHUNK_SIZE, 0);
461 + LOG_ERR("invalid size '%s'", p);
463 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
465 + char *p = input_str + strlen(FREE_STR);
469 + size = memparse(p, NULL);
470 + LOG_ERR("/proc/vc-cma: free %d", size);
472 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
473 + size / VC_CMA_CHUNK_SIZE, 0);
475 + LOG_ERR("invalid size '%s'", p);
477 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
478 + char *p = input_str + strlen(DEBUG_STR);
481 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
483 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
485 + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
487 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
490 + char *p = input_str + strlen(RESERVE_STR);
493 + size = memparse(p, NULL);
495 + reserved = vc_cma_set_reserve(size, current->tgid);
496 + rc = (reserved >= 0) ? size : reserved;
503 +/****************************************************************************
505 +* File Operations for /proc interface.
507 +***************************************************************************/
509 +static const struct file_operations vc_cma_proc_fops = {
510 + .open = vc_cma_proc_open,
512 + .write = vc_cma_proc_write,
513 + .llseek = seq_lseek,
514 + .release = single_release
517 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
519 + struct vc_cma_reserve_user *user = NULL;
523 + if (down_interruptible(&vc_cma_reserve_mutex))
524 + return -ERESTARTSYS;
526 + for (i = 0; i < vc_cma_reserve_count; i++) {
527 + if (pid == vc_cma_reserve_users[i].pid) {
528 + user = &vc_cma_reserve_users[i];
529 + delta = reserve - user->reserve;
531 + user->reserve = reserve;
533 + /* Remove this entry by copying downwards */
534 + while ((i + 1) < vc_cma_reserve_count) {
535 + user[0].pid = user[1].pid;
536 + user[0].reserve = user[1].reserve;
540 + vc_cma_reserve_count--;
547 + if (reserve && !user) {
548 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
549 + LOG_ERR("vc-cma: Too many reservations - "
550 + "increase CMA_RESERVE_COUNT_MAX");
551 + up(&vc_cma_reserve_mutex);
554 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
556 + user->reserve = reserve;
558 + vc_cma_reserve_count++;
561 + vc_cma_reserve_total += delta;
563 + send_vc_msg(VC_CMA_MSG_RESERVE,
564 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
566 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
568 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
569 + reserve, pid, vc_cma_reserve_total);
571 + up(&vc_cma_reserve_mutex);
573 + return vc_cma_reserve_total;
576 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
577 + VCHIQ_HEADER_T * header,
578 + VCHIQ_SERVICE_HANDLE_T service,
579 + void *bulk_userdata)
582 + case VCHIQ_MESSAGE_AVAILABLE:
583 + if (!send_worker_msg(header))
584 + return VCHIQ_RETRY;
586 + case VCHIQ_SERVICE_CLOSED:
587 + LOG_DBG("CMA service closed");
590 + LOG_ERR("Unexpected CMA callback reason %d", reason);
593 + return VCHIQ_SUCCESS;
596 +static void send_vc_msg(unsigned short type,
597 + unsigned short param1, unsigned short param2)
599 + unsigned short msg[] = { type, param1, param2 };
600 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
601 + VCHIQ_STATUS_T ret;
602 + vchiq_use_service(cma_service);
603 + ret = vchiq_queue_message(cma_service, &elem, 1);
604 + vchiq_release_service(cma_service);
605 + if (ret != VCHIQ_SUCCESS)
606 + LOG_ERR("vchiq_queue_message returned %x", ret);
609 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
611 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
613 + vchiu_queue_push(&cma_msg_queue, msg);
614 + up(&vc_cma_worker_queue_push_mutex);
618 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
621 + for (i = 0; i < num_chunks; i++) {
622 + struct page *chunk;
623 + unsigned int chunk_num;
624 + uint8_t *chunk_addr;
625 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
627 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
629 + VC_CMA_CHUNK_ORDER);
633 + chunk_addr = page_address(chunk);
634 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
635 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
639 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
640 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
641 + VC_CMA_CHUNK_SIZE) != 0);
642 + if (chunk_num >= vc_cma_chunks) {
643 + LOG_ERR("%s: ===============================",
645 + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
646 + "bad SPARSEMEM configuration?",
647 + __func__, (unsigned int)page_to_phys(chunk),
648 + vc_cma_base, vc_cma_base + vc_cma_size - 1);
649 + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
650 + (void*)0/*vc_cma_device.dev.cma_area*/);
651 + LOG_ERR("%s: ===============================",
655 + reply->params[i] = chunk_num;
656 + vc_cma_chunks_used++;
659 + if (i < num_chunks) {
660 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
661 + "for %x bytes (alloc %d of %d, %d free)",
662 + __func__, VC_CMA_CHUNK_SIZE, i,
663 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
667 + LOG_DBG("CMA allocated %d chunks -> %d used",
668 + num_chunks, vc_cma_chunks_used);
669 + reply->type = VC_CMA_MSG_ALLOCATED;
672 + VCHIQ_ELEMENT_T elem = {
674 + offsetof(struct cma_msg, params[0]) +
675 + num_chunks * sizeof(reply->params[0])
677 + VCHIQ_STATUS_T ret;
678 + vchiq_use_service(cma_service);
679 + ret = vchiq_queue_message(cma_service, &elem, 1);
680 + vchiq_release_service(cma_service);
681 + if (ret != VCHIQ_SUCCESS)
682 + LOG_ERR("vchiq_queue_message return " "%x", ret);
688 +static int cma_worker_proc(void *param)
690 + static struct cma_msg reply;
694 + VCHIQ_HEADER_T *msg;
695 + static struct cma_msg msg_copy;
696 + struct cma_msg *cma_msg = &msg_copy;
697 + int type, msg_size;
699 + msg = vchiu_queue_pop(&cma_msg_queue);
700 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
701 + msg_size = msg->size;
702 + memcpy(&msg_copy, msg->data, msg_size);
703 + type = cma_msg->type;
704 + vchiq_release_message(cma_service, msg);
708 + if (type == VC_CMA_MSG_QUIT)
710 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
720 + case VC_CMA_MSG_ALLOC:{
721 + int num_chunks, free_chunks;
722 + num_chunks = cma_msg->params[0];
724 + vc_cma_chunks - vc_cma_chunks_used;
725 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
726 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
728 + ("CMA_MSG_ALLOC - chunk count (%d) "
729 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
731 + VC_CMA_MAX_PARAMS_PER_MSG);
732 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
735 + if (num_chunks > free_chunks) {
737 + ("CMA_MSG_ALLOC - chunk count (%d) "
738 + "exceeds free chunks (%d)",
739 + num_chunks, free_chunks);
740 + num_chunks = free_chunks;
743 + vc_cma_alloc_chunks(num_chunks, &reply);
747 + case VC_CMA_MSG_FREE:{
750 + offsetof(struct cma_msg,
752 + sizeof(cma_msg->params[0]);
754 + BUG_ON(chunk_count <= 0);
756 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
757 + chunk_count, cma_msg->params[0]);
758 + for (i = 0; i < chunk_count; i++) {
759 + int chunk_num = cma_msg->params[i];
760 + struct page *page = vc_cma_base_page +
761 + chunk_num * PAGES_PER_CHUNK;
762 + if (chunk_num >= vc_cma_chunks) {
764 + ("CMA_MSG_FREE - chunk %d of %d"
765 + " (value %x) exceeds maximum "
766 + "(%x)", i, chunk_count,
768 + vc_cma_chunks - 1);
772 + if (!dma_release_from_contiguous
773 + (NULL /*&vc_cma_device.dev*/, page,
774 + PAGES_PER_CHUNK)) {
776 + ("CMA_MSG_FREE - failed to "
777 + "release chunk %d (phys %x, "
778 + "page %x)", chunk_num,
779 + page_to_phys(page),
780 + (unsigned int)page);
782 + vc_cma_chunks_used--;
784 + LOG_DBG("CMA released %d chunks -> %d used",
785 + i, vc_cma_chunks_used);
789 + case VC_CMA_MSG_UPDATE_RESERVE:{
790 + int chunks_needed =
791 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
793 + / VC_CMA_CHUNK_SIZE) -
794 + vc_cma_chunks_reserved;
797 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
800 + /* Cap the reservations to what is available */
801 + if (chunks_needed > 0) {
802 + if (chunks_needed >
804 + vc_cma_chunks_used))
807 + vc_cma_chunks_used);
810 + vc_cma_alloc_chunks(chunks_needed,
815 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
817 + vc_cma_chunks_reserved += chunks_needed;
822 + LOG_ERR("unexpected msg type %d", type);
827 + LOG_DBG("quitting...");
831 +/****************************************************************************
833 +* vc_cma_connected_init
835 +* This function is called once the videocore has been connected.
837 +***************************************************************************/
839 +static void vc_cma_connected_init(void)
841 + VCHIQ_SERVICE_PARAMS_T service_params;
843 + LOG_DBG("vc_cma_connected_init");
845 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
846 + LOG_ERR("could not create CMA msg queue");
850 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
851 + goto fail_vchiq_init;
853 + vchiq_connect(cma_instance);
855 + service_params.fourcc = VC_CMA_FOURCC;
856 + service_params.callback = cma_service_callback;
857 + service_params.userdata = NULL;
858 + service_params.version = VC_CMA_VERSION;
859 + service_params.version_min = VC_CMA_VERSION;
861 + if (vchiq_open_service(cma_instance, &service_params,
862 + &cma_service) != VCHIQ_SUCCESS) {
863 + LOG_ERR("failed to open service - already in use?");
864 + goto fail_vchiq_open;
867 + vchiq_release_service(cma_service);
869 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
871 + LOG_ERR("could not create CMA worker thread");
874 + set_user_nice(cma_worker, -20);
875 + wake_up_process(cma_worker);
880 + vchiq_close_service(cma_service);
882 + vchiq_shutdown(cma_instance);
884 + vchiu_queue_delete(&cma_msg_queue);
890 +loud_error_header(void)
895 + LOG_ERR("============================================================"
896 + "================");
897 + LOG_ERR("============================================================"
898 + "================");
905 +loud_error_footer(void)
907 + if (!in_loud_error)
911 + LOG_ERR("============================================================"
912 + "================");
913 + LOG_ERR("============================================================"
914 + "================");
920 +static int check_cma_config(void) { return 1; }
923 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
924 + const char *symbol,
925 + void *buf, size_t bufsize)
927 + VC_MEM_ADDR_T vcMemAddr;
932 + if (!LookupVideoCoreSymbol(handle, symbol,
935 + loud_error_header();
937 + "failed to find VC symbol \"%s\".",
939 + loud_error_footer();
943 + if (vcMemSize != bufsize) {
944 + loud_error_header();
946 + "VC symbol \"%s\" is the wrong size.",
948 + loud_error_footer();
952 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
953 + vcMapAddr += mm_vc_mem_phys_addr;
954 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
955 + if (mapAddr == 0) {
956 + loud_error_header();
958 + "failed to ioremap \"%s\" @ 0x%x "
959 + "(phys: 0x%x, size: %u).",
961 + (unsigned int)vcMapAddr,
962 + (unsigned int)vcMemAddr,
963 + (unsigned int)vcMemSize);
964 + loud_error_footer();
968 + memcpy(buf, mapAddr, bufsize);
976 +check_cma_config(void)
978 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
979 + VC_MEM_ADDR_T mempool_start;
980 + VC_MEM_ADDR_T mempool_end;
981 + VC_MEM_ADDR_T mempool_offline_start;
982 + VC_MEM_ADDR_T mempool_offline_end;
983 + VC_MEM_ADDR_T cam_alloc_base;
984 + VC_MEM_ADDR_T cam_alloc_size;
985 + VC_MEM_ADDR_T cam_alloc_end;
988 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
991 + /* Read the relevant VideoCore variables */
992 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
994 + sizeof(mempool_start)))
997 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
999 + sizeof(mempool_end)))
1002 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
1003 + &mempool_offline_start,
1004 + sizeof(mempool_offline_start)))
1007 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
1008 + &mempool_offline_end,
1009 + sizeof(mempool_offline_end)))
1012 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1014 + sizeof(cam_alloc_base)))
1017 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1019 + sizeof(cam_alloc_size)))
1022 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1026 + /* Now the sanity checks */
1027 + if (!mempool_offline_start)
1028 + mempool_offline_start = mempool_start;
1029 + if (!mempool_offline_end)
1030 + mempool_offline_end = mempool_end;
1032 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1033 + loud_error_header();
1035 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1036 + "vc_cma_base(%x)",
1037 + mempool_offline_start,
1038 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1043 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1044 + (vc_cma_base + vc_cma_size)) {
1045 + loud_error_header();
1047 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1048 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1049 + mempool_offline_start,
1050 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1051 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1055 + if (mempool_end < mempool_start) {
1056 + loud_error_header();
1058 + "__MEMPOOL_END(%x) must not be before "
1059 + "__MEMPOOL_START(%x)",
1065 + if (mempool_offline_end < mempool_offline_start) {
1066 + loud_error_header();
1068 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1069 + "__MEMPOOL_OFFLINE_START(%x)",
1070 + mempool_offline_end,
1071 + mempool_offline_start);
1075 + if (mempool_offline_start < mempool_start) {
1076 + loud_error_header();
1078 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1079 + "__MEMPOOL_START(%x)",
1080 + mempool_offline_start,
1085 + if (mempool_offline_end > mempool_end) {
1086 + loud_error_header();
1088 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1089 + "__MEMPOOL_END(%x)",
1090 + mempool_offline_end,
1095 + if ((cam_alloc_base < mempool_end) &&
1096 + (cam_alloc_end > mempool_start)) {
1097 + loud_error_header();
1099 + "cam_alloc pool(%x-%x) overlaps "
1101 + cam_alloc_base, cam_alloc_end,
1102 + mempool_start, mempool_end);
1106 + loud_error_footer();
1109 + CloseVideoCoreMemory(mem_hndl);
1116 +static int vc_cma_init(void)
1119 + struct device *dev;
1121 + if (!check_cma_config())
1124 + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
1125 + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
1126 + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
1127 + vc_cma_size, vc_cma_size / (1024 * 1024));
1128 + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
1129 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1131 + vc_cma_base_page = phys_to_page(vc_cma_base);
1133 + if (vc_cma_chunks) {
1134 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1136 + for (vc_cma_chunks_used = 0;
1137 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1138 + struct page *chunk;
1139 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
1141 + VC_CMA_CHUNK_ORDER);
1144 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1145 + VC_CMA_CHUNK_SIZE) != 0);
1147 + if (vc_cma_chunks_used != chunks_needed) {
1148 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1149 + "bytes, allocation %d of %d)",
1150 + __func__, VC_CMA_CHUNK_SIZE,
1151 + vc_cma_chunks_used, chunks_needed);
1155 + vchiq_add_connected_callback(vc_cma_connected_init);
1158 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1160 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1164 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1165 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1167 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1168 + goto out_unregister;
1171 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1172 + if (IS_ERR(vc_cma_class)) {
1173 + rc = PTR_ERR(vc_cma_class);
1174 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1175 + goto out_cdev_del;
1178 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1180 + if (IS_ERR(dev)) {
1181 + rc = PTR_ERR(dev);
1182 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1183 + goto out_class_destroy;
1186 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1187 + if (vc_cma_proc_entry == NULL) {
1189 + LOG_ERR("%s: proc_create failed", __func__);
1190 + goto out_device_destroy;
1193 + vc_cma_inited = 1;
1196 +out_device_destroy:
1197 + device_destroy(vc_cma_class, vc_cma_devnum);
1200 + class_destroy(vc_cma_class);
1201 + vc_cma_class = NULL;
1204 + cdev_del(&vc_cma_cdev);
1207 + unregister_chrdev_region(vc_cma_devnum, 1);
1210 + /* It is tempting to try to clean up by calling
1211 + dma_release_from_contiguous for all allocated chunks, but it isn't
1212 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1213 + VideoCore is already using that memory, so giving it back to Linux
1214 + is likely to be fatal.
1219 +/****************************************************************************
1223 +***************************************************************************/
1225 +static void __exit vc_cma_exit(void)
1227 + LOG_DBG("%s: called", __func__);
1229 + if (vc_cma_inited) {
1230 + remove_proc_entry(DRIVER_NAME, NULL);
1231 + device_destroy(vc_cma_class, vc_cma_devnum);
1232 + class_destroy(vc_cma_class);
1233 + cdev_del(&vc_cma_cdev);
1234 + unregister_chrdev_region(vc_cma_devnum, 1);
1238 +module_init(vc_cma_init);
1239 +module_exit(vc_cma_exit);
1240 +MODULE_LICENSE("GPL");
1241 +MODULE_AUTHOR("Broadcom Corporation");
1242 diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
1243 index c085ede..43d2ac9 100644
1244 --- a/drivers/misc/Makefile
1245 +++ b/drivers/misc/Makefile
1246 @@ -51,7 +51,7 @@ obj-$(CONFIG_INTEL_MEI) += mei/
1247 obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
1248 obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
1249 obj-$(CONFIG_SRAM) += sram.o
1250 -obj-y += vc04_services/
1251 +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
1253 obj-$(CONFIG_GENWQE) += genwqe/
1254 obj-$(CONFIG_ECHO) += echo/
1255 diff --git a/include/linux/broadcom/vc_cma.h b/include/linux/broadcom/vc_cma.h
1256 new file mode 100644
1257 index 0000000..5325832
1259 +++ b/include/linux/broadcom/vc_cma.h
1261 +/*****************************************************************************
1262 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1264 +* Unless you and Broadcom execute a separate written software license
1265 +* agreement governing use of this software, this software is licensed to you
1266 +* under the terms of the GNU General Public License version 2, available at
1267 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1269 +* Notwithstanding the above, under no circumstances may you combine this
1270 +* software in any way with any other Broadcom software provided under a
1271 +* license other than the GPL, without Broadcom's express prior written
1273 +*****************************************************************************/
1275 +#if !defined( VC_CMA_H )
1278 +#include <linux/ioctl.h>
1280 +#define VC_CMA_IOC_MAGIC 0xc5
1282 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1285 +extern void __init vc_cma_early_init(void);
1286 +extern void __init vc_cma_reserve(void);
1289 +#endif /* VC_CMA_H */