Anji Jonnala | 04729cd | 2012-10-12 07:24:49 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | #include <linux/debugfs.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | |
| 27 | #include <mach/msm_iomap.h> |
| 28 | #include "rpm_stats.h" |
| 29 | #define MSG_RAM_SIZE_PER_MASTER 32 |
| 30 | |
| 31 | enum { |
| 32 | NUMSHUTDOWNS, |
| 33 | ACTIVECORES, |
| 34 | MASTER_ID_MAX, |
| 35 | }; |
| 36 | |
| 37 | static char *msm_rpm_master_stats_id_labels[MASTER_ID_MAX] = { |
| 38 | [NUMSHUTDOWNS] = "num_shutdowns", |
| 39 | [ACTIVECORES] = "active_cores", |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | struct msm_rpm_master_stats { |
| 44 | unsigned long numshutdowns; |
| 45 | unsigned long active_cores; |
| 46 | }; |
| 47 | |
| 48 | struct msm_rpm_master_stats_private_data { |
| 49 | void __iomem *reg_base; |
| 50 | u32 len; |
| 51 | char **master_names; |
| 52 | u32 nomasters; |
| 53 | char buf[256]; |
| 54 | struct msm_rpm_master_stats_platform_data *platform_data; |
| 55 | }; |
| 56 | |
| 57 | static int msm_rpm_master_stats_file_close(struct inode *inode, |
| 58 | struct file *file) |
| 59 | { |
| 60 | struct msm_rpm_master_stats_private_data *private = file->private_data; |
| 61 | |
| 62 | if (private->reg_base) |
| 63 | iounmap(private->reg_base); |
| 64 | kfree(file->private_data); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int msm_rpm_master_copy_stats( |
| 70 | struct msm_rpm_master_stats_private_data *pdata) |
| 71 | { |
| 72 | struct msm_rpm_master_stats record; |
| 73 | static int nomasters; |
| 74 | int count; |
| 75 | static DEFINE_MUTEX(msm_rpm_master_stats_mutex); |
| 76 | int j = 0; |
| 77 | |
| 78 | mutex_lock(&msm_rpm_master_stats_mutex); |
| 79 | /* |
| 80 | * iterrate possible nomasters times. |
| 81 | * 8960, 8064 have 5 masters. |
| 82 | * 8930 has 4 masters. |
| 83 | * 9x15 has 3 masters. |
| 84 | */ |
| 85 | if (nomasters > pdata->nomasters - 1) { |
| 86 | nomasters = 0; |
| 87 | mutex_unlock(&msm_rpm_master_stats_mutex); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | record.numshutdowns = readl_relaxed(pdata->reg_base + |
| 92 | (nomasters * MSG_RAM_SIZE_PER_MASTER)); |
| 93 | record.active_cores = readl_relaxed(pdata->reg_base + |
| 94 | (nomasters * MSG_RAM_SIZE_PER_MASTER + 4)); |
| 95 | |
| 96 | count = snprintf(pdata->buf, sizeof(pdata->buf), |
| 97 | "%s\n\t%s:%lu\n\t%s:%lu\n", |
| 98 | pdata->master_names[nomasters], |
| 99 | msm_rpm_master_stats_id_labels[0], |
| 100 | record.numshutdowns, |
| 101 | msm_rpm_master_stats_id_labels[1], |
| 102 | record.active_cores); |
| 103 | |
| 104 | j = find_first_bit(&record.active_cores, BITS_PER_LONG); |
| 105 | while (j < BITS_PER_LONG) { |
| 106 | count += snprintf(pdata->buf + count, |
| 107 | sizeof(pdata->buf) - count, |
| 108 | "\t\tcore%d\n", j); |
| 109 | j = find_next_bit(&record.active_cores, |
| 110 | BITS_PER_LONG, j + 1); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | nomasters++; |
| 115 | mutex_unlock(&msm_rpm_master_stats_mutex); |
| 116 | return count; |
| 117 | } |
| 118 | |
| 119 | static int msm_rpm_master_stats_file_read(struct file *file, char __user *bufu, |
| 120 | size_t count, loff_t *ppos) |
| 121 | { |
| 122 | struct msm_rpm_master_stats_private_data *prvdata; |
| 123 | struct msm_rpm_master_stats_platform_data *pdata; |
| 124 | |
| 125 | prvdata = file->private_data; |
| 126 | if (!prvdata) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | pdata = prvdata->platform_data; |
| 130 | if (!pdata) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | if (!bufu || count < 0) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | if ((*ppos <= pdata->phys_size)) { |
| 137 | prvdata->len = msm_rpm_master_copy_stats(prvdata); |
| 138 | *ppos = 0; |
| 139 | } |
| 140 | |
| 141 | return simple_read_from_buffer(bufu, count, ppos, |
| 142 | prvdata->buf, prvdata->len); |
| 143 | } |
| 144 | |
| 145 | static int msm_rpm_master_stats_file_open(struct inode *inode, |
| 146 | struct file *file) |
| 147 | { |
| 148 | struct msm_rpm_master_stats_private_data *prvdata; |
| 149 | struct msm_rpm_master_stats_platform_data *pdata; |
| 150 | |
| 151 | pdata = inode->i_private; |
| 152 | |
| 153 | file->private_data = |
| 154 | kmalloc(sizeof(struct msm_rpm_master_stats_private_data), |
| 155 | GFP_KERNEL); |
| 156 | |
| 157 | if (!file->private_data) |
| 158 | return -ENOMEM; |
| 159 | prvdata = file->private_data; |
| 160 | |
| 161 | prvdata->reg_base = ioremap(pdata->phys_addr_base, |
| 162 | pdata->phys_size); |
| 163 | if (!prvdata->reg_base) { |
| 164 | kfree(file->private_data); |
| 165 | prvdata = NULL; |
| 166 | pr_err("%s: ERROR could not ioremap start=%p, len=%u\n", |
| 167 | __func__, (void *)pdata->phys_addr_base, |
| 168 | pdata->phys_size); |
| 169 | return -EBUSY; |
| 170 | } |
| 171 | |
| 172 | prvdata->len = 0; |
| 173 | prvdata->nomasters = pdata->nomasters; |
| 174 | prvdata->master_names = pdata->masters; |
| 175 | prvdata->platform_data = pdata; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static const struct file_operations msm_rpm_master_stats_fops = { |
| 180 | .owner = THIS_MODULE, |
| 181 | .open = msm_rpm_master_stats_file_open, |
| 182 | .read = msm_rpm_master_stats_file_read, |
| 183 | .release = msm_rpm_master_stats_file_close, |
| 184 | .llseek = no_llseek, |
| 185 | }; |
| 186 | |
| 187 | static int __devinit msm_rpm_master_stats_probe(struct platform_device *pdev) |
| 188 | { |
| 189 | struct dentry *dent; |
| 190 | struct msm_rpm_master_stats_platform_data *pdata; |
| 191 | struct resource *res; |
| 192 | |
| 193 | pdata = pdev->dev.platform_data; |
| 194 | if (!pdata) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 198 | pdata->phys_addr_base = res->start; |
| 199 | pdata->phys_size = resource_size(res); |
| 200 | |
| 201 | dent = debugfs_create_file("rpm_master_stats", S_IRUGO, NULL, |
| 202 | pdev->dev.platform_data, &msm_rpm_master_stats_fops); |
| 203 | |
| 204 | if (!dent) { |
| 205 | pr_err("%s: ERROR debugfs_create_file failed\n", __func__); |
| 206 | return -ENOMEM; |
| 207 | } |
| 208 | platform_set_drvdata(pdev, dent); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int __devexit msm_rpm_master_stats_remove(struct platform_device *pdev) |
| 213 | { |
| 214 | struct dentry *dent; |
| 215 | |
| 216 | dent = platform_get_drvdata(pdev); |
| 217 | debugfs_remove(dent); |
| 218 | platform_set_drvdata(pdev, NULL); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static struct platform_driver msm_rpm_master_stats_driver = { |
| 223 | .probe = msm_rpm_master_stats_probe, |
| 224 | .remove = __devexit_p(msm_rpm_master_stats_remove), |
| 225 | .driver = { |
| 226 | .name = "msm_rpm_master_stat", |
| 227 | .owner = THIS_MODULE, |
| 228 | }, |
| 229 | }; |
| 230 | |
| 231 | static int __init msm_rpm_master_stats_init(void) |
| 232 | { |
| 233 | return platform_driver_register(&msm_rpm_master_stats_driver); |
| 234 | } |
| 235 | |
| 236 | static void __exit msm_rpm_master_stats_exit(void) |
| 237 | { |
| 238 | platform_driver_unregister(&msm_rpm_master_stats_driver); |
| 239 | } |
| 240 | |
| 241 | module_init(msm_rpm_master_stats_init); |
| 242 | module_exit(msm_rpm_master_stats_exit); |
| 243 | |
| 244 | MODULE_LICENSE("GPL v2"); |
| 245 | MODULE_DESCRIPTION("MSM RPM Master Statistics driver"); |
| 246 | MODULE_VERSION("1.0"); |
| 247 | MODULE_ALIAS("platform:msm_master_stat_log"); |