1f33ecfdeSHaren Myneni // SPDX-License-Identifier: GPL-2.0-or-later
2f33ecfdeSHaren Myneni /*
3f33ecfdeSHaren Myneni  * Copyright 2020-21 IBM Corp.
4f33ecfdeSHaren Myneni  */
5f33ecfdeSHaren Myneni 
6f33ecfdeSHaren Myneni #define pr_fmt(fmt) "vas: " fmt
7f33ecfdeSHaren Myneni 
8f33ecfdeSHaren Myneni #include <linux/module.h>
9f33ecfdeSHaren Myneni #include <linux/kernel.h>
10f33ecfdeSHaren Myneni #include <linux/export.h>
11f33ecfdeSHaren Myneni #include <linux/types.h>
12f33ecfdeSHaren Myneni #include <linux/delay.h>
13b22f2d88SHaren Myneni #include <linux/slab.h>
146d0aaf5eSHaren Myneni #include <linux/interrupt.h>
15c736fb97SMichael Ellerman #include <linux/irqdomain.h>
16ca77d488SHaren Myneni #include <asm/machdep.h>
17f33ecfdeSHaren Myneni #include <asm/hvcall.h>
18f33ecfdeSHaren Myneni #include <asm/plpar_wrappers.h>
1946d60bdbSChristophe Leroy #include <asm/firmware.h>
20c040c748SMichael Ellerman #include <asm/vphn.h>
21f33ecfdeSHaren Myneni #include <asm/vas.h>
22f33ecfdeSHaren Myneni #include "vas.h"
23f33ecfdeSHaren Myneni 
24f33ecfdeSHaren Myneni #define VAS_INVALID_WIN_ADDRESS	0xFFFFFFFFFFFFFFFFul
25f33ecfdeSHaren Myneni #define VAS_DEFAULT_DOMAIN_ID	0xFFFFFFFFFFFFFFFFul
26f33ecfdeSHaren Myneni /* The hypervisor allows one credit per window right now */
27f33ecfdeSHaren Myneni #define DEF_WIN_CREDS		1
28f33ecfdeSHaren Myneni 
29ca77d488SHaren Myneni static struct vas_all_caps caps_all;
30ca77d488SHaren Myneni static bool copypaste_feat;
31278fe1ccSHaren Myneni static struct hv_vas_cop_feat_caps hv_cop_caps;
32ca77d488SHaren Myneni 
33ca77d488SHaren Myneni static struct vas_caps vascaps[VAS_MAX_FEAT_TYPE];
34b22f2d88SHaren Myneni static DEFINE_MUTEX(vas_pseries_mutex);
3537e67648SHaren Myneni static bool migration_in_progress;
36ca77d488SHaren Myneni 
hcall_return_busy_check(long rc)37f33ecfdeSHaren Myneni static long hcall_return_busy_check(long rc)
38f33ecfdeSHaren Myneni {
39f33ecfdeSHaren Myneni 	/* Check if we are stalled for some time */
40f33ecfdeSHaren Myneni 	if (H_IS_LONG_BUSY(rc)) {
41f33ecfdeSHaren Myneni 		msleep(get_longbusy_msecs(rc));
42f33ecfdeSHaren Myneni 		rc = H_BUSY;
43f33ecfdeSHaren Myneni 	} else if (rc == H_BUSY) {
44f33ecfdeSHaren Myneni 		cond_resched();
45f33ecfdeSHaren Myneni 	}
46f33ecfdeSHaren Myneni 
47f33ecfdeSHaren Myneni 	return rc;
48f33ecfdeSHaren Myneni }
49f33ecfdeSHaren Myneni 
50f33ecfdeSHaren Myneni /*
51f33ecfdeSHaren Myneni  * Allocate VAS window hcall
52f33ecfdeSHaren Myneni  */
h_allocate_vas_window(struct pseries_vas_window * win,u64 * domain,u8 wintype,u16 credits)53f33ecfdeSHaren Myneni static int h_allocate_vas_window(struct pseries_vas_window *win, u64 *domain,
54f33ecfdeSHaren Myneni 				     u8 wintype, u16 credits)
55f33ecfdeSHaren Myneni {
56f33ecfdeSHaren Myneni 	long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
57f33ecfdeSHaren Myneni 	long rc;
58f33ecfdeSHaren Myneni 
59f33ecfdeSHaren Myneni 	do {
60f33ecfdeSHaren Myneni 		rc = plpar_hcall9(H_ALLOCATE_VAS_WINDOW, retbuf, wintype,
61f33ecfdeSHaren Myneni 				  credits, domain[0], domain[1], domain[2],
62f33ecfdeSHaren Myneni 				  domain[3], domain[4], domain[5]);
63f33ecfdeSHaren Myneni 
64f33ecfdeSHaren Myneni 		rc = hcall_return_busy_check(rc);
65f33ecfdeSHaren Myneni 	} while (rc == H_BUSY);
66f33ecfdeSHaren Myneni 
67f33ecfdeSHaren Myneni 	if (rc == H_SUCCESS) {
68f33ecfdeSHaren Myneni 		if (win->win_addr == VAS_INVALID_WIN_ADDRESS) {
69f33ecfdeSHaren Myneni 			pr_err("H_ALLOCATE_VAS_WINDOW: COPY/PASTE is not supported\n");
70f33ecfdeSHaren Myneni 			return -ENOTSUPP;
71f33ecfdeSHaren Myneni 		}
72f33ecfdeSHaren Myneni 		win->vas_win.winid = retbuf[0];
73f33ecfdeSHaren Myneni 		win->win_addr = retbuf[1];
74f33ecfdeSHaren Myneni 		win->complete_irq = retbuf[2];
75f33ecfdeSHaren Myneni 		win->fault_irq = retbuf[3];
76f33ecfdeSHaren Myneni 		return 0;
77f33ecfdeSHaren Myneni 	}
78f33ecfdeSHaren Myneni 
79f33ecfdeSHaren Myneni 	pr_err("H_ALLOCATE_VAS_WINDOW error: %ld, wintype: %u, credits: %u\n",
80f33ecfdeSHaren Myneni 		rc, wintype, credits);
81f33ecfdeSHaren Myneni 
82f33ecfdeSHaren Myneni 	return -EIO;
83f33ecfdeSHaren Myneni }
84f33ecfdeSHaren Myneni 
85f33ecfdeSHaren Myneni /*
86f33ecfdeSHaren Myneni  * Deallocate VAS window hcall.
87f33ecfdeSHaren Myneni  */
h_deallocate_vas_window(u64 winid)88f33ecfdeSHaren Myneni static int h_deallocate_vas_window(u64 winid)
89f33ecfdeSHaren Myneni {
90f33ecfdeSHaren Myneni 	long rc;
91f33ecfdeSHaren Myneni 
92f33ecfdeSHaren Myneni 	do {
93f33ecfdeSHaren Myneni 		rc = plpar_hcall_norets(H_DEALLOCATE_VAS_WINDOW, winid);
94f33ecfdeSHaren Myneni 
95f33ecfdeSHaren Myneni 		rc = hcall_return_busy_check(rc);
96f33ecfdeSHaren Myneni 	} while (rc == H_BUSY);
97f33ecfdeSHaren Myneni 
98f33ecfdeSHaren Myneni 	if (rc == H_SUCCESS)
99f33ecfdeSHaren Myneni 		return 0;
100f33ecfdeSHaren Myneni 
101f33ecfdeSHaren Myneni 	pr_err("H_DEALLOCATE_VAS_WINDOW error: %ld, winid: %llu\n",
102f33ecfdeSHaren Myneni 		rc, winid);
103f33ecfdeSHaren Myneni 	return -EIO;
104f33ecfdeSHaren Myneni }
105f33ecfdeSHaren Myneni 
106f33ecfdeSHaren Myneni /*
107f33ecfdeSHaren Myneni  * Modify VAS window.
108f33ecfdeSHaren Myneni  * After the window is opened with allocate window hcall, configure it
109f33ecfdeSHaren Myneni  * with flags and LPAR PID before using.
110f33ecfdeSHaren Myneni  */
h_modify_vas_window(struct pseries_vas_window * win)111f33ecfdeSHaren Myneni static int h_modify_vas_window(struct pseries_vas_window *win)
112f33ecfdeSHaren Myneni {
113f33ecfdeSHaren Myneni 	long rc;
114f33ecfdeSHaren Myneni 
115f33ecfdeSHaren Myneni 	/*
116f33ecfdeSHaren Myneni 	 * AMR value is not supported in Linux VAS implementation.
117f33ecfdeSHaren Myneni 	 * The hypervisor ignores it if 0 is passed.
118f33ecfdeSHaren Myneni 	 */
119f33ecfdeSHaren Myneni 	do {
120f33ecfdeSHaren Myneni 		rc = plpar_hcall_norets(H_MODIFY_VAS_WINDOW,
121976410cdSHaren Myneni 					win->vas_win.winid, win->pid, 0,
122f33ecfdeSHaren Myneni 					VAS_MOD_WIN_FLAGS, 0);
123f33ecfdeSHaren Myneni 
124f33ecfdeSHaren Myneni 		rc = hcall_return_busy_check(rc);
125f33ecfdeSHaren Myneni 	} while (rc == H_BUSY);
126f33ecfdeSHaren Myneni 
127f33ecfdeSHaren Myneni 	if (rc == H_SUCCESS)
128f33ecfdeSHaren Myneni 		return 0;
129f33ecfdeSHaren Myneni 
130976410cdSHaren Myneni 	pr_err("H_MODIFY_VAS_WINDOW error: %ld, winid %u pid %u\n",
131976410cdSHaren Myneni 			rc, win->vas_win.winid, win->pid);
132f33ecfdeSHaren Myneni 	return -EIO;
133f33ecfdeSHaren Myneni }
134f33ecfdeSHaren Myneni 
135f33ecfdeSHaren Myneni /*
136f33ecfdeSHaren Myneni  * This hcall is used to determine the capabilities from the hypervisor.
137f33ecfdeSHaren Myneni  * @hcall: H_QUERY_VAS_CAPABILITIES or H_QUERY_NX_CAPABILITIES
138f33ecfdeSHaren Myneni  * @query_type: If 0 is passed, the hypervisor returns the overall
139f33ecfdeSHaren Myneni  *		capabilities which provides all feature(s) that are
140f33ecfdeSHaren Myneni  *		available. Then query the hypervisor to get the
141f33ecfdeSHaren Myneni  *		corresponding capabilities for the specific feature.
142f33ecfdeSHaren Myneni  *		Example: H_QUERY_VAS_CAPABILITIES provides VAS GZIP QoS
143f33ecfdeSHaren Myneni  *			and VAS GZIP Default capabilities.
144f33ecfdeSHaren Myneni  *			H_QUERY_NX_CAPABILITIES provides NX GZIP
145f33ecfdeSHaren Myneni  *			capabilities.
146f33ecfdeSHaren Myneni  * @result: Return buffer to save capabilities.
147f33ecfdeSHaren Myneni  */
h_query_vas_capabilities(const u64 hcall,u8 query_type,u64 result)148f33ecfdeSHaren Myneni int h_query_vas_capabilities(const u64 hcall, u8 query_type, u64 result)
149f33ecfdeSHaren Myneni {
150f33ecfdeSHaren Myneni 	long rc;
151f33ecfdeSHaren Myneni 
152f33ecfdeSHaren Myneni 	rc = plpar_hcall_norets(hcall, query_type, result);
153f33ecfdeSHaren Myneni 
154f33ecfdeSHaren Myneni 	if (rc == H_SUCCESS)
155f33ecfdeSHaren Myneni 		return 0;
156f33ecfdeSHaren Myneni 
1570a006aceSNicholas Piggin 	/* H_FUNCTION means HV does not support VAS so don't print an error */
1580a006aceSNicholas Piggin 	if (rc != H_FUNCTION) {
1590a006aceSNicholas Piggin 		pr_err("%s error %ld, query_type %u, result buffer 0x%llx\n",
1600a006aceSNicholas Piggin 			(hcall == H_QUERY_VAS_CAPABILITIES) ?
1610a006aceSNicholas Piggin 				"H_QUERY_VAS_CAPABILITIES" :
1620a006aceSNicholas Piggin 				"H_QUERY_NX_CAPABILITIES",
1630a006aceSNicholas Piggin 			rc, query_type, result);
1640a006aceSNicholas Piggin 	}
1650a006aceSNicholas Piggin 
166f33ecfdeSHaren Myneni 	return -EIO;
167f33ecfdeSHaren Myneni }
168b22f2d88SHaren Myneni EXPORT_SYMBOL_GPL(h_query_vas_capabilities);
169b22f2d88SHaren Myneni 
170b22f2d88SHaren Myneni /*
1716d0aaf5eSHaren Myneni  * hcall to get fault CRB from the hypervisor.
1726d0aaf5eSHaren Myneni  */
h_get_nx_fault(u32 winid,u64 buffer)1736d0aaf5eSHaren Myneni static int h_get_nx_fault(u32 winid, u64 buffer)
1746d0aaf5eSHaren Myneni {
1756d0aaf5eSHaren Myneni 	long rc;
1766d0aaf5eSHaren Myneni 
1776d0aaf5eSHaren Myneni 	rc = plpar_hcall_norets(H_GET_NX_FAULT, winid, buffer);
1786d0aaf5eSHaren Myneni 
1796d0aaf5eSHaren Myneni 	if (rc == H_SUCCESS)
1806d0aaf5eSHaren Myneni 		return 0;
1816d0aaf5eSHaren Myneni 
1826d0aaf5eSHaren Myneni 	pr_err("H_GET_NX_FAULT error: %ld, winid %u, buffer 0x%llx\n",
1836d0aaf5eSHaren Myneni 		rc, winid, buffer);
1846d0aaf5eSHaren Myneni 	return -EIO;
1856d0aaf5eSHaren Myneni 
1866d0aaf5eSHaren Myneni }
1876d0aaf5eSHaren Myneni 
1886d0aaf5eSHaren Myneni /*
1896d0aaf5eSHaren Myneni  * Handle the fault interrupt.
1906d0aaf5eSHaren Myneni  * When the fault interrupt is received for each window, query the
1916d0aaf5eSHaren Myneni  * hypervisor to get the fault CRB on the specific fault. Then
1926d0aaf5eSHaren Myneni  * process the CRB by updating CSB or send signal if the user space
1936d0aaf5eSHaren Myneni  * CSB is invalid.
1946d0aaf5eSHaren Myneni  * Note: The hypervisor forwards an interrupt for each fault request.
1956d0aaf5eSHaren Myneni  *	So one fault CRB to process for each H_GET_NX_FAULT hcall.
1966d0aaf5eSHaren Myneni  */
pseries_vas_fault_thread_fn(int irq,void * data)1974cb26607SCédric Le Goater static irqreturn_t pseries_vas_fault_thread_fn(int irq, void *data)
1986d0aaf5eSHaren Myneni {
1996d0aaf5eSHaren Myneni 	struct pseries_vas_window *txwin = data;
2006d0aaf5eSHaren Myneni 	struct coprocessor_request_block crb;
2016d0aaf5eSHaren Myneni 	struct vas_user_win_ref *tsk_ref;
2026d0aaf5eSHaren Myneni 	int rc;
2036d0aaf5eSHaren Myneni 
20489ed0b76SHaren Myneni 	while (atomic_read(&txwin->pending_faults)) {
2056d0aaf5eSHaren Myneni 		rc = h_get_nx_fault(txwin->vas_win.winid, (u64)virt_to_phys(&crb));
2066d0aaf5eSHaren Myneni 		if (!rc) {
2076d0aaf5eSHaren Myneni 			tsk_ref = &txwin->vas_win.task_ref;
2086d0aaf5eSHaren Myneni 			vas_dump_crb(&crb);
2096d0aaf5eSHaren Myneni 			vas_update_csb(&crb, tsk_ref);
2106d0aaf5eSHaren Myneni 		}
21189ed0b76SHaren Myneni 		atomic_dec(&txwin->pending_faults);
21289ed0b76SHaren Myneni 	}
2136d0aaf5eSHaren Myneni 
2146d0aaf5eSHaren Myneni 	return IRQ_HANDLED;
2156d0aaf5eSHaren Myneni }
2166d0aaf5eSHaren Myneni 
2176d0aaf5eSHaren Myneni /*
21889ed0b76SHaren Myneni  * irq_default_primary_handler() can be used only with IRQF_ONESHOT
21989ed0b76SHaren Myneni  * which disables IRQ before executing the thread handler and enables
22089ed0b76SHaren Myneni  * it after. But this disabling interrupt sets the VAS IRQ OFF
22189ed0b76SHaren Myneni  * state in the hypervisor. If the NX generates fault interrupt
22289ed0b76SHaren Myneni  * during this window, the hypervisor will not deliver this
22389ed0b76SHaren Myneni  * interrupt to the LPAR. So use VAS specific IRQ handler instead
22489ed0b76SHaren Myneni  * of calling the default primary handler.
22589ed0b76SHaren Myneni  */
pseries_vas_irq_handler(int irq,void * data)22689ed0b76SHaren Myneni static irqreturn_t pseries_vas_irq_handler(int irq, void *data)
22789ed0b76SHaren Myneni {
22889ed0b76SHaren Myneni 	struct pseries_vas_window *txwin = data;
22989ed0b76SHaren Myneni 
23089ed0b76SHaren Myneni 	/*
23189ed0b76SHaren Myneni 	 * The thread hanlder will process this interrupt if it is
23289ed0b76SHaren Myneni 	 * already running.
23389ed0b76SHaren Myneni 	 */
23489ed0b76SHaren Myneni 	atomic_inc(&txwin->pending_faults);
23589ed0b76SHaren Myneni 
23689ed0b76SHaren Myneni 	return IRQ_WAKE_THREAD;
23789ed0b76SHaren Myneni }
23889ed0b76SHaren Myneni 
23989ed0b76SHaren Myneni /*
240b22f2d88SHaren Myneni  * Allocate window and setup IRQ mapping.
241b22f2d88SHaren Myneni  */
allocate_setup_window(struct pseries_vas_window * txwin,u64 * domain,u8 wintype)242b22f2d88SHaren Myneni static int allocate_setup_window(struct pseries_vas_window *txwin,
243b22f2d88SHaren Myneni 				 u64 *domain, u8 wintype)
244b22f2d88SHaren Myneni {
245b22f2d88SHaren Myneni 	int rc;
246b22f2d88SHaren Myneni 
247b22f2d88SHaren Myneni 	rc = h_allocate_vas_window(txwin, domain, wintype, DEF_WIN_CREDS);
248b22f2d88SHaren Myneni 	if (rc)
249b22f2d88SHaren Myneni 		return rc;
2506d0aaf5eSHaren Myneni 	/*
2516d0aaf5eSHaren Myneni 	 * On PowerVM, the hypervisor setup and forwards the fault
2526d0aaf5eSHaren Myneni 	 * interrupt per window. So the IRQ setup and fault handling
2536d0aaf5eSHaren Myneni 	 * will be done for each open window separately.
2546d0aaf5eSHaren Myneni 	 */
2556d0aaf5eSHaren Myneni 	txwin->fault_virq = irq_create_mapping(NULL, txwin->fault_irq);
2566d0aaf5eSHaren Myneni 	if (!txwin->fault_virq) {
2576d0aaf5eSHaren Myneni 		pr_err("Failed irq mapping %d\n", txwin->fault_irq);
2586d0aaf5eSHaren Myneni 		rc = -EINVAL;
2596d0aaf5eSHaren Myneni 		goto out_win;
2606d0aaf5eSHaren Myneni 	}
2616d0aaf5eSHaren Myneni 
2626d0aaf5eSHaren Myneni 	txwin->name = kasprintf(GFP_KERNEL, "vas-win-%d",
2636d0aaf5eSHaren Myneni 				txwin->vas_win.winid);
2646d0aaf5eSHaren Myneni 	if (!txwin->name) {
2656d0aaf5eSHaren Myneni 		rc = -ENOMEM;
2666d0aaf5eSHaren Myneni 		goto out_irq;
2676d0aaf5eSHaren Myneni 	}
2686d0aaf5eSHaren Myneni 
26989ed0b76SHaren Myneni 	rc = request_threaded_irq(txwin->fault_virq,
27089ed0b76SHaren Myneni 				  pseries_vas_irq_handler,
27189ed0b76SHaren Myneni 				  pseries_vas_fault_thread_fn, 0,
2726d0aaf5eSHaren Myneni 				  txwin->name, txwin);
2736d0aaf5eSHaren Myneni 	if (rc) {
2746d0aaf5eSHaren Myneni 		pr_err("VAS-Window[%d]: Request IRQ(%u) failed with %d\n",
2756d0aaf5eSHaren Myneni 		       txwin->vas_win.winid, txwin->fault_virq, rc);
2766d0aaf5eSHaren Myneni 		goto out_free;
2776d0aaf5eSHaren Myneni 	}
278b22f2d88SHaren Myneni 
279b22f2d88SHaren Myneni 	txwin->vas_win.wcreds_max = DEF_WIN_CREDS;
280b22f2d88SHaren Myneni 
281b22f2d88SHaren Myneni 	return 0;
2826d0aaf5eSHaren Myneni out_free:
2836d0aaf5eSHaren Myneni 	kfree(txwin->name);
2846d0aaf5eSHaren Myneni out_irq:
2856d0aaf5eSHaren Myneni 	irq_dispose_mapping(txwin->fault_virq);
2866d0aaf5eSHaren Myneni out_win:
2876d0aaf5eSHaren Myneni 	h_deallocate_vas_window(txwin->vas_win.winid);
2886d0aaf5eSHaren Myneni 	return rc;
2896d0aaf5eSHaren Myneni }
2906d0aaf5eSHaren Myneni 
free_irq_setup(struct pseries_vas_window * txwin)2916d0aaf5eSHaren Myneni static inline void free_irq_setup(struct pseries_vas_window *txwin)
2926d0aaf5eSHaren Myneni {
2936d0aaf5eSHaren Myneni 	free_irq(txwin->fault_virq, txwin);
2946d0aaf5eSHaren Myneni 	kfree(txwin->name);
2956d0aaf5eSHaren Myneni 	irq_dispose_mapping(txwin->fault_virq);
296b22f2d88SHaren Myneni }
297b22f2d88SHaren Myneni 
vas_allocate_window(int vas_id,u64 flags,enum vas_cop_type cop_type)298b22f2d88SHaren Myneni static struct vas_window *vas_allocate_window(int vas_id, u64 flags,
299b22f2d88SHaren Myneni 					      enum vas_cop_type cop_type)
300b22f2d88SHaren Myneni {
301b22f2d88SHaren Myneni 	long domain[PLPAR_HCALL9_BUFSIZE] = {VAS_DEFAULT_DOMAIN_ID};
302b22f2d88SHaren Myneni 	struct vas_cop_feat_caps *cop_feat_caps;
303b22f2d88SHaren Myneni 	struct vas_caps *caps;
304b22f2d88SHaren Myneni 	struct pseries_vas_window *txwin;
305b22f2d88SHaren Myneni 	int rc;
306b22f2d88SHaren Myneni 
307b22f2d88SHaren Myneni 	txwin = kzalloc(sizeof(*txwin), GFP_KERNEL);
308b22f2d88SHaren Myneni 	if (!txwin)
309b22f2d88SHaren Myneni 		return ERR_PTR(-ENOMEM);
310b22f2d88SHaren Myneni 
311b22f2d88SHaren Myneni 	/*
312b22f2d88SHaren Myneni 	 * A VAS window can have many credits which means that many
313b22f2d88SHaren Myneni 	 * requests can be issued simultaneously. But the hypervisor
314b22f2d88SHaren Myneni 	 * restricts one credit per window.
315b22f2d88SHaren Myneni 	 * The hypervisor introduces 2 different types of credits:
316b22f2d88SHaren Myneni 	 * Default credit type (Uses normal priority FIFO):
317b22f2d88SHaren Myneni 	 *	A limited number of credits are assigned to partitions
318b22f2d88SHaren Myneni 	 *	based on processor entitlement. But these credits may be
319b22f2d88SHaren Myneni 	 *	over-committed on a system depends on whether the CPUs
320b22f2d88SHaren Myneni 	 *	are in shared or dedicated modes - that is, more requests
321b22f2d88SHaren Myneni 	 *	may be issued across the system than NX can service at
322b22f2d88SHaren Myneni 	 *	once which can result in paste command failure (RMA_busy).
323b22f2d88SHaren Myneni 	 *	Then the process has to resend requests or fall-back to
324b22f2d88SHaren Myneni 	 *	SW compression.
325b22f2d88SHaren Myneni 	 * Quality of Service (QoS) credit type (Uses high priority FIFO):
326b22f2d88SHaren Myneni 	 *	To avoid NX HW contention, the system admins can assign
327b22f2d88SHaren Myneni 	 *	QoS credits for each LPAR so that this partition is
328b22f2d88SHaren Myneni 	 *	guaranteed access to NX resources. These credits are
329b22f2d88SHaren Myneni 	 *	assigned to partitions via the HMC.
330b22f2d88SHaren Myneni 	 *	Refer PAPR for more information.
331b22f2d88SHaren Myneni 	 *
332b22f2d88SHaren Myneni 	 * Allocate window with QoS credits if user requested. Otherwise
333b22f2d88SHaren Myneni 	 * default credits are used.
334b22f2d88SHaren Myneni 	 */
335b22f2d88SHaren Myneni 	if (flags & VAS_TX_WIN_FLAG_QOS_CREDIT)
336b22f2d88SHaren Myneni 		caps = &vascaps[VAS_GZIP_QOS_FEAT_TYPE];
337b22f2d88SHaren Myneni 	else
338b22f2d88SHaren Myneni 		caps = &vascaps[VAS_GZIP_DEF_FEAT_TYPE];
339b22f2d88SHaren Myneni 
340b22f2d88SHaren Myneni 	cop_feat_caps = &caps->caps;
341b22f2d88SHaren Myneni 
34240562fe4SHaren Myneni 	if (atomic_inc_return(&cop_feat_caps->nr_used_credits) >
34340562fe4SHaren Myneni 			atomic_read(&cop_feat_caps->nr_total_credits)) {
3446d21fb7dSHaren Myneni 		pr_err_ratelimited("Credits are not available to allocate window\n");
345b22f2d88SHaren Myneni 		rc = -EINVAL;
346b22f2d88SHaren Myneni 		goto out;
347b22f2d88SHaren Myneni 	}
348b22f2d88SHaren Myneni 
349b22f2d88SHaren Myneni 	if (vas_id == -1) {
350b22f2d88SHaren Myneni 		/*
351b22f2d88SHaren Myneni 		 * The user space is requesting to allocate a window on
352b22f2d88SHaren Myneni 		 * a VAS instance where the process is executing.
353b22f2d88SHaren Myneni 		 * On PowerVM, domain values are passed to the hypervisor
354b22f2d88SHaren Myneni 		 * to select VAS instance. Useful if the process is
355b22f2d88SHaren Myneni 		 * affinity to NUMA node.
356b22f2d88SHaren Myneni 		 * The hypervisor selects VAS instance if
357b22f2d88SHaren Myneni 		 * VAS_DEFAULT_DOMAIN_ID (-1) is passed for domain values.
358b22f2d88SHaren Myneni 		 * The h_allocate_vas_window hcall is defined to take a
359b22f2d88SHaren Myneni 		 * domain values as specified by h_home_node_associativity,
360b22f2d88SHaren Myneni 		 * So no unpacking needs to be done.
361b22f2d88SHaren Myneni 		 */
362b22f2d88SHaren Myneni 		rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, domain,
363f3e5d9e5SHaren Myneni 				  VPHN_FLAG_VCPU, hard_smp_processor_id());
364b22f2d88SHaren Myneni 		if (rc != H_SUCCESS) {
365b22f2d88SHaren Myneni 			pr_err("H_HOME_NODE_ASSOCIATIVITY error: %d\n", rc);
366b22f2d88SHaren Myneni 			goto out;
367b22f2d88SHaren Myneni 		}
368b22f2d88SHaren Myneni 	}
369b22f2d88SHaren Myneni 
370976410cdSHaren Myneni 	txwin->pid = mfspr(SPRN_PID);
371976410cdSHaren Myneni 
372b22f2d88SHaren Myneni 	/*
373b22f2d88SHaren Myneni 	 * Allocate / Deallocate window hcalls and setup / free IRQs
374b22f2d88SHaren Myneni 	 * have to be protected with mutex.
375b22f2d88SHaren Myneni 	 * Open VAS window: Allocate window hcall and setup IRQ
376b22f2d88SHaren Myneni 	 * Close VAS window: Deallocate window hcall and free IRQ
377b22f2d88SHaren Myneni 	 *	The hypervisor waits until all NX requests are
378b22f2d88SHaren Myneni 	 *	completed before closing the window. So expects OS
379b22f2d88SHaren Myneni 	 *	to handle NX faults, means IRQ can be freed only
380b22f2d88SHaren Myneni 	 *	after the deallocate window hcall is returned.
381b22f2d88SHaren Myneni 	 * So once the window is closed with deallocate hcall before
382b22f2d88SHaren Myneni 	 * the IRQ is freed, it can be assigned to new allocate
383b22f2d88SHaren Myneni 	 * hcall with the same fault IRQ by the hypervisor. It can
384b22f2d88SHaren Myneni 	 * result in setup IRQ fail for the new window since the
385b22f2d88SHaren Myneni 	 * same fault IRQ is not freed by the OS before.
386b22f2d88SHaren Myneni 	 */
387b22f2d88SHaren Myneni 	mutex_lock(&vas_pseries_mutex);
388*e1b45baaSHaren Myneni 	if (migration_in_progress) {
38937e67648SHaren Myneni 		rc = -EBUSY;
390*e1b45baaSHaren Myneni 	} else {
391b22f2d88SHaren Myneni 		rc = allocate_setup_window(txwin, (u64 *)&domain[0],
392b22f2d88SHaren Myneni 				   cop_feat_caps->win_type);
393*e1b45baaSHaren Myneni 		if (!rc)
394*e1b45baaSHaren Myneni 			caps->nr_open_wins_progress++;
395*e1b45baaSHaren Myneni 	}
396*e1b45baaSHaren Myneni 
397b22f2d88SHaren Myneni 	mutex_unlock(&vas_pseries_mutex);
398b22f2d88SHaren Myneni 	if (rc)
399b22f2d88SHaren Myneni 		goto out;
400b22f2d88SHaren Myneni 
401b22f2d88SHaren Myneni 	/*
402b22f2d88SHaren Myneni 	 * Modify window and it is ready to use.
403b22f2d88SHaren Myneni 	 */
404b22f2d88SHaren Myneni 	rc = h_modify_vas_window(txwin);
405b22f2d88SHaren Myneni 	if (!rc)
406b22f2d88SHaren Myneni 		rc = get_vas_user_win_ref(&txwin->vas_win.task_ref);
407b22f2d88SHaren Myneni 	if (rc)
408b22f2d88SHaren Myneni 		goto out_free;
409b22f2d88SHaren Myneni 
410b22f2d88SHaren Myneni 	txwin->win_type = cop_feat_caps->win_type;
411*e1b45baaSHaren Myneni 
4128ef7b9e1SHaren Myneni 	/*
413*e1b45baaSHaren Myneni 	 * The migration SUSPEND thread sets migration_in_progress and
414*e1b45baaSHaren Myneni 	 * closes all open windows from the list. But the window is
415*e1b45baaSHaren Myneni 	 * added to the list after open and modify HCALLs. So possible
416*e1b45baaSHaren Myneni 	 * that migration_in_progress is set before modify HCALL which
417*e1b45baaSHaren Myneni 	 * may cause some windows are still open when the hypervisor
418*e1b45baaSHaren Myneni 	 * initiates the migration.
419*e1b45baaSHaren Myneni 	 * So checks the migration_in_progress flag again and close all
420*e1b45baaSHaren Myneni 	 * open windows.
421*e1b45baaSHaren Myneni 	 *
4228ef7b9e1SHaren Myneni 	 * Possible to lose the acquired credit with DLPAR core
4238ef7b9e1SHaren Myneni 	 * removal after the window is opened. So if there are any
4248ef7b9e1SHaren Myneni 	 * closed windows (means with lost credits), do not give new
4258ef7b9e1SHaren Myneni 	 * window to user space. New windows will be opened only
4268ef7b9e1SHaren Myneni 	 * after the existing windows are reopened when credits are
4278ef7b9e1SHaren Myneni 	 * available.
4288ef7b9e1SHaren Myneni 	 */
429*e1b45baaSHaren Myneni 	mutex_lock(&vas_pseries_mutex);
430*e1b45baaSHaren Myneni 	if (!caps->nr_close_wins && !migration_in_progress) {
431b22f2d88SHaren Myneni 		list_add(&txwin->win_list, &caps->list);
4328ef7b9e1SHaren Myneni 		caps->nr_open_windows++;
433*e1b45baaSHaren Myneni 		caps->nr_open_wins_progress--;
4348ef7b9e1SHaren Myneni 		mutex_unlock(&vas_pseries_mutex);
4358ef7b9e1SHaren Myneni 		vas_user_win_add_mm_context(&txwin->vas_win.task_ref);
4368ef7b9e1SHaren Myneni 		return &txwin->vas_win;
4378ef7b9e1SHaren Myneni 	}
438b22f2d88SHaren Myneni 	mutex_unlock(&vas_pseries_mutex);
439b22f2d88SHaren Myneni 
4408ef7b9e1SHaren Myneni 	put_vas_user_win_ref(&txwin->vas_win.task_ref);
4418ef7b9e1SHaren Myneni 	rc = -EBUSY;
4426d21fb7dSHaren Myneni 	pr_err_ratelimited("No credit is available to allocate window\n");
443b22f2d88SHaren Myneni 
444b22f2d88SHaren Myneni out_free:
4456d0aaf5eSHaren Myneni 	/*
4466d0aaf5eSHaren Myneni 	 * Window is not operational. Free IRQ before closing
4476d0aaf5eSHaren Myneni 	 * window so that do not have to hold mutex.
4486d0aaf5eSHaren Myneni 	 */
4496d0aaf5eSHaren Myneni 	free_irq_setup(txwin);
450b22f2d88SHaren Myneni 	h_deallocate_vas_window(txwin->vas_win.winid);
451*e1b45baaSHaren Myneni 	/*
452*e1b45baaSHaren Myneni 	 * Hold mutex and reduce nr_open_wins_progress counter.
453*e1b45baaSHaren Myneni 	 */
454*e1b45baaSHaren Myneni 	mutex_lock(&vas_pseries_mutex);
455*e1b45baaSHaren Myneni 	caps->nr_open_wins_progress--;
456*e1b45baaSHaren Myneni 	mutex_unlock(&vas_pseries_mutex);
457b22f2d88SHaren Myneni out:
45840562fe4SHaren Myneni 	atomic_dec(&cop_feat_caps->nr_used_credits);
459b22f2d88SHaren Myneni 	kfree(txwin);
460b22f2d88SHaren Myneni 	return ERR_PTR(rc);
461b22f2d88SHaren Myneni }
462b22f2d88SHaren Myneni 
vas_paste_address(struct vas_window * vwin)463b22f2d88SHaren Myneni static u64 vas_paste_address(struct vas_window *vwin)
464b22f2d88SHaren Myneni {
465b22f2d88SHaren Myneni 	struct pseries_vas_window *win;
466b22f2d88SHaren Myneni 
467b22f2d88SHaren Myneni 	win = container_of(vwin, struct pseries_vas_window, vas_win);
468b22f2d88SHaren Myneni 	return win->win_addr;
469b22f2d88SHaren Myneni }
470b22f2d88SHaren Myneni 
deallocate_free_window(struct pseries_vas_window * win)471b22f2d88SHaren Myneni static int deallocate_free_window(struct pseries_vas_window *win)
472b22f2d88SHaren Myneni {
473b22f2d88SHaren Myneni 	int rc = 0;
474b22f2d88SHaren Myneni 
4756d0aaf5eSHaren Myneni 	/*
4766d0aaf5eSHaren Myneni 	 * The hypervisor waits for all requests including faults
4776d0aaf5eSHaren Myneni 	 * are processed before closing the window - Means all
4786d0aaf5eSHaren Myneni 	 * credits have to be returned. In the case of fault
4796d0aaf5eSHaren Myneni 	 * request, a credit is returned after OS issues
4806d0aaf5eSHaren Myneni 	 * H_GET_NX_FAULT hcall.
4816d0aaf5eSHaren Myneni 	 * So free IRQ after executing H_DEALLOCATE_VAS_WINDOW
4826d0aaf5eSHaren Myneni 	 * hcall.
4836d0aaf5eSHaren Myneni 	 */
484b22f2d88SHaren Myneni 	rc = h_deallocate_vas_window(win->vas_win.winid);
4856d0aaf5eSHaren Myneni 	if (!rc)
4866d0aaf5eSHaren Myneni 		free_irq_setup(win);
487b22f2d88SHaren Myneni 
488b22f2d88SHaren Myneni 	return rc;
489b22f2d88SHaren Myneni }
490b22f2d88SHaren Myneni 
vas_deallocate_window(struct vas_window * vwin)491b22f2d88SHaren Myneni static int vas_deallocate_window(struct vas_window *vwin)
492b22f2d88SHaren Myneni {
493b22f2d88SHaren Myneni 	struct pseries_vas_window *win;
494b22f2d88SHaren Myneni 	struct vas_cop_feat_caps *caps;
495b22f2d88SHaren Myneni 	int rc = 0;
496b22f2d88SHaren Myneni 
497b22f2d88SHaren Myneni 	if (!vwin)
498b22f2d88SHaren Myneni 		return -EINVAL;
499b22f2d88SHaren Myneni 
500b22f2d88SHaren Myneni 	win = container_of(vwin, struct pseries_vas_window, vas_win);
501b22f2d88SHaren Myneni 
502b22f2d88SHaren Myneni 	/* Should not happen */
503b22f2d88SHaren Myneni 	if (win->win_type >= VAS_MAX_FEAT_TYPE) {
504b22f2d88SHaren Myneni 		pr_err("Window (%u): Invalid window type %u\n",
505b22f2d88SHaren Myneni 				vwin->winid, win->win_type);
506b22f2d88SHaren Myneni 		return -EINVAL;
507b22f2d88SHaren Myneni 	}
508b22f2d88SHaren Myneni 
509b22f2d88SHaren Myneni 	caps = &vascaps[win->win_type].caps;
510b22f2d88SHaren Myneni 	mutex_lock(&vas_pseries_mutex);
5118ef7b9e1SHaren Myneni 	/*
5128ef7b9e1SHaren Myneni 	 * VAS window is already closed in the hypervisor when
513716d7a2eSHaren Myneni 	 * lost the credit or with migration. So just remove the entry
514716d7a2eSHaren Myneni 	 * from the list, remove task references and free vas_window
5158ef7b9e1SHaren Myneni 	 * struct.
5168ef7b9e1SHaren Myneni 	 */
517716d7a2eSHaren Myneni 	if (!(win->vas_win.status & VAS_WIN_NO_CRED_CLOSE) &&
518716d7a2eSHaren Myneni 		!(win->vas_win.status & VAS_WIN_MIGRATE_CLOSE)) {
519b22f2d88SHaren Myneni 		rc = deallocate_free_window(win);
520b22f2d88SHaren Myneni 		if (rc) {
521b22f2d88SHaren Myneni 			mutex_unlock(&vas_pseries_mutex);
522b22f2d88SHaren Myneni 			return rc;
523b22f2d88SHaren Myneni 		}
5248ef7b9e1SHaren Myneni 	} else
5258ef7b9e1SHaren Myneni 		vascaps[win->win_type].nr_close_wins--;
526b22f2d88SHaren Myneni 
527b22f2d88SHaren Myneni 	list_del(&win->win_list);
52840562fe4SHaren Myneni 	atomic_dec(&caps->nr_used_credits);
5298ef7b9e1SHaren Myneni 	vascaps[win->win_type].nr_open_windows--;
530b22f2d88SHaren Myneni 	mutex_unlock(&vas_pseries_mutex);
531b22f2d88SHaren Myneni 
532b22f2d88SHaren Myneni 	mm_context_remove_vas_window(vwin->task_ref.mm);
533b4bda59bSNicholas Piggin 	put_vas_user_win_ref(&vwin->task_ref);
534b22f2d88SHaren Myneni 
535b22f2d88SHaren Myneni 	kfree(win);
536b22f2d88SHaren Myneni 	return 0;
537b22f2d88SHaren Myneni }
538b22f2d88SHaren Myneni 
539b22f2d88SHaren Myneni static const struct vas_user_win_ops vops_pseries = {
540b22f2d88SHaren Myneni 	.open_win	= vas_allocate_window,	/* Open and configure window */
541b22f2d88SHaren Myneni 	.paste_addr	= vas_paste_address,	/* To do copy/paste */
542b22f2d88SHaren Myneni 	.close_win	= vas_deallocate_window, /* Close window */
543b22f2d88SHaren Myneni };
544b22f2d88SHaren Myneni 
545b22f2d88SHaren Myneni /*
546b22f2d88SHaren Myneni  * Supporting only nx-gzip coprocessor type now, but this API code
547b22f2d88SHaren Myneni  * extended to other coprocessor types later.
548b22f2d88SHaren Myneni  */
vas_register_api_pseries(struct module * mod,enum vas_cop_type cop_type,const char * name)549b22f2d88SHaren Myneni int vas_register_api_pseries(struct module *mod, enum vas_cop_type cop_type,
550b22f2d88SHaren Myneni 			     const char *name)
551b22f2d88SHaren Myneni {
552b22f2d88SHaren Myneni 	if (!copypaste_feat)
553b22f2d88SHaren Myneni 		return -ENOTSUPP;
554b22f2d88SHaren Myneni 
55591986d7fSye xingchen 	return vas_register_coproc_api(mod, cop_type, name, &vops_pseries);
556b22f2d88SHaren Myneni }
557b22f2d88SHaren Myneni EXPORT_SYMBOL_GPL(vas_register_api_pseries);
558b22f2d88SHaren Myneni 
vas_unregister_api_pseries(void)559b22f2d88SHaren Myneni void vas_unregister_api_pseries(void)
560b22f2d88SHaren Myneni {
561b22f2d88SHaren Myneni 	vas_unregister_coproc_api();
562b22f2d88SHaren Myneni }
563b22f2d88SHaren Myneni EXPORT_SYMBOL_GPL(vas_unregister_api_pseries);
564ca77d488SHaren Myneni 
565ca77d488SHaren Myneni /*
566ca77d488SHaren Myneni  * Get the specific capabilities based on the feature type.
567ca77d488SHaren Myneni  * Right now supports GZIP default and GZIP QoS capabilities.
568ca77d488SHaren Myneni  */
get_vas_capabilities(u8 feat,enum vas_cop_feat_type type,struct hv_vas_cop_feat_caps * hv_caps)569e14ff96dSNick Child static int __init get_vas_capabilities(u8 feat, enum vas_cop_feat_type type,
570ca77d488SHaren Myneni 				struct hv_vas_cop_feat_caps *hv_caps)
571ca77d488SHaren Myneni {
572ca77d488SHaren Myneni 	struct vas_cop_feat_caps *caps;
573ca77d488SHaren Myneni 	struct vas_caps *vcaps;
574ca77d488SHaren Myneni 	int rc = 0;
575ca77d488SHaren Myneni 
576ca77d488SHaren Myneni 	vcaps = &vascaps[type];
577ca77d488SHaren Myneni 	memset(vcaps, 0, sizeof(*vcaps));
578ca77d488SHaren Myneni 	INIT_LIST_HEAD(&vcaps->list);
579ca77d488SHaren Myneni 
5808ef7b9e1SHaren Myneni 	vcaps->feat = feat;
581ca77d488SHaren Myneni 	caps = &vcaps->caps;
582ca77d488SHaren Myneni 
583ca77d488SHaren Myneni 	rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES, feat,
584ca77d488SHaren Myneni 					  (u64)virt_to_phys(hv_caps));
585ca77d488SHaren Myneni 	if (rc)
586ca77d488SHaren Myneni 		return rc;
587ca77d488SHaren Myneni 
588ca77d488SHaren Myneni 	caps->user_mode = hv_caps->user_mode;
589ca77d488SHaren Myneni 	if (!(caps->user_mode & VAS_COPY_PASTE_USER_MODE)) {
590ca77d488SHaren Myneni 		pr_err("User space COPY/PASTE is not supported\n");
591ca77d488SHaren Myneni 		return -ENOTSUPP;
592ca77d488SHaren Myneni 	}
593ca77d488SHaren Myneni 
594ca77d488SHaren Myneni 	caps->descriptor = be64_to_cpu(hv_caps->descriptor);
595ca77d488SHaren Myneni 	caps->win_type = hv_caps->win_type;
596ca77d488SHaren Myneni 	if (caps->win_type >= VAS_MAX_FEAT_TYPE) {
597ca77d488SHaren Myneni 		pr_err("Unsupported window type %u\n", caps->win_type);
598ca77d488SHaren Myneni 		return -EINVAL;
599ca77d488SHaren Myneni 	}
600ca77d488SHaren Myneni 	caps->max_lpar_creds = be16_to_cpu(hv_caps->max_lpar_creds);
601ca77d488SHaren Myneni 	caps->max_win_creds = be16_to_cpu(hv_caps->max_win_creds);
60240562fe4SHaren Myneni 	atomic_set(&caps->nr_total_credits,
603ca77d488SHaren Myneni 		   be16_to_cpu(hv_caps->target_lpar_creds));
604ca77d488SHaren Myneni 	if (feat == VAS_GZIP_DEF_FEAT) {
605ca77d488SHaren Myneni 		caps->def_lpar_creds = be16_to_cpu(hv_caps->def_lpar_creds);
606ca77d488SHaren Myneni 
607ca77d488SHaren Myneni 		if (caps->max_win_creds < DEF_WIN_CREDS) {
608ca77d488SHaren Myneni 			pr_err("Window creds(%u) > max allowed window creds(%u)\n",
609ca77d488SHaren Myneni 			       DEF_WIN_CREDS, caps->max_win_creds);
610ca77d488SHaren Myneni 			return -EINVAL;
611ca77d488SHaren Myneni 		}
612ca77d488SHaren Myneni 	}
613ca77d488SHaren Myneni 
614b903737bSHaren Myneni 	rc = sysfs_add_vas_caps(caps);
615b903737bSHaren Myneni 	if (rc)
616b903737bSHaren Myneni 		return rc;
617b903737bSHaren Myneni 
618ca77d488SHaren Myneni 	copypaste_feat = true;
619ca77d488SHaren Myneni 
620ca77d488SHaren Myneni 	return 0;
621ca77d488SHaren Myneni }
622ca77d488SHaren Myneni 
6238ef7b9e1SHaren Myneni /*
624c656cfe5SHaren Myneni  * VAS windows can be closed due to lost credits when the core is
625c656cfe5SHaren Myneni  * removed. So reopen them if credits are available due to DLPAR
626c656cfe5SHaren Myneni  * core add and set the window active status. When NX sees the page
627c656cfe5SHaren Myneni  * fault on the unmapped paste address, the kernel handles the fault
628c656cfe5SHaren Myneni  * by setting the remapping to new paste address if the window is
629c656cfe5SHaren Myneni  * active.
630c656cfe5SHaren Myneni  */
reconfig_open_windows(struct vas_caps * vcaps,int creds,bool migrate)631716d7a2eSHaren Myneni static int reconfig_open_windows(struct vas_caps *vcaps, int creds,
632716d7a2eSHaren Myneni 				 bool migrate)
633c656cfe5SHaren Myneni {
634c656cfe5SHaren Myneni 	long domain[PLPAR_HCALL9_BUFSIZE] = {VAS_DEFAULT_DOMAIN_ID};
635c656cfe5SHaren Myneni 	struct vas_cop_feat_caps *caps = &vcaps->caps;
636c656cfe5SHaren Myneni 	struct pseries_vas_window *win = NULL, *tmp;
637c656cfe5SHaren Myneni 	int rc, mv_ents = 0;
638716d7a2eSHaren Myneni 	int flag;
639c656cfe5SHaren Myneni 
640c656cfe5SHaren Myneni 	/*
641c656cfe5SHaren Myneni 	 * Nothing to do if there are no closed windows.
642c656cfe5SHaren Myneni 	 */
643c656cfe5SHaren Myneni 	if (!vcaps->nr_close_wins)
644c656cfe5SHaren Myneni 		return 0;
645c656cfe5SHaren Myneni 
646c656cfe5SHaren Myneni 	/*
647c656cfe5SHaren Myneni 	 * For the core removal, the hypervisor reduces the credits
648c656cfe5SHaren Myneni 	 * assigned to the LPAR and the kernel closes VAS windows
649c656cfe5SHaren Myneni 	 * in the hypervisor depends on reduced credits. The kernel
650c656cfe5SHaren Myneni 	 * uses LIFO (the last windows that are opened will be closed
651c656cfe5SHaren Myneni 	 * first) and expects to open in the same order when credits
652c656cfe5SHaren Myneni 	 * are available.
653c656cfe5SHaren Myneni 	 * For example, 40 windows are closed when the LPAR lost 2 cores
654c656cfe5SHaren Myneni 	 * (dedicated). If 1 core is added, this LPAR can have 20 more
655c656cfe5SHaren Myneni 	 * credits. It means the kernel can reopen 20 windows. So move
656c656cfe5SHaren Myneni 	 * 20 entries in the VAS windows lost and reopen next 20 windows.
657716d7a2eSHaren Myneni 	 * For partition migration, reopen all windows that are closed
658716d7a2eSHaren Myneni 	 * during resume.
659c656cfe5SHaren Myneni 	 */
660716d7a2eSHaren Myneni 	if ((vcaps->nr_close_wins > creds) && !migrate)
661c656cfe5SHaren Myneni 		mv_ents = vcaps->nr_close_wins - creds;
662c656cfe5SHaren Myneni 
663c656cfe5SHaren Myneni 	list_for_each_entry_safe(win, tmp, &vcaps->list, win_list) {
664c656cfe5SHaren Myneni 		if (!mv_ents)
665c656cfe5SHaren Myneni 			break;
666c656cfe5SHaren Myneni 
667c656cfe5SHaren Myneni 		mv_ents--;
668c656cfe5SHaren Myneni 	}
669c656cfe5SHaren Myneni 
670716d7a2eSHaren Myneni 	/*
671716d7a2eSHaren Myneni 	 * Open windows if they are closed only with migration or
672716d7a2eSHaren Myneni 	 * DLPAR (lost credit) before.
673716d7a2eSHaren Myneni 	 */
674716d7a2eSHaren Myneni 	if (migrate)
675716d7a2eSHaren Myneni 		flag = VAS_WIN_MIGRATE_CLOSE;
676716d7a2eSHaren Myneni 	else
677716d7a2eSHaren Myneni 		flag = VAS_WIN_NO_CRED_CLOSE;
678716d7a2eSHaren Myneni 
679c656cfe5SHaren Myneni 	list_for_each_entry_safe_from(win, tmp, &vcaps->list, win_list) {
680c656cfe5SHaren Myneni 		/*
681716d7a2eSHaren Myneni 		 * This window is closed with DLPAR and migration events.
682716d7a2eSHaren Myneni 		 * So reopen the window with the last event.
683716d7a2eSHaren Myneni 		 * The user space is not suspended with the current
684716d7a2eSHaren Myneni 		 * migration notifier. So the user space can issue DLPAR
685716d7a2eSHaren Myneni 		 * CPU hotplug while migration in progress. In this case
686716d7a2eSHaren Myneni 		 * this window will be opened with the last event.
687c656cfe5SHaren Myneni 		 */
688716d7a2eSHaren Myneni 		if ((win->vas_win.status & VAS_WIN_NO_CRED_CLOSE) &&
689716d7a2eSHaren Myneni 			(win->vas_win.status & VAS_WIN_MIGRATE_CLOSE)) {
690716d7a2eSHaren Myneni 			win->vas_win.status &= ~flag;
691716d7a2eSHaren Myneni 			continue;
692716d7a2eSHaren Myneni 		}
693716d7a2eSHaren Myneni 
694716d7a2eSHaren Myneni 		/*
695716d7a2eSHaren Myneni 		 * Nothing to do on this window if it is not closed
696716d7a2eSHaren Myneni 		 * with this flag
697716d7a2eSHaren Myneni 		 */
698716d7a2eSHaren Myneni 		if (!(win->vas_win.status & flag))
699c656cfe5SHaren Myneni 			continue;
700c656cfe5SHaren Myneni 
701c656cfe5SHaren Myneni 		rc = allocate_setup_window(win, (u64 *)&domain[0],
702c656cfe5SHaren Myneni 					   caps->win_type);
703c656cfe5SHaren Myneni 		if (rc)
704c656cfe5SHaren Myneni 			return rc;
705c656cfe5SHaren Myneni 
706c656cfe5SHaren Myneni 		rc = h_modify_vas_window(win);
707c656cfe5SHaren Myneni 		if (rc)
708c656cfe5SHaren Myneni 			goto out;
709c656cfe5SHaren Myneni 
710c656cfe5SHaren Myneni 		mutex_lock(&win->vas_win.task_ref.mmap_mutex);
711c656cfe5SHaren Myneni 		/*
712c656cfe5SHaren Myneni 		 * Set window status to active
713c656cfe5SHaren Myneni 		 */
714716d7a2eSHaren Myneni 		win->vas_win.status &= ~flag;
715c656cfe5SHaren Myneni 		mutex_unlock(&win->vas_win.task_ref.mmap_mutex);
716c656cfe5SHaren Myneni 		win->win_type = caps->win_type;
717c656cfe5SHaren Myneni 		if (!--vcaps->nr_close_wins)
718c656cfe5SHaren Myneni 			break;
719c656cfe5SHaren Myneni 	}
720c656cfe5SHaren Myneni 
721c656cfe5SHaren Myneni 	return 0;
722c656cfe5SHaren Myneni out:
723c656cfe5SHaren Myneni 	/*
724c656cfe5SHaren Myneni 	 * Window modify HCALL failed. So close the window to the
725c656cfe5SHaren Myneni 	 * hypervisor and return.
726c656cfe5SHaren Myneni 	 */
727c656cfe5SHaren Myneni 	free_irq_setup(win);
728c656cfe5SHaren Myneni 	h_deallocate_vas_window(win->vas_win.winid);
729c656cfe5SHaren Myneni 	return rc;
730c656cfe5SHaren Myneni }
731c656cfe5SHaren Myneni 
732c656cfe5SHaren Myneni /*
7338ef7b9e1SHaren Myneni  * The hypervisor reduces the available credits if the LPAR lost core. It
7348ef7b9e1SHaren Myneni  * means the excessive windows should not be active and the user space
7358ef7b9e1SHaren Myneni  * should not be using these windows to send compression requests to NX.
7368ef7b9e1SHaren Myneni  * So the kernel closes the excessive windows and unmap the paste address
7378ef7b9e1SHaren Myneni  * such that the user space receives paste instruction failure. Then up to
7388ef7b9e1SHaren Myneni  * the user space to fall back to SW compression and manage with the
7398ef7b9e1SHaren Myneni  * existing windows.
7408ef7b9e1SHaren Myneni  */
reconfig_close_windows(struct vas_caps * vcap,int excess_creds,bool migrate)741716d7a2eSHaren Myneni static int reconfig_close_windows(struct vas_caps *vcap, int excess_creds,
742716d7a2eSHaren Myneni 									bool migrate)
7438ef7b9e1SHaren Myneni {
7448ef7b9e1SHaren Myneni 	struct pseries_vas_window *win, *tmp;
7458ef7b9e1SHaren Myneni 	struct vas_user_win_ref *task_ref;
7468ef7b9e1SHaren Myneni 	struct vm_area_struct *vma;
747716d7a2eSHaren Myneni 	int rc = 0, flag;
748716d7a2eSHaren Myneni 
749716d7a2eSHaren Myneni 	if (migrate)
750716d7a2eSHaren Myneni 		flag = VAS_WIN_MIGRATE_CLOSE;
751716d7a2eSHaren Myneni 	else
752716d7a2eSHaren Myneni 		flag = VAS_WIN_NO_CRED_CLOSE;
7538ef7b9e1SHaren Myneni 
7548ef7b9e1SHaren Myneni 	list_for_each_entry_safe(win, tmp, &vcap->list, win_list) {
7558ef7b9e1SHaren Myneni 		/*
7568ef7b9e1SHaren Myneni 		 * This window is already closed due to lost credit
757716d7a2eSHaren Myneni 		 * or for migration before. Go for next window.
758716d7a2eSHaren Myneni 		 * For migration, nothing to do since this window
759716d7a2eSHaren Myneni 		 * closed for DLPAR and will be reopened even on
760716d7a2eSHaren Myneni 		 * the destination system with other DLPAR operation.
7618ef7b9e1SHaren Myneni 		 */
762716d7a2eSHaren Myneni 		if ((win->vas_win.status & VAS_WIN_MIGRATE_CLOSE) ||
763716d7a2eSHaren Myneni 			(win->vas_win.status & VAS_WIN_NO_CRED_CLOSE)) {
764716d7a2eSHaren Myneni 			win->vas_win.status |= flag;
7658ef7b9e1SHaren Myneni 			continue;
766716d7a2eSHaren Myneni 		}
7678ef7b9e1SHaren Myneni 
7688ef7b9e1SHaren Myneni 		task_ref = &win->vas_win.task_ref;
769b59c9dc4SHaren Myneni 		/*
770b59c9dc4SHaren Myneni 		 * VAS mmap (coproc_mmap()) and its fault handler
771b59c9dc4SHaren Myneni 		 * (vas_mmap_fault()) are called after holding mmap lock.
772b59c9dc4SHaren Myneni 		 * So hold mmap mutex after mmap_lock to avoid deadlock.
773b59c9dc4SHaren Myneni 		 */
774b59c9dc4SHaren Myneni 		mmap_write_lock(task_ref->mm);
7758ef7b9e1SHaren Myneni 		mutex_lock(&task_ref->mmap_mutex);
7768ef7b9e1SHaren Myneni 		vma = task_ref->vma;
7778ef7b9e1SHaren Myneni 		/*
7788ef7b9e1SHaren Myneni 		 * Number of available credits are reduced, So select
7798ef7b9e1SHaren Myneni 		 * and close windows.
7808ef7b9e1SHaren Myneni 		 */
781716d7a2eSHaren Myneni 		win->vas_win.status |= flag;
7828ef7b9e1SHaren Myneni 
7838ef7b9e1SHaren Myneni 		/*
7848ef7b9e1SHaren Myneni 		 * vma is set in the original mapping. But this mapping
7858ef7b9e1SHaren Myneni 		 * is done with mmap() after the window is opened with ioctl.
7868ef7b9e1SHaren Myneni 		 * so we may not see the original mapping if the core remove
7878ef7b9e1SHaren Myneni 		 * is done before the original mmap() and after the ioctl.
7888ef7b9e1SHaren Myneni 		 */
7898ef7b9e1SHaren Myneni 		if (vma)
790e9adcfecSMike Kravetz 			zap_vma_pages(vma);
7918ef7b9e1SHaren Myneni 
7928ef7b9e1SHaren Myneni 		mutex_unlock(&task_ref->mmap_mutex);
793b59c9dc4SHaren Myneni 		mmap_write_unlock(task_ref->mm);
7948ef7b9e1SHaren Myneni 		/*
7958ef7b9e1SHaren Myneni 		 * Close VAS window in the hypervisor, but do not
7968ef7b9e1SHaren Myneni 		 * free vas_window struct since it may be reused
7978ef7b9e1SHaren Myneni 		 * when the credit is available later (DLPAR with
7988ef7b9e1SHaren Myneni 		 * adding cores). This struct will be used
7998ef7b9e1SHaren Myneni 		 * later when the process issued with close(FD).
8008ef7b9e1SHaren Myneni 		 */
8018ef7b9e1SHaren Myneni 		rc = deallocate_free_window(win);
802716d7a2eSHaren Myneni 		/*
803716d7a2eSHaren Myneni 		 * This failure is from the hypervisor.
804716d7a2eSHaren Myneni 		 * No way to stop migration for these failures.
805716d7a2eSHaren Myneni 		 * So ignore error and continue closing other windows.
806716d7a2eSHaren Myneni 		 */
807716d7a2eSHaren Myneni 		if (rc && !migrate)
8088ef7b9e1SHaren Myneni 			return rc;
8098ef7b9e1SHaren Myneni 
8108ef7b9e1SHaren Myneni 		vcap->nr_close_wins++;
8118ef7b9e1SHaren Myneni 
812716d7a2eSHaren Myneni 		/*
813716d7a2eSHaren Myneni 		 * For migration, do not depend on lpar_creds in case if
814716d7a2eSHaren Myneni 		 * mismatch with the hypervisor value (should not happen).
815716d7a2eSHaren Myneni 		 * So close all active windows in the list and will be
816716d7a2eSHaren Myneni 		 * reopened windows based on the new lpar_creds on the
817716d7a2eSHaren Myneni 		 * destination system during resume.
818716d7a2eSHaren Myneni 		 */
819716d7a2eSHaren Myneni 		if (!migrate && !--excess_creds)
8208ef7b9e1SHaren Myneni 			break;
8218ef7b9e1SHaren Myneni 	}
8228ef7b9e1SHaren Myneni 
8238ef7b9e1SHaren Myneni 	return 0;
8248ef7b9e1SHaren Myneni }
8258ef7b9e1SHaren Myneni 
8268ef7b9e1SHaren Myneni /*
8278ef7b9e1SHaren Myneni  * Get new VAS capabilities when the core add/removal configuration
8288ef7b9e1SHaren Myneni  * changes. Reconfig window configurations based on the credits
8298ef7b9e1SHaren Myneni  * availability from this new capabilities.
8308ef7b9e1SHaren Myneni  */
vas_reconfig_capabilties(u8 type,int new_nr_creds)83157831bfbSHaren Myneni int vas_reconfig_capabilties(u8 type, int new_nr_creds)
8328ef7b9e1SHaren Myneni {
8338ef7b9e1SHaren Myneni 	struct vas_cop_feat_caps *caps;
83457831bfbSHaren Myneni 	int old_nr_creds;
8358ef7b9e1SHaren Myneni 	struct vas_caps *vcaps;
8368ef7b9e1SHaren Myneni 	int rc = 0, nr_active_wins;
8378ef7b9e1SHaren Myneni 
8388ef7b9e1SHaren Myneni 	if (type >= VAS_MAX_FEAT_TYPE) {
8398ef7b9e1SHaren Myneni 		pr_err("Invalid credit type %d\n", type);
8408ef7b9e1SHaren Myneni 		return -EINVAL;
8418ef7b9e1SHaren Myneni 	}
8428ef7b9e1SHaren Myneni 
8438ef7b9e1SHaren Myneni 	vcaps = &vascaps[type];
8448ef7b9e1SHaren Myneni 	caps = &vcaps->caps;
8458ef7b9e1SHaren Myneni 
8468ef7b9e1SHaren Myneni 	mutex_lock(&vas_pseries_mutex);
8478ef7b9e1SHaren Myneni 
8488ef7b9e1SHaren Myneni 	old_nr_creds = atomic_read(&caps->nr_total_credits);
8498ef7b9e1SHaren Myneni 
8508ef7b9e1SHaren Myneni 	atomic_set(&caps->nr_total_credits, new_nr_creds);
8518ef7b9e1SHaren Myneni 	/*
8528ef7b9e1SHaren Myneni 	 * The total number of available credits may be decreased or
8531fd02f66SJulia Lawall 	 * increased with DLPAR operation. Means some windows have to be
8548ef7b9e1SHaren Myneni 	 * closed / reopened. Hold the vas_pseries_mutex so that the
855901a30cfSJason Wang 	 * user space can not open new windows.
8568ef7b9e1SHaren Myneni 	 */
857c656cfe5SHaren Myneni 	if (old_nr_creds <  new_nr_creds) {
858c656cfe5SHaren Myneni 		/*
859c656cfe5SHaren Myneni 		 * If the existing target credits is less than the new
860c656cfe5SHaren Myneni 		 * target, reopen windows if they are closed due to
861c656cfe5SHaren Myneni 		 * the previous DLPAR (core removal).
862c656cfe5SHaren Myneni 		 */
863716d7a2eSHaren Myneni 		rc = reconfig_open_windows(vcaps, new_nr_creds - old_nr_creds,
864716d7a2eSHaren Myneni 					   false);
865c656cfe5SHaren Myneni 	} else {
8668ef7b9e1SHaren Myneni 		/*
8678ef7b9e1SHaren Myneni 		 * # active windows is more than new LPAR available
8688ef7b9e1SHaren Myneni 		 * credits. So close the excessive windows.
8698ef7b9e1SHaren Myneni 		 * On pseries, each window will have 1 credit.
8708ef7b9e1SHaren Myneni 		 */
8718ef7b9e1SHaren Myneni 		nr_active_wins = vcaps->nr_open_windows - vcaps->nr_close_wins;
8728ef7b9e1SHaren Myneni 		if (nr_active_wins > new_nr_creds)
8738ef7b9e1SHaren Myneni 			rc = reconfig_close_windows(vcaps,
874716d7a2eSHaren Myneni 					nr_active_wins - new_nr_creds,
875716d7a2eSHaren Myneni 					false);
8768ef7b9e1SHaren Myneni 	}
8778ef7b9e1SHaren Myneni 
8788ef7b9e1SHaren Myneni 	mutex_unlock(&vas_pseries_mutex);
8798ef7b9e1SHaren Myneni 	return rc;
8808ef7b9e1SHaren Myneni }
8812147783dSHaren Myneni 
pseries_vas_dlpar_cpu(void)8822147783dSHaren Myneni int pseries_vas_dlpar_cpu(void)
8832147783dSHaren Myneni {
8842147783dSHaren Myneni 	int new_nr_creds, rc;
8852147783dSHaren Myneni 
886eca9f6e6SHaren Myneni 	/*
887eca9f6e6SHaren Myneni 	 * NX-GZIP is not enabled. Nothing to do for DLPAR event
888eca9f6e6SHaren Myneni 	 */
889eca9f6e6SHaren Myneni 	if (!copypaste_feat)
890eca9f6e6SHaren Myneni 		return 0;
891eca9f6e6SHaren Myneni 
892eca9f6e6SHaren Myneni 
8932147783dSHaren Myneni 	rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES,
8942147783dSHaren Myneni 				      vascaps[VAS_GZIP_DEF_FEAT_TYPE].feat,
8952147783dSHaren Myneni 				      (u64)virt_to_phys(&hv_cop_caps));
8962147783dSHaren Myneni 	if (!rc) {
8972147783dSHaren Myneni 		new_nr_creds = be16_to_cpu(hv_cop_caps.target_lpar_creds);
8982147783dSHaren Myneni 		rc = vas_reconfig_capabilties(VAS_GZIP_DEF_FEAT_TYPE, new_nr_creds);
8992147783dSHaren Myneni 	}
9002147783dSHaren Myneni 
9012147783dSHaren Myneni 	if (rc)
9022147783dSHaren Myneni 		pr_err("Failed reconfig VAS capabilities with DLPAR\n");
9032147783dSHaren Myneni 
9042147783dSHaren Myneni 	return rc;
9052147783dSHaren Myneni }
9062147783dSHaren Myneni 
9078ef7b9e1SHaren Myneni /*
9088ef7b9e1SHaren Myneni  * Total number of default credits available (target_credits)
9098ef7b9e1SHaren Myneni  * in LPAR depends on number of cores configured. It varies based on
9108ef7b9e1SHaren Myneni  * whether processors are in shared mode or dedicated mode.
9118ef7b9e1SHaren Myneni  * Get the notifier when CPU configuration is changed with DLPAR
9128ef7b9e1SHaren Myneni  * operation so that get the new target_credits (vas default capabilities)
9138ef7b9e1SHaren Myneni  * and then update the existing windows usage if needed.
9148ef7b9e1SHaren Myneni  */
pseries_vas_notifier(struct notifier_block * nb,unsigned long action,void * data)9158ef7b9e1SHaren Myneni static int pseries_vas_notifier(struct notifier_block *nb,
9168ef7b9e1SHaren Myneni 				unsigned long action, void *data)
9178ef7b9e1SHaren Myneni {
9188ef7b9e1SHaren Myneni 	struct of_reconfig_data *rd = data;
9198ef7b9e1SHaren Myneni 	struct device_node *dn = rd->dn;
9208ef7b9e1SHaren Myneni 	const __be32 *intserv = NULL;
9212147783dSHaren Myneni 	int len;
9222147783dSHaren Myneni 
9232147783dSHaren Myneni 	/*
9242147783dSHaren Myneni 	 * For shared CPU partition, the hypervisor assigns total credits
9252147783dSHaren Myneni 	 * based on entitled core capacity. So updating VAS windows will
9262147783dSHaren Myneni 	 * be called from lparcfg_write().
9272147783dSHaren Myneni 	 */
9282147783dSHaren Myneni 	if (is_shared_processor())
9292147783dSHaren Myneni 		return NOTIFY_OK;
9308ef7b9e1SHaren Myneni 
9318ef7b9e1SHaren Myneni 	if ((action == OF_RECONFIG_ATTACH_NODE) ||
9328ef7b9e1SHaren Myneni 		(action == OF_RECONFIG_DETACH_NODE))
9338ef7b9e1SHaren Myneni 		intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s",
9348ef7b9e1SHaren Myneni 					  &len);
9358ef7b9e1SHaren Myneni 	/*
9368ef7b9e1SHaren Myneni 	 * Processor config is not changed
9378ef7b9e1SHaren Myneni 	 */
9388ef7b9e1SHaren Myneni 	if (!intserv)
9398ef7b9e1SHaren Myneni 		return NOTIFY_OK;
9408ef7b9e1SHaren Myneni 
9412147783dSHaren Myneni 	return pseries_vas_dlpar_cpu();
9428ef7b9e1SHaren Myneni }
9438ef7b9e1SHaren Myneni 
9448ef7b9e1SHaren Myneni static struct notifier_block pseries_vas_nb = {
9458ef7b9e1SHaren Myneni 	.notifier_call = pseries_vas_notifier,
9468ef7b9e1SHaren Myneni };
9478ef7b9e1SHaren Myneni 
94837e67648SHaren Myneni /*
94937e67648SHaren Myneni  * For LPM, all windows have to be closed on the source partition
95037e67648SHaren Myneni  * before migration and reopen them on the destination partition
95137e67648SHaren Myneni  * after migration. So closing windows during suspend and
95237e67648SHaren Myneni  * reopen them during resume.
95337e67648SHaren Myneni  */
vas_migration_handler(int action)95437e67648SHaren Myneni int vas_migration_handler(int action)
95537e67648SHaren Myneni {
95637e67648SHaren Myneni 	struct vas_cop_feat_caps *caps;
95737e67648SHaren Myneni 	int old_nr_creds, new_nr_creds = 0;
95837e67648SHaren Myneni 	struct vas_caps *vcaps;
95937e67648SHaren Myneni 	int i, rc = 0;
96037e67648SHaren Myneni 
961*e1b45baaSHaren Myneni 	pr_info("VAS migration event %d\n", action);
962*e1b45baaSHaren Myneni 
96337e67648SHaren Myneni 	/*
96437e67648SHaren Myneni 	 * NX-GZIP is not enabled. Nothing to do for migration.
96537e67648SHaren Myneni 	 */
96637e67648SHaren Myneni 	if (!copypaste_feat)
96737e67648SHaren Myneni 		return rc;
96837e67648SHaren Myneni 
96937e67648SHaren Myneni 	if (action == VAS_SUSPEND)
97037e67648SHaren Myneni 		migration_in_progress = true;
97137e67648SHaren Myneni 	else
97237e67648SHaren Myneni 		migration_in_progress = false;
97337e67648SHaren Myneni 
97437e67648SHaren Myneni 	for (i = 0; i < VAS_MAX_FEAT_TYPE; i++) {
97537e67648SHaren Myneni 		vcaps = &vascaps[i];
97637e67648SHaren Myneni 		caps = &vcaps->caps;
97737e67648SHaren Myneni 		old_nr_creds = atomic_read(&caps->nr_total_credits);
97837e67648SHaren Myneni 
97937e67648SHaren Myneni 		rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES,
98037e67648SHaren Myneni 					      vcaps->feat,
98137e67648SHaren Myneni 					      (u64)virt_to_phys(&hv_cop_caps));
98237e67648SHaren Myneni 		if (!rc) {
98337e67648SHaren Myneni 			new_nr_creds = be16_to_cpu(hv_cop_caps.target_lpar_creds);
98437e67648SHaren Myneni 			/*
98537e67648SHaren Myneni 			 * Should not happen. But incase print messages, close
98637e67648SHaren Myneni 			 * all windows in the list during suspend and reopen
98737e67648SHaren Myneni 			 * windows based on new lpar_creds on the destination
98837e67648SHaren Myneni 			 * system.
98937e67648SHaren Myneni 			 */
99037e67648SHaren Myneni 			if (old_nr_creds != new_nr_creds) {
99137e67648SHaren Myneni 				pr_err("Target credits mismatch with the hypervisor\n");
99237e67648SHaren Myneni 				pr_err("state(%d): lpar creds: %d HV lpar creds: %d\n",
99337e67648SHaren Myneni 					action, old_nr_creds, new_nr_creds);
99437e67648SHaren Myneni 				pr_err("Used creds: %d, Active creds: %d\n",
99537e67648SHaren Myneni 					atomic_read(&caps->nr_used_credits),
99637e67648SHaren Myneni 					vcaps->nr_open_windows - vcaps->nr_close_wins);
99737e67648SHaren Myneni 			}
99837e67648SHaren Myneni 		} else {
99937e67648SHaren Myneni 			pr_err("state(%d): Get VAS capabilities failed with %d\n",
100037e67648SHaren Myneni 				action, rc);
100137e67648SHaren Myneni 			/*
100237e67648SHaren Myneni 			 * We can not stop migration with the current lpm
100337e67648SHaren Myneni 			 * implementation. So continue closing all windows in
100437e67648SHaren Myneni 			 * the list (during suspend) and return without
100537e67648SHaren Myneni 			 * opening windows (during resume) if VAS capabilities
100637e67648SHaren Myneni 			 * HCALL failed.
100737e67648SHaren Myneni 			 */
100837e67648SHaren Myneni 			if (action == VAS_RESUME)
100937e67648SHaren Myneni 				goto out;
101037e67648SHaren Myneni 		}
101137e67648SHaren Myneni 
101237e67648SHaren Myneni 		switch (action) {
101337e67648SHaren Myneni 		case VAS_SUSPEND:
1014*e1b45baaSHaren Myneni 			mutex_lock(&vas_pseries_mutex);
101537e67648SHaren Myneni 			rc = reconfig_close_windows(vcaps, vcaps->nr_open_windows,
101637e67648SHaren Myneni 							true);
1017*e1b45baaSHaren Myneni 			/*
1018*e1b45baaSHaren Myneni 			 * Windows are included in the list after successful
1019*e1b45baaSHaren Myneni 			 * open. So wait for closing these in-progress open
1020*e1b45baaSHaren Myneni 			 * windows in vas_allocate_window() which will be
1021*e1b45baaSHaren Myneni 			 * done if the migration_in_progress is set.
1022*e1b45baaSHaren Myneni 			 */
1023*e1b45baaSHaren Myneni 			while (vcaps->nr_open_wins_progress) {
1024*e1b45baaSHaren Myneni 				mutex_unlock(&vas_pseries_mutex);
1025*e1b45baaSHaren Myneni 				msleep(10);
1026*e1b45baaSHaren Myneni 				mutex_lock(&vas_pseries_mutex);
1027*e1b45baaSHaren Myneni 			}
1028*e1b45baaSHaren Myneni 			mutex_unlock(&vas_pseries_mutex);
102937e67648SHaren Myneni 			break;
103037e67648SHaren Myneni 		case VAS_RESUME:
1031*e1b45baaSHaren Myneni 			mutex_lock(&vas_pseries_mutex);
103237e67648SHaren Myneni 			atomic_set(&caps->nr_total_credits, new_nr_creds);
103337e67648SHaren Myneni 			rc = reconfig_open_windows(vcaps, new_nr_creds, true);
1034*e1b45baaSHaren Myneni 			mutex_unlock(&vas_pseries_mutex);
103537e67648SHaren Myneni 			break;
103637e67648SHaren Myneni 		default:
103737e67648SHaren Myneni 			/* should not happen */
103837e67648SHaren Myneni 			pr_err("Invalid migration action %d\n", action);
103937e67648SHaren Myneni 			rc = -EINVAL;
104037e67648SHaren Myneni 			goto out;
104137e67648SHaren Myneni 		}
104237e67648SHaren Myneni 
104337e67648SHaren Myneni 		/*
104437e67648SHaren Myneni 		 * Ignore errors during suspend and return for resume.
104537e67648SHaren Myneni 		 */
104637e67648SHaren Myneni 		if (rc && (action == VAS_RESUME))
104737e67648SHaren Myneni 			goto out;
104837e67648SHaren Myneni 	}
104937e67648SHaren Myneni 
1050*e1b45baaSHaren Myneni 	pr_info("VAS migration event (%d) successful\n", action);
1051*e1b45baaSHaren Myneni 
105237e67648SHaren Myneni out:
105337e67648SHaren Myneni 	return rc;
105437e67648SHaren Myneni }
105537e67648SHaren Myneni 
pseries_vas_init(void)1056ca77d488SHaren Myneni static int __init pseries_vas_init(void)
1057ca77d488SHaren Myneni {
1058ca77d488SHaren Myneni 	struct hv_vas_all_caps *hv_caps;
1059278fe1ccSHaren Myneni 	int rc = 0;
1060ca77d488SHaren Myneni 
1061ca77d488SHaren Myneni 	/*
1062ca77d488SHaren Myneni 	 * Linux supports user space COPY/PASTE only with Radix
1063ca77d488SHaren Myneni 	 */
1064ca77d488SHaren Myneni 	if (!radix_enabled()) {
1065eca9f6e6SHaren Myneni 		copypaste_feat = false;
1066ca77d488SHaren Myneni 		pr_err("API is supported only with radix page tables\n");
1067ca77d488SHaren Myneni 		return -ENOTSUPP;
1068ca77d488SHaren Myneni 	}
1069ca77d488SHaren Myneni 
1070ca77d488SHaren Myneni 	hv_caps = kmalloc(sizeof(*hv_caps), GFP_KERNEL);
1071ca77d488SHaren Myneni 	if (!hv_caps)
1072ca77d488SHaren Myneni 		return -ENOMEM;
1073ca77d488SHaren Myneni 	/*
1074ca77d488SHaren Myneni 	 * Get VAS overall capabilities by passing 0 to feature type.
1075ca77d488SHaren Myneni 	 */
1076ca77d488SHaren Myneni 	rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES, 0,
1077ca77d488SHaren Myneni 					  (u64)virt_to_phys(hv_caps));
1078ca77d488SHaren Myneni 	if (rc)
1079ca77d488SHaren Myneni 		goto out;
1080ca77d488SHaren Myneni 
1081ca77d488SHaren Myneni 	caps_all.descriptor = be64_to_cpu(hv_caps->descriptor);
1082ca77d488SHaren Myneni 	caps_all.feat_type = be64_to_cpu(hv_caps->feat_type);
1083ca77d488SHaren Myneni 
1084b903737bSHaren Myneni 	sysfs_pseries_vas_init(&caps_all);
1085b903737bSHaren Myneni 
1086ca77d488SHaren Myneni 	/*
1087ca77d488SHaren Myneni 	 * QOS capabilities available
1088ca77d488SHaren Myneni 	 */
1089ca77d488SHaren Myneni 	if (caps_all.feat_type & VAS_GZIP_QOS_FEAT_BIT) {
1090ca77d488SHaren Myneni 		rc = get_vas_capabilities(VAS_GZIP_QOS_FEAT,
1091278fe1ccSHaren Myneni 					  VAS_GZIP_QOS_FEAT_TYPE, &hv_cop_caps);
1092ca77d488SHaren Myneni 
1093ca77d488SHaren Myneni 		if (rc)
1094278fe1ccSHaren Myneni 			goto out;
1095ca77d488SHaren Myneni 	}
1096ca77d488SHaren Myneni 	/*
1097ca77d488SHaren Myneni 	 * Default capabilities available
1098ca77d488SHaren Myneni 	 */
1099278fe1ccSHaren Myneni 	if (caps_all.feat_type & VAS_GZIP_DEF_FEAT_BIT)
1100ca77d488SHaren Myneni 		rc = get_vas_capabilities(VAS_GZIP_DEF_FEAT,
1101278fe1ccSHaren Myneni 					  VAS_GZIP_DEF_FEAT_TYPE, &hv_cop_caps);
1102ca77d488SHaren Myneni 
1103278fe1ccSHaren Myneni 	if (!rc && copypaste_feat) {
1104278fe1ccSHaren Myneni 		if (firmware_has_feature(FW_FEATURE_LPAR))
11058ef7b9e1SHaren Myneni 			of_reconfig_notifier_register(&pseries_vas_nb);
11068ef7b9e1SHaren Myneni 
1107ca77d488SHaren Myneni 		pr_info("GZIP feature is available\n");
1108278fe1ccSHaren Myneni 	} else {
1109278fe1ccSHaren Myneni 		/*
1110278fe1ccSHaren Myneni 		 * Should not happen, but only when get default
1111278fe1ccSHaren Myneni 		 * capabilities HCALL failed. So disable copy paste
1112278fe1ccSHaren Myneni 		 * feature.
1113278fe1ccSHaren Myneni 		 */
1114278fe1ccSHaren Myneni 		copypaste_feat = false;
1115278fe1ccSHaren Myneni 	}
1116ca77d488SHaren Myneni 
1117ca77d488SHaren Myneni out:
1118ca77d488SHaren Myneni 	kfree(hv_caps);
1119ca77d488SHaren Myneni 	return rc;
1120ca77d488SHaren Myneni }
1121ca77d488SHaren Myneni machine_device_initcall(pseries, pseries_vas_init);
1122