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 diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
24 index 1386749..fcf51a3 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 a324f93..aef3ed0 100644
38 --- a/drivers/char/Makefile
39 +++ b/drivers/char/Makefile
40 @@ -61,3 +61,5 @@ obj-$(CONFIG_JS_RTC) += js-rtc.o
43 obj-$(CONFIG_TILE_SROM) += tile-srom.o
45 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
46 diff --git a/drivers/char/broadcom/Kconfig b/drivers/char/broadcom/Kconfig
48 index 0000000..f089943
50 +++ b/drivers/char/broadcom/Kconfig
53 +# Broadcom char driver config
56 +menuconfig BRCM_CHAR_DRIVERS
57 + bool "Broadcom Char Drivers"
59 + Broadcom's char drivers
62 + bool "Videocore CMA"
63 + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
66 + Helper for videocore CMA access.
68 diff --git a/drivers/char/broadcom/Makefile b/drivers/char/broadcom/Makefile
70 index 0000000..13c5bca
72 +++ b/drivers/char/broadcom/Makefile
74 +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
75 diff --git a/drivers/char/broadcom/vc_cma/Makefile b/drivers/char/broadcom/vc_cma/Makefile
77 index 0000000..6a94de4
79 +++ b/drivers/char/broadcom/vc_cma/Makefile
81 +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
83 +ccflags-y += -Iinclude/linux/broadcom
84 +ccflags-y += -Idrivers/misc/vc04_services
85 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
86 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
88 +ccflags-y += -D__KERNEL__
89 +ccflags-y += -D__linux__
92 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
94 +vc-cma-objs := vc_cma.o
95 diff --git a/drivers/char/broadcom/vc_cma/vc_cma.c b/drivers/char/broadcom/vc_cma/vc_cma.c
97 index 0000000..a635f9f
99 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
102 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
104 + * Redistribution and use in source and binary forms, with or without
105 + * modification, are permitted provided that the following conditions
107 + * 1. Redistributions of source code must retain the above copyright
108 + * notice, this list of conditions, and the following disclaimer,
109 + * without modification.
110 + * 2. Redistributions in binary form must reproduce the above copyright
111 + * notice, this list of conditions and the following disclaimer in the
112 + * documentation and/or other materials provided with the distribution.
113 + * 3. The names of the above-listed copyright holders may not be used
114 + * to endorse or promote products derived from this software without
115 + * specific prior written permission.
117 + * ALTERNATIVELY, this software may be distributed under the terms of the
118 + * GNU General Public License ("GPL") version 2, as published by the Free
119 + * Software Foundation.
121 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
122 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
123 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
124 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
125 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
126 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
127 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
128 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
129 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
130 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
131 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
134 +#include <linux/kernel.h>
135 +#include <linux/module.h>
136 +#include <linux/kthread.h>
137 +#include <linux/fs.h>
138 +#include <linux/device.h>
139 +#include <linux/cdev.h>
140 +#include <linux/mm.h>
141 +#include <linux/proc_fs.h>
142 +#include <linux/seq_file.h>
143 +#include <linux/dma-mapping.h>
144 +#include <linux/dma-contiguous.h>
145 +#include <linux/platform_device.h>
146 +#include <linux/uaccess.h>
147 +#include <asm/cacheflush.h>
151 +#include "vchiq_util.h"
152 +#include "vchiq_connected.h"
153 +//#include "debug_sym.h"
154 +//#include "vc_mem.h"
156 +#define DRIVER_NAME "vc-cma"
158 +#define LOG_DBG(fmt, ...) \
159 + if (vc_cma_debug) \
160 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
161 +#define LOG_ERR(fmt, ...) \
162 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
164 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
165 +#define VC_CMA_VERSION 2
167 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
168 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
169 +#define VC_CMA_MAX_PARAMS_PER_MSG \
170 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
171 +#define VC_CMA_RESERVE_COUNT_MAX 16
173 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
175 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
177 +#define loud_error(...) \
178 + LOG_ERR("===== " __VA_ARGS__)
184 + VC_CMA_MSG_ALLOC, /* chunk count */
185 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
186 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
187 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
188 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
189 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
190 + VC_CMA_MSG_UPDATE_RESERVE,
195 + unsigned short type;
196 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
199 +struct vc_cma_reserve_user {
201 + unsigned int reserve;
204 +/* Device (/dev) related variables */
205 +static dev_t vc_cma_devnum;
206 +static struct class *vc_cma_class;
207 +static struct cdev vc_cma_cdev;
208 +static int vc_cma_inited;
209 +static int vc_cma_debug;
212 +static struct proc_dir_entry *vc_cma_proc_entry;
214 +phys_addr_t vc_cma_base;
215 +struct page *vc_cma_base_page;
216 +unsigned int vc_cma_size;
217 +EXPORT_SYMBOL(vc_cma_size);
218 +unsigned int vc_cma_initial;
219 +unsigned int vc_cma_chunks;
220 +unsigned int vc_cma_chunks_used;
221 +unsigned int vc_cma_chunks_reserved;
223 +static int in_loud_error;
225 +unsigned int vc_cma_reserve_total;
226 +unsigned int vc_cma_reserve_count;
227 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
228 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
229 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
231 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
232 +static struct platform_device vc_cma_device = {
236 + .dma_mask = &vc_cma_dma_mask,
237 + .coherent_dma_mask = DMA_BIT_MASK(32),
241 +static VCHIQ_INSTANCE_T cma_instance;
242 +static VCHIQ_SERVICE_HANDLE_T cma_service;
243 +static VCHIU_QUEUE_T cma_msg_queue;
244 +static struct task_struct *cma_worker;
246 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
247 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
248 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
249 + VCHIQ_HEADER_T * header,
250 + VCHIQ_SERVICE_HANDLE_T service,
251 + void *bulk_userdata);
252 +static void send_vc_msg(unsigned short type,
253 + unsigned short param1, unsigned short param2);
254 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
256 +static int early_vc_cma_mem(char *p)
258 + unsigned int new_size;
259 + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
260 + vc_cma_size = memparse(p, &p);
261 + vc_cma_initial = vc_cma_size;
263 + vc_cma_size = memparse(p + 1, &p);
265 + vc_cma_base = memparse(p + 1, &p);
267 + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
268 + & ~(VC_CMA_CHUNK_SIZE - 1);
269 + if (new_size > vc_cma_size)
271 + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
272 + & ~(VC_CMA_CHUNK_SIZE - 1);
273 + if (vc_cma_initial > vc_cma_size)
274 + vc_cma_initial = vc_cma_size;
275 + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
276 + & ~(VC_CMA_CHUNK_SIZE - 1);
278 + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
279 + vc_cma_size, (unsigned int)vc_cma_base);
284 +early_param("vc-cma-mem", early_vc_cma_mem);
286 +void vc_cma_early_init(void)
288 + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
290 + int rc = platform_device_register(&vc_cma_device);
291 + LOG_DBG("platform_device_register -> %d", rc);
295 +void vc_cma_reserve(void)
297 + /* if vc_cma_size is set, then declare vc CMA area of the same
298 + * size from the end of memory
301 + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
302 + vc_cma_base, 0) == 0) {
304 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
305 + vc_cma_size, (unsigned int)vc_cma_base);
309 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
312 +/****************************************************************************
316 +***************************************************************************/
318 +static int vc_cma_open(struct inode *inode, struct file *file)
326 +/****************************************************************************
330 +***************************************************************************/
332 +static int vc_cma_release(struct inode *inode, struct file *file)
337 + vc_cma_set_reserve(0, current->tgid);
342 +/****************************************************************************
346 +***************************************************************************/
348 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
356 + case VC_CMA_IOC_RESERVE:
357 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
362 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
369 +/****************************************************************************
371 +* File Operations for the driver.
373 +***************************************************************************/
375 +static const struct file_operations vc_cma_fops = {
376 + .owner = THIS_MODULE,
377 + .open = vc_cma_open,
378 + .release = vc_cma_release,
379 + .unlocked_ioctl = vc_cma_ioctl,
382 +/****************************************************************************
386 +***************************************************************************/
388 +static int vc_cma_show_info(struct seq_file *m, void *v)
392 + seq_printf(m, "Videocore CMA:\n");
393 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
394 + seq_printf(m, " Length : %08x\n", vc_cma_size);
395 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
396 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
397 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
398 + (int)vc_cma_chunks,
399 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
400 + seq_printf(m, " Used : %4d (%d bytes)\n",
401 + (int)vc_cma_chunks_used,
402 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
403 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
404 + (unsigned int)vc_cma_chunks_reserved,
405 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
407 + for (i = 0; i < vc_cma_reserve_count; i++) {
408 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
409 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
413 + seq_printf(m, "\n");
418 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
420 + return single_open(file, vc_cma_show_info, NULL);
423 +/****************************************************************************
427 +***************************************************************************/
429 +static int vc_cma_proc_write(struct file *file,
430 + const char __user *buffer,
431 + size_t size, loff_t *ppos)
434 + char input_str[20];
436 + memset(input_str, 0, sizeof(input_str));
438 + if (size > sizeof(input_str)) {
439 + LOG_ERR("%s: input string length too long", __func__);
443 + if (copy_from_user(input_str, buffer, size - 1)) {
444 + LOG_ERR("%s: failed to get input string", __func__);
447 +#define ALLOC_STR "alloc"
448 +#define FREE_STR "free"
449 +#define DEBUG_STR "debug"
450 +#define RESERVE_STR "reserve"
451 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
453 + char *p = input_str + strlen(ALLOC_STR);
457 + size = memparse(p, NULL);
458 + LOG_ERR("/proc/vc-cma: alloc %d", size);
460 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
461 + size / VC_CMA_CHUNK_SIZE, 0);
463 + LOG_ERR("invalid size '%s'", p);
465 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
467 + char *p = input_str + strlen(FREE_STR);
471 + size = memparse(p, NULL);
472 + LOG_ERR("/proc/vc-cma: free %d", size);
474 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
475 + size / VC_CMA_CHUNK_SIZE, 0);
477 + LOG_ERR("invalid size '%s'", p);
479 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
480 + char *p = input_str + strlen(DEBUG_STR);
483 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
485 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
487 + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
489 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
492 + char *p = input_str + strlen(RESERVE_STR);
495 + size = memparse(p, NULL);
497 + reserved = vc_cma_set_reserve(size, current->tgid);
498 + rc = (reserved >= 0) ? size : reserved;
505 +/****************************************************************************
507 +* File Operations for /proc interface.
509 +***************************************************************************/
511 +static const struct file_operations vc_cma_proc_fops = {
512 + .open = vc_cma_proc_open,
514 + .write = vc_cma_proc_write,
515 + .llseek = seq_lseek,
516 + .release = single_release
519 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
521 + struct vc_cma_reserve_user *user = NULL;
525 + if (down_interruptible(&vc_cma_reserve_mutex))
526 + return -ERESTARTSYS;
528 + for (i = 0; i < vc_cma_reserve_count; i++) {
529 + if (pid == vc_cma_reserve_users[i].pid) {
530 + user = &vc_cma_reserve_users[i];
531 + delta = reserve - user->reserve;
533 + user->reserve = reserve;
535 + /* Remove this entry by copying downwards */
536 + while ((i + 1) < vc_cma_reserve_count) {
537 + user[0].pid = user[1].pid;
538 + user[0].reserve = user[1].reserve;
542 + vc_cma_reserve_count--;
549 + if (reserve && !user) {
550 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
551 + LOG_ERR("vc-cma: Too many reservations - "
552 + "increase CMA_RESERVE_COUNT_MAX");
553 + up(&vc_cma_reserve_mutex);
556 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
558 + user->reserve = reserve;
560 + vc_cma_reserve_count++;
563 + vc_cma_reserve_total += delta;
565 + send_vc_msg(VC_CMA_MSG_RESERVE,
566 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
568 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
570 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
571 + reserve, pid, vc_cma_reserve_total);
573 + up(&vc_cma_reserve_mutex);
575 + return vc_cma_reserve_total;
578 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
579 + VCHIQ_HEADER_T * header,
580 + VCHIQ_SERVICE_HANDLE_T service,
581 + void *bulk_userdata)
584 + case VCHIQ_MESSAGE_AVAILABLE:
585 + if (!send_worker_msg(header))
586 + return VCHIQ_RETRY;
588 + case VCHIQ_SERVICE_CLOSED:
589 + LOG_DBG("CMA service closed");
592 + LOG_ERR("Unexpected CMA callback reason %d", reason);
595 + return VCHIQ_SUCCESS;
598 +static void send_vc_msg(unsigned short type,
599 + unsigned short param1, unsigned short param2)
601 + unsigned short msg[] = { type, param1, param2 };
602 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
603 + VCHIQ_STATUS_T ret;
604 + vchiq_use_service(cma_service);
605 + ret = vchiq_queue_message(cma_service, &elem, 1);
606 + vchiq_release_service(cma_service);
607 + if (ret != VCHIQ_SUCCESS)
608 + LOG_ERR("vchiq_queue_message returned %x", ret);
611 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
613 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
615 + vchiu_queue_push(&cma_msg_queue, msg);
616 + up(&vc_cma_worker_queue_push_mutex);
620 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
623 + for (i = 0; i < num_chunks; i++) {
624 + struct page *chunk;
625 + unsigned int chunk_num;
626 + uint8_t *chunk_addr;
627 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
629 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
631 + VC_CMA_CHUNK_ORDER);
635 + chunk_addr = page_address(chunk);
636 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
637 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
641 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
642 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
643 + VC_CMA_CHUNK_SIZE) != 0);
644 + if (chunk_num >= vc_cma_chunks) {
645 + LOG_ERR("%s: ===============================",
647 + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
648 + "bad SPARSEMEM configuration?",
649 + __func__, (unsigned int)page_to_phys(chunk),
650 + vc_cma_base, vc_cma_base + vc_cma_size - 1);
651 + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
652 + (void*)0/*vc_cma_device.dev.cma_area*/);
653 + LOG_ERR("%s: ===============================",
657 + reply->params[i] = chunk_num;
658 + vc_cma_chunks_used++;
661 + if (i < num_chunks) {
662 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
663 + "for %x bytes (alloc %d of %d, %d free)",
664 + __func__, VC_CMA_CHUNK_SIZE, i,
665 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
669 + LOG_DBG("CMA allocated %d chunks -> %d used",
670 + num_chunks, vc_cma_chunks_used);
671 + reply->type = VC_CMA_MSG_ALLOCATED;
674 + VCHIQ_ELEMENT_T elem = {
676 + offsetof(struct cma_msg, params[0]) +
677 + num_chunks * sizeof(reply->params[0])
679 + VCHIQ_STATUS_T ret;
680 + vchiq_use_service(cma_service);
681 + ret = vchiq_queue_message(cma_service, &elem, 1);
682 + vchiq_release_service(cma_service);
683 + if (ret != VCHIQ_SUCCESS)
684 + LOG_ERR("vchiq_queue_message return " "%x", ret);
690 +static int cma_worker_proc(void *param)
692 + static struct cma_msg reply;
696 + VCHIQ_HEADER_T *msg;
697 + static struct cma_msg msg_copy;
698 + struct cma_msg *cma_msg = &msg_copy;
699 + int type, msg_size;
701 + msg = vchiu_queue_pop(&cma_msg_queue);
702 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
703 + msg_size = msg->size;
704 + memcpy(&msg_copy, msg->data, msg_size);
705 + type = cma_msg->type;
706 + vchiq_release_message(cma_service, msg);
710 + if (type == VC_CMA_MSG_QUIT)
712 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
722 + case VC_CMA_MSG_ALLOC:{
723 + int num_chunks, free_chunks;
724 + num_chunks = cma_msg->params[0];
726 + vc_cma_chunks - vc_cma_chunks_used;
727 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
728 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
730 + ("CMA_MSG_ALLOC - chunk count (%d) "
731 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
733 + VC_CMA_MAX_PARAMS_PER_MSG);
734 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
737 + if (num_chunks > free_chunks) {
739 + ("CMA_MSG_ALLOC - chunk count (%d) "
740 + "exceeds free chunks (%d)",
741 + num_chunks, free_chunks);
742 + num_chunks = free_chunks;
745 + vc_cma_alloc_chunks(num_chunks, &reply);
749 + case VC_CMA_MSG_FREE:{
752 + offsetof(struct cma_msg,
754 + sizeof(cma_msg->params[0]);
756 + BUG_ON(chunk_count <= 0);
758 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
759 + chunk_count, cma_msg->params[0]);
760 + for (i = 0; i < chunk_count; i++) {
761 + int chunk_num = cma_msg->params[i];
762 + struct page *page = vc_cma_base_page +
763 + chunk_num * PAGES_PER_CHUNK;
764 + if (chunk_num >= vc_cma_chunks) {
766 + ("CMA_MSG_FREE - chunk %d of %d"
767 + " (value %x) exceeds maximum "
768 + "(%x)", i, chunk_count,
770 + vc_cma_chunks - 1);
774 + if (!dma_release_from_contiguous
775 + (NULL /*&vc_cma_device.dev*/, page,
776 + PAGES_PER_CHUNK)) {
778 + ("CMA_MSG_FREE - failed to "
779 + "release chunk %d (phys %x, "
780 + "page %x)", chunk_num,
781 + page_to_phys(page),
782 + (unsigned int)page);
784 + vc_cma_chunks_used--;
786 + LOG_DBG("CMA released %d chunks -> %d used",
787 + i, vc_cma_chunks_used);
791 + case VC_CMA_MSG_UPDATE_RESERVE:{
792 + int chunks_needed =
793 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
795 + / VC_CMA_CHUNK_SIZE) -
796 + vc_cma_chunks_reserved;
799 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
802 + /* Cap the reservations to what is available */
803 + if (chunks_needed > 0) {
804 + if (chunks_needed >
806 + vc_cma_chunks_used))
809 + vc_cma_chunks_used);
812 + vc_cma_alloc_chunks(chunks_needed,
817 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
819 + vc_cma_chunks_reserved += chunks_needed;
824 + LOG_ERR("unexpected msg type %d", type);
829 + LOG_DBG("quitting...");
833 +/****************************************************************************
835 +* vc_cma_connected_init
837 +* This function is called once the videocore has been connected.
839 +***************************************************************************/
841 +static void vc_cma_connected_init(void)
843 + VCHIQ_SERVICE_PARAMS_T service_params;
845 + LOG_DBG("vc_cma_connected_init");
847 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
848 + LOG_ERR("could not create CMA msg queue");
852 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
853 + goto fail_vchiq_init;
855 + vchiq_connect(cma_instance);
857 + service_params.fourcc = VC_CMA_FOURCC;
858 + service_params.callback = cma_service_callback;
859 + service_params.userdata = NULL;
860 + service_params.version = VC_CMA_VERSION;
861 + service_params.version_min = VC_CMA_VERSION;
863 + if (vchiq_open_service(cma_instance, &service_params,
864 + &cma_service) != VCHIQ_SUCCESS) {
865 + LOG_ERR("failed to open service - already in use?");
866 + goto fail_vchiq_open;
869 + vchiq_release_service(cma_service);
871 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
873 + LOG_ERR("could not create CMA worker thread");
876 + set_user_nice(cma_worker, -20);
877 + wake_up_process(cma_worker);
882 + vchiq_close_service(cma_service);
884 + vchiq_shutdown(cma_instance);
886 + vchiu_queue_delete(&cma_msg_queue);
892 +loud_error_header(void)
897 + LOG_ERR("============================================================"
898 + "================");
899 + LOG_ERR("============================================================"
900 + "================");
907 +loud_error_footer(void)
909 + if (!in_loud_error)
913 + LOG_ERR("============================================================"
914 + "================");
915 + LOG_ERR("============================================================"
916 + "================");
922 +static int check_cma_config(void) { return 1; }
925 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
926 + const char *symbol,
927 + void *buf, size_t bufsize)
929 + VC_MEM_ADDR_T vcMemAddr;
934 + if (!LookupVideoCoreSymbol(handle, symbol,
937 + loud_error_header();
939 + "failed to find VC symbol \"%s\".",
941 + loud_error_footer();
945 + if (vcMemSize != bufsize) {
946 + loud_error_header();
948 + "VC symbol \"%s\" is the wrong size.",
950 + loud_error_footer();
954 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
955 + vcMapAddr += mm_vc_mem_phys_addr;
956 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
957 + if (mapAddr == 0) {
958 + loud_error_header();
960 + "failed to ioremap \"%s\" @ 0x%x "
961 + "(phys: 0x%x, size: %u).",
963 + (unsigned int)vcMapAddr,
964 + (unsigned int)vcMemAddr,
965 + (unsigned int)vcMemSize);
966 + loud_error_footer();
970 + memcpy(buf, mapAddr, bufsize);
978 +check_cma_config(void)
980 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
981 + VC_MEM_ADDR_T mempool_start;
982 + VC_MEM_ADDR_T mempool_end;
983 + VC_MEM_ADDR_T mempool_offline_start;
984 + VC_MEM_ADDR_T mempool_offline_end;
985 + VC_MEM_ADDR_T cam_alloc_base;
986 + VC_MEM_ADDR_T cam_alloc_size;
987 + VC_MEM_ADDR_T cam_alloc_end;
990 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
993 + /* Read the relevant VideoCore variables */
994 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
996 + sizeof(mempool_start)))
999 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
1001 + sizeof(mempool_end)))
1004 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
1005 + &mempool_offline_start,
1006 + sizeof(mempool_offline_start)))
1009 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
1010 + &mempool_offline_end,
1011 + sizeof(mempool_offline_end)))
1014 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1016 + sizeof(cam_alloc_base)))
1019 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1021 + sizeof(cam_alloc_size)))
1024 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1028 + /* Now the sanity checks */
1029 + if (!mempool_offline_start)
1030 + mempool_offline_start = mempool_start;
1031 + if (!mempool_offline_end)
1032 + mempool_offline_end = mempool_end;
1034 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1035 + loud_error_header();
1037 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1038 + "vc_cma_base(%x)",
1039 + mempool_offline_start,
1040 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1045 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1046 + (vc_cma_base + vc_cma_size)) {
1047 + loud_error_header();
1049 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1050 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1051 + mempool_offline_start,
1052 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1053 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1057 + if (mempool_end < mempool_start) {
1058 + loud_error_header();
1060 + "__MEMPOOL_END(%x) must not be before "
1061 + "__MEMPOOL_START(%x)",
1067 + if (mempool_offline_end < mempool_offline_start) {
1068 + loud_error_header();
1070 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1071 + "__MEMPOOL_OFFLINE_START(%x)",
1072 + mempool_offline_end,
1073 + mempool_offline_start);
1077 + if (mempool_offline_start < mempool_start) {
1078 + loud_error_header();
1080 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1081 + "__MEMPOOL_START(%x)",
1082 + mempool_offline_start,
1087 + if (mempool_offline_end > mempool_end) {
1088 + loud_error_header();
1090 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1091 + "__MEMPOOL_END(%x)",
1092 + mempool_offline_end,
1097 + if ((cam_alloc_base < mempool_end) &&
1098 + (cam_alloc_end > mempool_start)) {
1099 + loud_error_header();
1101 + "cam_alloc pool(%x-%x) overlaps "
1103 + cam_alloc_base, cam_alloc_end,
1104 + mempool_start, mempool_end);
1108 + loud_error_footer();
1111 + CloseVideoCoreMemory(mem_hndl);
1118 +static int vc_cma_init(void)
1121 + struct device *dev;
1123 + if (!check_cma_config())
1126 + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
1127 + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
1128 + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
1129 + vc_cma_size, vc_cma_size / (1024 * 1024));
1130 + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
1131 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1133 + vc_cma_base_page = phys_to_page(vc_cma_base);
1135 + if (vc_cma_chunks) {
1136 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1138 + for (vc_cma_chunks_used = 0;
1139 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1140 + struct page *chunk;
1141 + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
1143 + VC_CMA_CHUNK_ORDER);
1146 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1147 + VC_CMA_CHUNK_SIZE) != 0);
1149 + if (vc_cma_chunks_used != chunks_needed) {
1150 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1151 + "bytes, allocation %d of %d)",
1152 + __func__, VC_CMA_CHUNK_SIZE,
1153 + vc_cma_chunks_used, chunks_needed);
1157 + vchiq_add_connected_callback(vc_cma_connected_init);
1160 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1162 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1166 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1167 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1169 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1170 + goto out_unregister;
1173 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1174 + if (IS_ERR(vc_cma_class)) {
1175 + rc = PTR_ERR(vc_cma_class);
1176 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1177 + goto out_cdev_del;
1180 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1182 + if (IS_ERR(dev)) {
1183 + rc = PTR_ERR(dev);
1184 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1185 + goto out_class_destroy;
1188 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1189 + if (vc_cma_proc_entry == NULL) {
1191 + LOG_ERR("%s: proc_create failed", __func__);
1192 + goto out_device_destroy;
1195 + vc_cma_inited = 1;
1198 +out_device_destroy:
1199 + device_destroy(vc_cma_class, vc_cma_devnum);
1202 + class_destroy(vc_cma_class);
1203 + vc_cma_class = NULL;
1206 + cdev_del(&vc_cma_cdev);
1209 + unregister_chrdev_region(vc_cma_devnum, 1);
1212 + /* It is tempting to try to clean up by calling
1213 + dma_release_from_contiguous for all allocated chunks, but it isn't
1214 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1215 + VideoCore is already using that memory, so giving it back to Linux
1216 + is likely to be fatal.
1221 +/****************************************************************************
1225 +***************************************************************************/
1227 +static void __exit vc_cma_exit(void)
1229 + LOG_DBG("%s: called", __func__);
1231 + if (vc_cma_inited) {
1232 + remove_proc_entry(DRIVER_NAME, NULL);
1233 + device_destroy(vc_cma_class, vc_cma_devnum);
1234 + class_destroy(vc_cma_class);
1235 + cdev_del(&vc_cma_cdev);
1236 + unregister_chrdev_region(vc_cma_devnum, 1);
1240 +module_init(vc_cma_init);
1241 +module_exit(vc_cma_exit);
1242 +MODULE_LICENSE("GPL");
1243 +MODULE_AUTHOR("Broadcom Corporation");
1244 diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
1245 index ad85616..8418ba7 100644
1246 --- a/drivers/misc/Makefile
1247 +++ b/drivers/misc/Makefile
1248 @@ -52,6 +52,6 @@ obj-$(CONFIG_INTEL_MEI) += mei/
1249 obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
1250 obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
1251 obj-$(CONFIG_SRAM) += sram.o
1252 -obj-y += vc04_services/
1253 +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
1255 obj-$(CONFIG_GENWQE) += genwqe/
1256 diff --git a/include/linux/broadcom/vc_cma.h b/include/linux/broadcom/vc_cma.h
1257 new file mode 100644
1258 index 0000000..5325832
1260 +++ b/include/linux/broadcom/vc_cma.h
1262 +/*****************************************************************************
1263 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1265 +* Unless you and Broadcom execute a separate written software license
1266 +* agreement governing use of this software, this software is licensed to you
1267 +* under the terms of the GNU General Public License version 2, available at
1268 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1270 +* Notwithstanding the above, under no circumstances may you combine this
1271 +* software in any way with any other Broadcom software provided under a
1272 +* license other than the GPL, without Broadcom's express prior written
1274 +*****************************************************************************/
1276 +#if !defined( VC_CMA_H )
1279 +#include <linux/ioctl.h>
1281 +#define VC_CMA_IOC_MAGIC 0xc5
1283 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1286 +extern void __init vc_cma_early_init(void);
1287 +extern void __init vc_cma_reserve(void);
1290 +#endif /* VC_CMA_H */