#define pr_fmt(fmt) "vas: " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/export.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/irqdomain.h>
#include <asm/machdep.h>
#include <asm/hvcall.h>
#include <asm/plpar_wrappers.h>
#include <asm/firmware.h>
#include <asm/vphn.h>
#include <asm/vas.h>
#include "vas.h"
#define VAS_INVALID_WIN_ADDRESS 0xFFFFFFFFFFFFFFFFul
#define VAS_DEFAULT_DOMAIN_ID 0xFFFFFFFFFFFFFFFFul
#define DEF_WIN_CREDS 1
static struct vas_all_caps caps_all;
static bool copypaste_feat;
static struct hv_vas_cop_feat_caps hv_cop_caps;
static struct vas_caps vascaps[VAS_MAX_FEAT_TYPE];
static DEFINE_MUTEX(vas_pseries_mutex);
static bool migration_in_progress;
static long hcall_return_busy_check(long rc)
{
if (H_IS_LONG_BUSY(rc)) {
unsigned int ms;
ms = clamp(get_longbusy_msecs(rc), 1, 10);
usleep_range(ms * (USEC_PER_MSEC / 10), ms * USEC_PER_MSEC);
rc = H_BUSY;
} else if (rc == H_BUSY) {
cond_resched();
}
return rc;
}
static int h_allocate_vas_window(struct pseries_vas_window *win, u64 *domain,
u8 wintype, u16 credits)
{
long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
long rc;
do {
rc = plpar_hcall9(H_ALLOCATE_VAS_WINDOW, retbuf, wintype,
credits, domain[0], domain[1], domain[2],
domain[3], domain[4], domain[5]);
rc = hcall_return_busy_check(rc);
} while (rc == H_BUSY);
if (rc == H_SUCCESS) {
if (win->win_addr == VAS_INVALID_WIN_ADDRESS) {
pr_err("H_ALLOCATE_VAS_WINDOW: COPY/PASTE is not supported\n");
return -ENOTSUPP;
}
win->vas_win.winid = retbuf[0];
win->win_addr = retbuf[1];
win->complete_irq = retbuf[2];
win->fault_irq = retbuf[3];
return 0;
}
pr_err("H_ALLOCATE_VAS_WINDOW error: %ld, wintype: %u, credits: %u\n",
rc, wintype, credits);
return -EIO;
}
static int h_deallocate_vas_window(u64 winid)
{
long rc;
do {
rc = plpar_hcall_norets(H_DEALLOCATE_VAS_WINDOW, winid);
rc = hcall_return_busy_check(rc);
} while (rc == H_BUSY);
if (rc == H_SUCCESS)
return 0;
pr_err("H_DEALLOCATE_VAS_WINDOW error: %ld, winid: %llu\n",
rc, winid);
return -EIO;
}
static int h_modify_vas_window(struct pseries_vas_window *win)
{
long rc;
do {
rc = plpar_hcall_norets(H_MODIFY_VAS_WINDOW,
win->vas_win.winid, win->pid, 0,
VAS_MOD_WIN_FLAGS, 0);
rc = hcall_return_busy_check(rc);
} while (rc == H_BUSY);
if (rc == H_SUCCESS)
return 0;
pr_err("H_MODIFY_VAS_WINDOW error: %ld, winid %u pid %u\n",
rc, win->vas_win.winid, win->pid);
return -EIO;
}
int h_query_vas_capabilities(const u64 hcall, u8 query_type, u64 result)
{
long rc;
rc = plpar_hcall_norets(hcall, query_type, result);
if (rc == H_SUCCESS)
return 0;
if (rc != H_FUNCTION) {
pr_err("%s error %ld, query_type %u, result buffer 0x%llx\n",
(hcall == H_QUERY_VAS_CAPABILITIES) ?
"H_QUERY_VAS_CAPABILITIES" :
"H_QUERY_NX_CAPABILITIES",
rc, query_type, result);
}
return -EIO;
}
EXPORT_SYMBOL_GPL(h_query_vas_capabilities);
static int h_get_nx_fault(u32 winid, u64 buffer)
{
long rc;
rc = plpar_hcall_norets(H_GET_NX_FAULT, winid, buffer);
if (rc == H_SUCCESS)
return 0;
pr_err("H_GET_NX_FAULT error: %ld, winid %u, buffer 0x%llx\n",
rc, winid, buffer);
return -EIO;
}
static irqreturn_t pseries_vas_fault_thread_fn(int irq, void *data)
{
struct pseries_vas_window *txwin = data;
struct coprocessor_request_block crb;
struct vas_user_win_ref *tsk_ref;
int rc;
while (atomic_read(&txwin->pending_faults)) {
rc = h_get_nx_fault(txwin->vas_win.winid, (u64)virt_to_phys(&crb));
if (!rc) {
tsk_ref = &txwin->vas_win.task_ref;
vas_dump_crb(&crb);
vas_update_csb(&crb, tsk_ref);
}
atomic_dec(&txwin->pending_faults);
}
return IRQ_HANDLED;
}
static irqreturn_t pseries_vas_irq_handler(int irq, void *data)
{
struct pseries_vas_window *txwin = data;
atomic_inc(&txwin->pending_faults);
return IRQ_WAKE_THREAD;
}
static int allocate_setup_window(struct pseries_vas_window *txwin,
u64 *domain, u8 wintype)
{
int rc;
rc = h_allocate_vas_window(txwin, domain, wintype, DEF_WIN_CREDS);
if (rc)
return rc;
txwin->fault_virq = irq_create_mapping(NULL, txwin->fault_irq);
if (!txwin->fault_virq) {
pr_err("Failed irq mapping %d\n", txwin->fault_irq);
rc = -EINVAL;
goto out_win;
}
txwin->name = kasprintf(GFP_KERNEL, "vas-win-%d",
txwin->vas_win.winid);
if (!txwin->name) {
rc = -ENOMEM;
goto out_irq;
}
rc = request_threaded_irq(txwin->fault_virq,
pseries_vas_irq_handler,
pseries_vas_fault_thread_fn, 0,
txwin->name, txwin);
if (rc) {
pr_err("VAS-Window[%d]: Request IRQ(%u) failed with %d\n",
txwin->vas_win.winid, txwin->fault_virq, rc);
goto out_free;
}
txwin->vas_win.wcreds_max = DEF_WIN_CREDS;
return 0;
out_free:
kfree(txwin->name);
out_irq:
irq_dispose_mapping(txwin->fault_virq);
out_win:
h_deallocate_vas_window(txwin->vas_win.winid);
return rc;
}
static inline void free_irq_setup(struct pseries_vas_window *txwin)
{
free_irq(txwin->fault_virq, txwin);
kfree(txwin->name);
irq_dispose_mapping(txwin->fault_virq);
}
static struct vas_window *vas_allocate_window(int vas_id, u64 flags,
enum vas_cop_type cop_type)
{
long domain[PLPAR_HCALL9_BUFSIZE] = {VAS_DEFAULT_DOMAIN_ID};
struct vas_cop_feat_caps *cop_feat_caps;
struct vas_caps *caps;
struct pseries_vas_window *txwin;
int rc;
txwin = kzalloc_obj(*txwin);
if (!txwin)
return ERR_PTR(-ENOMEM);
if (flags & VAS_TX_WIN_FLAG_QOS_CREDIT)
caps = &vascaps[VAS_GZIP_QOS_FEAT_TYPE];
else
caps = &vascaps[VAS_GZIP_DEF_FEAT_TYPE];
cop_feat_caps = &caps->caps;
if (atomic_inc_return(&cop_feat_caps->nr_used_credits) >
atomic_read(&cop_feat_caps->nr_total_credits)) {
pr_err_ratelimited("Credits are not available to allocate window\n");
rc = -EINVAL;
goto out;
}
if (vas_id == -1) {
rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, domain,
VPHN_FLAG_VCPU, hard_smp_processor_id());
if (rc != H_SUCCESS) {
pr_err("H_HOME_NODE_ASSOCIATIVITY error: %d\n", rc);
goto out;
}
}
txwin->pid = mfspr(SPRN_PID);
mutex_lock(&vas_pseries_mutex);
if (migration_in_progress) {
rc = -EBUSY;
} else {
rc = allocate_setup_window(txwin, (u64 *)&domain[0],
cop_feat_caps->win_type);
if (!rc)
caps->nr_open_wins_progress++;
}
mutex_unlock(&vas_pseries_mutex);
if (rc)
goto out;
rc = h_modify_vas_window(txwin);
if (!rc)
rc = get_vas_user_win_ref(&txwin->vas_win.task_ref);
if (rc)
goto out_free;
txwin->win_type = cop_feat_caps->win_type;
mutex_lock(&vas_pseries_mutex);
if (!caps->nr_close_wins && !migration_in_progress) {
list_add(&txwin->win_list, &caps->list);
caps->nr_open_windows++;
caps->nr_open_wins_progress--;
mutex_unlock(&vas_pseries_mutex);
vas_user_win_add_mm_context(&txwin->vas_win.task_ref);
return &txwin->vas_win;
}
mutex_unlock(&vas_pseries_mutex);
put_vas_user_win_ref(&txwin->vas_win.task_ref);
rc = -EBUSY;
pr_err_ratelimited("No credit is available to allocate window\n");
out_free:
free_irq_setup(txwin);
h_deallocate_vas_window(txwin->vas_win.winid);
mutex_lock(&vas_pseries_mutex);
caps->nr_open_wins_progress--;
mutex_unlock(&vas_pseries_mutex);
out:
atomic_dec(&cop_feat_caps->nr_used_credits);
kfree(txwin);
return ERR_PTR(rc);
}
static u64 vas_paste_address(struct vas_window *vwin)
{
struct pseries_vas_window *win;
win = container_of(vwin, struct pseries_vas_window, vas_win);
return win->win_addr;
}
static int deallocate_free_window(struct pseries_vas_window *win)
{
int rc = 0;
rc = h_deallocate_vas_window(win->vas_win.winid);
if (!rc)
free_irq_setup(win);
return rc;
}
static int vas_deallocate_window(struct vas_window *vwin)
{
struct pseries_vas_window *win;
struct vas_cop_feat_caps *caps;
int rc = 0;
if (!vwin)
return -EINVAL;
win = container_of(vwin, struct pseries_vas_window, vas_win);
if (win->win_type >= VAS_MAX_FEAT_TYPE) {
pr_err("Window (%u): Invalid window type %u\n",
vwin->winid, win->win_type);
return -EINVAL;
}
caps = &vascaps[win->win_type].caps;
mutex_lock(&vas_pseries_mutex);
if (!(win->vas_win.status & VAS_WIN_NO_CRED_CLOSE) &&
!(win->vas_win.status & VAS_WIN_MIGRATE_CLOSE)) {
rc = deallocate_free_window(win);
if (rc) {
mutex_unlock(&vas_pseries_mutex);
return rc;
}
} else
vascaps[win->win_type].nr_close_wins--;
list_del(&win->win_list);
atomic_dec(&caps->nr_used_credits);
vascaps[win->win_type].nr_open_windows--;
mutex_unlock(&vas_pseries_mutex);
mm_context_remove_vas_window(vwin->task_ref.mm);
put_vas_user_win_ref(&vwin->task_ref);
kfree(win);
return 0;
}
static const struct vas_user_win_ops vops_pseries = {
.open_win = vas_allocate_window,
.paste_addr = vas_paste_address,
.close_win = vas_deallocate_window,
};
int vas_register_api_pseries(struct module *mod, enum vas_cop_type cop_type,
const char *name)
{
if (!copypaste_feat)
return -ENOTSUPP;
return vas_register_coproc_api(mod, cop_type, name, &vops_pseries);
}
EXPORT_SYMBOL_GPL(vas_register_api_pseries);
void vas_unregister_api_pseries(void)
{
vas_unregister_coproc_api();
}
EXPORT_SYMBOL_GPL(vas_unregister_api_pseries);
static int __init get_vas_capabilities(u8 feat, enum vas_cop_feat_type type,
struct hv_vas_cop_feat_caps *hv_caps)
{
struct vas_cop_feat_caps *caps;
struct vas_caps *vcaps;
int rc = 0;
vcaps = &vascaps[type];
memset(vcaps, 0, sizeof(*vcaps));
INIT_LIST_HEAD(&vcaps->list);
vcaps->feat = feat;
caps = &vcaps->caps;
rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES, feat,
(u64)virt_to_phys(hv_caps));
if (rc)
return rc;
caps->user_mode = hv_caps->user_mode;
if (!(caps->user_mode & VAS_COPY_PASTE_USER_MODE)) {
pr_err("User space COPY/PASTE is not supported\n");
return -ENOTSUPP;
}
caps->descriptor = be64_to_cpu(hv_caps->descriptor);
caps->win_type = hv_caps->win_type;
if (caps->win_type >= VAS_MAX_FEAT_TYPE) {
pr_err("Unsupported window type %u\n", caps->win_type);
return -EINVAL;
}
caps->max_lpar_creds = be16_to_cpu(hv_caps->max_lpar_creds);
caps->max_win_creds = be16_to_cpu(hv_caps->max_win_creds);
atomic_set(&caps->nr_total_credits,
be16_to_cpu(hv_caps->target_lpar_creds));
if (feat == VAS_GZIP_DEF_FEAT) {
caps->def_lpar_creds = be16_to_cpu(hv_caps->def_lpar_creds);
if (caps->max_win_creds < DEF_WIN_CREDS) {
pr_err("Window creds(%u) > max allowed window creds(%u)\n",
DEF_WIN_CREDS, caps->max_win_creds);
return -EINVAL;
}
}
rc = sysfs_add_vas_caps(caps);
if (rc)
return rc;
copypaste_feat = true;
return 0;
}
static int reconfig_open_windows(struct vas_caps *vcaps, int creds,
bool migrate)
{
long domain[PLPAR_HCALL9_BUFSIZE] = {VAS_DEFAULT_DOMAIN_ID};
struct vas_cop_feat_caps *caps = &vcaps->caps;
struct pseries_vas_window *win = NULL, *tmp;
int rc, mv_ents = 0;
int flag;
if (!vcaps->nr_close_wins)
return 0;
if ((vcaps->nr_close_wins > creds) && !migrate)
mv_ents = vcaps->nr_close_wins - creds;
list_for_each_entry_safe(win, tmp, &vcaps->list, win_list) {
if (!mv_ents)
break;
mv_ents--;
}
if (migrate)
flag = VAS_WIN_MIGRATE_CLOSE;
else
flag = VAS_WIN_NO_CRED_CLOSE;
list_for_each_entry_safe_from(win, tmp, &vcaps->list, win_list) {
if ((win->vas_win.status & VAS_WIN_NO_CRED_CLOSE) &&
(win->vas_win.status & VAS_WIN_MIGRATE_CLOSE)) {
win->vas_win.status &= ~flag;
continue;
}
if (!(win->vas_win.status & flag))
continue;
rc = allocate_setup_window(win, (u64 *)&domain[0],
caps->win_type);
if (rc)
return rc;
rc = h_modify_vas_window(win);
if (rc)
goto out;
mutex_lock(&win->vas_win.task_ref.mmap_mutex);
win->vas_win.status &= ~flag;
mutex_unlock(&win->vas_win.task_ref.mmap_mutex);
win->win_type = caps->win_type;
if (!--vcaps->nr_close_wins)
break;
}
return 0;
out:
free_irq_setup(win);
h_deallocate_vas_window(win->vas_win.winid);
return rc;
}
static int reconfig_close_windows(struct vas_caps *vcap, int excess_creds,
bool migrate)
{
struct pseries_vas_window *win, *tmp;
struct vas_user_win_ref *task_ref;
struct vm_area_struct *vma;
int rc = 0, flag;
if (migrate)
flag = VAS_WIN_MIGRATE_CLOSE;
else
flag = VAS_WIN_NO_CRED_CLOSE;
list_for_each_entry_safe(win, tmp, &vcap->list, win_list) {
if ((win->vas_win.status & VAS_WIN_MIGRATE_CLOSE) ||
(win->vas_win.status & VAS_WIN_NO_CRED_CLOSE)) {
win->vas_win.status |= flag;
continue;
}
task_ref = &win->vas_win.task_ref;
mmap_write_lock(task_ref->mm);
mutex_lock(&task_ref->mmap_mutex);
vma = task_ref->vma;
win->vas_win.status |= flag;
if (vma)
zap_vma_pages(vma);
mutex_unlock(&task_ref->mmap_mutex);
mmap_write_unlock(task_ref->mm);
rc = deallocate_free_window(win);
if (rc && !migrate)
return rc;
vcap->nr_close_wins++;
if (!migrate && !--excess_creds)
break;
}
return 0;
}
int vas_reconfig_capabilties(u8 type, int new_nr_creds)
{
struct vas_cop_feat_caps *caps;
int old_nr_creds;
struct vas_caps *vcaps;
int rc = 0, nr_active_wins;
if (type >= VAS_MAX_FEAT_TYPE) {
pr_err("Invalid credit type %d\n", type);
return -EINVAL;
}
vcaps = &vascaps[type];
caps = &vcaps->caps;
mutex_lock(&vas_pseries_mutex);
old_nr_creds = atomic_read(&caps->nr_total_credits);
atomic_set(&caps->nr_total_credits, new_nr_creds);
if (old_nr_creds < new_nr_creds) {
rc = reconfig_open_windows(vcaps, new_nr_creds - old_nr_creds,
false);
} else {
nr_active_wins = vcaps->nr_open_windows - vcaps->nr_close_wins;
if (nr_active_wins > new_nr_creds)
rc = reconfig_close_windows(vcaps,
nr_active_wins - new_nr_creds,
false);
}
mutex_unlock(&vas_pseries_mutex);
return rc;
}
int pseries_vas_dlpar_cpu(void)
{
int new_nr_creds, rc;
if (!copypaste_feat)
return 0;
rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES,
vascaps[VAS_GZIP_DEF_FEAT_TYPE].feat,
(u64)virt_to_phys(&hv_cop_caps));
if (!rc) {
new_nr_creds = be16_to_cpu(hv_cop_caps.target_lpar_creds);
rc = vas_reconfig_capabilties(VAS_GZIP_DEF_FEAT_TYPE, new_nr_creds);
}
if (rc)
pr_err("Failed reconfig VAS capabilities with DLPAR\n");
return rc;
}
static int pseries_vas_notifier(struct notifier_block *nb,
unsigned long action, void *data)
{
struct of_reconfig_data *rd = data;
struct device_node *dn = rd->dn;
const __be32 *intserv = NULL;
int len;
if (is_shared_processor())
return NOTIFY_OK;
if ((action == OF_RECONFIG_ATTACH_NODE) ||
(action == OF_RECONFIG_DETACH_NODE))
intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s",
&len);
if (!intserv)
return NOTIFY_OK;
return pseries_vas_dlpar_cpu();
}
static struct notifier_block pseries_vas_nb = {
.notifier_call = pseries_vas_notifier,
};
int vas_migration_handler(int action)
{
struct vas_cop_feat_caps *caps;
int old_nr_creds, new_nr_creds = 0;
struct vas_caps *vcaps;
int i, rc = 0;
pr_info("VAS migration event %d\n", action);
if (!copypaste_feat)
return rc;
if (action == VAS_SUSPEND)
migration_in_progress = true;
else
migration_in_progress = false;
for (i = 0; i < VAS_MAX_FEAT_TYPE; i++) {
vcaps = &vascaps[i];
caps = &vcaps->caps;
old_nr_creds = atomic_read(&caps->nr_total_credits);
rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES,
vcaps->feat,
(u64)virt_to_phys(&hv_cop_caps));
if (!rc) {
new_nr_creds = be16_to_cpu(hv_cop_caps.target_lpar_creds);
if (old_nr_creds != new_nr_creds) {
pr_err("Target credits mismatch with the hypervisor\n");
pr_err("state(%d): lpar creds: %d HV lpar creds: %d\n",
action, old_nr_creds, new_nr_creds);
pr_err("Used creds: %d, Active creds: %d\n",
atomic_read(&caps->nr_used_credits),
vcaps->nr_open_windows - vcaps->nr_close_wins);
}
} else {
pr_err("state(%d): Get VAS capabilities failed with %d\n",
action, rc);
if (action == VAS_RESUME)
goto out;
}
switch (action) {
case VAS_SUSPEND:
mutex_lock(&vas_pseries_mutex);
rc = reconfig_close_windows(vcaps, vcaps->nr_open_windows,
true);
while (vcaps->nr_open_wins_progress) {
mutex_unlock(&vas_pseries_mutex);
msleep(10);
mutex_lock(&vas_pseries_mutex);
}
mutex_unlock(&vas_pseries_mutex);
break;
case VAS_RESUME:
mutex_lock(&vas_pseries_mutex);
atomic_set(&caps->nr_total_credits, new_nr_creds);
rc = reconfig_open_windows(vcaps, new_nr_creds, true);
mutex_unlock(&vas_pseries_mutex);
break;
default:
pr_err("Invalid migration action %d\n", action);
rc = -EINVAL;
goto out;
}
if (rc && (action == VAS_RESUME))
goto out;
}
pr_info("VAS migration event (%d) successful\n", action);
out:
return rc;
}
static int __init pseries_vas_init(void)
{
struct hv_vas_all_caps *hv_caps;
int rc = 0;
if (!radix_enabled()) {
copypaste_feat = false;
pr_err("API is supported only with radix page tables\n");
return -ENOTSUPP;
}
hv_caps = kmalloc_obj(*hv_caps);
if (!hv_caps)
return -ENOMEM;
rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES, 0,
(u64)virt_to_phys(hv_caps));
if (rc)
goto out;
caps_all.descriptor = be64_to_cpu(hv_caps->descriptor);
caps_all.feat_type = be64_to_cpu(hv_caps->feat_type);
sysfs_pseries_vas_init(&caps_all);
if (caps_all.feat_type & VAS_GZIP_QOS_FEAT_BIT) {
rc = get_vas_capabilities(VAS_GZIP_QOS_FEAT,
VAS_GZIP_QOS_FEAT_TYPE, &hv_cop_caps);
if (rc)
goto out;
}
if (caps_all.feat_type & VAS_GZIP_DEF_FEAT_BIT)
rc = get_vas_capabilities(VAS_GZIP_DEF_FEAT,
VAS_GZIP_DEF_FEAT_TYPE, &hv_cop_caps);
if (!rc && copypaste_feat) {
if (firmware_has_feature(FW_FEATURE_LPAR))
of_reconfig_notifier_register(&pseries_vas_nb);
pr_info("GZIP feature is available\n");
} else {
copypaste_feat = false;
}
out:
kfree(hv_caps);
return rc;
}
machine_device_initcall(pseries, pseries_vas_init);