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