Naveen Ramaraj | 51f5e8b | 2012-04-09 15:58:40 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2012, Code Aurora Forum. 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 <mach/ocmem_priv.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/rbtree.h> |
| 19 | #include <linux/genalloc.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/debugfs.h> |
| 24 | #include <linux/seq_file.h> |
| 25 | |
| 26 | struct ocmem_partition { |
| 27 | const char *name; |
| 28 | int id; |
| 29 | unsigned long p_start; |
| 30 | unsigned long p_size; |
| 31 | unsigned long p_min; |
| 32 | }; |
| 33 | |
| 34 | struct ocmem_plat_data { |
| 35 | void __iomem *vbase; |
| 36 | unsigned long size; |
| 37 | unsigned long base; |
| 38 | struct ocmem_partition *parts; |
| 39 | int nr_parts; |
| 40 | }; |
| 41 | |
| 42 | struct ocmem_zone zones[OCMEM_CLIENT_MAX]; |
| 43 | |
| 44 | struct ocmem_zone *get_zone(unsigned id) |
| 45 | { |
| 46 | if (id < OCMEM_GRAPHICS || id >= OCMEM_CLIENT_MAX) |
| 47 | return NULL; |
| 48 | else |
| 49 | return &zones[id]; |
| 50 | } |
| 51 | |
| 52 | static struct ocmem_plat_data *ocmem_pdata; |
| 53 | |
| 54 | #define CLIENT_NAME_MAX 10 |
| 55 | /* Must be in sync with enum ocmem_client */ |
| 56 | static const char *client_names[OCMEM_CLIENT_MAX] = { |
| 57 | "graphics", |
| 58 | "video", |
| 59 | "camera", |
| 60 | "hp_audio", |
| 61 | "voice", |
| 62 | "lp_audio", |
| 63 | "sensors", |
| 64 | "blast", |
| 65 | }; |
| 66 | |
| 67 | struct ocmem_quota_table { |
| 68 | const char *name; |
| 69 | int id; |
| 70 | unsigned long start; |
| 71 | unsigned long size; |
| 72 | unsigned long min; |
| 73 | }; |
| 74 | |
| 75 | /* This static table will go away with device tree support */ |
| 76 | static struct ocmem_quota_table qt[OCMEM_CLIENT_MAX] = { |
| 77 | /* name, id, start, size, min */ |
| 78 | { "graphics", OCMEM_GRAPHICS, 0x0, 0x100000, 0x80000}, |
| 79 | { "video", OCMEM_VIDEO, 0x100000, 0x80000, 0x55000}, |
| 80 | { "camera", OCMEM_CAMERA, 0x0, 0x0, 0x0}, |
| 81 | { "voice", OCMEM_VOICE, 0x0, 0x0, 0x0 }, |
| 82 | { "hp_audio", OCMEM_HP_AUDIO, 0x0, 0x0, 0x0}, |
| 83 | { "lp_audio", OCMEM_LP_AUDIO, 0x80000, 0xA0000, 0xA0000}, |
| 84 | { "blast", OCMEM_BLAST, 0x120000, 0x20000, 0x20000}, |
| 85 | { "sensors", OCMEM_SENSORS, 0x140000, 0x40000, 0x40000}, |
| 86 | }; |
| 87 | |
| 88 | static inline int get_id(const char *name) |
| 89 | { |
| 90 | int i = 0; |
| 91 | for (i = 0 ; i < OCMEM_CLIENT_MAX; i++) { |
| 92 | if (strncmp(name, client_names[i], CLIENT_NAME_MAX) == 0) |
| 93 | return i; |
| 94 | } |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
| 98 | static struct ocmem_plat_data *parse_static_config(struct platform_device *pdev) |
| 99 | { |
| 100 | struct ocmem_plat_data *pdata = NULL; |
| 101 | struct ocmem_partition *parts = NULL; |
| 102 | struct device *dev = &pdev->dev; |
| 103 | int nr_parts; |
| 104 | int i; |
| 105 | int j; |
| 106 | |
| 107 | pdata = devm_kzalloc(dev, sizeof(struct ocmem_plat_data), |
| 108 | GFP_KERNEL); |
| 109 | |
| 110 | if (!pdata) { |
| 111 | dev_err(dev, "Unable to allocate memory for" |
| 112 | " platform data\n"); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | for (i = 0 ; i < ARRAY_SIZE(qt); i++) |
| 117 | if (qt[i].size != 0x0) |
| 118 | nr_parts++; |
| 119 | |
| 120 | if (nr_parts == 0x0) { |
| 121 | dev_err(dev, "No valid ocmem partitions\n"); |
| 122 | return NULL; |
| 123 | } else |
| 124 | dev_info(dev, "Total partitions = %d\n", nr_parts); |
| 125 | |
| 126 | parts = devm_kzalloc(dev, sizeof(struct ocmem_partition) * nr_parts, |
| 127 | GFP_KERNEL); |
| 128 | |
| 129 | if (!parts) { |
| 130 | dev_err(dev, "Unable to allocate memory for" |
| 131 | " partition data\n"); |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | for (i = 0, j = 0; i < ARRAY_SIZE(qt); i++) { |
| 136 | if (qt[i].size == 0x0) { |
| 137 | dev_dbg(dev, "Skipping creation of pool for %s\n", |
| 138 | qt[i].name); |
| 139 | continue; |
| 140 | } |
| 141 | parts[j].id = qt[i].id; |
| 142 | parts[j].p_size = qt[i].size; |
| 143 | parts[j].p_start = qt[i].start; |
| 144 | parts[j].p_min = qt[i].min; |
| 145 | j++; |
| 146 | } |
| 147 | BUG_ON(j != nr_parts); |
| 148 | pdata->nr_parts = nr_parts; |
| 149 | pdata->parts = parts; |
| 150 | pdata->base = OCMEM_PHYS_BASE; |
| 151 | pdata->size = OCMEM_PHYS_SIZE; |
| 152 | return pdata; |
| 153 | } |
| 154 | |
| 155 | static struct ocmem_plat_data *parse_dt_config(struct platform_device *pdev) |
| 156 | { |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | static int ocmem_zone_init(struct platform_device *pdev) |
| 161 | { |
| 162 | |
| 163 | int ret = -1; |
| 164 | int i = 0; |
| 165 | unsigned active_zones = 0; |
| 166 | |
| 167 | struct ocmem_zone *zone = NULL; |
| 168 | struct ocmem_zone_ops *z_ops = NULL; |
| 169 | struct device *dev = &pdev->dev; |
| 170 | unsigned long start; |
| 171 | struct ocmem_plat_data *pdata = NULL; |
| 172 | |
| 173 | pdata = platform_get_drvdata(pdev); |
| 174 | |
| 175 | for (i = 0; i < pdata->nr_parts; i++) { |
| 176 | struct ocmem_partition *part = &pdata->parts[i]; |
| 177 | zone = get_zone(part->id); |
| 178 | |
| 179 | dev_dbg(dev, "Partition %d, start %lx, size %lx for %s\n", |
| 180 | i, part->p_start, part->p_size, |
| 181 | client_names[part->id]); |
| 182 | |
| 183 | if (part->p_size > pdata->size) { |
| 184 | dev_alert(dev, "Quota > ocmem_size for id:%d\n", |
| 185 | part->id); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | zone->z_pool = gen_pool_create(PAGE_SHIFT, -1); |
| 190 | |
| 191 | if (!zone->z_pool) { |
| 192 | dev_alert(dev, "Creating pool failed for id:%d\n", |
| 193 | part->id); |
| 194 | return -EBUSY; |
| 195 | } |
| 196 | |
| 197 | start = pdata->base + part->p_start; |
| 198 | ret = gen_pool_add(zone->z_pool, start, |
| 199 | part->p_size, -1); |
| 200 | |
| 201 | if (ret < 0) { |
| 202 | gen_pool_destroy(zone->z_pool); |
| 203 | dev_alert(dev, "Unable to back pool %d with " |
| 204 | "buffer:%lx\n", part->id, part->p_size); |
| 205 | return -EBUSY; |
| 206 | } |
| 207 | |
| 208 | /* Initialize zone allocators */ |
| 209 | z_ops = devm_kzalloc(dev, sizeof(struct ocmem_zone_ops), |
| 210 | GFP_KERNEL); |
| 211 | if (!z_ops) { |
| 212 | pr_alert("ocmem: Unable to allocate memory for" |
| 213 | "zone ops:%d\n", i); |
| 214 | return -EBUSY; |
| 215 | } |
| 216 | |
| 217 | /* Initialize zone parameters */ |
| 218 | zone->z_start = start; |
| 219 | zone->z_head = zone->z_start; |
| 220 | zone->z_end = start + part->p_size; |
| 221 | zone->z_tail = zone->z_end; |
| 222 | zone->z_free = part->p_size; |
| 223 | zone->owner = part->id; |
| 224 | zone->active_regions = 0; |
| 225 | zone->max_regions = 0; |
| 226 | INIT_LIST_HEAD(&zone->region_list); |
| 227 | zone->z_ops = z_ops; |
| 228 | active_zones++; |
| 229 | |
| 230 | if (active_zones == 1) |
| 231 | pr_info("Physical OCMEM zone layout:\n"); |
| 232 | |
| 233 | pr_info(" zone %s\t: 0x%08lx - 0x%08lx (%4ld KB)\n", |
| 234 | client_names[part->id], zone->z_start, |
| 235 | zone->z_end, part->p_size/SZ_1K); |
| 236 | } |
| 237 | |
| 238 | dev_info(dev, "Total active zones = %d\n", active_zones); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static int __devinit msm_ocmem_probe(struct platform_device *pdev) |
| 243 | { |
| 244 | struct device *dev = &pdev->dev; |
| 245 | |
| 246 | if (!pdev->dev.of_node->child) { |
| 247 | dev_info(dev, "Missing Configuration in Device Tree\n"); |
| 248 | ocmem_pdata = parse_static_config(pdev); |
| 249 | } else { |
| 250 | ocmem_pdata = parse_dt_config(pdev); |
| 251 | } |
| 252 | |
| 253 | /* Check if we have some configuration data to start */ |
| 254 | if (!ocmem_pdata) |
| 255 | return -ENODEV; |
| 256 | |
| 257 | /* Sanity Checks */ |
| 258 | BUG_ON(!IS_ALIGNED(ocmem_pdata->size, PAGE_SIZE)); |
| 259 | BUG_ON(!IS_ALIGNED(ocmem_pdata->base, PAGE_SIZE)); |
| 260 | |
| 261 | platform_set_drvdata(pdev, ocmem_pdata); |
| 262 | |
| 263 | if (ocmem_zone_init(pdev)) |
| 264 | return -EBUSY; |
| 265 | |
| 266 | dev_info(dev, "initialized successfully\n"); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int __devexit msm_ocmem_remove(struct platform_device *pdev) |
| 271 | { |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static struct of_device_id msm_ocmem_dt_match[] = { |
| 276 | { .compatible = "qcom,msm_ocmem", |
| 277 | }, |
| 278 | {} |
| 279 | }; |
| 280 | |
| 281 | static struct platform_driver msm_ocmem_driver = { |
| 282 | .probe = msm_ocmem_probe, |
| 283 | .remove = __devexit_p(msm_ocmem_remove), |
| 284 | .driver = { |
| 285 | .name = "msm_ocmem", |
| 286 | .owner = THIS_MODULE, |
| 287 | .of_match_table = msm_ocmem_dt_match, |
| 288 | }, |
| 289 | }; |
| 290 | |
| 291 | static int __init ocmem_init(void) |
| 292 | { |
| 293 | return platform_driver_register(&msm_ocmem_driver); |
| 294 | } |
| 295 | subsys_initcall(ocmem_init); |
| 296 | |
| 297 | static void __exit ocmem_exit(void) |
| 298 | { |
| 299 | platform_driver_unregister(&msm_ocmem_driver); |
| 300 | } |
| 301 | module_exit(ocmem_exit); |
| 302 | |
| 303 | MODULE_LICENSE("GPL v2"); |
| 304 | MODULE_DESCRIPTION("Support for On-Chip Memory on MSM"); |