Srinu Gorle | cf8c675 | 2018-01-19 18:36:13 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, 2018 The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/debugfs.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include "vmem.h" |
| 18 | |
| 19 | struct vmem_debugfs_cookie { |
| 20 | phys_addr_t addr; |
| 21 | size_t size; |
| 22 | }; |
| 23 | |
| 24 | static int __vmem_alloc_get(void *priv, u64 *val) |
| 25 | { |
| 26 | struct vmem_debugfs_cookie *cookie = priv; |
| 27 | |
| 28 | *val = cookie->size; |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static int __vmem_alloc_set(void *priv, u64 val) |
| 33 | { |
| 34 | struct vmem_debugfs_cookie *cookie = priv; |
| 35 | int rc = 0; |
| 36 | |
| 37 | switch (val) { |
| 38 | case 0: /* free */ |
| 39 | vmem_free(cookie->addr); |
| 40 | cookie->size = 0; |
| 41 | break; |
| 42 | default: |
| 43 | rc = vmem_allocate(val, &cookie->addr); |
| 44 | cookie->size = val; |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | return rc; |
| 49 | } |
| 50 | |
| 51 | DEFINE_SIMPLE_ATTRIBUTE(fops_vmem_alloc, __vmem_alloc_get, |
| 52 | __vmem_alloc_set, "%llu"); |
| 53 | |
| 54 | struct dentry *vmem_debugfs_init(struct platform_device *pdev) |
| 55 | { |
| 56 | struct vmem_debugfs_cookie *alloc_cookie = NULL; |
| 57 | struct dentry *debugfs_root = NULL; |
| 58 | |
| 59 | alloc_cookie = devm_kzalloc(&pdev->dev, sizeof(*alloc_cookie), |
| 60 | GFP_KERNEL); |
| 61 | if (!alloc_cookie) |
| 62 | goto exit; |
| 63 | |
| 64 | debugfs_root = debugfs_create_dir("vmem", NULL); |
| 65 | if (IS_ERR_OR_NULL(debugfs_root)) { |
| 66 | pr_warn("Failed to create '<debugfs>/vmem'\n"); |
| 67 | debugfs_root = NULL; |
| 68 | goto exit; |
| 69 | } |
| 70 | |
| 71 | debugfs_create_file("alloc", 0600, debugfs_root, |
| 72 | alloc_cookie, &fops_vmem_alloc); |
| 73 | |
| 74 | exit: |
| 75 | return debugfs_root; |
| 76 | } |
| 77 | |
| 78 | void vmem_debugfs_deinit(struct dentry *debugfs_root) |
| 79 | { |
| 80 | debugfs_remove_recursive(debugfs_root); |
| 81 | } |
| 82 | |