Merge tag 'powerpc-4.8-2' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[deliverable/linux.git] / arch / s390 / hypfs / hypfs_vm.c
CommitLineData
31cb4bd3
MH
1/*
2 * Hypervisor filesystem for Linux on s390. z/VM implementation.
3 *
a53c8fab 4 * Copyright IBM Corp. 2006
31cb4bd3
MH
5 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
6 */
7
8#include <linux/types.h>
9#include <linux/errno.h>
10#include <linux/string.h>
11#include <linux/vmalloc.h>
1ec2772e 12#include <asm/diag.h>
31cb4bd3 13#include <asm/ebcdic.h>
57b28f66 14#include <asm/timex.h>
31cb4bd3
MH
15#include "hypfs.h"
16
17#define NAME_LEN 8
57b28f66 18#define DBFS_D2FC_HDR_VERSION 0
31cb4bd3
MH
19
20static char local_guest[] = " ";
21static char all_guests[] = "* ";
22static char *guest_query;
23
24struct diag2fc_data {
25 __u32 version;
26 __u32 flags;
27 __u64 used_cpu;
28 __u64 el_time;
29 __u64 mem_min_kb;
30 __u64 mem_max_kb;
31 __u64 mem_share_kb;
32 __u64 mem_used_kb;
33 __u32 pcpus;
34 __u32 lcpus;
35 __u32 vcpus;
36a55402 36 __u32 ocpus;
31cb4bd3
MH
37 __u32 cpu_max;
38 __u32 cpu_shares;
39 __u32 cpu_use_samp;
40 __u32 cpu_delay_samp;
41 __u32 page_wait_samp;
42 __u32 idle_samp;
43 __u32 other_samp;
44 __u32 total_samp;
45 char guest_name[NAME_LEN];
46};
47
48struct diag2fc_parm_list {
49 char userid[NAME_LEN];
50 char aci_grp[NAME_LEN];
51 __u64 addr;
52 __u32 size;
53 __u32 fmt;
54};
55
56static int diag2fc(int size, char* query, void *addr)
57{
58 unsigned long residual_cnt;
59 unsigned long rc;
60 struct diag2fc_parm_list parm_list;
61
62 memcpy(parm_list.userid, query, NAME_LEN);
63 ASCEBC(parm_list.userid, NAME_LEN);
64 parm_list.addr = (unsigned long) addr ;
65 parm_list.size = size;
66 parm_list.fmt = 0x02;
67 memset(parm_list.aci_grp, 0x40, NAME_LEN);
68 rc = -1;
69
1ec2772e 70 diag_stat_inc(DIAG_STAT_X2FC);
31cb4bd3
MH
71 asm volatile(
72 " diag %0,%1,0x2fc\n"
6c22c986 73 "0: nopr %%r7\n"
31cb4bd3
MH
74 EX_TABLE(0b,0b)
75 : "=d" (residual_cnt), "+d" (rc) : "0" (&parm_list) : "memory");
76
77 if ((rc != 0 ) && (rc != -2))
78 return rc;
79 else
80 return -residual_cnt;
81}
82
57b28f66
MH
83/*
84 * Allocate buffer for "query" and store diag 2fc at "offset"
85 */
86static void *diag2fc_store(char *query, unsigned int *count, int offset)
31cb4bd3 87{
57b28f66 88 void *data;
31cb4bd3 89 int size;
31cb4bd3
MH
90
91 do {
92 size = diag2fc(0, query, NULL);
93 if (size < 0)
94 return ERR_PTR(-EACCES);
57b28f66 95 data = vmalloc(size + offset);
31cb4bd3
MH
96 if (!data)
97 return ERR_PTR(-ENOMEM);
57b28f66 98 if (diag2fc(size, query, data + offset) == 0)
31cb4bd3
MH
99 break;
100 vfree(data);
101 } while (1);
57b28f66 102 *count = (size / sizeof(struct diag2fc_data));
31cb4bd3
MH
103
104 return data;
105}
106
2fcb3686 107static void diag2fc_free(const void *data)
31cb4bd3
MH
108{
109 vfree(data);
110}
111
f78e41e3 112#define ATTRIBUTE(dir, name, member) \
31cb4bd3
MH
113do { \
114 void *rc; \
a118cfdf 115 rc = hypfs_create_u64(dir, name, member); \
31cb4bd3
MH
116 if (IS_ERR(rc)) \
117 return PTR_ERR(rc); \
118} while(0)
119
f78e41e3 120static int hpyfs_vm_create_guest(struct dentry *systems_dir,
31cb4bd3
MH
121 struct diag2fc_data *data)
122{
123 char guest_name[NAME_LEN + 1] = {};
124 struct dentry *guest_dir, *cpus_dir, *samples_dir, *mem_dir;
125 int dedicated_flag, capped_value;
126
127 capped_value = (data->flags & 0x00000006) >> 1;
128 dedicated_flag = (data->flags & 0x00000008) >> 3;
129
130 /* guest dir */
131 memcpy(guest_name, data->guest_name, NAME_LEN);
132 EBCASC(guest_name, NAME_LEN);
1d802e24 133 strim(guest_name);
a118cfdf 134 guest_dir = hypfs_mkdir(systems_dir, guest_name);
31cb4bd3
MH
135 if (IS_ERR(guest_dir))
136 return PTR_ERR(guest_dir);
f78e41e3 137 ATTRIBUTE(guest_dir, "onlinetime_us", data->el_time);
31cb4bd3
MH
138
139 /* logical cpu information */
a118cfdf 140 cpus_dir = hypfs_mkdir(guest_dir, "cpus");
31cb4bd3
MH
141 if (IS_ERR(cpus_dir))
142 return PTR_ERR(cpus_dir);
f78e41e3
AV
143 ATTRIBUTE(cpus_dir, "cputime_us", data->used_cpu);
144 ATTRIBUTE(cpus_dir, "capped", capped_value);
145 ATTRIBUTE(cpus_dir, "dedicated", dedicated_flag);
146 ATTRIBUTE(cpus_dir, "count", data->vcpus);
36a55402
MH
147 /*
148 * Note: The "weight_min" attribute got the wrong name.
149 * The value represents the number of non-stopped (operating)
150 * CPUS.
151 */
152 ATTRIBUTE(cpus_dir, "weight_min", data->ocpus);
f78e41e3
AV
153 ATTRIBUTE(cpus_dir, "weight_max", data->cpu_max);
154 ATTRIBUTE(cpus_dir, "weight_cur", data->cpu_shares);
31cb4bd3
MH
155
156 /* memory information */
a118cfdf 157 mem_dir = hypfs_mkdir(guest_dir, "mem");
31cb4bd3
MH
158 if (IS_ERR(mem_dir))
159 return PTR_ERR(mem_dir);
f78e41e3
AV
160 ATTRIBUTE(mem_dir, "min_KiB", data->mem_min_kb);
161 ATTRIBUTE(mem_dir, "max_KiB", data->mem_max_kb);
162 ATTRIBUTE(mem_dir, "used_KiB", data->mem_used_kb);
163 ATTRIBUTE(mem_dir, "share_KiB", data->mem_share_kb);
31cb4bd3
MH
164
165 /* samples */
a118cfdf 166 samples_dir = hypfs_mkdir(guest_dir, "samples");
31cb4bd3
MH
167 if (IS_ERR(samples_dir))
168 return PTR_ERR(samples_dir);
f78e41e3
AV
169 ATTRIBUTE(samples_dir, "cpu_using", data->cpu_use_samp);
170 ATTRIBUTE(samples_dir, "cpu_delay", data->cpu_delay_samp);
171 ATTRIBUTE(samples_dir, "mem_delay", data->page_wait_samp);
172 ATTRIBUTE(samples_dir, "idle", data->idle_samp);
173 ATTRIBUTE(samples_dir, "other", data->other_samp);
174 ATTRIBUTE(samples_dir, "total", data->total_samp);
31cb4bd3
MH
175 return 0;
176}
177
f78e41e3 178int hypfs_vm_create_files(struct dentry *root)
31cb4bd3
MH
179{
180 struct dentry *dir, *file;
181 struct diag2fc_data *data;
57b28f66
MH
182 unsigned int count = 0;
183 int rc, i;
31cb4bd3 184
57b28f66 185 data = diag2fc_store(guest_query, &count, 0);
31cb4bd3
MH
186 if (IS_ERR(data))
187 return PTR_ERR(data);
188
189 /* Hpervisor Info */
a118cfdf 190 dir = hypfs_mkdir(root, "hyp");
31cb4bd3
MH
191 if (IS_ERR(dir)) {
192 rc = PTR_ERR(dir);
193 goto failed;
194 }
a118cfdf 195 file = hypfs_create_str(dir, "type", "z/VM Hypervisor");
31cb4bd3
MH
196 if (IS_ERR(file)) {
197 rc = PTR_ERR(file);
198 goto failed;
199 }
200
201 /* physical cpus */
a118cfdf 202 dir = hypfs_mkdir(root, "cpus");
31cb4bd3
MH
203 if (IS_ERR(dir)) {
204 rc = PTR_ERR(dir);
205 goto failed;
206 }
a118cfdf 207 file = hypfs_create_u64(dir, "count", data->lcpus);
31cb4bd3
MH
208 if (IS_ERR(file)) {
209 rc = PTR_ERR(file);
210 goto failed;
211 }
212
213 /* guests */
a118cfdf 214 dir = hypfs_mkdir(root, "systems");
31cb4bd3
MH
215 if (IS_ERR(dir)) {
216 rc = PTR_ERR(dir);
217 goto failed;
218 }
219
220 for (i = 0; i < count; i++) {
f78e41e3 221 rc = hpyfs_vm_create_guest(dir, &(data[i]));
31cb4bd3
MH
222 if (rc)
223 goto failed;
224 }
225 diag2fc_free(data);
226 return 0;
227
228failed:
229 diag2fc_free(data);
230 return rc;
231}
232
57b28f66
MH
233struct dbfs_d2fc_hdr {
234 u64 len; /* Length of d2fc buffer without header */
235 u16 version; /* Version of header */
e38f9781 236 char tod_ext[STORE_CLOCK_EXT_SIZE]; /* TOD clock for d2fc */
57b28f66
MH
237 u64 count; /* Number of VM guests in d2fc buffer */
238 char reserved[30];
239} __attribute__ ((packed));
240
241struct dbfs_d2fc {
242 struct dbfs_d2fc_hdr hdr; /* 64 byte header */
243 char buf[]; /* d2fc buffer */
244} __attribute__ ((packed));
245
2fcb3686 246static int dbfs_diag2fc_create(void **data, void **data_free_ptr, size_t *size)
57b28f66 247{
2fcb3686 248 struct dbfs_d2fc *d2fc;
57b28f66
MH
249 unsigned int count;
250
2fcb3686
MH
251 d2fc = diag2fc_store(guest_query, &count, sizeof(d2fc->hdr));
252 if (IS_ERR(d2fc))
253 return PTR_ERR(d2fc);
1aae0560 254 get_tod_clock_ext(d2fc->hdr.tod_ext);
2fcb3686
MH
255 d2fc->hdr.len = count * sizeof(struct diag2fc_data);
256 d2fc->hdr.version = DBFS_D2FC_HDR_VERSION;
257 d2fc->hdr.count = count;
258 memset(&d2fc->hdr.reserved, 0, sizeof(d2fc->hdr.reserved));
259 *data = d2fc;
260 *data_free_ptr = d2fc;
261 *size = d2fc->hdr.len + sizeof(struct dbfs_d2fc_hdr);
57b28f66
MH
262 return 0;
263}
264
2fcb3686
MH
265static struct hypfs_dbfs_file dbfs_file_2fc = {
266 .name = "diag_2fc",
267 .data_create = dbfs_diag2fc_create,
268 .data_free = diag2fc_free,
57b28f66
MH
269};
270
31cb4bd3
MH
271int hypfs_vm_init(void)
272{
57b28f66
MH
273 if (!MACHINE_IS_VM)
274 return 0;
31cb4bd3
MH
275 if (diag2fc(0, all_guests, NULL) > 0)
276 guest_query = all_guests;
277 else if (diag2fc(0, local_guest, NULL) > 0)
278 guest_query = local_guest;
279 else
280 return -EACCES;
2fcb3686 281 return hypfs_dbfs_create_file(&dbfs_file_2fc);
31cb4bd3 282}
57b28f66
MH
283
284void hypfs_vm_exit(void)
285{
286 if (!MACHINE_IS_VM)
287 return;
2fcb3686 288 hypfs_dbfs_remove_file(&dbfs_file_2fc);
57b28f66 289}
This page took 0.629554 seconds and 5 git commands to generate.