1 From 0db77c38e5817cf501d2bb29d21e7389491078d3 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 3 Jul 2013 00:31:47 +0100
4 Subject: [PATCH 08/54] cma: Add vc_cma driver to enable use of CMA
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 drivers/char/Kconfig | 2 +
9 drivers/char/Makefile | 2 +
10 drivers/char/broadcom/Kconfig | 16 +
11 drivers/char/broadcom/Makefile | 1 +
12 drivers/char/broadcom/vc_cma/Makefile | 14 +
13 drivers/char/broadcom/vc_cma/vc_cma.c | 1143 +++++++++++++++++++++++++++++++++
14 drivers/misc/Makefile | 2 +-
15 include/linux/broadcom/vc_cma.h | 29 +
16 8 files changed, 1208 insertions(+), 1 deletion(-)
17 create mode 100644 drivers/char/broadcom/Kconfig
18 create mode 100644 drivers/char/broadcom/Makefile
19 create mode 100644 drivers/char/broadcom/vc_cma/Makefile
20 create mode 100644 drivers/char/broadcom/vc_cma/vc_cma.c
21 create mode 100644 include/linux/broadcom/vc_cma.h
23 --- a/drivers/char/Kconfig
24 +++ b/drivers/char/Kconfig
25 @@ -581,6 +581,8 @@ config DEVPORT
27 source "drivers/s390/char/Kconfig"
29 +source "drivers/char/broadcom/Kconfig"
32 bool "Enable device interface for some SMD packet ports"
34 --- a/drivers/char/Makefile
35 +++ b/drivers/char/Makefile
36 @@ -61,3 +61,5 @@ obj-$(CONFIG_JS_RTC) += js-rtc.o
39 obj-$(CONFIG_TILE_SROM) += tile-srom.o
41 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
43 +++ b/drivers/char/broadcom/Kconfig
46 +# Broadcom char driver config
49 +menuconfig BRCM_CHAR_DRIVERS
50 + bool "Broadcom Char Drivers"
52 + Broadcom's char drivers
55 + bool "Videocore CMA"
56 + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
59 + Helper for videocore CMA access.
62 +++ b/drivers/char/broadcom/Makefile
64 +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
66 +++ b/drivers/char/broadcom/vc_cma/Makefile
68 +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
70 +ccflags-y += -Iinclude/linux/broadcom
71 +ccflags-y += -Idrivers/misc/vc04_services
72 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
73 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
75 +ccflags-y += -D__KERNEL__
76 +ccflags-y += -D__linux__
79 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
81 +vc-cma-objs := vc_cma.o
83 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
86 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
88 + * Redistribution and use in source and binary forms, with or without
89 + * modification, are permitted provided that the following conditions
91 + * 1. Redistributions of source code must retain the above copyright
92 + * notice, this list of conditions, and the following disclaimer,
93 + * without modification.
94 + * 2. Redistributions in binary form must reproduce the above copyright
95 + * notice, this list of conditions and the following disclaimer in the
96 + * documentation and/or other materials provided with the distribution.
97 + * 3. The names of the above-listed copyright holders may not be used
98 + * to endorse or promote products derived from this software without
99 + * specific prior written permission.
101 + * ALTERNATIVELY, this software may be distributed under the terms of the
102 + * GNU General Public License ("GPL") version 2, as published by the Free
103 + * Software Foundation.
105 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
106 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
107 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
108 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
109 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
110 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
111 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
112 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
113 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
114 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
115 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
118 +#include <linux/kernel.h>
119 +#include <linux/module.h>
120 +#include <linux/kthread.h>
121 +#include <linux/fs.h>
122 +#include <linux/device.h>
123 +#include <linux/cdev.h>
124 +#include <linux/mm.h>
125 +#include <linux/proc_fs.h>
126 +#include <linux/seq_file.h>
127 +#include <linux/dma-mapping.h>
128 +#include <linux/dma-contiguous.h>
129 +#include <linux/platform_device.h>
130 +#include <linux/uaccess.h>
131 +#include <asm/cacheflush.h>
135 +#include "vchiq_util.h"
136 +#include "vchiq_connected.h"
137 +//#include "debug_sym.h"
138 +//#include "vc_mem.h"
140 +#define DRIVER_NAME "vc-cma"
142 +#define LOG_DBG(fmt, ...) \
143 + if (vc_cma_debug) \
144 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
145 +#define LOG_ERR(fmt, ...) \
146 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
148 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
149 +#define VC_CMA_VERSION 2
151 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
152 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
153 +#define VC_CMA_MAX_PARAMS_PER_MSG \
154 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
155 +#define VC_CMA_RESERVE_COUNT_MAX 16
157 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
159 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
161 +#define loud_error(...) \
162 + LOG_ERR("===== " __VA_ARGS__)
168 + VC_CMA_MSG_ALLOC, /* chunk count */
169 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
170 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
171 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
172 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
173 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
174 + VC_CMA_MSG_UPDATE_RESERVE,
179 + unsigned short type;
180 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
183 +struct vc_cma_reserve_user {
185 + unsigned int reserve;
188 +/* Device (/dev) related variables */
189 +static dev_t vc_cma_devnum;
190 +static struct class *vc_cma_class;
191 +static struct cdev vc_cma_cdev;
192 +static int vc_cma_inited;
193 +static int vc_cma_debug;
196 +static struct proc_dir_entry *vc_cma_proc_entry;
198 +phys_addr_t vc_cma_base;
199 +struct page *vc_cma_base_page;
200 +unsigned int vc_cma_size;
201 +EXPORT_SYMBOL(vc_cma_size);
202 +unsigned int vc_cma_initial;
203 +unsigned int vc_cma_chunks;
204 +unsigned int vc_cma_chunks_used;
205 +unsigned int vc_cma_chunks_reserved;
207 +static int in_loud_error;
209 +unsigned int vc_cma_reserve_total;
210 +unsigned int vc_cma_reserve_count;
211 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
212 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
213 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
215 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
216 +static struct platform_device vc_cma_device = {
220 + .dma_mask = &vc_cma_dma_mask,
221 + .coherent_dma_mask = DMA_BIT_MASK(32),
225 +static VCHIQ_INSTANCE_T cma_instance;
226 +static VCHIQ_SERVICE_HANDLE_T cma_service;
227 +static VCHIU_QUEUE_T cma_msg_queue;
228 +static struct task_struct *cma_worker;
230 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
231 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
232 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
233 + VCHIQ_HEADER_T * header,
234 + VCHIQ_SERVICE_HANDLE_T service,
235 + void *bulk_userdata);
236 +static void send_vc_msg(unsigned short type,
237 + unsigned short param1, unsigned short param2);
238 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
240 +static int early_vc_cma_mem(char *p)
242 + unsigned int new_size;
243 + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
244 + vc_cma_size = memparse(p, &p);
245 + vc_cma_initial = vc_cma_size;
247 + vc_cma_size = memparse(p + 1, &p);
249 + vc_cma_base = memparse(p + 1, &p);
251 + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
252 + & ~(VC_CMA_CHUNK_SIZE - 1);
253 + if (new_size > vc_cma_size)
255 + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
256 + & ~(VC_CMA_CHUNK_SIZE - 1);
257 + if (vc_cma_initial > vc_cma_size)
258 + vc_cma_initial = vc_cma_size;
259 + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
260 + & ~(VC_CMA_CHUNK_SIZE - 1);
262 + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
263 + vc_cma_size, (unsigned int)vc_cma_base);
268 +early_param("vc-cma-mem", early_vc_cma_mem);
270 +void vc_cma_early_init(void)
272 + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
274 + int rc = platform_device_register(&vc_cma_device);
275 + LOG_DBG("platform_device_register -> %d", rc);
279 +void vc_cma_reserve(void)
281 + /* if vc_cma_size is set, then declare vc CMA area of the same
282 + * size from the end of memory
285 + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
286 + vc_cma_base, 0) == 0) {
288 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
289 + vc_cma_size, (unsigned int)vc_cma_base);
293 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
296 +/****************************************************************************
300 +***************************************************************************/
302 +static int vc_cma_open(struct inode *inode, struct file *file)
310 +/****************************************************************************
314 +***************************************************************************/
316 +static int vc_cma_release(struct inode *inode, struct file *file)
321 + vc_cma_set_reserve(0, current->tgid);
326 +/****************************************************************************
330 +***************************************************************************/
332 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
340 + case VC_CMA_IOC_RESERVE:
341 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
346 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
353 +/****************************************************************************
355 +* File Operations for the driver.
357 +***************************************************************************/
359 +static const struct file_operations vc_cma_fops = {
360 + .owner = THIS_MODULE,
361 + .open = vc_cma_open,
362 + .release = vc_cma_release,
363 + .unlocked_ioctl = vc_cma_ioctl,
366 +/****************************************************************************
370 +***************************************************************************/
372 +static int vc_cma_show_info(struct seq_file *m, void *v)
376 + seq_printf(m, "Videocore CMA:\n");
377 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
378 + seq_printf(m, " Length : %08x\n", vc_cma_size);
379 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
380 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
381 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
382 + (int)vc_cma_chunks,
383 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
384 + seq_printf(m, " Used : %4d (%d bytes)\n",
385 + (int)vc_cma_chunks_used,
386 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
387 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
388 + (unsigned int)vc_cma_chunks_reserved,
389 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
391 + for (i = 0; i < vc_cma_reserve_count; i++) {
392 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
393 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
397 + seq_printf(m, "\n");
402 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
404 + return single_open(file, vc_cma_show_info, NULL);
407 +/****************************************************************************
411 +***************************************************************************/
413 +static int vc_cma_proc_write(struct file *file,
414 + const char __user *buffer,
415 + size_t size, loff_t *ppos)
418 + char input_str[20];
420 + memset(input_str, 0, sizeof(input_str));
422 + if (size > sizeof(input_str)) {
423 + LOG_ERR("%s: input string length too long", __func__);
427 + if (copy_from_user(input_str, buffer, size - 1)) {
428 + LOG_ERR("%s: failed to get input string", __func__);
431 +#define ALLOC_STR "alloc"
432 +#define FREE_STR "free"
433 +#define DEBUG_STR "debug"
434 +#define RESERVE_STR "reserve"
435 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
437 + char *p = input_str + strlen(ALLOC_STR);
441 + size = memparse(p, NULL);
442 + LOG_ERR("/proc/vc-cma: alloc %d", size);
444 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
445 + size / VC_CMA_CHUNK_SIZE, 0);
447 + LOG_ERR("invalid size '%s'", p);
449 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
451 + char *p = input_str + strlen(FREE_STR);
455 + size = memparse(p, NULL);
456 + LOG_ERR("/proc/vc-cma: free %d", size);
458 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
459 + size / VC_CMA_CHUNK_SIZE, 0);
461 + LOG_ERR("invalid size '%s'", p);
463 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
464 + char *p = input_str + strlen(DEBUG_STR);
467 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
469 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
471 + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
473 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
476 + char *p = input_str + strlen(RESERVE_STR);
479 + size = memparse(p, NULL);
481 + reserved = vc_cma_set_reserve(size, current->tgid);
482 + rc = (reserved >= 0) ? size : reserved;
489 +/****************************************************************************
491 +* File Operations for /proc interface.
493 +***************************************************************************/
495 +static const struct file_operations vc_cma_proc_fops = {
496 + .open = vc_cma_proc_open,
498 + .write = vc_cma_proc_write,
499 + .llseek = seq_lseek,
500 + .release = single_release
503 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
505 + struct vc_cma_reserve_user *user = NULL;
509 + if (down_interruptible(&vc_cma_reserve_mutex))
510 + return -ERESTARTSYS;
512 + for (i = 0; i < vc_cma_reserve_count; i++) {
513 + if (pid == vc_cma_reserve_users[i].pid) {
514 + user = &vc_cma_reserve_users[i];
515 + delta = reserve - user->reserve;
517 + user->reserve = reserve;
519 + /* Remove this entry by copying downwards */
520 + while ((i + 1) < vc_cma_reserve_count) {
521 + user[0].pid = user[1].pid;
522 + user[0].reserve = user[1].reserve;
526 + vc_cma_reserve_count--;
533 + if (reserve && !user) {
534 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
535 + LOG_ERR("vc-cma: Too many reservations - "
536 + "increase CMA_RESERVE_COUNT_MAX");
537 + up(&vc_cma_reserve_mutex);
540 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
542 + user->reserve = reserve;
544 + vc_cma_reserve_count++;
547 + vc_cma_reserve_total += delta;
549 + send_vc_msg(VC_CMA_MSG_RESERVE,
550 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
552 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
554 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
555 + reserve, pid, vc_cma_reserve_total);
557 + up(&vc_cma_reserve_mutex);
559 + return vc_cma_reserve_total;
562 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
563 + VCHIQ_HEADER_T * header,
564 + VCHIQ_SERVICE_HANDLE_T service,
565 + void *bulk_userdata)
568 + case VCHIQ_MESSAGE_AVAILABLE:
569 + if (!send_worker_msg(header))
570 + return VCHIQ_RETRY;
572 + case VCHIQ_SERVICE_CLOSED:
573 + LOG_DBG("CMA service closed");
576 + LOG_ERR("Unexpected CMA callback reason %d", reason);
579 + return VCHIQ_SUCCESS;
582 +static void send_vc_msg(unsigned short type,
583 + unsigned short param1, unsigned short param2)
585 + unsigned short msg[] = { type, param1, param2 };
586 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
587 + VCHIQ_STATUS_T ret;
588 + vchiq_use_service(cma_service);
589 + ret = vchiq_queue_message(cma_service, &elem, 1);
590 + vchiq_release_service(cma_service);
591 + if (ret != VCHIQ_SUCCESS)
592 + LOG_ERR("vchiq_queue_message returned %x", ret);
595 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
597 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
599 + vchiu_queue_push(&cma_msg_queue, msg);
600 + up(&vc_cma_worker_queue_push_mutex);
604 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
607 + for (i = 0; i < num_chunks; i++) {
608 + struct page *chunk;
609 + unsigned int chunk_num;
610 + uint8_t *chunk_addr;
611 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
613 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
615 + VC_CMA_CHUNK_ORDER);
619 + chunk_addr = page_address(chunk);
620 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
621 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
625 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
626 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
627 + VC_CMA_CHUNK_SIZE) != 0);
628 + if (chunk_num >= vc_cma_chunks) {
629 + LOG_ERR("%s: ===============================",
631 + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
632 + "bad SPARSEMEM configuration?",
633 + __func__, (unsigned int)page_to_phys(chunk),
634 + vc_cma_base, vc_cma_base + vc_cma_size - 1);
635 + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
636 + (void*)0/*vc_cma_device.dev.cma_area*/);
637 + LOG_ERR("%s: ===============================",
641 + reply->params[i] = chunk_num;
642 + vc_cma_chunks_used++;
645 + if (i < num_chunks) {
646 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
647 + "for %x bytes (alloc %d of %d, %d free)",
648 + __func__, VC_CMA_CHUNK_SIZE, i,
649 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
653 + LOG_DBG("CMA allocated %d chunks -> %d used",
654 + num_chunks, vc_cma_chunks_used);
655 + reply->type = VC_CMA_MSG_ALLOCATED;
658 + VCHIQ_ELEMENT_T elem = {
660 + offsetof(struct cma_msg, params[0]) +
661 + num_chunks * sizeof(reply->params[0])
663 + VCHIQ_STATUS_T ret;
664 + vchiq_use_service(cma_service);
665 + ret = vchiq_queue_message(cma_service, &elem, 1);
666 + vchiq_release_service(cma_service);
667 + if (ret != VCHIQ_SUCCESS)
668 + LOG_ERR("vchiq_queue_message return " "%x", ret);
674 +static int cma_worker_proc(void *param)
676 + static struct cma_msg reply;
680 + VCHIQ_HEADER_T *msg;
681 + static struct cma_msg msg_copy;
682 + struct cma_msg *cma_msg = &msg_copy;
683 + int type, msg_size;
685 + msg = vchiu_queue_pop(&cma_msg_queue);
686 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
687 + msg_size = msg->size;
688 + memcpy(&msg_copy, msg->data, msg_size);
689 + type = cma_msg->type;
690 + vchiq_release_message(cma_service, msg);
694 + if (type == VC_CMA_MSG_QUIT)
696 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
706 + case VC_CMA_MSG_ALLOC:{
707 + int num_chunks, free_chunks;
708 + num_chunks = cma_msg->params[0];
710 + vc_cma_chunks - vc_cma_chunks_used;
711 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
712 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
714 + ("CMA_MSG_ALLOC - chunk count (%d) "
715 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
717 + VC_CMA_MAX_PARAMS_PER_MSG);
718 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
721 + if (num_chunks > free_chunks) {
723 + ("CMA_MSG_ALLOC - chunk count (%d) "
724 + "exceeds free chunks (%d)",
725 + num_chunks, free_chunks);
726 + num_chunks = free_chunks;
729 + vc_cma_alloc_chunks(num_chunks, &reply);
733 + case VC_CMA_MSG_FREE:{
736 + offsetof(struct cma_msg,
738 + sizeof(cma_msg->params[0]);
740 + BUG_ON(chunk_count <= 0);
742 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
743 + chunk_count, cma_msg->params[0]);
744 + for (i = 0; i < chunk_count; i++) {
745 + int chunk_num = cma_msg->params[i];
746 + struct page *page = vc_cma_base_page +
747 + chunk_num * PAGES_PER_CHUNK;
748 + if (chunk_num >= vc_cma_chunks) {
750 + ("CMA_MSG_FREE - chunk %d of %d"
751 + " (value %x) exceeds maximum "
752 + "(%x)", i, chunk_count,
754 + vc_cma_chunks - 1);
758 + if (!dma_release_from_contiguous
759 + (NULL /*&vc_cma_device.dev*/, page,
760 + PAGES_PER_CHUNK)) {
762 + ("CMA_MSG_FREE - failed to "
763 + "release chunk %d (phys %x, "
764 + "page %x)", chunk_num,
765 + page_to_phys(page),
766 + (unsigned int)page);
768 + vc_cma_chunks_used--;
770 + LOG_DBG("CMA released %d chunks -> %d used",
771 + i, vc_cma_chunks_used);
775 + case VC_CMA_MSG_UPDATE_RESERVE:{
776 + int chunks_needed =
777 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
779 + / VC_CMA_CHUNK_SIZE) -
780 + vc_cma_chunks_reserved;
783 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
786 + /* Cap the reservations to what is available */
787 + if (chunks_needed > 0) {
788 + if (chunks_needed >
790 + vc_cma_chunks_used))
793 + vc_cma_chunks_used);
796 + vc_cma_alloc_chunks(chunks_needed,
801 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
803 + vc_cma_chunks_reserved += chunks_needed;
808 + LOG_ERR("unexpected msg type %d", type);
813 + LOG_DBG("quitting...");
817 +/****************************************************************************
819 +* vc_cma_connected_init
821 +* This function is called once the videocore has been connected.
823 +***************************************************************************/
825 +static void vc_cma_connected_init(void)
827 + VCHIQ_SERVICE_PARAMS_T service_params;
829 + LOG_DBG("vc_cma_connected_init");
831 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
832 + LOG_ERR("could not create CMA msg queue");
836 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
837 + goto fail_vchiq_init;
839 + vchiq_connect(cma_instance);
841 + service_params.fourcc = VC_CMA_FOURCC;
842 + service_params.callback = cma_service_callback;
843 + service_params.userdata = NULL;
844 + service_params.version = VC_CMA_VERSION;
845 + service_params.version_min = VC_CMA_VERSION;
847 + if (vchiq_open_service(cma_instance, &service_params,
848 + &cma_service) != VCHIQ_SUCCESS) {
849 + LOG_ERR("failed to open service - already in use?");
850 + goto fail_vchiq_open;
853 + vchiq_release_service(cma_service);
855 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
857 + LOG_ERR("could not create CMA worker thread");
860 + set_user_nice(cma_worker, -20);
861 + wake_up_process(cma_worker);
866 + vchiq_close_service(cma_service);
868 + vchiq_shutdown(cma_instance);
870 + vchiu_queue_delete(&cma_msg_queue);
876 +loud_error_header(void)
881 + LOG_ERR("============================================================"
882 + "================");
883 + LOG_ERR("============================================================"
884 + "================");
891 +loud_error_footer(void)
893 + if (!in_loud_error)
897 + LOG_ERR("============================================================"
898 + "================");
899 + LOG_ERR("============================================================"
900 + "================");
906 +static int check_cma_config(void) { return 1; }
909 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
910 + const char *symbol,
911 + void *buf, size_t bufsize)
913 + VC_MEM_ADDR_T vcMemAddr;
918 + if (!LookupVideoCoreSymbol(handle, symbol,
921 + loud_error_header();
923 + "failed to find VC symbol \"%s\".",
925 + loud_error_footer();
929 + if (vcMemSize != bufsize) {
930 + loud_error_header();
932 + "VC symbol \"%s\" is the wrong size.",
934 + loud_error_footer();
938 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
939 + vcMapAddr += mm_vc_mem_phys_addr;
940 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
941 + if (mapAddr == 0) {
942 + loud_error_header();
944 + "failed to ioremap \"%s\" @ 0x%x "
945 + "(phys: 0x%x, size: %u).",
947 + (unsigned int)vcMapAddr,
948 + (unsigned int)vcMemAddr,
949 + (unsigned int)vcMemSize);
950 + loud_error_footer();
954 + memcpy(buf, mapAddr, bufsize);
962 +check_cma_config(void)
964 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
965 + VC_MEM_ADDR_T mempool_start;
966 + VC_MEM_ADDR_T mempool_end;
967 + VC_MEM_ADDR_T mempool_offline_start;
968 + VC_MEM_ADDR_T mempool_offline_end;
969 + VC_MEM_ADDR_T cam_alloc_base;
970 + VC_MEM_ADDR_T cam_alloc_size;
971 + VC_MEM_ADDR_T cam_alloc_end;
974 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
977 + /* Read the relevant VideoCore variables */
978 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
980 + sizeof(mempool_start)))
983 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
985 + sizeof(mempool_end)))
988 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
989 + &mempool_offline_start,
990 + sizeof(mempool_offline_start)))
993 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
994 + &mempool_offline_end,
995 + sizeof(mempool_offline_end)))
998 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1000 + sizeof(cam_alloc_base)))
1003 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1005 + sizeof(cam_alloc_size)))
1008 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1012 + /* Now the sanity checks */
1013 + if (!mempool_offline_start)
1014 + mempool_offline_start = mempool_start;
1015 + if (!mempool_offline_end)
1016 + mempool_offline_end = mempool_end;
1018 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1019 + loud_error_header();
1021 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1022 + "vc_cma_base(%x)",
1023 + mempool_offline_start,
1024 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1029 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1030 + (vc_cma_base + vc_cma_size)) {
1031 + loud_error_header();
1033 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1034 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1035 + mempool_offline_start,
1036 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1037 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1041 + if (mempool_end < mempool_start) {
1042 + loud_error_header();
1044 + "__MEMPOOL_END(%x) must not be before "
1045 + "__MEMPOOL_START(%x)",
1051 + if (mempool_offline_end < mempool_offline_start) {
1052 + loud_error_header();
1054 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1055 + "__MEMPOOL_OFFLINE_START(%x)",
1056 + mempool_offline_end,
1057 + mempool_offline_start);
1061 + if (mempool_offline_start < mempool_start) {
1062 + loud_error_header();
1064 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1065 + "__MEMPOOL_START(%x)",
1066 + mempool_offline_start,
1071 + if (mempool_offline_end > mempool_end) {
1072 + loud_error_header();
1074 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1075 + "__MEMPOOL_END(%x)",
1076 + mempool_offline_end,
1081 + if ((cam_alloc_base < mempool_end) &&
1082 + (cam_alloc_end > mempool_start)) {
1083 + loud_error_header();
1085 + "cam_alloc pool(%x-%x) overlaps "
1087 + cam_alloc_base, cam_alloc_end,
1088 + mempool_start, mempool_end);
1092 + loud_error_footer();
1095 + CloseVideoCoreMemory(mem_hndl);
1102 +static int vc_cma_init(void)
1105 + struct device *dev;
1107 + if (!check_cma_config())
1110 + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
1111 + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
1112 + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
1113 + vc_cma_size, vc_cma_size / (1024 * 1024));
1114 + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
1115 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1117 + vc_cma_base_page = phys_to_page(vc_cma_base);
1119 + if (vc_cma_chunks) {
1120 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1122 + for (vc_cma_chunks_used = 0;
1123 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1124 + struct page *chunk;
1125 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
1127 + VC_CMA_CHUNK_ORDER);
1130 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1131 + VC_CMA_CHUNK_SIZE) != 0);
1133 + if (vc_cma_chunks_used != chunks_needed) {
1134 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1135 + "bytes, allocation %d of %d)",
1136 + __func__, VC_CMA_CHUNK_SIZE,
1137 + vc_cma_chunks_used, chunks_needed);
1141 + vchiq_add_connected_callback(vc_cma_connected_init);
1144 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1146 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1150 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1151 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1153 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1154 + goto out_unregister;
1157 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1158 + if (IS_ERR(vc_cma_class)) {
1159 + rc = PTR_ERR(vc_cma_class);
1160 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1161 + goto out_cdev_del;
1164 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1166 + if (IS_ERR(dev)) {
1167 + rc = PTR_ERR(dev);
1168 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1169 + goto out_class_destroy;
1172 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1173 + if (vc_cma_proc_entry == NULL) {
1175 + LOG_ERR("%s: proc_create failed", __func__);
1176 + goto out_device_destroy;
1179 + vc_cma_inited = 1;
1182 +out_device_destroy:
1183 + device_destroy(vc_cma_class, vc_cma_devnum);
1186 + class_destroy(vc_cma_class);
1187 + vc_cma_class = NULL;
1190 + cdev_del(&vc_cma_cdev);
1193 + unregister_chrdev_region(vc_cma_devnum, 1);
1196 + /* It is tempting to try to clean up by calling
1197 + dma_release_from_contiguous for all allocated chunks, but it isn't
1198 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1199 + VideoCore is already using that memory, so giving it back to Linux
1200 + is likely to be fatal.
1205 +/****************************************************************************
1209 +***************************************************************************/
1211 +static void __exit vc_cma_exit(void)
1213 + LOG_DBG("%s: called", __func__);
1215 + if (vc_cma_inited) {
1216 + remove_proc_entry(DRIVER_NAME, NULL);
1217 + device_destroy(vc_cma_class, vc_cma_devnum);
1218 + class_destroy(vc_cma_class);
1219 + cdev_del(&vc_cma_cdev);
1220 + unregister_chrdev_region(vc_cma_devnum, 1);
1224 +module_init(vc_cma_init);
1225 +module_exit(vc_cma_exit);
1226 +MODULE_LICENSE("GPL");
1227 +MODULE_AUTHOR("Broadcom Corporation");
1228 --- a/drivers/misc/Makefile
1229 +++ b/drivers/misc/Makefile
1230 @@ -52,6 +52,6 @@ obj-$(CONFIG_INTEL_MEI) += mei/
1231 obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
1232 obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
1233 obj-$(CONFIG_SRAM) += sram.o
1234 -obj-y += vc04_services/
1235 +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
1237 obj-$(CONFIG_GENWQE) += genwqe/
1239 +++ b/include/linux/broadcom/vc_cma.h
1241 +/*****************************************************************************
1242 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1244 +* Unless you and Broadcom execute a separate written software license
1245 +* agreement governing use of this software, this software is licensed to you
1246 +* under the terms of the GNU General Public License version 2, available at
1247 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1249 +* Notwithstanding the above, under no circumstances may you combine this
1250 +* software in any way with any other Broadcom software provided under a
1251 +* license other than the GPL, without Broadcom's express prior written
1253 +*****************************************************************************/
1255 +#if !defined( VC_CMA_H )
1258 +#include <linux/ioctl.h>
1260 +#define VC_CMA_IOC_MAGIC 0xc5
1262 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1265 +extern void __init vc_cma_early_init(void);
1266 +extern void __init vc_cma_reserve(void);
1269 +#endif /* VC_CMA_H */