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