1 From dec8e7bfb37280733ec278955f1bd8dd2bc176b8 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] cma: Add vc_cma driver to enable use of CMA
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Signed-off-by: popcornmix <popcornmix@gmail.com>
11 vc_cma: Make the vc_cma area the default contiguous DMA area
13 vc_cma: Provide empty functions when module is not built
15 Providing empty functions saves the users from guarding the
16 function call with an #if clause.
17 Move __init markings from prototypes to functions.
19 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
21 drivers/char/Kconfig | 2 +
22 drivers/char/Makefile | 1 +
23 drivers/char/broadcom/Kconfig | 15 +
24 drivers/char/broadcom/Makefile | 1 +
25 drivers/char/broadcom/vc_cma/Makefile | 14 +
26 drivers/char/broadcom/vc_cma/vc_cma.c | 1193 +++++++++++++++++++++++++++++++++
27 include/linux/broadcom/vc_cma.h | 36 +
28 7 files changed, 1262 insertions(+)
29 create mode 100644 drivers/char/broadcom/Kconfig
30 create mode 100644 drivers/char/broadcom/Makefile
31 create mode 100644 drivers/char/broadcom/vc_cma/Makefile
32 create mode 100644 drivers/char/broadcom/vc_cma/vc_cma.c
33 create mode 100644 include/linux/broadcom/vc_cma.h
35 --- a/drivers/char/Kconfig
36 +++ b/drivers/char/Kconfig
39 menu "Character devices"
41 +source "drivers/char/broadcom/Kconfig"
43 source "drivers/tty/Kconfig"
46 --- a/drivers/char/Makefile
47 +++ b/drivers/char/Makefile
48 @@ -60,3 +60,4 @@ js-rtc-y = rtc.o
50 obj-$(CONFIG_TILE_SROM) += tile-srom.o
51 obj-$(CONFIG_XILLYBUS) += xillybus/
52 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
54 +++ b/drivers/char/broadcom/Kconfig
57 +# Broadcom char driver config
60 +menuconfig BRCM_CHAR_DRIVERS
61 + bool "Broadcom Char Drivers"
63 + Broadcom's char drivers
66 + bool "Videocore CMA"
67 + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
70 + Helper for videocore CMA access.
72 +++ b/drivers/char/broadcom/Makefile
74 +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
76 +++ b/drivers/char/broadcom/vc_cma/Makefile
78 +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
80 +ccflags-y += -Iinclude/linux/broadcom
81 +ccflags-y += -Idrivers/misc/vc04_services
82 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
83 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
85 +ccflags-y += -D__KERNEL__
86 +ccflags-y += -D__linux__
89 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
91 +vc-cma-objs := vc_cma.o
93 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
96 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
98 + * Redistribution and use in source and binary forms, with or without
99 + * modification, are permitted provided that the following conditions
101 + * 1. Redistributions of source code must retain the above copyright
102 + * notice, this list of conditions, and the following disclaimer,
103 + * without modification.
104 + * 2. Redistributions in binary form must reproduce the above copyright
105 + * notice, this list of conditions and the following disclaimer in the
106 + * documentation and/or other materials provided with the distribution.
107 + * 3. The names of the above-listed copyright holders may not be used
108 + * to endorse or promote products derived from this software without
109 + * specific prior written permission.
111 + * ALTERNATIVELY, this software may be distributed under the terms of the
112 + * GNU General Public License ("GPL") version 2, as published by the Free
113 + * Software Foundation.
115 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
116 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
117 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
118 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
119 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
120 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
121 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
122 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
123 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
124 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
125 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128 +#include <linux/kernel.h>
129 +#include <linux/module.h>
130 +#include <linux/kthread.h>
131 +#include <linux/fs.h>
132 +#include <linux/device.h>
133 +#include <linux/cdev.h>
134 +#include <linux/mm.h>
135 +#include <linux/proc_fs.h>
136 +#include <linux/seq_file.h>
137 +#include <linux/dma-mapping.h>
138 +#include <linux/dma-contiguous.h>
139 +#include <linux/platform_device.h>
140 +#include <linux/uaccess.h>
141 +#include <asm/cacheflush.h>
145 +#include "vchiq_util.h"
146 +#include "vchiq_connected.h"
147 +//#include "debug_sym.h"
148 +//#include "vc_mem.h"
150 +#define DRIVER_NAME "vc-cma"
152 +#define LOG_DBG(fmt, ...) \
153 + if (vc_cma_debug) \
154 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
155 +#define LOG_INFO(fmt, ...) \
156 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
157 +#define LOG_ERR(fmt, ...) \
158 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
160 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
161 +#define VC_CMA_VERSION 2
163 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
164 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
165 +#define VC_CMA_MAX_PARAMS_PER_MSG \
166 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
167 +#define VC_CMA_RESERVE_COUNT_MAX 16
169 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
171 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
173 +#define loud_error(...) \
174 + LOG_ERR("===== " __VA_ARGS__)
180 + VC_CMA_MSG_ALLOC, /* chunk count */
181 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
182 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
183 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
184 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
185 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
186 + VC_CMA_MSG_UPDATE_RESERVE,
191 + unsigned short type;
192 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
195 +struct vc_cma_reserve_user {
197 + unsigned int reserve;
200 +/* Device (/dev) related variables */
201 +static dev_t vc_cma_devnum;
202 +static struct class *vc_cma_class;
203 +static struct cdev vc_cma_cdev;
204 +static int vc_cma_inited;
205 +static int vc_cma_debug;
208 +static struct proc_dir_entry *vc_cma_proc_entry;
210 +phys_addr_t vc_cma_base;
211 +struct page *vc_cma_base_page;
212 +unsigned int vc_cma_size;
213 +EXPORT_SYMBOL(vc_cma_size);
214 +unsigned int vc_cma_initial;
215 +unsigned int vc_cma_chunks;
216 +unsigned int vc_cma_chunks_used;
217 +unsigned int vc_cma_chunks_reserved;
220 +void *vc_cma_dma_alloc;
221 +unsigned int vc_cma_dma_size;
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 __init 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 __init 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(&vc_cma_device.dev, vc_cma_size,
302 + vc_cma_base, 0) == 0) {
303 + if (!dev_get_cma_area(NULL)) {
304 + /* There is no default CMA area - make this
306 + struct cma *vc_cma_area = dev_get_cma_area(
307 + &vc_cma_device.dev);
308 + dma_contiguous_set_default(vc_cma_area);
309 + LOG_INFO("vc_cma_reserve - using vc_cma as "
310 + "the default contiguous DMA area");
313 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
314 + vc_cma_size, (unsigned int)vc_cma_base);
318 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
321 +/****************************************************************************
325 +***************************************************************************/
327 +static int vc_cma_open(struct inode *inode, struct file *file)
335 +/****************************************************************************
339 +***************************************************************************/
341 +static int vc_cma_release(struct inode *inode, struct file *file)
346 + vc_cma_set_reserve(0, current->tgid);
351 +/****************************************************************************
355 +***************************************************************************/
357 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
365 + case VC_CMA_IOC_RESERVE:
366 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
371 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
378 +/****************************************************************************
380 +* File Operations for the driver.
382 +***************************************************************************/
384 +static const struct file_operations vc_cma_fops = {
385 + .owner = THIS_MODULE,
386 + .open = vc_cma_open,
387 + .release = vc_cma_release,
388 + .unlocked_ioctl = vc_cma_ioctl,
391 +/****************************************************************************
395 +***************************************************************************/
397 +static int vc_cma_show_info(struct seq_file *m, void *v)
401 + seq_printf(m, "Videocore CMA:\n");
402 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
403 + seq_printf(m, " Length : %08x\n", vc_cma_size);
404 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
405 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
406 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
407 + (int)vc_cma_chunks,
408 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
409 + seq_printf(m, " Used : %4d (%d bytes)\n",
410 + (int)vc_cma_chunks_used,
411 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
412 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
413 + (unsigned int)vc_cma_chunks_reserved,
414 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
416 + for (i = 0; i < vc_cma_reserve_count; i++) {
417 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
418 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
421 + seq_printf(m, " dma_alloc : %p (%d pages)\n",
422 + vc_cma_dma_alloc ? page_address(vc_cma_dma_alloc) : 0,
425 + seq_printf(m, "\n");
430 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
432 + return single_open(file, vc_cma_show_info, NULL);
435 +/****************************************************************************
439 +***************************************************************************/
441 +static int vc_cma_proc_write(struct file *file,
442 + const char __user *buffer,
443 + size_t size, loff_t *ppos)
446 + char input_str[20];
448 + memset(input_str, 0, sizeof(input_str));
450 + if (size > sizeof(input_str)) {
451 + LOG_ERR("%s: input string length too long", __func__);
455 + if (copy_from_user(input_str, buffer, size - 1)) {
456 + LOG_ERR("%s: failed to get input string", __func__);
459 +#define ALLOC_STR "alloc"
460 +#define FREE_STR "free"
461 +#define DEBUG_STR "debug"
462 +#define RESERVE_STR "reserve"
463 +#define DMA_ALLOC_STR "dma_alloc"
464 +#define DMA_FREE_STR "dma_free"
465 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
467 + char *p = input_str + strlen(ALLOC_STR);
471 + alloc_size = memparse(p, NULL);
472 + LOG_INFO("/proc/vc-cma: alloc %d", alloc_size);
474 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
475 + alloc_size / VC_CMA_CHUNK_SIZE, 0);
477 + LOG_ERR("invalid size '%s'", p);
479 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
481 + char *p = input_str + strlen(FREE_STR);
485 + alloc_size = memparse(p, NULL);
486 + LOG_INFO("/proc/vc-cma: free %d", alloc_size);
488 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
489 + alloc_size / VC_CMA_CHUNK_SIZE, 0);
491 + LOG_ERR("invalid size '%s'", p);
493 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
494 + char *p = input_str + strlen(DEBUG_STR);
497 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
499 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
501 + LOG_INFO("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
503 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
506 + char *p = input_str + strlen(RESERVE_STR);
509 + alloc_size = memparse(p, NULL);
511 + reserved = vc_cma_set_reserve(alloc_size, current->tgid);
512 + rc = (reserved >= 0) ? size : reserved;
513 + } else if (strncmp(input_str, DMA_ALLOC_STR, strlen(DMA_ALLOC_STR)) == 0) {
515 + char *p = input_str + strlen(DMA_ALLOC_STR);
518 + alloc_size = memparse(p, NULL);
520 + if (vc_cma_dma_alloc) {
521 + dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
523 + vc_cma_dma_alloc = NULL;
524 + vc_cma_dma_size = 0;
526 + vc_cma_dma_alloc = dma_alloc_from_contiguous(NULL, alloc_size, 0);
527 + vc_cma_dma_size = (vc_cma_dma_alloc ? alloc_size : 0);
528 + if (vc_cma_dma_alloc)
529 + LOG_INFO("dma_alloc(%d pages) -> %p", alloc_size, page_address(vc_cma_dma_alloc));
531 + LOG_ERR("dma_alloc(%d pages) failed", alloc_size);
533 + } else if (strncmp(input_str, DMA_FREE_STR, strlen(DMA_FREE_STR)) == 0) {
534 + if (vc_cma_dma_alloc) {
535 + dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
537 + vc_cma_dma_alloc = NULL;
538 + vc_cma_dma_size = 0;
547 +/****************************************************************************
549 +* File Operations for /proc interface.
551 +***************************************************************************/
553 +static const struct file_operations vc_cma_proc_fops = {
554 + .open = vc_cma_proc_open,
556 + .write = vc_cma_proc_write,
557 + .llseek = seq_lseek,
558 + .release = single_release
561 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
563 + struct vc_cma_reserve_user *user = NULL;
567 + if (down_interruptible(&vc_cma_reserve_mutex))
568 + return -ERESTARTSYS;
570 + for (i = 0; i < vc_cma_reserve_count; i++) {
571 + if (pid == vc_cma_reserve_users[i].pid) {
572 + user = &vc_cma_reserve_users[i];
573 + delta = reserve - user->reserve;
575 + user->reserve = reserve;
577 + /* Remove this entry by copying downwards */
578 + while ((i + 1) < vc_cma_reserve_count) {
579 + user[0].pid = user[1].pid;
580 + user[0].reserve = user[1].reserve;
584 + vc_cma_reserve_count--;
591 + if (reserve && !user) {
592 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
593 + LOG_ERR("vc-cma: Too many reservations - "
594 + "increase CMA_RESERVE_COUNT_MAX");
595 + up(&vc_cma_reserve_mutex);
598 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
600 + user->reserve = reserve;
602 + vc_cma_reserve_count++;
605 + vc_cma_reserve_total += delta;
607 + send_vc_msg(VC_CMA_MSG_RESERVE,
608 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
610 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
612 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
613 + reserve, pid, vc_cma_reserve_total);
615 + up(&vc_cma_reserve_mutex);
617 + return vc_cma_reserve_total;
620 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
621 + VCHIQ_HEADER_T * header,
622 + VCHIQ_SERVICE_HANDLE_T service,
623 + void *bulk_userdata)
626 + case VCHIQ_MESSAGE_AVAILABLE:
627 + if (!send_worker_msg(header))
628 + return VCHIQ_RETRY;
630 + case VCHIQ_SERVICE_CLOSED:
631 + LOG_DBG("CMA service closed");
634 + LOG_ERR("Unexpected CMA callback reason %d", reason);
637 + return VCHIQ_SUCCESS;
640 +static void send_vc_msg(unsigned short type,
641 + unsigned short param1, unsigned short param2)
643 + unsigned short msg[] = { type, param1, param2 };
644 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
645 + VCHIQ_STATUS_T ret;
646 + vchiq_use_service(cma_service);
647 + ret = vchiq_queue_message(cma_service, &elem, 1);
648 + vchiq_release_service(cma_service);
649 + if (ret != VCHIQ_SUCCESS)
650 + LOG_ERR("vchiq_queue_message returned %x", ret);
653 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
655 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
657 + vchiu_queue_push(&cma_msg_queue, msg);
658 + up(&vc_cma_worker_queue_push_mutex);
662 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
665 + for (i = 0; i < num_chunks; i++) {
666 + struct page *chunk;
667 + unsigned int chunk_num;
668 + uint8_t *chunk_addr;
669 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
671 + chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
673 + VC_CMA_CHUNK_ORDER);
677 + chunk_addr = page_address(chunk);
678 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
679 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
683 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
684 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
685 + VC_CMA_CHUNK_SIZE) != 0);
686 + if (chunk_num >= vc_cma_chunks) {
687 + phys_addr_t _pa = vc_cma_base + vc_cma_size - 1;
688 + LOG_ERR("%s: ===============================",
690 + LOG_ERR("%s: chunk phys %x, vc_cma %pa-%pa - "
691 + "bad SPARSEMEM configuration?",
692 + __func__, (unsigned int)page_to_phys(chunk),
693 + &vc_cma_base, &_pa);
694 + LOG_ERR("%s: dev->cma_area = %p", __func__,
695 + (void*)0/*vc_cma_device.dev.cma_area*/);
696 + LOG_ERR("%s: ===============================",
700 + reply->params[i] = chunk_num;
701 + vc_cma_chunks_used++;
704 + if (i < num_chunks) {
705 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
706 + "for %x bytes (alloc %d of %d, %d free)",
707 + __func__, VC_CMA_CHUNK_SIZE, i,
708 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
712 + LOG_DBG("CMA allocated %d chunks -> %d used",
713 + num_chunks, vc_cma_chunks_used);
714 + reply->type = VC_CMA_MSG_ALLOCATED;
717 + VCHIQ_ELEMENT_T elem = {
719 + offsetof(struct cma_msg, params[0]) +
720 + num_chunks * sizeof(reply->params[0])
722 + VCHIQ_STATUS_T ret;
723 + vchiq_use_service(cma_service);
724 + ret = vchiq_queue_message(cma_service, &elem, 1);
725 + vchiq_release_service(cma_service);
726 + if (ret != VCHIQ_SUCCESS)
727 + LOG_ERR("vchiq_queue_message return " "%x", ret);
733 +static int cma_worker_proc(void *param)
735 + static struct cma_msg reply;
739 + VCHIQ_HEADER_T *msg;
740 + static struct cma_msg msg_copy;
741 + struct cma_msg *cma_msg = &msg_copy;
742 + int type, msg_size;
744 + msg = vchiu_queue_pop(&cma_msg_queue);
745 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
746 + msg_size = msg->size;
747 + memcpy(&msg_copy, msg->data, msg_size);
748 + type = cma_msg->type;
749 + vchiq_release_message(cma_service, msg);
753 + if (type == VC_CMA_MSG_QUIT)
755 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
765 + case VC_CMA_MSG_ALLOC:{
766 + int num_chunks, free_chunks;
767 + num_chunks = cma_msg->params[0];
769 + vc_cma_chunks - vc_cma_chunks_used;
770 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
771 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
773 + ("CMA_MSG_ALLOC - chunk count (%d) "
774 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
776 + VC_CMA_MAX_PARAMS_PER_MSG);
777 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
780 + if (num_chunks > free_chunks) {
782 + ("CMA_MSG_ALLOC - chunk count (%d) "
783 + "exceeds free chunks (%d)",
784 + num_chunks, free_chunks);
785 + num_chunks = free_chunks;
788 + vc_cma_alloc_chunks(num_chunks, &reply);
792 + case VC_CMA_MSG_FREE:{
795 + offsetof(struct cma_msg,
797 + sizeof(cma_msg->params[0]);
799 + BUG_ON(chunk_count <= 0);
801 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
802 + chunk_count, cma_msg->params[0]);
803 + for (i = 0; i < chunk_count; i++) {
804 + int chunk_num = cma_msg->params[i];
805 + struct page *page = vc_cma_base_page +
806 + chunk_num * PAGES_PER_CHUNK;
807 + if (chunk_num >= vc_cma_chunks) {
809 + ("CMA_MSG_FREE - chunk %d of %d"
810 + " (value %x) exceeds maximum "
811 + "(%x)", i, chunk_count,
813 + vc_cma_chunks - 1);
817 + if (!dma_release_from_contiguous
818 + (&vc_cma_device.dev, page,
819 + PAGES_PER_CHUNK)) {
820 + phys_addr_t _pa = page_to_phys(page);
822 + ("CMA_MSG_FREE - failed to "
823 + "release chunk %d (phys %pa, "
824 + "page %x)", chunk_num,
826 + (unsigned int)page);
828 + vc_cma_chunks_used--;
830 + LOG_DBG("CMA released %d chunks -> %d used",
831 + i, vc_cma_chunks_used);
835 + case VC_CMA_MSG_UPDATE_RESERVE:{
836 + int chunks_needed =
837 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
839 + / VC_CMA_CHUNK_SIZE) -
840 + vc_cma_chunks_reserved;
843 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
846 + /* Cap the reservations to what is available */
847 + if (chunks_needed > 0) {
848 + if (chunks_needed >
850 + vc_cma_chunks_used))
853 + vc_cma_chunks_used);
856 + vc_cma_alloc_chunks(chunks_needed,
861 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
863 + vc_cma_chunks_reserved += chunks_needed;
868 + LOG_ERR("unexpected msg type %d", type);
873 + LOG_DBG("quitting...");
877 +/****************************************************************************
879 +* vc_cma_connected_init
881 +* This function is called once the videocore has been connected.
883 +***************************************************************************/
885 +static void vc_cma_connected_init(void)
887 + VCHIQ_SERVICE_PARAMS_T service_params;
889 + LOG_DBG("vc_cma_connected_init");
891 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
892 + LOG_ERR("could not create CMA msg queue");
896 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
897 + goto fail_vchiq_init;
899 + vchiq_connect(cma_instance);
901 + service_params.fourcc = VC_CMA_FOURCC;
902 + service_params.callback = cma_service_callback;
903 + service_params.userdata = NULL;
904 + service_params.version = VC_CMA_VERSION;
905 + service_params.version_min = VC_CMA_VERSION;
907 + if (vchiq_open_service(cma_instance, &service_params,
908 + &cma_service) != VCHIQ_SUCCESS) {
909 + LOG_ERR("failed to open service - already in use?");
910 + goto fail_vchiq_open;
913 + vchiq_release_service(cma_service);
915 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
917 + LOG_ERR("could not create CMA worker thread");
920 + set_user_nice(cma_worker, -20);
921 + wake_up_process(cma_worker);
926 + vchiq_close_service(cma_service);
928 + vchiq_shutdown(cma_instance);
930 + vchiu_queue_delete(&cma_msg_queue);
936 +loud_error_header(void)
941 + LOG_ERR("============================================================"
942 + "================");
943 + LOG_ERR("============================================================"
944 + "================");
951 +loud_error_footer(void)
953 + if (!in_loud_error)
957 + LOG_ERR("============================================================"
958 + "================");
959 + LOG_ERR("============================================================"
960 + "================");
966 +static int check_cma_config(void) { return 1; }
969 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
970 + const char *symbol,
971 + void *buf, size_t bufsize)
973 + VC_MEM_ADDR_T vcMemAddr;
978 + if (!LookupVideoCoreSymbol(handle, symbol,
981 + loud_error_header();
983 + "failed to find VC symbol \"%s\".",
985 + loud_error_footer();
989 + if (vcMemSize != bufsize) {
990 + loud_error_header();
992 + "VC symbol \"%s\" is the wrong size.",
994 + loud_error_footer();
998 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
999 + vcMapAddr += mm_vc_mem_phys_addr;
1000 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
1001 + if (mapAddr == 0) {
1002 + loud_error_header();
1004 + "failed to ioremap \"%s\" @ 0x%x "
1005 + "(phys: 0x%x, size: %u).",
1007 + (unsigned int)vcMapAddr,
1008 + (unsigned int)vcMemAddr,
1009 + (unsigned int)vcMemSize);
1010 + loud_error_footer();
1014 + memcpy(buf, mapAddr, bufsize);
1022 +check_cma_config(void)
1024 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
1025 + VC_MEM_ADDR_T mempool_start;
1026 + VC_MEM_ADDR_T mempool_end;
1027 + VC_MEM_ADDR_T mempool_offline_start;
1028 + VC_MEM_ADDR_T mempool_offline_end;
1029 + VC_MEM_ADDR_T cam_alloc_base;
1030 + VC_MEM_ADDR_T cam_alloc_size;
1031 + VC_MEM_ADDR_T cam_alloc_end;
1034 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
1037 + /* Read the relevant VideoCore variables */
1038 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
1040 + sizeof(mempool_start)))
1043 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
1045 + sizeof(mempool_end)))
1048 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
1049 + &mempool_offline_start,
1050 + sizeof(mempool_offline_start)))
1053 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
1054 + &mempool_offline_end,
1055 + sizeof(mempool_offline_end)))
1058 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1060 + sizeof(cam_alloc_base)))
1063 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1065 + sizeof(cam_alloc_size)))
1068 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1072 + /* Now the sanity checks */
1073 + if (!mempool_offline_start)
1074 + mempool_offline_start = mempool_start;
1075 + if (!mempool_offline_end)
1076 + mempool_offline_end = mempool_end;
1078 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1079 + loud_error_header();
1081 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1082 + "vc_cma_base(%x)",
1083 + mempool_offline_start,
1084 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1089 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1090 + (vc_cma_base + vc_cma_size)) {
1091 + loud_error_header();
1093 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1094 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1095 + mempool_offline_start,
1096 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1097 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1101 + if (mempool_end < mempool_start) {
1102 + loud_error_header();
1104 + "__MEMPOOL_END(%x) must not be before "
1105 + "__MEMPOOL_START(%x)",
1111 + if (mempool_offline_end < mempool_offline_start) {
1112 + loud_error_header();
1114 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1115 + "__MEMPOOL_OFFLINE_START(%x)",
1116 + mempool_offline_end,
1117 + mempool_offline_start);
1121 + if (mempool_offline_start < mempool_start) {
1122 + loud_error_header();
1124 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1125 + "__MEMPOOL_START(%x)",
1126 + mempool_offline_start,
1131 + if (mempool_offline_end > mempool_end) {
1132 + loud_error_header();
1134 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1135 + "__MEMPOOL_END(%x)",
1136 + mempool_offline_end,
1141 + if ((cam_alloc_base < mempool_end) &&
1142 + (cam_alloc_end > mempool_start)) {
1143 + loud_error_header();
1145 + "cam_alloc pool(%x-%x) overlaps "
1147 + cam_alloc_base, cam_alloc_end,
1148 + mempool_start, mempool_end);
1152 + loud_error_footer();
1155 + CloseVideoCoreMemory(mem_hndl);
1162 +static int vc_cma_init(void)
1165 + struct device *dev;
1167 + if (!check_cma_config())
1170 + LOG_INFO("vc-cma: Videocore CMA driver");
1171 + LOG_INFO("vc-cma: vc_cma_base = %pa", &vc_cma_base);
1172 + LOG_INFO("vc-cma: vc_cma_size = 0x%08x (%u MiB)",
1173 + vc_cma_size, vc_cma_size / (1024 * 1024));
1174 + LOG_INFO("vc-cma: vc_cma_initial = 0x%08x (%u MiB)",
1175 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1177 + vc_cma_base_page = phys_to_page(vc_cma_base);
1179 + if (vc_cma_chunks) {
1180 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1182 + for (vc_cma_chunks_used = 0;
1183 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1184 + struct page *chunk;
1185 + chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
1187 + VC_CMA_CHUNK_ORDER);
1190 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1191 + VC_CMA_CHUNK_SIZE) != 0);
1193 + if (vc_cma_chunks_used != chunks_needed) {
1194 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1195 + "bytes, allocation %d of %d)",
1196 + __func__, VC_CMA_CHUNK_SIZE,
1197 + vc_cma_chunks_used, chunks_needed);
1201 + vchiq_add_connected_callback(vc_cma_connected_init);
1204 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1206 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1210 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1211 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1213 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1214 + goto out_unregister;
1217 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1218 + if (IS_ERR(vc_cma_class)) {
1219 + rc = PTR_ERR(vc_cma_class);
1220 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1221 + goto out_cdev_del;
1224 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1226 + if (IS_ERR(dev)) {
1227 + rc = PTR_ERR(dev);
1228 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1229 + goto out_class_destroy;
1232 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1233 + if (vc_cma_proc_entry == NULL) {
1235 + LOG_ERR("%s: proc_create failed", __func__);
1236 + goto out_device_destroy;
1239 + vc_cma_inited = 1;
1242 +out_device_destroy:
1243 + device_destroy(vc_cma_class, vc_cma_devnum);
1246 + class_destroy(vc_cma_class);
1247 + vc_cma_class = NULL;
1250 + cdev_del(&vc_cma_cdev);
1253 + unregister_chrdev_region(vc_cma_devnum, 1);
1256 + /* It is tempting to try to clean up by calling
1257 + dma_release_from_contiguous for all allocated chunks, but it isn't
1258 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1259 + VideoCore is already using that memory, so giving it back to Linux
1260 + is likely to be fatal.
1265 +/****************************************************************************
1269 +***************************************************************************/
1271 +static void __exit vc_cma_exit(void)
1273 + LOG_DBG("%s: called", __func__);
1275 + if (vc_cma_inited) {
1276 + remove_proc_entry(DRIVER_NAME, NULL);
1277 + device_destroy(vc_cma_class, vc_cma_devnum);
1278 + class_destroy(vc_cma_class);
1279 + cdev_del(&vc_cma_cdev);
1280 + unregister_chrdev_region(vc_cma_devnum, 1);
1284 +module_init(vc_cma_init);
1285 +module_exit(vc_cma_exit);
1286 +MODULE_LICENSE("GPL");
1287 +MODULE_AUTHOR("Broadcom Corporation");
1289 +++ b/include/linux/broadcom/vc_cma.h
1291 +/*****************************************************************************
1292 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1294 +* Unless you and Broadcom execute a separate written software license
1295 +* agreement governing use of this software, this software is licensed to you
1296 +* under the terms of the GNU General Public License version 2, available at
1297 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1299 +* Notwithstanding the above, under no circumstances may you combine this
1300 +* software in any way with any other Broadcom software provided under a
1301 +* license other than the GPL, without Broadcom's express prior written
1303 +*****************************************************************************/
1305 +#if !defined( VC_CMA_H )
1308 +#include <linux/ioctl.h>
1310 +#define VC_CMA_IOC_MAGIC 0xc5
1312 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1316 +#ifdef CONFIG_BCM_VC_CMA
1317 +void vc_cma_early_init(void);
1318 +void vc_cma_reserve(void);
1320 +static inline void vc_cma_early_init(void) { }
1321 +static inline void vc_cma_reserve(void) { }
1326 +#endif /* VC_CMA_H */