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