2 * drivers/staging/android/ion/rockchip/rockchip_ion_snapshot.c
4 * Copyright (C) 2011-2014 ROCKCHIP, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) "ion_snapshot: " fmt
12 #include <linux/kernel.h>
13 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/vmalloc.h>
17 #include <asm/uaccess.h>
18 #include <linux/debugfs.h>
20 #define LOG_BUF_LEN (1 << CONFIG_ION_SNAPSHOT_BUF_SHIFT)
21 #define LOG_BUF_PAGE_ORDER (CONFIG_ION_SNAPSHOT_BUF_SHIFT - PAGE_SHIFT)
23 static char last_ion_buf[LOG_BUF_LEN];
24 // snapshot for current
25 static char* ion_snapshot_buf;
27 static ssize_t last_ion_read(struct file *file, char __user *buf,
28 size_t len, loff_t *offset)
33 if (pos >= LOG_BUF_LEN || last_ion_buf[0]==0)
36 count = min(len, (size_t)(LOG_BUF_LEN - pos));
37 if (copy_to_user(buf, &last_ion_buf[pos], count))
44 static const struct file_operations last_ion_fops = {
46 .read = last_ion_read,
49 static ssize_t ion_snapshot_read(struct file *file, char __user *buf,
50 size_t len, loff_t *offset)
55 if (pos >= LOG_BUF_LEN || ion_snapshot_buf[0]==0)
58 count = min(len, (size_t)(LOG_BUF_LEN - pos));
59 if (copy_to_user(buf, &ion_snapshot_buf[pos], count))
66 static const struct file_operations ion_snapshot_fops = {
68 .read = ion_snapshot_read,
71 char *rockchip_ion_snapshot_get(size_t *size)
74 return ion_snapshot_buf;
77 int rockchip_ion_snapshot_debugfs(struct dentry* root)
79 struct dentry* last_ion_dentry;
80 struct dentry* ion_snapshot_dentry;
82 last_ion_dentry = debugfs_create_file("last_ion", 0664,
84 NULL, &last_ion_fops);
85 if (!last_ion_dentry) {
87 path = dentry_path(root, buf, 256);
88 pr_err("Failed to create client debugfs at %s/%s\n",
92 ion_snapshot_dentry = debugfs_create_file("ion_snapshot", 0664,
94 NULL, &ion_snapshot_fops);
95 if (!ion_snapshot_dentry) {
97 path = dentry_path(root, buf, 256);
98 pr_err("Failed to create client debugfs at %s/%s\n",
99 path, "ion_snapshot");
105 static void * __init last_ion_vmap(phys_addr_t start, unsigned int page_count)
107 struct page *pages[page_count + 1];
110 for (i = 0; i < page_count; i++) {
111 phys_addr_t addr = start + i * PAGE_SIZE;
112 pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
114 pages[page_count] = pfn_to_page(start >> PAGE_SHIFT);
115 return vmap(pages, page_count + 1, VM_MAP, pgprot_noncached(PAGE_KERNEL));
118 static int __init rockchip_ion_snapshot_init(void)
122 log_buf = (char *)__get_free_pages(GFP_KERNEL, LOG_BUF_PAGE_ORDER);
124 pr_err("failed to __get_free_pages(%d)\n", LOG_BUF_PAGE_ORDER);
128 ion_snapshot_buf = last_ion_vmap(virt_to_phys(log_buf), 1 << LOG_BUF_PAGE_ORDER);
129 if (!ion_snapshot_buf) {
130 pr_err("failed to map %d pages at 0x%lx\n", 1 << LOG_BUF_PAGE_ORDER,
131 (unsigned long)virt_to_phys(log_buf));
135 pr_info("0x%lx map to 0x%p and copy to 0x%p (version 0.1)\n",
136 (unsigned long)virt_to_phys(log_buf), ion_snapshot_buf,
139 memcpy(last_ion_buf, ion_snapshot_buf, LOG_BUF_LEN);
140 memset(ion_snapshot_buf, 0, LOG_BUF_LEN);
145 postcore_initcall(rockchip_ion_snapshot_init);