^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright 2017 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <asm/pnv-ocxl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/xive.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "ocxl_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct afu_irq {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) int hw_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) unsigned int virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) irqreturn_t (*handler)(void *private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) void (*free_private)(void *private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) void *private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int ocxl_irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u64 ocxl_irq_id_to_offset(struct ocxl_context *ctx, int irq_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return ctx->afu->irq_base_offset + (irq_id << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int ocxl_irq_set_handler(struct ocxl_context *ctx, int irq_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) irqreturn_t (*handler)(void *private),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void (*free_private)(void *private),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct afu_irq *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) mutex_lock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) irq = idr_find(&ctx->irq_idr, irq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) irq->handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) irq->private = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) irq->free_private = free_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) // Fall through to unlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) mutex_unlock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) EXPORT_SYMBOL_GPL(ocxl_irq_set_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static irqreturn_t afu_irq_handler(int virq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct afu_irq *irq = (struct afu_irq *) data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) trace_ocxl_afu_irq_receive(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (irq->handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return irq->handler(irq->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return IRQ_HANDLED; // Just drop it on the ground
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) irq->virq = irq_create_mapping(NULL, irq->hw_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!irq->virq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) pr_err("irq_create_mapping failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_debug("hw_irq %d mapped to virq %u\n", irq->hw_irq, irq->virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) irq->name = kasprintf(GFP_KERNEL, "ocxl-afu-%u", irq->virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!irq->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) irq_dispose_mapping(irq->virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) rc = request_irq(irq->virq, afu_irq_handler, 0, irq->name, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) kfree(irq->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) irq->name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) irq_dispose_mapping(irq->virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pr_err("request_irq failed: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void release_afu_irq(struct afu_irq *irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) free_irq(irq->virq, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) irq_dispose_mapping(irq->virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kfree(irq->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct afu_irq *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) irq = kzalloc(sizeof(struct afu_irq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * We limit the number of afu irqs per context and per link to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * avoid a single process or user depleting the pool of IPIs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) mutex_lock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) irq->id = idr_alloc(&ctx->irq_idr, irq, 0, MAX_IRQ_PER_CONTEXT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (irq->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) rc = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) rc = ocxl_link_irq_alloc(ctx->afu->fn->link, &irq->hw_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto err_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) rc = setup_afu_irq(ctx, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) trace_ocxl_afu_irq_alloc(ctx->pasid, irq->id, irq->virq, irq->hw_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mutex_unlock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *irq_id = irq->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) err_idr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) idr_remove(&ctx->irq_idr, irq->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) mutex_unlock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) kfree(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) EXPORT_SYMBOL_GPL(ocxl_afu_irq_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void afu_irq_free(struct afu_irq *irq, struct ocxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) trace_ocxl_afu_irq_free(ctx->pasid, irq->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (ctx->mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unmap_mapping_range(ctx->mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ocxl_irq_id_to_offset(ctx, irq->id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 1 << PAGE_SHIFT, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) release_afu_irq(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (irq->free_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) irq->free_private(irq->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) kfree(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int ocxl_afu_irq_free(struct ocxl_context *ctx, int irq_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct afu_irq *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) mutex_lock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) irq = idr_find(&ctx->irq_idr, irq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) mutex_unlock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) idr_remove(&ctx->irq_idr, irq->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) afu_irq_free(irq, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) mutex_unlock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) EXPORT_SYMBOL_GPL(ocxl_afu_irq_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) void ocxl_afu_irq_free_all(struct ocxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct afu_irq *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mutex_lock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) idr_for_each_entry(&ctx->irq_idr, irq, id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) afu_irq_free(irq, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) mutex_unlock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, int irq_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct xive_irq_data *xd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct afu_irq *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) u64 addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) mutex_lock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) irq = idr_find(&ctx->irq_idr, irq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) xd = irq_get_handler_data(irq->virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) addr = xd ? xd->trig_page : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) mutex_unlock(&ctx->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) EXPORT_SYMBOL_GPL(ocxl_afu_irq_get_addr);