summaryrefslogtreecommitdiffstats
path: root/drivers/platform/tegra/tegra_cpu_sysfs.c
blob: 3c98e302781851b3657b0d8b1fb7b2bc692faee6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */
#include <linux/cpu.h>
#include <linux/module.h>
#include <linux/platform/tegra/tegra18_cpu_map.h>
#include <linux/platform/tegra/tegra-cpu.h>
#include <linux/cpuhotplug.h>

enum cputype_t {
	INVALID = 0x00,
	CARMEL = 0x1,
	DENVER,
	A57,
	OTHERS,
	CPUTYPE_MAX = OTHERS + 1,
	CPUTYPE_FORCE_U32 = 0xFFFFFFFF
};

static struct kobject *tegra_cpu_type;
static struct kobject *tegra_cpu[CPUTYPE_MAX];
static enum cputype_t cpu_to_type[NR_CPUS];
static enum cpuhp_state assigned_cpu_state;

static struct kobject *create_dir(const char *name, struct kobject *parent)
{
	pr_debug("%s: about to create %s with parent %s\n",
		__func__, name, kobject_name(parent));
	return kobject_create_and_add(name, parent);
}

static void remove_dir(struct kobject *obj)
{
	if (!obj)
		return;
	pr_debug("%s: removing %s\n", __func__, kobject_name(obj));
	kobject_put(obj);
}

static int tegra_cpu_prepare_down(unsigned int cpu)
{
	char name[8];
	int i;
	enum cputype_t cpu_type;

	pr_debug("%s: cpu = %d\n", __func__, cpu);
	cpu_type = cpu_to_type[cpu];

	if (!tegra_cpu[cpu_type])
		return -ENODEV;

	snprintf(name, sizeof(name), "cpu%d", cpu);
	sysfs_remove_link(tegra_cpu[cpu_type], name);
	cpu_to_type[cpu] = INVALID;

	for_each_possible_cpu(i) {
		if (cpu_to_type[i] == cpu_type)
			return 0;
	}
	remove_dir(tegra_cpu[cpu_type]);
	return 0;
}

static int tegra_cpu_online(unsigned int cpu)
{
	struct device *target;
	enum cputype_t cpu_type;
	char *cpu_type_str;
	int ret;

	pr_debug("%s: cpu = %d\n", __func__, cpu);

	target = get_cpu_device(cpu);
	if (!target)
		return -ENODEV;

	if (tegra_is_cpu_carmel(cpu)) {
		cpu_type = CARMEL;
		cpu_type_str = "carmel";

	} else if (tegra18_is_cpu_denver(cpu)) {
		cpu_type = DENVER;
		cpu_type_str = "denver";

	} else if (tegra18_is_cpu_arm(cpu)) {
		cpu_type = A57;
		cpu_type_str = "a57";

	} else {
		cpu_type = OTHERS;
		cpu_type_str = "others";
	}

	if (!tegra_cpu[cpu_type]) {
		tegra_cpu[cpu_type] = create_dir(cpu_type_str, tegra_cpu_type);
		if (tegra_cpu[cpu_type] == NULL)
			return -ENOMEM;
	}

	ret = sysfs_create_link(tegra_cpu[cpu_type],
			&target->kobj, dev_name(target));
	if (ret)
		return ret;

	cpu_to_type[cpu] = cpu_type;
	return 0;
}

static int __init tegra_cpu_sysfs_init(void)
{
	int ret = -ENOMEM;

	tegra_cpu_type = create_dir("tegra_cpu_type",
		get_cpu_device(smp_processor_id())->kobj.parent);

	if (!tegra_cpu_type)
		goto err;

	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
	"cpu/tegra_cpu_sysfs:online", tegra_cpu_online, tegra_cpu_prepare_down);
	if (ret < 0) {
		remove_dir(tegra_cpu_type);
		goto err;
	}

	assigned_cpu_state = ret;
	ret = 0;
err:
	return ret;

}
module_init(tegra_cpu_sysfs_init);

static void __exit tegra_cpu_sysfs_exit(void)
{
	cpuhp_remove_state(assigned_cpu_state);
	remove_dir(tegra_cpu_type);
}
module_exit(tegra_cpu_sysfs_exit);

MODULE_AUTHOR("Achal Verma <achalv@nvidia.com>");
MODULE_DESCRIPTION("Tegra CPU Topology sysfs");
MODULE_LICENSE("GPL v2");