powerpc/prom: Drop support for old FDT versions
[linux-2.6-block.git] / arch / powerpc / platforms / powernv / memtrace.c
CommitLineData
9d5171a8
RG
1/*
2 * Copyright (C) IBM Corporation, 2014, 2017
3 * Anton Blanchard, Rashmica Gupta.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#define pr_fmt(fmt) "memtrace: " fmt
12
13#include <linux/bitops.h>
14#include <linux/string.h>
15#include <linux/memblock.h>
16#include <linux/init.h>
17#include <linux/moduleparam.h>
18#include <linux/fs.h>
19#include <linux/debugfs.h>
20#include <linux/slab.h>
21#include <linux/memory.h>
22#include <linux/memory_hotplug.h>
23#include <asm/machdep.h>
24#include <asm/debugfs.h>
25
26/* This enables us to keep track of the memory removed from each node. */
27struct memtrace_entry {
28 void *mem;
29 u64 start;
30 u64 size;
31 u32 nid;
32 struct dentry *dir;
33 char name[16];
34};
35
36static u64 memtrace_size;
37
38static struct memtrace_entry *memtrace_array;
39static unsigned int memtrace_array_nr;
40
41
42static ssize_t memtrace_read(struct file *filp, char __user *ubuf,
43 size_t count, loff_t *ppos)
44{
45 struct memtrace_entry *ent = filp->private_data;
46
47 return simple_read_from_buffer(ubuf, count, ppos, ent->mem, ent->size);
48}
49
50static bool valid_memtrace_range(struct memtrace_entry *dev,
51 unsigned long start, unsigned long size)
52{
53 if ((start >= dev->start) &&
54 ((start + size) <= (dev->start + dev->size)))
55 return true;
56
57 return false;
58}
59
60static int memtrace_mmap(struct file *filp, struct vm_area_struct *vma)
61{
62 unsigned long size = vma->vm_end - vma->vm_start;
63 struct memtrace_entry *dev = filp->private_data;
64
65 if (!valid_memtrace_range(dev, vma->vm_pgoff << PAGE_SHIFT, size))
66 return -EINVAL;
67
68 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
69
70 if (remap_pfn_range(vma, vma->vm_start,
71 vma->vm_pgoff + (dev->start >> PAGE_SHIFT),
72 size, vma->vm_page_prot))
73 return -EAGAIN;
74
75 return 0;
76}
77
78static const struct file_operations memtrace_fops = {
79 .llseek = default_llseek,
80 .read = memtrace_read,
81 .mmap = memtrace_mmap,
82 .open = simple_open,
83};
84
9d5171a8
RG
85static int check_memblock_online(struct memory_block *mem, void *arg)
86{
87 if (mem->state != MEM_ONLINE)
88 return -1;
89
90 return 0;
91}
92
93static int change_memblock_state(struct memory_block *mem, void *arg)
94{
95 unsigned long state = (unsigned long)arg;
96
97 mem->state = state;
98
99 return 0;
100}
101
102static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages)
103{
104 u64 end_pfn = start_pfn + nr_pages - 1;
105
106 if (walk_memory_range(start_pfn, end_pfn, NULL,
107 check_memblock_online))
108 return false;
109
110 walk_memory_range(start_pfn, end_pfn, (void *)MEM_GOING_OFFLINE,
111 change_memblock_state);
112
113 if (offline_pages(start_pfn, nr_pages)) {
114 walk_memory_range(start_pfn, end_pfn, (void *)MEM_ONLINE,
115 change_memblock_state);
116 return false;
117 }
118
119 walk_memory_range(start_pfn, end_pfn, (void *)MEM_OFFLINE,
120 change_memblock_state);
121
9d5171a8
RG
122 lock_device_hotplug();
123 remove_memory(nid, start_pfn << PAGE_SHIFT, nr_pages << PAGE_SHIFT);
124 unlock_device_hotplug();
125
126 return true;
127}
128
129static u64 memtrace_alloc_node(u32 nid, u64 size)
130{
131 u64 start_pfn, end_pfn, nr_pages;
132 u64 base_pfn;
133
134 if (!NODE_DATA(nid) || !node_spanned_pages(nid))
135 return 0;
136
137 start_pfn = node_start_pfn(nid);
138 end_pfn = node_end_pfn(nid);
139 nr_pages = size >> PAGE_SHIFT;
140
141 /* Trace memory needs to be aligned to the size */
142 end_pfn = round_down(end_pfn - nr_pages, nr_pages);
143
144 for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) {
145 if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true)
146 return base_pfn << PAGE_SHIFT;
147 }
148
149 return 0;
150}
151
152static int memtrace_init_regions_runtime(u64 size)
153{
154 u32 nid;
155 u64 m;
156
157 memtrace_array = kcalloc(num_online_nodes(),
158 sizeof(struct memtrace_entry), GFP_KERNEL);
159 if (!memtrace_array) {
160 pr_err("Failed to allocate memtrace_array\n");
161 return -EINVAL;
162 }
163
164 for_each_online_node(nid) {
165 m = memtrace_alloc_node(nid, size);
166
167 /*
168 * A node might not have any local memory, so warn but
169 * continue on.
170 */
171 if (!m) {
172 pr_err("Failed to allocate trace memory on node %d\n", nid);
173 continue;
174 }
175
176 pr_info("Allocated trace memory on node %d at 0x%016llx\n", nid, m);
177
178 memtrace_array[memtrace_array_nr].start = m;
179 memtrace_array[memtrace_array_nr].size = size;
180 memtrace_array[memtrace_array_nr].nid = nid;
181 memtrace_array_nr++;
182 }
183
184 return 0;
185}
186
187static struct dentry *memtrace_debugfs_dir;
188
189static int memtrace_init_debugfs(void)
190{
191 int ret = 0;
192 int i;
193
194 for (i = 0; i < memtrace_array_nr; i++) {
195 struct dentry *dir;
196 struct memtrace_entry *ent = &memtrace_array[i];
197
198 ent->mem = ioremap(ent->start, ent->size);
199 /* Warn but continue on */
200 if (!ent->mem) {
201 pr_err("Failed to map trace memory at 0x%llx\n",
202 ent->start);
203 ret = -1;
204 continue;
205 }
206
207 snprintf(ent->name, 16, "%08x", ent->nid);
208 dir = debugfs_create_dir(ent->name, memtrace_debugfs_dir);
209 if (!dir)
210 return -1;
211
212 ent->dir = dir;
213 debugfs_create_file("trace", 0400, dir, ent, &memtrace_fops);
214 debugfs_create_x64("start", 0400, dir, &ent->start);
215 debugfs_create_x64("size", 0400, dir, &ent->size);
216 }
217
218 return ret;
219}
220
221static int memtrace_enable_set(void *data, u64 val)
222{
223 if (memtrace_size)
224 return -EINVAL;
225
226 if (!val)
227 return -EINVAL;
228
229 /* Make sure size is aligned to a memory block */
230 if (val & (memory_block_size_bytes() - 1))
231 return -EINVAL;
232
233 if (memtrace_init_regions_runtime(val))
234 return -EINVAL;
235
236 if (memtrace_init_debugfs())
237 return -EINVAL;
238
239 memtrace_size = val;
240
241 return 0;
242}
243
244static int memtrace_enable_get(void *data, u64 *val)
245{
246 *val = memtrace_size;
247 return 0;
248}
249
250DEFINE_SIMPLE_ATTRIBUTE(memtrace_init_fops, memtrace_enable_get,
251 memtrace_enable_set, "0x%016llx\n");
252
253static int memtrace_init(void)
254{
255 memtrace_debugfs_dir = debugfs_create_dir("memtrace",
256 powerpc_debugfs_root);
257 if (!memtrace_debugfs_dir)
258 return -1;
259
260 debugfs_create_file("enable", 0600, memtrace_debugfs_dir,
261 NULL, &memtrace_init_fops);
262
263 return 0;
264}
265machine_device_initcall(powernv, memtrace_init);