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