Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livep...
[cascardo/linux.git] / arch / s390 / hypfs / hypfs_diag0c.c
1 /*
2  * Hypervisor filesystem for Linux on s390
3  *
4  * Diag 0C implementation
5  *
6  * Copyright IBM Corp. 2014
7  */
8
9 #include <linux/slab.h>
10 #include <linux/cpu.h>
11 #include <asm/hypfs.h>
12 #include "hypfs.h"
13
14 #define DBFS_D0C_HDR_VERSION 0
15
16 /*
17  * Execute diagnose 0c in 31 bit mode
18  */
19 static void diag0c(struct hypfs_diag0c_entry *entry)
20 {
21         asm volatile (
22 #ifdef CONFIG_64BIT
23                 "       sam31\n"
24                 "       diag    %0,%0,0x0c\n"
25                 "       sam64\n"
26 #else
27                 "       diag %0,%0,0x0c\n"
28 #endif
29                 : /* no output register */
30                 : "a" (entry)
31                 : "memory");
32 }
33
34 /*
35  * Get hypfs_diag0c_entry from CPU vector and store diag0c data
36  */
37 static void diag0c_fn(void *data)
38 {
39         diag0c(((void **) data)[smp_processor_id()]);
40 }
41
42 /*
43  * Allocate buffer and store diag 0c data
44  */
45 static void *diag0c_store(unsigned int *count)
46 {
47         struct hypfs_diag0c_data *diag0c_data;
48         unsigned int cpu_count, cpu, i;
49         void **cpu_vec;
50
51         get_online_cpus();
52         cpu_count = num_online_cpus();
53         cpu_vec = kmalloc(sizeof(*cpu_vec) * num_possible_cpus(), GFP_KERNEL);
54         if (!cpu_vec)
55                 goto fail_put_online_cpus;
56         /* Note: Diag 0c needs 8 byte alignment and real storage */
57         diag0c_data = kzalloc(sizeof(struct hypfs_diag0c_hdr) +
58                               cpu_count * sizeof(struct hypfs_diag0c_entry),
59                               GFP_KERNEL | GFP_DMA);
60         if (!diag0c_data)
61                 goto fail_kfree_cpu_vec;
62         i = 0;
63         /* Fill CPU vector for each online CPU */
64         for_each_online_cpu(cpu) {
65                 diag0c_data->entry[i].cpu = cpu;
66                 cpu_vec[cpu] = &diag0c_data->entry[i++];
67         }
68         /* Collect data all CPUs */
69         on_each_cpu(diag0c_fn, cpu_vec, 1);
70         *count = cpu_count;
71         kfree(cpu_vec);
72         put_online_cpus();
73         return diag0c_data;
74
75 fail_kfree_cpu_vec:
76         kfree(cpu_vec);
77 fail_put_online_cpus:
78         put_online_cpus();
79         return ERR_PTR(-ENOMEM);
80 }
81
82 /*
83  * Hypfs DBFS callback: Free diag 0c data
84  */
85 static void dbfs_diag0c_free(const void *data)
86 {
87         kfree(data);
88 }
89
90 /*
91  * Hypfs DBFS callback: Create diag 0c data
92  */
93 static int dbfs_diag0c_create(void **data, void **data_free_ptr, size_t *size)
94 {
95         struct hypfs_diag0c_data *diag0c_data;
96         unsigned int count;
97
98         diag0c_data = diag0c_store(&count);
99         if (IS_ERR(diag0c_data))
100                 return PTR_ERR(diag0c_data);
101         memset(&diag0c_data->hdr, 0, sizeof(diag0c_data->hdr));
102         get_tod_clock_ext(diag0c_data->hdr.tod_ext);
103         diag0c_data->hdr.len = count * sizeof(struct hypfs_diag0c_entry);
104         diag0c_data->hdr.version = DBFS_D0C_HDR_VERSION;
105         diag0c_data->hdr.count = count;
106         *data = diag0c_data;
107         *data_free_ptr = diag0c_data;
108         *size = diag0c_data->hdr.len + sizeof(struct hypfs_diag0c_hdr);
109         return 0;
110 }
111
112 /*
113  * Hypfs DBFS file structure
114  */
115 static struct hypfs_dbfs_file dbfs_file_0c = {
116         .name           = "diag_0c",
117         .data_create    = dbfs_diag0c_create,
118         .data_free      = dbfs_diag0c_free,
119 };
120
121 /*
122  * Initialize diag 0c interface for z/VM
123  */
124 int __init hypfs_diag0c_init(void)
125 {
126         if (!MACHINE_IS_VM)
127                 return 0;
128         return hypfs_dbfs_create_file(&dbfs_file_0c);
129 }
130
131 /*
132  * Shutdown diag 0c interface for z/VM
133  */
134 void hypfs_diag0c_exit(void)
135 {
136         if (!MACHINE_IS_VM)
137                 return;
138         hypfs_dbfs_remove_file(&dbfs_file_0c);
139 }