1 From 4d96064d4c95d73d22563cea403eccd72e7b0da0 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 | 7 +
26 drivers/char/broadcom/vc_cma/vc_cma.c | 1193 +++++++++++++++++++++++++++++++++
27 include/linux/broadcom/vc_cma.h | 36 +
28 7 files changed, 1255 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
49 obj-$(CONFIG_TILE_SROM) += tile-srom.o
50 obj-$(CONFIG_XILLYBUS) += xillybus/
51 obj-$(CONFIG_POWERNV_OP_PANEL) += powernv-op-panel.o
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-$(CONFIG_BCM_VC_CMA) += -Wall -Wstrict-prototypes -Wno-trigraphs -Werror
79 +ccflags-$(CONFIG_BCM_VC_CMA) += -Iinclude/linux/broadcom -Idrivers/staging/vc04_services -Idrivers/staging/vc04_services/interface/vchi -Idrivers/staging/vc04_services/interface/vchiq_arm
80 +ccflags-$(CONFIG_BCM_VC_CMA) += -D__KERNEL__ -D__linux__
82 +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
84 +vc-cma-objs := vc_cma.o
86 +++ b/drivers/char/broadcom/vc_cma/vc_cma.c
89 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
91 + * Redistribution and use in source and binary forms, with or without
92 + * modification, are permitted provided that the following conditions
94 + * 1. Redistributions of source code must retain the above copyright
95 + * notice, this list of conditions, and the following disclaimer,
96 + * without modification.
97 + * 2. Redistributions in binary form must reproduce the above copyright
98 + * notice, this list of conditions and the following disclaimer in the
99 + * documentation and/or other materials provided with the distribution.
100 + * 3. The names of the above-listed copyright holders may not be used
101 + * to endorse or promote products derived from this software without
102 + * specific prior written permission.
104 + * ALTERNATIVELY, this software may be distributed under the terms of the
105 + * GNU General Public License ("GPL") version 2, as published by the Free
106 + * Software Foundation.
108 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
109 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
110 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
111 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
112 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
113 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
114 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
115 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
116 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
117 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
118 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
121 +#include <linux/kernel.h>
122 +#include <linux/module.h>
123 +#include <linux/kthread.h>
124 +#include <linux/fs.h>
125 +#include <linux/device.h>
126 +#include <linux/cdev.h>
127 +#include <linux/mm.h>
128 +#include <linux/proc_fs.h>
129 +#include <linux/seq_file.h>
130 +#include <linux/dma-mapping.h>
131 +#include <linux/dma-contiguous.h>
132 +#include <linux/platform_device.h>
133 +#include <linux/uaccess.h>
134 +#include <asm/cacheflush.h>
138 +#include "vchiq_util.h"
139 +#include "vchiq_connected.h"
140 +//#include "debug_sym.h"
141 +//#include "vc_mem.h"
143 +#define DRIVER_NAME "vc-cma"
145 +#define LOG_DBG(fmt, ...) \
146 + if (vc_cma_debug) \
147 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
148 +#define LOG_INFO(fmt, ...) \
149 + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
150 +#define LOG_ERR(fmt, ...) \
151 + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
153 +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
154 +#define VC_CMA_VERSION 2
156 +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
157 +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
158 +#define VC_CMA_MAX_PARAMS_PER_MSG \
159 + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
160 +#define VC_CMA_RESERVE_COUNT_MAX 16
162 +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
164 +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
166 +#define loud_error(...) \
167 + LOG_ERR("===== " __VA_ARGS__)
173 + VC_CMA_MSG_ALLOC, /* chunk count */
174 + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
175 + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
176 + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
177 + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
178 + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
179 + VC_CMA_MSG_UPDATE_RESERVE,
184 + unsigned short type;
185 + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
188 +struct vc_cma_reserve_user {
190 + unsigned int reserve;
193 +/* Device (/dev) related variables */
194 +static dev_t vc_cma_devnum;
195 +static struct class *vc_cma_class;
196 +static struct cdev vc_cma_cdev;
197 +static int vc_cma_inited;
198 +static int vc_cma_debug;
201 +static struct proc_dir_entry *vc_cma_proc_entry;
203 +phys_addr_t vc_cma_base;
204 +struct page *vc_cma_base_page;
205 +unsigned int vc_cma_size;
206 +EXPORT_SYMBOL(vc_cma_size);
207 +unsigned int vc_cma_initial;
208 +unsigned int vc_cma_chunks;
209 +unsigned int vc_cma_chunks_used;
210 +unsigned int vc_cma_chunks_reserved;
213 +void *vc_cma_dma_alloc;
214 +unsigned int vc_cma_dma_size;
216 +static int in_loud_error;
218 +unsigned int vc_cma_reserve_total;
219 +unsigned int vc_cma_reserve_count;
220 +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
221 +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
222 +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
224 +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
225 +static struct platform_device vc_cma_device = {
229 + .dma_mask = &vc_cma_dma_mask,
230 + .coherent_dma_mask = DMA_BIT_MASK(32),
234 +static VCHIQ_INSTANCE_T cma_instance;
235 +static VCHIQ_SERVICE_HANDLE_T cma_service;
236 +static VCHIU_QUEUE_T cma_msg_queue;
237 +static struct task_struct *cma_worker;
239 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
240 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
241 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
242 + VCHIQ_HEADER_T * header,
243 + VCHIQ_SERVICE_HANDLE_T service,
244 + void *bulk_userdata);
245 +static void send_vc_msg(unsigned short type,
246 + unsigned short param1, unsigned short param2);
247 +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
249 +static int early_vc_cma_mem(char *p)
251 + unsigned int new_size;
252 + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
253 + vc_cma_size = memparse(p, &p);
254 + vc_cma_initial = vc_cma_size;
256 + vc_cma_size = memparse(p + 1, &p);
258 + vc_cma_base = memparse(p + 1, &p);
260 + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
261 + & ~(VC_CMA_CHUNK_SIZE - 1);
262 + if (new_size > vc_cma_size)
264 + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
265 + & ~(VC_CMA_CHUNK_SIZE - 1);
266 + if (vc_cma_initial > vc_cma_size)
267 + vc_cma_initial = vc_cma_size;
268 + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
269 + & ~(VC_CMA_CHUNK_SIZE - 1);
271 + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
272 + vc_cma_size, (unsigned int)vc_cma_base);
277 +early_param("vc-cma-mem", early_vc_cma_mem);
279 +void __init vc_cma_early_init(void)
281 + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
283 + int rc = platform_device_register(&vc_cma_device);
284 + LOG_DBG("platform_device_register -> %d", rc);
288 +void __init vc_cma_reserve(void)
290 + /* if vc_cma_size is set, then declare vc CMA area of the same
291 + * size from the end of memory
294 + if (dma_declare_contiguous(&vc_cma_device.dev, vc_cma_size,
295 + vc_cma_base, 0) == 0) {
296 + if (!dev_get_cma_area(NULL)) {
297 + /* There is no default CMA area - make this
299 + struct cma *vc_cma_area = dev_get_cma_area(
300 + &vc_cma_device.dev);
301 + dma_contiguous_set_default(vc_cma_area);
302 + LOG_INFO("vc_cma_reserve - using vc_cma as "
303 + "the default contiguous DMA area");
306 + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
307 + vc_cma_size, (unsigned int)vc_cma_base);
311 + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
314 +/****************************************************************************
318 +***************************************************************************/
320 +static int vc_cma_open(struct inode *inode, struct file *file)
328 +/****************************************************************************
332 +***************************************************************************/
334 +static int vc_cma_release(struct inode *inode, struct file *file)
339 + vc_cma_set_reserve(0, current->tgid);
344 +/****************************************************************************
348 +***************************************************************************/
350 +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
358 + case VC_CMA_IOC_RESERVE:
359 + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
364 + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
371 +/****************************************************************************
373 +* File Operations for the driver.
375 +***************************************************************************/
377 +static const struct file_operations vc_cma_fops = {
378 + .owner = THIS_MODULE,
379 + .open = vc_cma_open,
380 + .release = vc_cma_release,
381 + .unlocked_ioctl = vc_cma_ioctl,
384 +/****************************************************************************
388 +***************************************************************************/
390 +static int vc_cma_show_info(struct seq_file *m, void *v)
394 + seq_printf(m, "Videocore CMA:\n");
395 + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
396 + seq_printf(m, " Length : %08x\n", vc_cma_size);
397 + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
398 + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
399 + seq_printf(m, " Chunks : %4d (%d bytes)\n",
400 + (int)vc_cma_chunks,
401 + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
402 + seq_printf(m, " Used : %4d (%d bytes)\n",
403 + (int)vc_cma_chunks_used,
404 + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
405 + seq_printf(m, " Reserved : %4d (%d bytes)\n",
406 + (unsigned int)vc_cma_chunks_reserved,
407 + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
409 + for (i = 0; i < vc_cma_reserve_count; i++) {
410 + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
411 + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
414 + seq_printf(m, " dma_alloc : %p (%d pages)\n",
415 + vc_cma_dma_alloc ? page_address(vc_cma_dma_alloc) : 0,
418 + seq_printf(m, "\n");
423 +static int vc_cma_proc_open(struct inode *inode, struct file *file)
425 + return single_open(file, vc_cma_show_info, NULL);
428 +/****************************************************************************
432 +***************************************************************************/
434 +static int vc_cma_proc_write(struct file *file,
435 + const char __user *buffer,
436 + size_t size, loff_t *ppos)
439 + char input_str[20];
441 + memset(input_str, 0, sizeof(input_str));
443 + if (size > sizeof(input_str)) {
444 + LOG_ERR("%s: input string length too long", __func__);
448 + if (copy_from_user(input_str, buffer, size - 1)) {
449 + LOG_ERR("%s: failed to get input string", __func__);
452 +#define ALLOC_STR "alloc"
453 +#define FREE_STR "free"
454 +#define DEBUG_STR "debug"
455 +#define RESERVE_STR "reserve"
456 +#define DMA_ALLOC_STR "dma_alloc"
457 +#define DMA_FREE_STR "dma_free"
458 + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
460 + char *p = input_str + strlen(ALLOC_STR);
464 + alloc_size = memparse(p, NULL);
465 + LOG_INFO("/proc/vc-cma: alloc %d", alloc_size);
467 + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
468 + alloc_size / VC_CMA_CHUNK_SIZE, 0);
470 + LOG_ERR("invalid size '%s'", p);
472 + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
474 + char *p = input_str + strlen(FREE_STR);
478 + alloc_size = memparse(p, NULL);
479 + LOG_INFO("/proc/vc-cma: free %d", alloc_size);
481 + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
482 + alloc_size / VC_CMA_CHUNK_SIZE, 0);
484 + LOG_ERR("invalid size '%s'", p);
486 + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
487 + char *p = input_str + strlen(DEBUG_STR);
490 + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
492 + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
494 + LOG_INFO("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
496 + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
499 + char *p = input_str + strlen(RESERVE_STR);
502 + alloc_size = memparse(p, NULL);
504 + reserved = vc_cma_set_reserve(alloc_size, current->tgid);
505 + rc = (reserved >= 0) ? size : reserved;
506 + } else if (strncmp(input_str, DMA_ALLOC_STR, strlen(DMA_ALLOC_STR)) == 0) {
508 + char *p = input_str + strlen(DMA_ALLOC_STR);
511 + alloc_size = memparse(p, NULL);
513 + if (vc_cma_dma_alloc) {
514 + dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
516 + vc_cma_dma_alloc = NULL;
517 + vc_cma_dma_size = 0;
519 + vc_cma_dma_alloc = dma_alloc_from_contiguous(NULL, alloc_size, 0);
520 + vc_cma_dma_size = (vc_cma_dma_alloc ? alloc_size : 0);
521 + if (vc_cma_dma_alloc)
522 + LOG_INFO("dma_alloc(%d pages) -> %p", alloc_size, page_address(vc_cma_dma_alloc));
524 + LOG_ERR("dma_alloc(%d pages) failed", alloc_size);
526 + } else if (strncmp(input_str, DMA_FREE_STR, strlen(DMA_FREE_STR)) == 0) {
527 + if (vc_cma_dma_alloc) {
528 + dma_release_from_contiguous(NULL, vc_cma_dma_alloc,
530 + vc_cma_dma_alloc = NULL;
531 + vc_cma_dma_size = 0;
540 +/****************************************************************************
542 +* File Operations for /proc interface.
544 +***************************************************************************/
546 +static const struct file_operations vc_cma_proc_fops = {
547 + .open = vc_cma_proc_open,
549 + .write = vc_cma_proc_write,
550 + .llseek = seq_lseek,
551 + .release = single_release
554 +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
556 + struct vc_cma_reserve_user *user = NULL;
560 + if (down_interruptible(&vc_cma_reserve_mutex))
561 + return -ERESTARTSYS;
563 + for (i = 0; i < vc_cma_reserve_count; i++) {
564 + if (pid == vc_cma_reserve_users[i].pid) {
565 + user = &vc_cma_reserve_users[i];
566 + delta = reserve - user->reserve;
568 + user->reserve = reserve;
570 + /* Remove this entry by copying downwards */
571 + while ((i + 1) < vc_cma_reserve_count) {
572 + user[0].pid = user[1].pid;
573 + user[0].reserve = user[1].reserve;
577 + vc_cma_reserve_count--;
584 + if (reserve && !user) {
585 + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
586 + LOG_ERR("vc-cma: Too many reservations - "
587 + "increase CMA_RESERVE_COUNT_MAX");
588 + up(&vc_cma_reserve_mutex);
591 + user = &vc_cma_reserve_users[vc_cma_reserve_count];
593 + user->reserve = reserve;
595 + vc_cma_reserve_count++;
598 + vc_cma_reserve_total += delta;
600 + send_vc_msg(VC_CMA_MSG_RESERVE,
601 + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
603 + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
605 + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
606 + reserve, pid, vc_cma_reserve_total);
608 + up(&vc_cma_reserve_mutex);
610 + return vc_cma_reserve_total;
613 +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
614 + VCHIQ_HEADER_T * header,
615 + VCHIQ_SERVICE_HANDLE_T service,
616 + void *bulk_userdata)
619 + case VCHIQ_MESSAGE_AVAILABLE:
620 + if (!send_worker_msg(header))
621 + return VCHIQ_RETRY;
623 + case VCHIQ_SERVICE_CLOSED:
624 + LOG_DBG("CMA service closed");
627 + LOG_ERR("Unexpected CMA callback reason %d", reason);
630 + return VCHIQ_SUCCESS;
633 +static void send_vc_msg(unsigned short type,
634 + unsigned short param1, unsigned short param2)
636 + unsigned short msg[] = { type, param1, param2 };
637 + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
638 + VCHIQ_STATUS_T ret;
639 + vchiq_use_service(cma_service);
640 + ret = vchiq_queue_message(cma_service, &elem, 1);
641 + vchiq_release_service(cma_service);
642 + if (ret != VCHIQ_SUCCESS)
643 + LOG_ERR("vchiq_queue_message returned %x", ret);
646 +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
648 + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
650 + vchiu_queue_push(&cma_msg_queue, msg);
651 + up(&vc_cma_worker_queue_push_mutex);
655 +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
658 + for (i = 0; i < num_chunks; i++) {
659 + struct page *chunk;
660 + unsigned int chunk_num;
661 + uint8_t *chunk_addr;
662 + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
664 + chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
666 + VC_CMA_CHUNK_ORDER);
670 + chunk_addr = page_address(chunk);
671 + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
672 + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
676 + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
677 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
678 + VC_CMA_CHUNK_SIZE) != 0);
679 + if (chunk_num >= vc_cma_chunks) {
680 + phys_addr_t _pa = vc_cma_base + vc_cma_size - 1;
681 + LOG_ERR("%s: ===============================",
683 + LOG_ERR("%s: chunk phys %x, vc_cma %pa-%pa - "
684 + "bad SPARSEMEM configuration?",
685 + __func__, (unsigned int)page_to_phys(chunk),
686 + &vc_cma_base, &_pa);
687 + LOG_ERR("%s: dev->cma_area = %p", __func__,
688 + (void*)0/*vc_cma_device.dev.cma_area*/);
689 + LOG_ERR("%s: ===============================",
693 + reply->params[i] = chunk_num;
694 + vc_cma_chunks_used++;
697 + if (i < num_chunks) {
698 + LOG_ERR("%s: dma_alloc_from_contiguous failed "
699 + "for %x bytes (alloc %d of %d, %d free)",
700 + __func__, VC_CMA_CHUNK_SIZE, i,
701 + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
705 + LOG_DBG("CMA allocated %d chunks -> %d used",
706 + num_chunks, vc_cma_chunks_used);
707 + reply->type = VC_CMA_MSG_ALLOCATED;
710 + VCHIQ_ELEMENT_T elem = {
712 + offsetof(struct cma_msg, params[0]) +
713 + num_chunks * sizeof(reply->params[0])
715 + VCHIQ_STATUS_T ret;
716 + vchiq_use_service(cma_service);
717 + ret = vchiq_queue_message(cma_service, &elem, 1);
718 + vchiq_release_service(cma_service);
719 + if (ret != VCHIQ_SUCCESS)
720 + LOG_ERR("vchiq_queue_message return " "%x", ret);
726 +static int cma_worker_proc(void *param)
728 + static struct cma_msg reply;
732 + VCHIQ_HEADER_T *msg;
733 + static struct cma_msg msg_copy;
734 + struct cma_msg *cma_msg = &msg_copy;
735 + int type, msg_size;
737 + msg = vchiu_queue_pop(&cma_msg_queue);
738 + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
739 + msg_size = msg->size;
740 + memcpy(&msg_copy, msg->data, msg_size);
741 + type = cma_msg->type;
742 + vchiq_release_message(cma_service, msg);
746 + if (type == VC_CMA_MSG_QUIT)
748 + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
758 + case VC_CMA_MSG_ALLOC:{
759 + int num_chunks, free_chunks;
760 + num_chunks = cma_msg->params[0];
762 + vc_cma_chunks - vc_cma_chunks_used;
763 + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
764 + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
766 + ("CMA_MSG_ALLOC - chunk count (%d) "
767 + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
769 + VC_CMA_MAX_PARAMS_PER_MSG);
770 + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
773 + if (num_chunks > free_chunks) {
775 + ("CMA_MSG_ALLOC - chunk count (%d) "
776 + "exceeds free chunks (%d)",
777 + num_chunks, free_chunks);
778 + num_chunks = free_chunks;
781 + vc_cma_alloc_chunks(num_chunks, &reply);
785 + case VC_CMA_MSG_FREE:{
788 + offsetof(struct cma_msg,
790 + sizeof(cma_msg->params[0]);
792 + BUG_ON(chunk_count <= 0);
794 + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
795 + chunk_count, cma_msg->params[0]);
796 + for (i = 0; i < chunk_count; i++) {
797 + int chunk_num = cma_msg->params[i];
798 + struct page *page = vc_cma_base_page +
799 + chunk_num * PAGES_PER_CHUNK;
800 + if (chunk_num >= vc_cma_chunks) {
802 + ("CMA_MSG_FREE - chunk %d of %d"
803 + " (value %x) exceeds maximum "
804 + "(%x)", i, chunk_count,
806 + vc_cma_chunks - 1);
810 + if (!dma_release_from_contiguous
811 + (&vc_cma_device.dev, page,
812 + PAGES_PER_CHUNK)) {
813 + phys_addr_t _pa = page_to_phys(page);
815 + ("CMA_MSG_FREE - failed to "
816 + "release chunk %d (phys %pa, "
817 + "page %x)", chunk_num,
819 + (unsigned int)page);
821 + vc_cma_chunks_used--;
823 + LOG_DBG("CMA released %d chunks -> %d used",
824 + i, vc_cma_chunks_used);
828 + case VC_CMA_MSG_UPDATE_RESERVE:{
829 + int chunks_needed =
830 + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
832 + / VC_CMA_CHUNK_SIZE) -
833 + vc_cma_chunks_reserved;
836 + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
839 + /* Cap the reservations to what is available */
840 + if (chunks_needed > 0) {
841 + if (chunks_needed >
843 + vc_cma_chunks_used))
846 + vc_cma_chunks_used);
849 + vc_cma_alloc_chunks(chunks_needed,
854 + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
856 + vc_cma_chunks_reserved += chunks_needed;
861 + LOG_ERR("unexpected msg type %d", type);
866 + LOG_DBG("quitting...");
870 +/****************************************************************************
872 +* vc_cma_connected_init
874 +* This function is called once the videocore has been connected.
876 +***************************************************************************/
878 +static void vc_cma_connected_init(void)
880 + VCHIQ_SERVICE_PARAMS_T service_params;
882 + LOG_DBG("vc_cma_connected_init");
884 + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
885 + LOG_ERR("could not create CMA msg queue");
889 + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
890 + goto fail_vchiq_init;
892 + vchiq_connect(cma_instance);
894 + service_params.fourcc = VC_CMA_FOURCC;
895 + service_params.callback = cma_service_callback;
896 + service_params.userdata = NULL;
897 + service_params.version = VC_CMA_VERSION;
898 + service_params.version_min = VC_CMA_VERSION;
900 + if (vchiq_open_service(cma_instance, &service_params,
901 + &cma_service) != VCHIQ_SUCCESS) {
902 + LOG_ERR("failed to open service - already in use?");
903 + goto fail_vchiq_open;
906 + vchiq_release_service(cma_service);
908 + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
910 + LOG_ERR("could not create CMA worker thread");
913 + set_user_nice(cma_worker, -20);
914 + wake_up_process(cma_worker);
919 + vchiq_close_service(cma_service);
921 + vchiq_shutdown(cma_instance);
923 + vchiu_queue_delete(&cma_msg_queue);
929 +loud_error_header(void)
934 + LOG_ERR("============================================================"
935 + "================");
936 + LOG_ERR("============================================================"
937 + "================");
944 +loud_error_footer(void)
946 + if (!in_loud_error)
950 + LOG_ERR("============================================================"
951 + "================");
952 + LOG_ERR("============================================================"
953 + "================");
959 +static int check_cma_config(void) { return 1; }
962 +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
963 + const char *symbol,
964 + void *buf, size_t bufsize)
966 + VC_MEM_ADDR_T vcMemAddr;
971 + if (!LookupVideoCoreSymbol(handle, symbol,
974 + loud_error_header();
976 + "failed to find VC symbol \"%s\".",
978 + loud_error_footer();
982 + if (vcMemSize != bufsize) {
983 + loud_error_header();
985 + "VC symbol \"%s\" is the wrong size.",
987 + loud_error_footer();
991 + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
992 + vcMapAddr += mm_vc_mem_phys_addr;
993 + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
994 + if (mapAddr == 0) {
995 + loud_error_header();
997 + "failed to ioremap \"%s\" @ 0x%x "
998 + "(phys: 0x%x, size: %u).",
1000 + (unsigned int)vcMapAddr,
1001 + (unsigned int)vcMemAddr,
1002 + (unsigned int)vcMemSize);
1003 + loud_error_footer();
1007 + memcpy(buf, mapAddr, bufsize);
1015 +check_cma_config(void)
1017 + VC_MEM_ACCESS_HANDLE_T mem_hndl;
1018 + VC_MEM_ADDR_T mempool_start;
1019 + VC_MEM_ADDR_T mempool_end;
1020 + VC_MEM_ADDR_T mempool_offline_start;
1021 + VC_MEM_ADDR_T mempool_offline_end;
1022 + VC_MEM_ADDR_T cam_alloc_base;
1023 + VC_MEM_ADDR_T cam_alloc_size;
1024 + VC_MEM_ADDR_T cam_alloc_end;
1027 + if (OpenVideoCoreMemory(&mem_hndl) != 0)
1030 + /* Read the relevant VideoCore variables */
1031 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
1033 + sizeof(mempool_start)))
1036 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
1038 + sizeof(mempool_end)))
1041 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
1042 + &mempool_offline_start,
1043 + sizeof(mempool_offline_start)))
1046 + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
1047 + &mempool_offline_end,
1048 + sizeof(mempool_offline_end)))
1051 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
1053 + sizeof(cam_alloc_base)))
1056 + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
1058 + sizeof(cam_alloc_size)))
1061 + cam_alloc_end = cam_alloc_base + cam_alloc_size;
1065 + /* Now the sanity checks */
1066 + if (!mempool_offline_start)
1067 + mempool_offline_start = mempool_start;
1068 + if (!mempool_offline_end)
1069 + mempool_offline_end = mempool_end;
1071 + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
1072 + loud_error_header();
1074 + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
1075 + "vc_cma_base(%x)",
1076 + mempool_offline_start,
1077 + VCADDR_TO_PHYSADDR(mempool_offline_start),
1082 + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
1083 + (vc_cma_base + vc_cma_size)) {
1084 + loud_error_header();
1086 + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
1087 + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
1088 + mempool_offline_start,
1089 + VCADDR_TO_PHYSADDR(mempool_offline_end),
1090 + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
1094 + if (mempool_end < mempool_start) {
1095 + loud_error_header();
1097 + "__MEMPOOL_END(%x) must not be before "
1098 + "__MEMPOOL_START(%x)",
1104 + if (mempool_offline_end < mempool_offline_start) {
1105 + loud_error_header();
1107 + "__MEMPOOL_OFFLINE_END(%x) must not be before "
1108 + "__MEMPOOL_OFFLINE_START(%x)",
1109 + mempool_offline_end,
1110 + mempool_offline_start);
1114 + if (mempool_offline_start < mempool_start) {
1115 + loud_error_header();
1117 + "__MEMPOOL_OFFLINE_START(%x) must not be before "
1118 + "__MEMPOOL_START(%x)",
1119 + mempool_offline_start,
1124 + if (mempool_offline_end > mempool_end) {
1125 + loud_error_header();
1127 + "__MEMPOOL_OFFLINE_END(%x) must not be after "
1128 + "__MEMPOOL_END(%x)",
1129 + mempool_offline_end,
1134 + if ((cam_alloc_base < mempool_end) &&
1135 + (cam_alloc_end > mempool_start)) {
1136 + loud_error_header();
1138 + "cam_alloc pool(%x-%x) overlaps "
1140 + cam_alloc_base, cam_alloc_end,
1141 + mempool_start, mempool_end);
1145 + loud_error_footer();
1148 + CloseVideoCoreMemory(mem_hndl);
1155 +static int vc_cma_init(void)
1158 + struct device *dev;
1160 + if (!check_cma_config())
1163 + LOG_INFO("vc-cma: Videocore CMA driver");
1164 + LOG_INFO("vc-cma: vc_cma_base = %pa", &vc_cma_base);
1165 + LOG_INFO("vc-cma: vc_cma_size = 0x%08x (%u MiB)",
1166 + vc_cma_size, vc_cma_size / (1024 * 1024));
1167 + LOG_INFO("vc-cma: vc_cma_initial = 0x%08x (%u MiB)",
1168 + vc_cma_initial, vc_cma_initial / (1024 * 1024));
1170 + vc_cma_base_page = phys_to_page(vc_cma_base);
1172 + if (vc_cma_chunks) {
1173 + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
1175 + for (vc_cma_chunks_used = 0;
1176 + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
1177 + struct page *chunk;
1178 + chunk = dma_alloc_from_contiguous(&vc_cma_device.dev,
1180 + VC_CMA_CHUNK_ORDER);
1183 + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
1184 + VC_CMA_CHUNK_SIZE) != 0);
1186 + if (vc_cma_chunks_used != chunks_needed) {
1187 + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
1188 + "bytes, allocation %d of %d)",
1189 + __func__, VC_CMA_CHUNK_SIZE,
1190 + vc_cma_chunks_used, chunks_needed);
1194 + vchiq_add_connected_callback(vc_cma_connected_init);
1197 + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
1199 + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
1203 + cdev_init(&vc_cma_cdev, &vc_cma_fops);
1204 + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
1206 + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
1207 + goto out_unregister;
1210 + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
1211 + if (IS_ERR(vc_cma_class)) {
1212 + rc = PTR_ERR(vc_cma_class);
1213 + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
1214 + goto out_cdev_del;
1217 + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
1219 + if (IS_ERR(dev)) {
1220 + rc = PTR_ERR(dev);
1221 + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
1222 + goto out_class_destroy;
1225 + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
1226 + if (vc_cma_proc_entry == NULL) {
1228 + LOG_ERR("%s: proc_create failed", __func__);
1229 + goto out_device_destroy;
1232 + vc_cma_inited = 1;
1235 +out_device_destroy:
1236 + device_destroy(vc_cma_class, vc_cma_devnum);
1239 + class_destroy(vc_cma_class);
1240 + vc_cma_class = NULL;
1243 + cdev_del(&vc_cma_cdev);
1246 + unregister_chrdev_region(vc_cma_devnum, 1);
1249 + /* It is tempting to try to clean up by calling
1250 + dma_release_from_contiguous for all allocated chunks, but it isn't
1251 + a very safe thing to do. If vc_cma_initial is non-zero it is because
1252 + VideoCore is already using that memory, so giving it back to Linux
1253 + is likely to be fatal.
1258 +/****************************************************************************
1262 +***************************************************************************/
1264 +static void __exit vc_cma_exit(void)
1266 + LOG_DBG("%s: called", __func__);
1268 + if (vc_cma_inited) {
1269 + remove_proc_entry(DRIVER_NAME, NULL);
1270 + device_destroy(vc_cma_class, vc_cma_devnum);
1271 + class_destroy(vc_cma_class);
1272 + cdev_del(&vc_cma_cdev);
1273 + unregister_chrdev_region(vc_cma_devnum, 1);
1277 +module_init(vc_cma_init);
1278 +module_exit(vc_cma_exit);
1279 +MODULE_LICENSE("GPL");
1280 +MODULE_AUTHOR("Broadcom Corporation");
1282 +++ b/include/linux/broadcom/vc_cma.h
1284 +/*****************************************************************************
1285 +* Copyright 2012 Broadcom Corporation. All rights reserved.
1287 +* Unless you and Broadcom execute a separate written software license
1288 +* agreement governing use of this software, this software is licensed to you
1289 +* under the terms of the GNU General Public License version 2, available at
1290 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1292 +* Notwithstanding the above, under no circumstances may you combine this
1293 +* software in any way with any other Broadcom software provided under a
1294 +* license other than the GPL, without Broadcom's express prior written
1296 +*****************************************************************************/
1298 +#if !defined( VC_CMA_H )
1301 +#include <linux/ioctl.h>
1303 +#define VC_CMA_IOC_MAGIC 0xc5
1305 +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
1309 +#ifdef CONFIG_BCM_VC_CMA
1310 +void vc_cma_early_init(void);
1311 +void vc_cma_reserve(void);
1313 +static inline void vc_cma_early_init(void) { }
1314 +static inline void vc_cma_reserve(void) { }
1319 +#endif /* VC_CMA_H */