blob: 358b75e3aeadf3e9d2454013c761596de9d01969 [file] [log] [blame]
Srinu Gorlecf8c6752018-01-19 18:36:13 +05301/*
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
19struct vmem_debugfs_cookie {
20 phys_addr_t addr;
21 size_t size;
22};
23
24static 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
32static 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
51DEFINE_SIMPLE_ATTRIBUTE(fops_vmem_alloc, __vmem_alloc_get,
52 __vmem_alloc_set, "%llu");
53
54struct 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
74exit:
75 return debugfs_root;
76}
77
78void vmem_debugfs_deinit(struct dentry *debugfs_root)
79{
80 debugfs_remove_recursive(debugfs_root);
81}
82