1 From 484bbb300f8c414aa66c555a5887e2e9dbac0cdd 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 010/203] cma: Add vc_cma driver to enable use of CMA
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 vc_cma: Make the vc_cma area the default contiguous DMA area
10 drivers/char/Kconfig | 2 +
11 drivers/char/Makefile | 1 +
12 drivers/char/broadcom/Kconfig | 15 +
13 drivers/char/broadcom/Makefile | 1 +
14 drivers/char/broadcom/vc_cma/Makefile | 14 +
15 drivers/char/broadcom/vc_cma/vc_cma.c | 1193 +++++++++++++++++++++++++++++++++
16 include/linux/broadcom/vc_cma.h | 29 +
17 7 files changed, 1255 insertions(+)
18 create mode 100644 drivers/char/broadcom/Kconfig
19 create mode 100644 drivers/char/broadcom/Makefile
20 create mode 100644 drivers/char/broadcom/vc_cma/Makefile
21 create mode 100644 drivers/char/broadcom/vc_cma/vc_cma.c
22 create mode 100644 include/linux/broadcom/vc_cma.h
24 --- a/drivers/char/Kconfig
25 +++ b/drivers/char/Kconfig
26 @@ -590,6 +590,8 @@ config DEVPORT
28 source "drivers/s390/char/Kconfig"
30 +source "drivers/char/broadcom/Kconfig"
33 bool "Enable device interface for some SMD packet ports"
35 --- a/drivers/char/Makefile
36 +++ b/drivers/char/Makefile
37 @@ -62,3 +62,4 @@ js-rtc-y = rtc.o
39 obj-$(CONFIG_TILE_SROM) += tile-srom.o
40 obj-$(CONFIG_XILLYBUS) += xillybus/
41 +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
43 +++ b/drivers/char/broadcom/Kconfig
46 +# Broadcom char driver config
49 +menuconfig BRCM_CHAR_DRIVERS
50 + bool "Broadcom Char Drivers"
52 + Broadcom's char drivers
55 + bool "Videocore CMA"
56 + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
59 + Helper for videocore CMA access.
61 +++ b/drivers/char/broadcom/Makefile
63 +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
65 +++ b/drivers/char/broadcom/vc_cma/Makefile
67 +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
69 +ccflags-y += -Iinclude/linux/broadcom
70 +ccflags-y += -Idrivers/misc/vc04_services
71 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
72 +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
74 +ccflags-y += -D__KERNEL__
75 +ccflags-y += -D__linux__
78 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
80 +vc-cma-objs := vc_cma.o
82 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
85 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
87 + * Redistribution and use in source and binary forms, with or without
88 + * modification, are permitted provided that the following conditions
90 + * 1. Redistributions of source code must retain the above copyright
91 + * notice, this list of conditions, and the following disclaimer,
92 + * without modification.
93 + * 2. Redistributions in binary form must reproduce the above copyright
94 + * notice, this list of conditions and the following disclaimer in the
95 + * documentation and/or other materials provided with the distribution.
96 + * 3. The names of the above-listed copyright holders may not be used
97 + * to endorse or promote products derived from this software without
98 + * specific prior written permission.
100 + * ALTERNATIVELY, this software may be distributed under the terms of the
101 + * GNU General Public License ("GPL") version 2, as published by the Free
102 + * Software Foundation.
104 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
105 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
106 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
107 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
108 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
109 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
110 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
111 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
112 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
113 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
114 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
117 +#include <linux/kernel.h>
118 +#include <linux/module.h>
119 +#include <linux/kthread.h>
120 +#include <linux/fs.h>
121 +#include <linux/device.h>
122 +#include <linux/cdev.h>
123 +#include <linux/mm.h>
124 +#include <linux/proc_fs.h>
125 +#include <linux/seq_file.h>
126 +#include <linux/dma-mapping.h>
127 +#include <linux/dma-contiguous.h>
128 +#include <linux/platform_device.h>
129 +#include <linux/uaccess.h>
130 +#include <asm/cacheflush.h>
134 +#include "vchiq_util.h"
135 +#include "vchiq_connected.h"
136 +//#include "debug_sym.h"
137 +//#include "vc_mem.h"
139 +#define DRIVER_NAME "vc-cma"
141 +#define LOG_DBG(fmt, ...) \
142 + if (vc_cma_debug) \
143 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
144 +#define LOG_INFO(fmt, ...) \
145 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
146 +#define LOG_ERR(fmt, ...) \
147 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
149 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
150 +#define VC_CMA_VERSION 2
152 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
153 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
154 +#define VC_CMA_MAX_PARAMS_PER_MSG \
155 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
156 +#define VC_CMA_RESERVE_COUNT_MAX 16
158 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
160 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
162 +#define loud_error(...) \
163 + LOG_ERR("===== " __VA_ARGS__)
169 + VC_CMA_MSG_ALLOC, /* chunk count */
170 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
171 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
172 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
173 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
174 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
175 + VC_CMA_MSG_UPDATE_RESERVE,
180 + unsigned short type;
181 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
184 +struct vc_cma_reserve_user {
186 + unsigned int reserve;
189 +/* Device (/dev) related variables */
190 +static dev_t vc_cma_devnum;
191 +static struct class *vc_cma_class;
192 +static struct cdev vc_cma_cdev;
193 +static int vc_cma_inited;
194 +static int vc_cma_debug;
197 +static struct proc_dir_entry *vc_cma_proc_entry;
199 +phys_addr_t vc_cma_base;
200 +struct page *vc_cma_base_page;
201 +unsigned int vc_cma_size;
202 +EXPORT_SYMBOL(vc_cma_size);
203 +unsigned int vc_cma_initial;
204 +unsigned int vc_cma_chunks;
205 +unsigned int vc_cma_chunks_used;
206 +unsigned int vc_cma_chunks_reserved;
209 +void *vc_cma_dma_alloc;
210 +unsigned int vc_cma_dma_size;
212 +static int in_loud_error;
214 +unsigned int vc_cma_reserve_total;
215 +unsigned int vc_cma_reserve_count;
216 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
217 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
218 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
220 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
221 +static struct platform_device vc_cma_device = {
225 + .dma_mask = &vc_cma_dma_mask,
226 + .coherent_dma_mask = DMA_BIT_MASK(32),
230 +static VCHIQ_INSTANCE_T cma_instance;
231 +static VCHIQ_SERVICE_HANDLE_T cma_service;
232 +static VCHIU_QUEUE_T cma_msg_queue;
233 +static struct task_struct *cma_worker;
235 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
236 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
237 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
238 + VCHIQ_HEADER_T * header,
239 + VCHIQ_SERVICE_HANDLE_T service,
240 + void *bulk_userdata);
241 +static void send_vc_msg(unsigned short type,
242 + unsigned short param1, unsigned short param2);
243 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
245 +static int early_vc_cma_mem(char *p)
247 + unsigned int new_size;
248 + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
249 + vc_cma_size = memparse(p, &p);
250 + vc_cma_initial = vc_cma_size;
252 + vc_cma_size = memparse(p + 1, &p);
254 + vc_cma_base = memparse(p + 1, &p);
256 + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
257 + & ~(VC_CMA_CHUNK_SIZE - 1);
258 + if (new_size > vc_cma_size)
260 + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
261 + & ~(VC_CMA_CHUNK_SIZE - 1);
262 + if (vc_cma_initial > vc_cma_size)
263 + vc_cma_initial = vc_cma_size;
264 + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
265 + & ~(VC_CMA_CHUNK_SIZE - 1);
267 + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
268 + vc_cma_size, (unsigned int)vc_cma_base);
273 +early_param("vc-cma-mem", early_vc_cma_mem);
275 +void vc_cma_early_init(void)
277 + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
279 + int rc = platform_device_register(&vc_cma_device);
280 + LOG_DBG("platform_device_register -> %d", rc);
284 +void vc_cma_reserve(void)
286 + /* if vc_cma_size is set, then declare vc CMA area of the same
287 + * size from the end of memory
290 + if (dma_declare_contiguous(&vc_cma_device.dev, vc_cma_size,
291 + vc_cma_base, 0) == 0) {
292 + if (!dev_get_cma_area(NULL)) {
293 + /* There is no default CMA area - make this
295 + struct cma *vc_cma_area = dev_get_cma_area(
296 + &vc_cma_device.dev);
297 + dma_contiguous_set_default(vc_cma_area);
298 + LOG_INFO("vc_cma_reserve - using vc_cma as "
299 + "the default contiguous DMA area");
302 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
303 + vc_cma_size, (unsigned int)vc_cma_base);
307 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
310 +/****************************************************************************
314 +***************************************************************************/
316 +static int vc_cma_open(struct inode *inode, struct file *file)
324 +/****************************************************************************
328 +***************************************************************************/
330 +static int vc_cma_release(struct inode *inode, struct file *file)
335 + vc_cma_set_reserve(0, current->tgid);
340 +/****************************************************************************
344 +***************************************************************************/
346 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
354 + case VC_CMA_IOC_RESERVE:
355 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
360 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
367 +/****************************************************************************
369 +* File Operations for the driver.
371 +***************************************************************************/
373 +static const struct file_operations vc_cma_fops = {
374 + .owner = THIS_MODULE,
375 + .open = vc_cma_open,
376 + .release = vc_cma_release,
377 + .unlocked_ioctl = vc_cma_ioctl,
380 +/****************************************************************************
384 +***************************************************************************/
386 +static int vc_cma_show_info(struct seq_file *m, void *v)
390 + seq_printf(m, "Videocore CMA:\n");
391 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
392 + seq_printf(m, " Length : %08x\n", vc_cma_size);
393 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
394 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
395 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
396 + (int)vc_cma_chunks,
397 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
398 + seq_printf(m, " Used : %4d (%d bytes)\n",
399 + (int)vc_cma_chunks_used,
400 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
401 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
402 + (unsigned int)vc_cma_chunks_reserved,
403 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
405 + for (i = 0; i < vc_cma_reserve_count; i++) {
406 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
407 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
410 + seq_printf(m, " dma_alloc : %p (%d pages)\n",
411 + vc_cma_dma_alloc ? page_address(vc_cma_dma_alloc) : 0,
414 + seq_printf(m, "\n");
419 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
421 + return single_open(file, vc_cma_show_info, NULL);
424 +/****************************************************************************
428 +***************************************************************************/
430 +static int vc_cma_proc_write(struct file *file,
431 + const char __user *buffer,
432 + size_t size, loff_t *ppos)
435 + char input_str[20];
437 + memset(input_str, 0, sizeof(input_str));
439 + if (size > sizeof(input_str)) {
440 + LOG_ERR("%s: input string length too long", __func__);
444 + if (copy_from_user(input_str, buffer, size - 1)) {
445 + LOG_ERR("%s: failed to get input string", __func__);
448 +#define ALLOC_STR "alloc"
449 +#define FREE_STR "free"
450 +#define DEBUG_STR "debug"
451 +#define RESERVE_STR "reserve"
452 +#define DMA_ALLOC_STR "dma_alloc"
453 +#define DMA_FREE_STR "dma_free"
454 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
456 + char *p = input_str + strlen(ALLOC_STR);
460 + alloc_size = memparse(p, NULL);
461 + LOG_INFO("/proc/vc-cma: alloc %d", alloc_size);
463 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
464 + alloc_size / VC_CMA_CHUNK_SIZE, 0);
466 + LOG_ERR("invalid size '%s'", p);
468 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
470 + char *p = input_str + strlen(FREE_STR);
474 + alloc_size = memparse(p, NULL);
475 + LOG_INFO("/proc/vc-cma: free %d", alloc_size);
477 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
478 + alloc_size / VC_CMA_CHUNK_SIZE, 0);
480 + LOG_ERR("invalid size '%s'", p);
482 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
483 + char *p = input_str + strlen(DEBUG_STR);
486 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
488 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
490 + LOG_INFO("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
492 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
495 + char *p = input_str + strlen(RESERVE_STR);
498 + alloc_size = memparse(p, NULL);
500 + reserved = vc_cma_set_reserve(alloc_size, current->tgid);
501 + rc = (reserved >= 0) ? size : reserved;
502 + } else if (strncmp(input_str, DMA_ALLOC_STR, strlen(DMA_ALLOC_STR)) == 0) {
504 + char *p = input_str + strlen(DMA_ALLOC_STR);
507 + alloc_size = memparse(p, NULL);
509 + if (vc_cma_dma_alloc) {
510 + dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
512 + vc_cma_dma_alloc = NULL;
513 + vc_cma_dma_size = 0;
515 + vc_cma_dma_alloc = dma_alloc_from_contiguous(NULL, alloc_size, 0);
516 + vc_cma_dma_size = (vc_cma_dma_alloc ? alloc_size : 0);
517 + if (vc_cma_dma_alloc)
518 + LOG_INFO("dma_alloc(%d pages) -> %p", alloc_size, page_address(vc_cma_dma_alloc));
520 + LOG_ERR("dma_alloc(%d pages) failed", alloc_size);
522 + } else if (strncmp(input_str, DMA_FREE_STR, strlen(DMA_FREE_STR)) == 0) {
523 + if (vc_cma_dma_alloc) {
524 + dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
526 + vc_cma_dma_alloc = NULL;
527 + vc_cma_dma_size = 0;
536 +/****************************************************************************
538 +* File Operations for /proc interface.
540 +***************************************************************************/
542 +static const struct file_operations vc_cma_proc_fops = {
543 + .open = vc_cma_proc_open,
545 + .write = vc_cma_proc_write,
546 + .llseek = seq_lseek,
547 + .release = single_release
550 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
552 + struct vc_cma_reserve_user *user = NULL;
556 + if (down_interruptible(&vc_cma_reserve_mutex))
557 + return -ERESTARTSYS;
559 + for (i = 0; i < vc_cma_reserve_count; i++) {
560 + if (pid == vc_cma_reserve_users[i].pid) {
561 + user = &vc_cma_reserve_users[i];
562 + delta = reserve - user->reserve;
564 + user->reserve = reserve;
566 + /* Remove this entry by copying downwards */
567 + while ((i + 1) < vc_cma_reserve_count) {
568 + user[0].pid = user[1].pid;
569 + user[0].reserve = user[1].reserve;
573 + vc_cma_reserve_count--;
580 + if (reserve && !user) {
581 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
582 + LOG_ERR("vc-cma: Too many reservations - "
583 + "increase CMA_RESERVE_COUNT_MAX");
584 + up(&vc_cma_reserve_mutex);
587 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
589 + user->reserve = reserve;
591 + vc_cma_reserve_count++;
594 + vc_cma_reserve_total += delta;
596 + send_vc_msg(VC_CMA_MSG_RESERVE,
597 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
599 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
601 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
602 + reserve, pid, vc_cma_reserve_total);
604 + up(&vc_cma_reserve_mutex);
606 + return vc_cma_reserve_total;
609 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
610 + VCHIQ_HEADER_T * header,
611 + VCHIQ_SERVICE_HANDLE_T service,
612 + void *bulk_userdata)
615 + case VCHIQ_MESSAGE_AVAILABLE:
616 + if (!send_worker_msg(header))
617 + return VCHIQ_RETRY;
619 + case VCHIQ_SERVICE_CLOSED:
620 + LOG_DBG("CMA service closed");
623 + LOG_ERR("Unexpected CMA callback reason %d", reason);
626 + return VCHIQ_SUCCESS;
629 +static void send_vc_msg(unsigned short type,
630 + unsigned short param1, unsigned short param2)
632 + unsigned short msg[] = { type, param1, param2 };
633 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
634 + VCHIQ_STATUS_T ret;
635 + vchiq_use_service(cma_service);
636 + ret = vchiq_queue_message(cma_service, &elem, 1);
637 + vchiq_release_service(cma_service);
638 + if (ret != VCHIQ_SUCCESS)
639 + LOG_ERR("vchiq_queue_message returned %x", ret);
642 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
644 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
646 + vchiu_queue_push(&cma_msg_queue, msg);
647 + up(&vc_cma_worker_queue_push_mutex);
651 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
654 + for (i = 0; i < num_chunks; i++) {
655 + struct page *chunk;
656 + unsigned int chunk_num;
657 + uint8_t *chunk_addr;
658 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
660 + chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
662 + VC_CMA_CHUNK_ORDER);
666 + chunk_addr = page_address(chunk);
667 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
668 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
672 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
673 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
674 + VC_CMA_CHUNK_SIZE) != 0);
675 + if (chunk_num >= vc_cma_chunks) {
676 + phys_addr_t _pa = vc_cma_base + vc_cma_size - 1;
677 + LOG_ERR("%s: ===============================",
679 + LOG_ERR("%s: chunk phys %x, vc_cma %pa-%pa - "
680 + "bad SPARSEMEM configuration?",
681 + __func__, (unsigned int)page_to_phys(chunk),
682 + &vc_cma_base, &_pa);
683 + LOG_ERR("%s: dev->cma_area = %p", __func__,
684 + (void*)0/*vc_cma_device.dev.cma_area*/);
685 + LOG_ERR("%s: ===============================",
689 + reply->params[i] = chunk_num;
690 + vc_cma_chunks_used++;
693 + if (i < num_chunks) {
694 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
695 + "for %x bytes (alloc %d of %d, %d free)",
696 + __func__, VC_CMA_CHUNK_SIZE, i,
697 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
701 + LOG_DBG("CMA allocated %d chunks -> %d used",
702 + num_chunks, vc_cma_chunks_used);
703 + reply->type = VC_CMA_MSG_ALLOCATED;
706 + VCHIQ_ELEMENT_T elem = {
708 + offsetof(struct cma_msg, params[0]) +
709 + num_chunks * sizeof(reply->params[0])
711 + VCHIQ_STATUS_T ret;
712 + vchiq_use_service(cma_service);
713 + ret = vchiq_queue_message(cma_service, &elem, 1);
714 + vchiq_release_service(cma_service);
715 + if (ret != VCHIQ_SUCCESS)
716 + LOG_ERR("vchiq_queue_message return " "%x", ret);
722 +static int cma_worker_proc(void *param)
724 + static struct cma_msg reply;
728 + VCHIQ_HEADER_T *msg;
729 + static struct cma_msg msg_copy;
730 + struct cma_msg *cma_msg = &msg_copy;
731 + int type, msg_size;
733 + msg = vchiu_queue_pop(&cma_msg_queue);
734 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
735 + msg_size = msg->size;
736 + memcpy(&msg_copy, msg->data, msg_size);
737 + type = cma_msg->type;
738 + vchiq_release_message(cma_service, msg);
742 + if (type == VC_CMA_MSG_QUIT)
744 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
754 + case VC_CMA_MSG_ALLOC:{
755 + int num_chunks, free_chunks;
756 + num_chunks = cma_msg->params[0];
758 + vc_cma_chunks - vc_cma_chunks_used;
759 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
760 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
762 + ("CMA_MSG_ALLOC - chunk count (%d) "
763 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
765 + VC_CMA_MAX_PARAMS_PER_MSG);
766 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
769 + if (num_chunks > free_chunks) {
771 + ("CMA_MSG_ALLOC - chunk count (%d) "
772 + "exceeds free chunks (%d)",
773 + num_chunks, free_chunks);
774 + num_chunks = free_chunks;
777 + vc_cma_alloc_chunks(num_chunks, &reply);
781 + case VC_CMA_MSG_FREE:{
784 + offsetof(struct cma_msg,
786 + sizeof(cma_msg->params[0]);
788 + BUG_ON(chunk_count <= 0);
790 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
791 + chunk_count, cma_msg->params[0]);
792 + for (i = 0; i < chunk_count; i++) {
793 + int chunk_num = cma_msg->params[i];
794 + struct page *page = vc_cma_base_page +
795 + chunk_num * PAGES_PER_CHUNK;
796 + if (chunk_num >= vc_cma_chunks) {
798 + ("CMA_MSG_FREE - chunk %d of %d"
799 + " (value %x) exceeds maximum "
800 + "(%x)", i, chunk_count,
802 + vc_cma_chunks - 1);
806 + if (!dma_release_from_contiguous
807 + (&vc_cma_device.dev, page,
808 + PAGES_PER_CHUNK)) {
809 + phys_addr_t _pa = page_to_phys(page);
811 + ("CMA_MSG_FREE - failed to "
812 + "release chunk %d (phys %pa, "
813 + "page %x)", chunk_num,
815 + (unsigned int)page);
817 + vc_cma_chunks_used--;
819 + LOG_DBG("CMA released %d chunks -> %d used",
820 + i, vc_cma_chunks_used);
824 + case VC_CMA_MSG_UPDATE_RESERVE:{
825 + int chunks_needed =
826 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
828 + / VC_CMA_CHUNK_SIZE) -
829 + vc_cma_chunks_reserved;
832 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
835 + /* Cap the reservations to what is available */
836 + if (chunks_needed > 0) {
837 + if (chunks_needed >
839 + vc_cma_chunks_used))
842 + vc_cma_chunks_used);
845 + vc_cma_alloc_chunks(chunks_needed,
850 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
852 + vc_cma_chunks_reserved += chunks_needed;
857 + LOG_ERR("unexpected msg type %d", type);
862 + LOG_DBG("quitting...");
866 +/****************************************************************************
868 +* vc_cma_connected_init
870 +* This function is called once the videocore has been connected.
872 +***************************************************************************/
874 +static void vc_cma_connected_init(void)
876 + VCHIQ_SERVICE_PARAMS_T service_params;
878 + LOG_DBG("vc_cma_connected_init");
880 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
881 + LOG_ERR("could not create CMA msg queue");
885 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
886 + goto fail_vchiq_init;
888 + vchiq_connect(cma_instance);
890 + service_params.fourcc = VC_CMA_FOURCC;
891 + service_params.callback = cma_service_callback;
892 + service_params.userdata = NULL;
893 + service_params.version = VC_CMA_VERSION;
894 + service_params.version_min = VC_CMA_VERSION;
896 + if (vchiq_open_service(cma_instance, &service_params,
897 + &cma_service) != VCHIQ_SUCCESS) {
898 + LOG_ERR("failed to open service - already in use?");
899 + goto fail_vchiq_open;
902 + vchiq_release_service(cma_service);
904 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
906 + LOG_ERR("could not create CMA worker thread");
909 + set_user_nice(cma_worker, -20);
910 + wake_up_process(cma_worker);
915 + vchiq_close_service(cma_service);
917 + vchiq_shutdown(cma_instance);
919 + vchiu_queue_delete(&cma_msg_queue);
925 +loud_error_header(void)
930 + LOG_ERR("============================================================"
931 + "================");
932 + LOG_ERR("============================================================"
933 + "================");
940 +loud_error_footer(void)
942 + if (!in_loud_error)
946 + LOG_ERR("============================================================"
947 + "================");
948 + LOG_ERR("============================================================"
949 + "================");
955 +static int check_cma_config(void) { return 1; }
958 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
959 + const char *symbol,
960 + void *buf, size_t bufsize)
962 + VC_MEM_ADDR_T vcMemAddr;
967 + if (!LookupVideoCoreSymbol(handle, symbol,
970 + loud_error_header();
972 + "failed to find VC symbol \"%s\".",
974 + loud_error_footer();
978 + if (vcMemSize != bufsize) {
979 + loud_error_header();
981 + "VC symbol \"%s\" is the wrong size.",
983 + loud_error_footer();
987 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
988 + vcMapAddr += mm_vc_mem_phys_addr;
989 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
990 + if (mapAddr == 0) {
991 + loud_error_header();
993 + "failed to ioremap \"%s\" @ 0x%x "
994 + "(phys: 0x%x, size: %u).",
996 + (unsigned int)vcMapAddr,
997 + (unsigned int)vcMemAddr,
998 + (unsigned int)vcMemSize);
999 + loud_error_footer();
1003 + memcpy(buf, mapAddr, bufsize);
1011 +check_cma_config(void)
1013 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
1014 + VC_MEM_ADDR_T mempool_start;
1015 + VC_MEM_ADDR_T mempool_end;
1016 + VC_MEM_ADDR_T mempool_offline_start;
1017 + VC_MEM_ADDR_T mempool_offline_end;
1018 + VC_MEM_ADDR_T cam_alloc_base;
1019 + VC_MEM_ADDR_T cam_alloc_size;
1020 + VC_MEM_ADDR_T cam_alloc_end;
1023 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
1026 + /* Read the relevant VideoCore variables */
1027 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
1029 + sizeof(mempool_start)))
1032 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
1034 + sizeof(mempool_end)))
1037 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
1038 + &mempool_offline_start,
1039 + sizeof(mempool_offline_start)))
1042 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
1043 + &mempool_offline_end,
1044 + sizeof(mempool_offline_end)))
1047 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1049 + sizeof(cam_alloc_base)))
1052 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1054 + sizeof(cam_alloc_size)))
1057 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1061 + /* Now the sanity checks */
1062 + if (!mempool_offline_start)
1063 + mempool_offline_start = mempool_start;
1064 + if (!mempool_offline_end)
1065 + mempool_offline_end = mempool_end;
1067 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1068 + loud_error_header();
1070 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1071 + "vc_cma_base(%x)",
1072 + mempool_offline_start,
1073 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1078 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1079 + (vc_cma_base + vc_cma_size)) {
1080 + loud_error_header();
1082 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1083 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1084 + mempool_offline_start,
1085 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1086 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1090 + if (mempool_end < mempool_start) {
1091 + loud_error_header();
1093 + "__MEMPOOL_END(%x) must not be before "
1094 + "__MEMPOOL_START(%x)",
1100 + if (mempool_offline_end < mempool_offline_start) {
1101 + loud_error_header();
1103 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1104 + "__MEMPOOL_OFFLINE_START(%x)",
1105 + mempool_offline_end,
1106 + mempool_offline_start);
1110 + if (mempool_offline_start < mempool_start) {
1111 + loud_error_header();
1113 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1114 + "__MEMPOOL_START(%x)",
1115 + mempool_offline_start,
1120 + if (mempool_offline_end > mempool_end) {
1121 + loud_error_header();
1123 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1124 + "__MEMPOOL_END(%x)",
1125 + mempool_offline_end,
1130 + if ((cam_alloc_base < mempool_end) &&
1131 + (cam_alloc_end > mempool_start)) {
1132 + loud_error_header();
1134 + "cam_alloc pool(%x-%x) overlaps "
1136 + cam_alloc_base, cam_alloc_end,
1137 + mempool_start, mempool_end);
1141 + loud_error_footer();
1144 + CloseVideoCoreMemory(mem_hndl);
1151 +static int vc_cma_init(void)
1154 + struct device *dev;
1156 + if (!check_cma_config())
1159 + LOG_INFO("vc-cma: Videocore CMA driver");
1160 + LOG_INFO("vc-cma: vc_cma_base = %pa", &vc_cma_base);
1161 + LOG_INFO("vc-cma: vc_cma_size = 0x%08x (%u MiB)",
1162 + vc_cma_size, vc_cma_size / (1024 * 1024));
1163 + LOG_INFO("vc-cma: vc_cma_initial = 0x%08x (%u MiB)",
1164 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1166 + vc_cma_base_page = phys_to_page(vc_cma_base);
1168 + if (vc_cma_chunks) {
1169 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1171 + for (vc_cma_chunks_used = 0;
1172 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1173 + struct page *chunk;
1174 + chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
1176 + VC_CMA_CHUNK_ORDER);
1179 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1180 + VC_CMA_CHUNK_SIZE) != 0);
1182 + if (vc_cma_chunks_used != chunks_needed) {
1183 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1184 + "bytes, allocation %d of %d)",
1185 + __func__, VC_CMA_CHUNK_SIZE,
1186 + vc_cma_chunks_used, chunks_needed);
1190 + vchiq_add_connected_callback(vc_cma_connected_init);
1193 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1195 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1199 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1200 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1202 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1203 + goto out_unregister;
1206 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1207 + if (IS_ERR(vc_cma_class)) {
1208 + rc = PTR_ERR(vc_cma_class);
1209 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1210 + goto out_cdev_del;
1213 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1215 + if (IS_ERR(dev)) {
1216 + rc = PTR_ERR(dev);
1217 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1218 + goto out_class_destroy;
1221 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1222 + if (vc_cma_proc_entry == NULL) {
1224 + LOG_ERR("%s: proc_create failed", __func__);
1225 + goto out_device_destroy;
1228 + vc_cma_inited = 1;
1231 +out_device_destroy:
1232 + device_destroy(vc_cma_class, vc_cma_devnum);
1235 + class_destroy(vc_cma_class);
1236 + vc_cma_class = NULL;
1239 + cdev_del(&vc_cma_cdev);
1242 + unregister_chrdev_region(vc_cma_devnum, 1);
1245 + /* It is tempting to try to clean up by calling
1246 + dma_release_from_contiguous for all allocated chunks, but it isn't
1247 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1248 + VideoCore is already using that memory, so giving it back to Linux
1249 + is likely to be fatal.
1254 +/****************************************************************************
1258 +***************************************************************************/
1260 +static void __exit vc_cma_exit(void)
1262 + LOG_DBG("%s: called", __func__);
1264 + if (vc_cma_inited) {
1265 + remove_proc_entry(DRIVER_NAME, NULL);
1266 + device_destroy(vc_cma_class, vc_cma_devnum);
1267 + class_destroy(vc_cma_class);
1268 + cdev_del(&vc_cma_cdev);
1269 + unregister_chrdev_region(vc_cma_devnum, 1);
1273 +module_init(vc_cma_init);
1274 +module_exit(vc_cma_exit);
1275 +MODULE_LICENSE("GPL");
1276 +MODULE_AUTHOR("Broadcom Corporation");
1278 +++ b/include/linux/broadcom/vc_cma.h
1280 +/*****************************************************************************
1281 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1283 +* Unless you and Broadcom execute a separate written software license
1284 +* agreement governing use of this software, this software is licensed to you
1285 +* under the terms of the GNU General Public License version 2, available at
1286 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1288 +* Notwithstanding the above, under no circumstances may you combine this
1289 +* software in any way with any other Broadcom software provided under a
1290 +* license other than the GPL, without Broadcom's express prior written
1292 +*****************************************************************************/
1294 +#if !defined( VC_CMA_H )
1297 +#include <linux/ioctl.h>
1299 +#define VC_CMA_IOC_MAGIC 0xc5
1301 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1304 +extern void __init vc_cma_early_init(void);
1305 +extern void __init vc_cma_reserve(void);
1308 +#endif /* VC_CMA_H */