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 --- 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 @@ -62,3 +62,4 @@ js-rtc-y = rtc.o
38 obj-$(CONFIG_TILE_SROM) += tile-srom.o
39 obj-$(CONFIG_XILLYBUS) += xillybus/
40 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
42 +++ b/drivers/char/broadcom/Kconfig
45 +# Broadcom char driver config
48 +menuconfig BRCM_CHAR_DRIVERS
49 + bool "Broadcom Char Drivers"
51 + Broadcom's char drivers
54 + bool "Videocore CMA"
55 + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
58 + Helper for videocore CMA access.
60 +++ b/drivers/char/broadcom/Makefile
62 +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
64 +++ b/drivers/char/broadcom/vc_cma/Makefile
66 +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
68 +ccflags-y += -Iinclude/linux/broadcom
69 +ccflags-y += -Idrivers/misc/vc04_services
70 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
71 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
73 +ccflags-y += -D__KERNEL__
74 +ccflags-y += -D__linux__
77 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
79 +vc-cma-objs := vc_cma.o
81 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
84 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
86 + * Redistribution and use in source and binary forms, with or without
87 + * modification, are permitted provided that the following conditions
89 + * 1. Redistributions of source code must retain the above copyright
90 + * notice, this list of conditions, and the following disclaimer,
91 + * without modification.
92 + * 2. Redistributions in binary form must reproduce the above copyright
93 + * notice, this list of conditions and the following disclaimer in the
94 + * documentation and/or other materials provided with the distribution.
95 + * 3. The names of the above-listed copyright holders may not be used
96 + * to endorse or promote products derived from this software without
97 + * specific prior written permission.
99 + * ALTERNATIVELY, this software may be distributed under the terms of the
100 + * GNU General Public License ("GPL") version 2, as published by the Free
101 + * Software Foundation.
103 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
104 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
105 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
106 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
107 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
108 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
109 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
110 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
111 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
112 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
113 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116 +#include <linux/kernel.h>
117 +#include <linux/module.h>
118 +#include <linux/kthread.h>
119 +#include <linux/fs.h>
120 +#include <linux/device.h>
121 +#include <linux/cdev.h>
122 +#include <linux/mm.h>
123 +#include <linux/proc_fs.h>
124 +#include <linux/seq_file.h>
125 +#include <linux/dma-mapping.h>
126 +#include <linux/dma-contiguous.h>
127 +#include <linux/platform_device.h>
128 +#include <linux/uaccess.h>
129 +#include <asm/cacheflush.h>
133 +#include "vchiq_util.h"
134 +#include "vchiq_connected.h"
135 +//#include "debug_sym.h"
136 +//#include "vc_mem.h"
138 +#define DRIVER_NAME "vc-cma"
140 +#define LOG_DBG(fmt, ...) \
141 + if (vc_cma_debug) \
142 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
143 +#define LOG_ERR(fmt, ...) \
144 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
146 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
147 +#define VC_CMA_VERSION 2
149 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
150 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
151 +#define VC_CMA_MAX_PARAMS_PER_MSG \
152 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
153 +#define VC_CMA_RESERVE_COUNT_MAX 16
155 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
157 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
159 +#define loud_error(...) \
160 + LOG_ERR("===== " __VA_ARGS__)
166 + VC_CMA_MSG_ALLOC, /* chunk count */
167 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
168 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
169 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
170 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
171 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
172 + VC_CMA_MSG_UPDATE_RESERVE,
177 + unsigned short type;
178 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
181 +struct vc_cma_reserve_user {
183 + unsigned int reserve;
186 +/* Device (/dev) related variables */
187 +static dev_t vc_cma_devnum;
188 +static struct class *vc_cma_class;
189 +static struct cdev vc_cma_cdev;
190 +static int vc_cma_inited;
191 +static int vc_cma_debug;
194 +static struct proc_dir_entry *vc_cma_proc_entry;
196 +phys_addr_t vc_cma_base;
197 +struct page *vc_cma_base_page;
198 +unsigned int vc_cma_size;
199 +EXPORT_SYMBOL(vc_cma_size);
200 +unsigned int vc_cma_initial;
201 +unsigned int vc_cma_chunks;
202 +unsigned int vc_cma_chunks_used;
203 +unsigned int vc_cma_chunks_reserved;
205 +static int in_loud_error;
207 +unsigned int vc_cma_reserve_total;
208 +unsigned int vc_cma_reserve_count;
209 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
210 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
211 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
213 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
214 +static struct platform_device vc_cma_device = {
218 + .dma_mask = &vc_cma_dma_mask,
219 + .coherent_dma_mask = DMA_BIT_MASK(32),
223 +static VCHIQ_INSTANCE_T cma_instance;
224 +static VCHIQ_SERVICE_HANDLE_T cma_service;
225 +static VCHIU_QUEUE_T cma_msg_queue;
226 +static struct task_struct *cma_worker;
228 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
229 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
230 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
231 + VCHIQ_HEADER_T * header,
232 + VCHIQ_SERVICE_HANDLE_T service,
233 + void *bulk_userdata);
234 +static void send_vc_msg(unsigned short type,
235 + unsigned short param1, unsigned short param2);
236 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
238 +static int early_vc_cma_mem(char *p)
240 + unsigned int new_size;
241 + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
242 + vc_cma_size = memparse(p, &p);
243 + vc_cma_initial = vc_cma_size;
245 + vc_cma_size = memparse(p + 1, &p);
247 + vc_cma_base = memparse(p + 1, &p);
249 + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
250 + & ~(VC_CMA_CHUNK_SIZE - 1);
251 + if (new_size > vc_cma_size)
253 + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
254 + & ~(VC_CMA_CHUNK_SIZE - 1);
255 + if (vc_cma_initial > vc_cma_size)
256 + vc_cma_initial = vc_cma_size;
257 + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
258 + & ~(VC_CMA_CHUNK_SIZE - 1);
260 + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
261 + vc_cma_size, (unsigned int)vc_cma_base);
266 +early_param("vc-cma-mem", early_vc_cma_mem);
268 +void vc_cma_early_init(void)
270 + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
272 + int rc = platform_device_register(&vc_cma_device);
273 + LOG_DBG("platform_device_register -> %d", rc);
277 +void vc_cma_reserve(void)
279 + /* if vc_cma_size is set, then declare vc CMA area of the same
280 + * size from the end of memory
283 + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
284 + vc_cma_base, 0) == 0) {
286 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
287 + vc_cma_size, (unsigned int)vc_cma_base);
291 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
294 +/****************************************************************************
298 +***************************************************************************/
300 +static int vc_cma_open(struct inode *inode, struct file *file)
308 +/****************************************************************************
312 +***************************************************************************/
314 +static int vc_cma_release(struct inode *inode, struct file *file)
319 + vc_cma_set_reserve(0, current->tgid);
324 +/****************************************************************************
328 +***************************************************************************/
330 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
338 + case VC_CMA_IOC_RESERVE:
339 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
344 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
351 +/****************************************************************************
353 +* File Operations for the driver.
355 +***************************************************************************/
357 +static const struct file_operations vc_cma_fops = {
358 + .owner = THIS_MODULE,
359 + .open = vc_cma_open,
360 + .release = vc_cma_release,
361 + .unlocked_ioctl = vc_cma_ioctl,
364 +/****************************************************************************
368 +***************************************************************************/
370 +static int vc_cma_show_info(struct seq_file *m, void *v)
374 + seq_printf(m, "Videocore CMA:\n");
375 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
376 + seq_printf(m, " Length : %08x\n", vc_cma_size);
377 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
378 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
379 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
380 + (int)vc_cma_chunks,
381 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
382 + seq_printf(m, " Used : %4d (%d bytes)\n",
383 + (int)vc_cma_chunks_used,
384 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
385 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
386 + (unsigned int)vc_cma_chunks_reserved,
387 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
389 + for (i = 0; i < vc_cma_reserve_count; i++) {
390 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
391 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
395 + seq_printf(m, "\n");
400 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
402 + return single_open(file, vc_cma_show_info, NULL);
405 +/****************************************************************************
409 +***************************************************************************/
411 +static int vc_cma_proc_write(struct file *file,
412 + const char __user *buffer,
413 + size_t size, loff_t *ppos)
416 + char input_str[20];
418 + memset(input_str, 0, sizeof(input_str));
420 + if (size > sizeof(input_str)) {
421 + LOG_ERR("%s: input string length too long", __func__);
425 + if (copy_from_user(input_str, buffer, size - 1)) {
426 + LOG_ERR("%s: failed to get input string", __func__);
429 +#define ALLOC_STR "alloc"
430 +#define FREE_STR "free"
431 +#define DEBUG_STR "debug"
432 +#define RESERVE_STR "reserve"
433 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
435 + char *p = input_str + strlen(ALLOC_STR);
439 + size = memparse(p, NULL);
440 + LOG_ERR("/proc/vc-cma: alloc %d", size);
442 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
443 + size / VC_CMA_CHUNK_SIZE, 0);
445 + LOG_ERR("invalid size '%s'", p);
447 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
449 + char *p = input_str + strlen(FREE_STR);
453 + size = memparse(p, NULL);
454 + LOG_ERR("/proc/vc-cma: free %d", size);
456 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
457 + size / VC_CMA_CHUNK_SIZE, 0);
459 + LOG_ERR("invalid size '%s'", p);
461 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
462 + char *p = input_str + strlen(DEBUG_STR);
465 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
467 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
469 + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
471 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
474 + char *p = input_str + strlen(RESERVE_STR);
477 + size = memparse(p, NULL);
479 + reserved = vc_cma_set_reserve(size, current->tgid);
480 + rc = (reserved >= 0) ? size : reserved;
487 +/****************************************************************************
489 +* File Operations for /proc interface.
491 +***************************************************************************/
493 +static const struct file_operations vc_cma_proc_fops = {
494 + .open = vc_cma_proc_open,
496 + .write = vc_cma_proc_write,
497 + .llseek = seq_lseek,
498 + .release = single_release
501 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
503 + struct vc_cma_reserve_user *user = NULL;
507 + if (down_interruptible(&vc_cma_reserve_mutex))
508 + return -ERESTARTSYS;
510 + for (i = 0; i < vc_cma_reserve_count; i++) {
511 + if (pid == vc_cma_reserve_users[i].pid) {
512 + user = &vc_cma_reserve_users[i];
513 + delta = reserve - user->reserve;
515 + user->reserve = reserve;
517 + /* Remove this entry by copying downwards */
518 + while ((i + 1) < vc_cma_reserve_count) {
519 + user[0].pid = user[1].pid;
520 + user[0].reserve = user[1].reserve;
524 + vc_cma_reserve_count--;
531 + if (reserve && !user) {
532 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
533 + LOG_ERR("vc-cma: Too many reservations - "
534 + "increase CMA_RESERVE_COUNT_MAX");
535 + up(&vc_cma_reserve_mutex);
538 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
540 + user->reserve = reserve;
542 + vc_cma_reserve_count++;
545 + vc_cma_reserve_total += delta;
547 + send_vc_msg(VC_CMA_MSG_RESERVE,
548 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
550 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
552 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
553 + reserve, pid, vc_cma_reserve_total);
555 + up(&vc_cma_reserve_mutex);
557 + return vc_cma_reserve_total;
560 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
561 + VCHIQ_HEADER_T * header,
562 + VCHIQ_SERVICE_HANDLE_T service,
563 + void *bulk_userdata)
566 + case VCHIQ_MESSAGE_AVAILABLE:
567 + if (!send_worker_msg(header))
568 + return VCHIQ_RETRY;
570 + case VCHIQ_SERVICE_CLOSED:
571 + LOG_DBG("CMA service closed");
574 + LOG_ERR("Unexpected CMA callback reason %d", reason);
577 + return VCHIQ_SUCCESS;
580 +static void send_vc_msg(unsigned short type,
581 + unsigned short param1, unsigned short param2)
583 + unsigned short msg[] = { type, param1, param2 };
584 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
585 + VCHIQ_STATUS_T ret;
586 + vchiq_use_service(cma_service);
587 + ret = vchiq_queue_message(cma_service, &elem, 1);
588 + vchiq_release_service(cma_service);
589 + if (ret != VCHIQ_SUCCESS)
590 + LOG_ERR("vchiq_queue_message returned %x", ret);
593 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
595 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
597 + vchiu_queue_push(&cma_msg_queue, msg);
598 + up(&vc_cma_worker_queue_push_mutex);
602 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
605 + for (i = 0; i < num_chunks; i++) {
606 + struct page *chunk;
607 + unsigned int chunk_num;
608 + uint8_t *chunk_addr;
609 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
611 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
613 + VC_CMA_CHUNK_ORDER);
617 + chunk_addr = page_address(chunk);
618 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
619 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
623 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
624 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
625 + VC_CMA_CHUNK_SIZE) != 0);
626 + if (chunk_num >= vc_cma_chunks) {
627 + LOG_ERR("%s: ===============================",
629 + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
630 + "bad SPARSEMEM configuration?",
631 + __func__, (unsigned int)page_to_phys(chunk),
632 + vc_cma_base, vc_cma_base + vc_cma_size - 1);
633 + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
634 + (void*)0/*vc_cma_device.dev.cma_area*/);
635 + LOG_ERR("%s: ===============================",
639 + reply->params[i] = chunk_num;
640 + vc_cma_chunks_used++;
643 + if (i < num_chunks) {
644 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
645 + "for %x bytes (alloc %d of %d, %d free)",
646 + __func__, VC_CMA_CHUNK_SIZE, i,
647 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
651 + LOG_DBG("CMA allocated %d chunks -> %d used",
652 + num_chunks, vc_cma_chunks_used);
653 + reply->type = VC_CMA_MSG_ALLOCATED;
656 + VCHIQ_ELEMENT_T elem = {
658 + offsetof(struct cma_msg, params[0]) +
659 + num_chunks * sizeof(reply->params[0])
661 + VCHIQ_STATUS_T ret;
662 + vchiq_use_service(cma_service);
663 + ret = vchiq_queue_message(cma_service, &elem, 1);
664 + vchiq_release_service(cma_service);
665 + if (ret != VCHIQ_SUCCESS)
666 + LOG_ERR("vchiq_queue_message return " "%x", ret);
672 +static int cma_worker_proc(void *param)
674 + static struct cma_msg reply;
678 + VCHIQ_HEADER_T *msg;
679 + static struct cma_msg msg_copy;
680 + struct cma_msg *cma_msg = &msg_copy;
681 + int type, msg_size;
683 + msg = vchiu_queue_pop(&cma_msg_queue);
684 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
685 + msg_size = msg->size;
686 + memcpy(&msg_copy, msg->data, msg_size);
687 + type = cma_msg->type;
688 + vchiq_release_message(cma_service, msg);
692 + if (type == VC_CMA_MSG_QUIT)
694 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
704 + case VC_CMA_MSG_ALLOC:{
705 + int num_chunks, free_chunks;
706 + num_chunks = cma_msg->params[0];
708 + vc_cma_chunks - vc_cma_chunks_used;
709 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
710 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
712 + ("CMA_MSG_ALLOC - chunk count (%d) "
713 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
715 + VC_CMA_MAX_PARAMS_PER_MSG);
716 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
719 + if (num_chunks > free_chunks) {
721 + ("CMA_MSG_ALLOC - chunk count (%d) "
722 + "exceeds free chunks (%d)",
723 + num_chunks, free_chunks);
724 + num_chunks = free_chunks;
727 + vc_cma_alloc_chunks(num_chunks, &reply);
731 + case VC_CMA_MSG_FREE:{
734 + offsetof(struct cma_msg,
736 + sizeof(cma_msg->params[0]);
738 + BUG_ON(chunk_count <= 0);
740 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
741 + chunk_count, cma_msg->params[0]);
742 + for (i = 0; i < chunk_count; i++) {
743 + int chunk_num = cma_msg->params[i];
744 + struct page *page = vc_cma_base_page +
745 + chunk_num * PAGES_PER_CHUNK;
746 + if (chunk_num >= vc_cma_chunks) {
748 + ("CMA_MSG_FREE - chunk %d of %d"
749 + " (value %x) exceeds maximum "
750 + "(%x)", i, chunk_count,
752 + vc_cma_chunks - 1);
756 + if (!dma_release_from_contiguous
757 + (NULL /*&vc_cma_device.dev*/, page,
758 + PAGES_PER_CHUNK)) {
760 + ("CMA_MSG_FREE - failed to "
761 + "release chunk %d (phys %x, "
762 + "page %x)", chunk_num,
763 + page_to_phys(page),
764 + (unsigned int)page);
766 + vc_cma_chunks_used--;
768 + LOG_DBG("CMA released %d chunks -> %d used",
769 + i, vc_cma_chunks_used);
773 + case VC_CMA_MSG_UPDATE_RESERVE:{
774 + int chunks_needed =
775 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
777 + / VC_CMA_CHUNK_SIZE) -
778 + vc_cma_chunks_reserved;
781 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
784 + /* Cap the reservations to what is available */
785 + if (chunks_needed > 0) {
786 + if (chunks_needed >
788 + vc_cma_chunks_used))
791 + vc_cma_chunks_used);
794 + vc_cma_alloc_chunks(chunks_needed,
799 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
801 + vc_cma_chunks_reserved += chunks_needed;
806 + LOG_ERR("unexpected msg type %d", type);
811 + LOG_DBG("quitting...");
815 +/****************************************************************************
817 +* vc_cma_connected_init
819 +* This function is called once the videocore has been connected.
821 +***************************************************************************/
823 +static void vc_cma_connected_init(void)
825 + VCHIQ_SERVICE_PARAMS_T service_params;
827 + LOG_DBG("vc_cma_connected_init");
829 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
830 + LOG_ERR("could not create CMA msg queue");
834 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
835 + goto fail_vchiq_init;
837 + vchiq_connect(cma_instance);
839 + service_params.fourcc = VC_CMA_FOURCC;
840 + service_params.callback = cma_service_callback;
841 + service_params.userdata = NULL;
842 + service_params.version = VC_CMA_VERSION;
843 + service_params.version_min = VC_CMA_VERSION;
845 + if (vchiq_open_service(cma_instance, &service_params,
846 + &cma_service) != VCHIQ_SUCCESS) {
847 + LOG_ERR("failed to open service - already in use?");
848 + goto fail_vchiq_open;
851 + vchiq_release_service(cma_service);
853 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
855 + LOG_ERR("could not create CMA worker thread");
858 + set_user_nice(cma_worker, -20);
859 + wake_up_process(cma_worker);
864 + vchiq_close_service(cma_service);
866 + vchiq_shutdown(cma_instance);
868 + vchiu_queue_delete(&cma_msg_queue);
874 +loud_error_header(void)
879 + LOG_ERR("============================================================"
880 + "================");
881 + LOG_ERR("============================================================"
882 + "================");
889 +loud_error_footer(void)
891 + if (!in_loud_error)
895 + LOG_ERR("============================================================"
896 + "================");
897 + LOG_ERR("============================================================"
898 + "================");
904 +static int check_cma_config(void) { return 1; }
907 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
908 + const char *symbol,
909 + void *buf, size_t bufsize)
911 + VC_MEM_ADDR_T vcMemAddr;
916 + if (!LookupVideoCoreSymbol(handle, symbol,
919 + loud_error_header();
921 + "failed to find VC symbol \"%s\".",
923 + loud_error_footer();
927 + if (vcMemSize != bufsize) {
928 + loud_error_header();
930 + "VC symbol \"%s\" is the wrong size.",
932 + loud_error_footer();
936 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
937 + vcMapAddr += mm_vc_mem_phys_addr;
938 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
939 + if (mapAddr == 0) {
940 + loud_error_header();
942 + "failed to ioremap \"%s\" @ 0x%x "
943 + "(phys: 0x%x, size: %u).",
945 + (unsigned int)vcMapAddr,
946 + (unsigned int)vcMemAddr,
947 + (unsigned int)vcMemSize);
948 + loud_error_footer();
952 + memcpy(buf, mapAddr, bufsize);
960 +check_cma_config(void)
962 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
963 + VC_MEM_ADDR_T mempool_start;
964 + VC_MEM_ADDR_T mempool_end;
965 + VC_MEM_ADDR_T mempool_offline_start;
966 + VC_MEM_ADDR_T mempool_offline_end;
967 + VC_MEM_ADDR_T cam_alloc_base;
968 + VC_MEM_ADDR_T cam_alloc_size;
969 + VC_MEM_ADDR_T cam_alloc_end;
972 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
975 + /* Read the relevant VideoCore variables */
976 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
978 + sizeof(mempool_start)))
981 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
983 + sizeof(mempool_end)))
986 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
987 + &mempool_offline_start,
988 + sizeof(mempool_offline_start)))
991 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
992 + &mempool_offline_end,
993 + sizeof(mempool_offline_end)))
996 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
998 + sizeof(cam_alloc_base)))
1001 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1003 + sizeof(cam_alloc_size)))
1006 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1010 + /* Now the sanity checks */
1011 + if (!mempool_offline_start)
1012 + mempool_offline_start = mempool_start;
1013 + if (!mempool_offline_end)
1014 + mempool_offline_end = mempool_end;
1016 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1017 + loud_error_header();
1019 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1020 + "vc_cma_base(%x)",
1021 + mempool_offline_start,
1022 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1027 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1028 + (vc_cma_base + vc_cma_size)) {
1029 + loud_error_header();
1031 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1032 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1033 + mempool_offline_start,
1034 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1035 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1039 + if (mempool_end < mempool_start) {
1040 + loud_error_header();
1042 + "__MEMPOOL_END(%x) must not be before "
1043 + "__MEMPOOL_START(%x)",
1049 + if (mempool_offline_end < mempool_offline_start) {
1050 + loud_error_header();
1052 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1053 + "__MEMPOOL_OFFLINE_START(%x)",
1054 + mempool_offline_end,
1055 + mempool_offline_start);
1059 + if (mempool_offline_start < mempool_start) {
1060 + loud_error_header();
1062 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1063 + "__MEMPOOL_START(%x)",
1064 + mempool_offline_start,
1069 + if (mempool_offline_end > mempool_end) {
1070 + loud_error_header();
1072 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1073 + "__MEMPOOL_END(%x)",
1074 + mempool_offline_end,
1079 + if ((cam_alloc_base < mempool_end) &&
1080 + (cam_alloc_end > mempool_start)) {
1081 + loud_error_header();
1083 + "cam_alloc pool(%x-%x) overlaps "
1085 + cam_alloc_base, cam_alloc_end,
1086 + mempool_start, mempool_end);
1090 + loud_error_footer();
1093 + CloseVideoCoreMemory(mem_hndl);
1100 +static int vc_cma_init(void)
1103 + struct device *dev;
1105 + if (!check_cma_config())
1108 + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
1109 + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
1110 + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
1111 + vc_cma_size, vc_cma_size / (1024 * 1024));
1112 + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
1113 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1115 + vc_cma_base_page = phys_to_page(vc_cma_base);
1117 + if (vc_cma_chunks) {
1118 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1120 + for (vc_cma_chunks_used = 0;
1121 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1122 + struct page *chunk;
1123 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
1125 + VC_CMA_CHUNK_ORDER);
1128 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1129 + VC_CMA_CHUNK_SIZE) != 0);
1131 + if (vc_cma_chunks_used != chunks_needed) {
1132 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1133 + "bytes, allocation %d of %d)",
1134 + __func__, VC_CMA_CHUNK_SIZE,
1135 + vc_cma_chunks_used, chunks_needed);
1139 + vchiq_add_connected_callback(vc_cma_connected_init);
1142 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1144 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1148 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1149 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1151 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1152 + goto out_unregister;
1155 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1156 + if (IS_ERR(vc_cma_class)) {
1157 + rc = PTR_ERR(vc_cma_class);
1158 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1159 + goto out_cdev_del;
1162 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1164 + if (IS_ERR(dev)) {
1165 + rc = PTR_ERR(dev);
1166 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1167 + goto out_class_destroy;
1170 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1171 + if (vc_cma_proc_entry == NULL) {
1173 + LOG_ERR("%s: proc_create failed", __func__);
1174 + goto out_device_destroy;
1177 + vc_cma_inited = 1;
1180 +out_device_destroy:
1181 + device_destroy(vc_cma_class, vc_cma_devnum);
1184 + class_destroy(vc_cma_class);
1185 + vc_cma_class = NULL;
1188 + cdev_del(&vc_cma_cdev);
1191 + unregister_chrdev_region(vc_cma_devnum, 1);
1194 + /* It is tempting to try to clean up by calling
1195 + dma_release_from_contiguous for all allocated chunks, but it isn't
1196 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1197 + VideoCore is already using that memory, so giving it back to Linux
1198 + is likely to be fatal.
1203 +/****************************************************************************
1207 +***************************************************************************/
1209 +static void __exit vc_cma_exit(void)
1211 + LOG_DBG("%s: called", __func__);
1213 + if (vc_cma_inited) {
1214 + remove_proc_entry(DRIVER_NAME, NULL);
1215 + device_destroy(vc_cma_class, vc_cma_devnum);
1216 + class_destroy(vc_cma_class);
1217 + cdev_del(&vc_cma_cdev);
1218 + unregister_chrdev_region(vc_cma_devnum, 1);
1222 +module_init(vc_cma_init);
1223 +module_exit(vc_cma_exit);
1224 +MODULE_LICENSE("GPL");
1225 +MODULE_AUTHOR("Broadcom Corporation");
1226 --- a/drivers/misc/Makefile
1227 +++ b/drivers/misc/Makefile
1228 @@ -51,7 +51,7 @@ obj-$(CONFIG_INTEL_MEI) += mei/
1229 obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
1230 obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
1231 obj-$(CONFIG_SRAM) += sram.o
1232 -obj-y += vc04_services/
1233 +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
1235 obj-$(CONFIG_GENWQE) += genwqe/
1236 obj-$(CONFIG_ECHO) += echo/
1238 +++ b/include/linux/broadcom/vc_cma.h
1240 +/*****************************************************************************
1241 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1243 +* Unless you and Broadcom execute a separate written software license
1244 +* agreement governing use of this software, this software is licensed to you
1245 +* under the terms of the GNU General Public License version 2, available at
1246 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1248 +* Notwithstanding the above, under no circumstances may you combine this
1249 +* software in any way with any other Broadcom software provided under a
1250 +* license other than the GPL, without Broadcom's express prior written
1252 +*****************************************************************************/
1254 +#if !defined( VC_CMA_H )
1257 +#include <linux/ioctl.h>
1259 +#define VC_CMA_IOC_MAGIC 0xc5
1261 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1264 +extern void __init vc_cma_early_init(void);
1265 +extern void __init vc_cma_reserve(void);
1268 +#endif /* VC_CMA_H */