1f204e0b8SIan Munsie /* 2f204e0b8SIan Munsie * Copyright 2014 IBM Corp. 3f204e0b8SIan Munsie * 4f204e0b8SIan Munsie * This program is free software; you can redistribute it and/or 5f204e0b8SIan Munsie * modify it under the terms of the GNU General Public License 6f204e0b8SIan Munsie * as published by the Free Software Foundation; either version 7f204e0b8SIan Munsie * 2 of the License, or (at your option) any later version. 8f204e0b8SIan Munsie */ 9f204e0b8SIan Munsie 10f204e0b8SIan Munsie #include <linux/interrupt.h> 11f204e0b8SIan Munsie #include <linux/workqueue.h> 12f204e0b8SIan Munsie #include <linux/sched.h> 13f204e0b8SIan Munsie #include <linux/wait.h> 14f204e0b8SIan Munsie #include <linux/slab.h> 15f204e0b8SIan Munsie #include <linux/pid.h> 16f204e0b8SIan Munsie #include <asm/cputable.h> 17f204e0b8SIan Munsie #include <misc/cxl.h> 18f204e0b8SIan Munsie 19f204e0b8SIan Munsie #include "cxl.h" 209bcf28cdSIan Munsie #include "trace.h" 21f204e0b8SIan Munsie 22f204e0b8SIan Munsie /* XXX: This is implementation specific */ 23f204e0b8SIan Munsie static irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr, u64 errstat) 24f204e0b8SIan Munsie { 25f204e0b8SIan Munsie u64 fir1, fir2, fir_slice, serr, afu_debug; 26f204e0b8SIan Munsie 27f204e0b8SIan Munsie fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1); 28f204e0b8SIan Munsie fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2); 29f204e0b8SIan Munsie fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An); 30f204e0b8SIan Munsie serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An); 31f204e0b8SIan Munsie afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An); 32f204e0b8SIan Munsie 33f204e0b8SIan Munsie dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat); 34f204e0b8SIan Munsie dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%.16llx\n", fir1); 35f204e0b8SIan Munsie dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%.16llx\n", fir2); 36f204e0b8SIan Munsie dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr); 37f204e0b8SIan Munsie dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%.16llx\n", fir_slice); 38f204e0b8SIan Munsie dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%.16llx\n", afu_debug); 39f204e0b8SIan Munsie 40f204e0b8SIan Munsie dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n"); 41f204e0b8SIan Munsie cxl_stop_trace(ctx->afu->adapter); 42f204e0b8SIan Munsie 43f204e0b8SIan Munsie return cxl_ack_irq(ctx, 0, errstat); 44f204e0b8SIan Munsie } 45f204e0b8SIan Munsie 46f204e0b8SIan Munsie irqreturn_t cxl_slice_irq_err(int irq, void *data) 47f204e0b8SIan Munsie { 48f204e0b8SIan Munsie struct cxl_afu *afu = data; 49f204e0b8SIan Munsie u64 fir_slice, errstat, serr, afu_debug; 50f204e0b8SIan Munsie 51f204e0b8SIan Munsie WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq); 52f204e0b8SIan Munsie 53f204e0b8SIan Munsie serr = cxl_p1n_read(afu, CXL_PSL_SERR_An); 54f204e0b8SIan Munsie fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An); 55f204e0b8SIan Munsie errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An); 56f204e0b8SIan Munsie afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An); 57f204e0b8SIan Munsie dev_crit(&afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr); 58f204e0b8SIan Munsie dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%.16llx\n", fir_slice); 59f204e0b8SIan Munsie dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%.16llx\n", errstat); 60f204e0b8SIan Munsie dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%.16llx\n", afu_debug); 61f204e0b8SIan Munsie 62f204e0b8SIan Munsie cxl_p1n_write(afu, CXL_PSL_SERR_An, serr); 63f204e0b8SIan Munsie 64f204e0b8SIan Munsie return IRQ_HANDLED; 65f204e0b8SIan Munsie } 66f204e0b8SIan Munsie 67f204e0b8SIan Munsie static irqreturn_t cxl_irq_err(int irq, void *data) 68f204e0b8SIan Munsie { 69f204e0b8SIan Munsie struct cxl *adapter = data; 70f204e0b8SIan Munsie u64 fir1, fir2, err_ivte; 71f204e0b8SIan Munsie 72f204e0b8SIan Munsie WARN(1, "CXL ERROR interrupt %i\n", irq); 73f204e0b8SIan Munsie 74f204e0b8SIan Munsie err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE); 75f204e0b8SIan Munsie dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%.16llx\n", err_ivte); 76f204e0b8SIan Munsie 77f204e0b8SIan Munsie dev_crit(&adapter->dev, "STOPPING CXL TRACE\n"); 78f204e0b8SIan Munsie cxl_stop_trace(adapter); 79f204e0b8SIan Munsie 80f204e0b8SIan Munsie fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1); 81f204e0b8SIan Munsie fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2); 82f204e0b8SIan Munsie 83f204e0b8SIan Munsie dev_crit(&adapter->dev, "PSL_FIR1: 0x%.16llx\nPSL_FIR2: 0x%.16llx\n", fir1, fir2); 84f204e0b8SIan Munsie 85f204e0b8SIan Munsie return IRQ_HANDLED; 86f204e0b8SIan Munsie } 87f204e0b8SIan Munsie 88f204e0b8SIan Munsie static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64 dsisr, u64 dar) 89f204e0b8SIan Munsie { 90f204e0b8SIan Munsie ctx->dsisr = dsisr; 91f204e0b8SIan Munsie ctx->dar = dar; 92f204e0b8SIan Munsie schedule_work(&ctx->fault_work); 93f204e0b8SIan Munsie return IRQ_HANDLED; 94f204e0b8SIan Munsie } 95f204e0b8SIan Munsie 96bc78b05bSIan Munsie static irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info) 97f204e0b8SIan Munsie { 98f204e0b8SIan Munsie struct cxl_context *ctx = data; 99f204e0b8SIan Munsie u64 dsisr, dar; 100f204e0b8SIan Munsie 101bc78b05bSIan Munsie dsisr = irq_info->dsisr; 102bc78b05bSIan Munsie dar = irq_info->dar; 103f204e0b8SIan Munsie 1049bcf28cdSIan Munsie trace_cxl_psl_irq(ctx, irq, dsisr, dar); 1059bcf28cdSIan Munsie 106f204e0b8SIan Munsie pr_devel("CXL interrupt %i for afu pe: %i DSISR: %#llx DAR: %#llx\n", irq, ctx->pe, dsisr, dar); 107f204e0b8SIan Munsie 108f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_DS) { 109f204e0b8SIan Munsie /* 110f204e0b8SIan Munsie * We don't inherently need to sleep to handle this, but we do 111f204e0b8SIan Munsie * need to get a ref to the task's mm, which we can't do from 112f204e0b8SIan Munsie * irq context without the potential for a deadlock since it 113f204e0b8SIan Munsie * takes the task_lock. An alternate option would be to keep a 114f204e0b8SIan Munsie * reference to the task's mm the entire time it has cxl open, 115f204e0b8SIan Munsie * but to do that we need to solve the issue where we hold a 116f204e0b8SIan Munsie * ref to the mm, but the mm can hold a ref to the fd after an 117f204e0b8SIan Munsie * mmap preventing anything from being cleaned up. 118f204e0b8SIan Munsie */ 119f204e0b8SIan Munsie pr_devel("Scheduling segment miss handling for later pe: %i\n", ctx->pe); 120f204e0b8SIan Munsie return schedule_cxl_fault(ctx, dsisr, dar); 121f204e0b8SIan Munsie } 122f204e0b8SIan Munsie 123f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_M) 124f204e0b8SIan Munsie pr_devel("CXL interrupt: PTE not found\n"); 125f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_P) 126f204e0b8SIan Munsie pr_devel("CXL interrupt: Storage protection violation\n"); 127f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_A) 128f204e0b8SIan Munsie pr_devel("CXL interrupt: AFU lock access to write through or cache inhibited storage\n"); 129f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_S) 130f204e0b8SIan Munsie pr_devel("CXL interrupt: Access was afu_wr or afu_zero\n"); 131f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_K) 132f204e0b8SIan Munsie pr_devel("CXL interrupt: Access not permitted by virtual page class key protection\n"); 133f204e0b8SIan Munsie 134f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_DM) { 135f204e0b8SIan Munsie /* 136f204e0b8SIan Munsie * In some cases we might be able to handle the fault 137f204e0b8SIan Munsie * immediately if hash_page would succeed, but we still need 138f204e0b8SIan Munsie * the task's mm, which as above we can't get without a lock 139f204e0b8SIan Munsie */ 140f204e0b8SIan Munsie pr_devel("Scheduling page fault handling for later pe: %i\n", ctx->pe); 141f204e0b8SIan Munsie return schedule_cxl_fault(ctx, dsisr, dar); 142f204e0b8SIan Munsie } 143f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_ST) 144f204e0b8SIan Munsie WARN(1, "CXL interrupt: Segment Table PTE not found\n"); 145f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_UR) 146f204e0b8SIan Munsie pr_devel("CXL interrupt: AURP PTE not found\n"); 147f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_PE) 148bc78b05bSIan Munsie return handle_psl_slice_error(ctx, dsisr, irq_info->errstat); 149f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_AE) { 150bc78b05bSIan Munsie pr_devel("CXL interrupt: AFU Error %.llx\n", irq_info->afu_err); 151f204e0b8SIan Munsie 152f204e0b8SIan Munsie if (ctx->pending_afu_err) { 153f204e0b8SIan Munsie /* 154f204e0b8SIan Munsie * This shouldn't happen - the PSL treats these errors 155f204e0b8SIan Munsie * as fatal and will have reset the AFU, so there's not 156f204e0b8SIan Munsie * much point buffering multiple AFU errors. 157f204e0b8SIan Munsie * OTOH if we DO ever see a storm of these come in it's 158f204e0b8SIan Munsie * probably best that we log them somewhere: 159f204e0b8SIan Munsie */ 160f204e0b8SIan Munsie dev_err_ratelimited(&ctx->afu->dev, "CXL AFU Error " 161f204e0b8SIan Munsie "undelivered to pe %i: %.llx\n", 162bc78b05bSIan Munsie ctx->pe, irq_info->afu_err); 163f204e0b8SIan Munsie } else { 164f204e0b8SIan Munsie spin_lock(&ctx->lock); 165bc78b05bSIan Munsie ctx->afu_err = irq_info->afu_err; 166f204e0b8SIan Munsie ctx->pending_afu_err = 1; 167f204e0b8SIan Munsie spin_unlock(&ctx->lock); 168f204e0b8SIan Munsie 169f204e0b8SIan Munsie wake_up_all(&ctx->wq); 170f204e0b8SIan Munsie } 171f204e0b8SIan Munsie 172f204e0b8SIan Munsie cxl_ack_irq(ctx, CXL_PSL_TFC_An_A, 0); 173*a6130ed2SIan Munsie return IRQ_HANDLED; 174f204e0b8SIan Munsie } 175f204e0b8SIan Munsie if (dsisr & CXL_PSL_DSISR_An_OC) 176f204e0b8SIan Munsie pr_devel("CXL interrupt: OS Context Warning\n"); 177f204e0b8SIan Munsie 178f204e0b8SIan Munsie WARN(1, "Unhandled CXL PSL IRQ\n"); 179f204e0b8SIan Munsie return IRQ_HANDLED; 180f204e0b8SIan Munsie } 181f204e0b8SIan Munsie 182bc78b05bSIan Munsie static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info) 183bc78b05bSIan Munsie { 184bc78b05bSIan Munsie if (irq_info->dsisr & CXL_PSL_DSISR_TRANS) 185bc78b05bSIan Munsie cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE); 186bc78b05bSIan Munsie else 187bc78b05bSIan Munsie cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A); 188bc78b05bSIan Munsie 189bc78b05bSIan Munsie return IRQ_HANDLED; 190bc78b05bSIan Munsie } 191bc78b05bSIan Munsie 192f204e0b8SIan Munsie static irqreturn_t cxl_irq_multiplexed(int irq, void *data) 193f204e0b8SIan Munsie { 194f204e0b8SIan Munsie struct cxl_afu *afu = data; 195f204e0b8SIan Munsie struct cxl_context *ctx; 196bc78b05bSIan Munsie struct cxl_irq_info irq_info; 197f204e0b8SIan Munsie int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff; 198f204e0b8SIan Munsie int ret; 199f204e0b8SIan Munsie 200bc78b05bSIan Munsie if ((ret = cxl_get_irq(afu, &irq_info))) { 201bc78b05bSIan Munsie WARN(1, "Unable to get CXL IRQ Info: %i\n", ret); 202bc78b05bSIan Munsie return fail_psl_irq(afu, &irq_info); 203bc78b05bSIan Munsie } 204bc78b05bSIan Munsie 205f204e0b8SIan Munsie rcu_read_lock(); 206f204e0b8SIan Munsie ctx = idr_find(&afu->contexts_idr, ph); 207f204e0b8SIan Munsie if (ctx) { 208bc78b05bSIan Munsie ret = cxl_irq(irq, ctx, &irq_info); 209f204e0b8SIan Munsie rcu_read_unlock(); 210f204e0b8SIan Munsie return ret; 211f204e0b8SIan Munsie } 212f204e0b8SIan Munsie rcu_read_unlock(); 213f204e0b8SIan Munsie 214bc78b05bSIan Munsie WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %.16llx DAR" 215bc78b05bSIan Munsie " %.16llx\n(Possible AFU HW issue - was a term/remove acked" 216bc78b05bSIan Munsie " with outstanding transactions?)\n", ph, irq_info.dsisr, 217bc78b05bSIan Munsie irq_info.dar); 218bc78b05bSIan Munsie return fail_psl_irq(afu, &irq_info); 219f204e0b8SIan Munsie } 220f204e0b8SIan Munsie 221f204e0b8SIan Munsie static irqreturn_t cxl_irq_afu(int irq, void *data) 222f204e0b8SIan Munsie { 223f204e0b8SIan Munsie struct cxl_context *ctx = data; 224f204e0b8SIan Munsie irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq)); 225f204e0b8SIan Munsie int irq_off, afu_irq = 1; 226f204e0b8SIan Munsie __u16 range; 227f204e0b8SIan Munsie int r; 228f204e0b8SIan Munsie 229f204e0b8SIan Munsie for (r = 1; r < CXL_IRQ_RANGES; r++) { 230f204e0b8SIan Munsie irq_off = hwirq - ctx->irqs.offset[r]; 231f204e0b8SIan Munsie range = ctx->irqs.range[r]; 232f204e0b8SIan Munsie if (irq_off >= 0 && irq_off < range) { 233f204e0b8SIan Munsie afu_irq += irq_off; 234f204e0b8SIan Munsie break; 235f204e0b8SIan Munsie } 236f204e0b8SIan Munsie afu_irq += range; 237f204e0b8SIan Munsie } 238f204e0b8SIan Munsie if (unlikely(r >= CXL_IRQ_RANGES)) { 239f204e0b8SIan Munsie WARN(1, "Recieved AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n", 240f204e0b8SIan Munsie ctx->pe, irq, hwirq); 241f204e0b8SIan Munsie return IRQ_HANDLED; 242f204e0b8SIan Munsie } 243f204e0b8SIan Munsie 2449bcf28cdSIan Munsie trace_cxl_afu_irq(ctx, afu_irq, irq, hwirq); 245f204e0b8SIan Munsie pr_devel("Received AFU interrupt %i for pe: %i (virq %i hwirq %lx)\n", 246f204e0b8SIan Munsie afu_irq, ctx->pe, irq, hwirq); 247f204e0b8SIan Munsie 248f204e0b8SIan Munsie if (unlikely(!ctx->irq_bitmap)) { 249f204e0b8SIan Munsie WARN(1, "Recieved AFU IRQ for context with no IRQ bitmap\n"); 250f204e0b8SIan Munsie return IRQ_HANDLED; 251f204e0b8SIan Munsie } 252f204e0b8SIan Munsie spin_lock(&ctx->lock); 253f204e0b8SIan Munsie set_bit(afu_irq - 1, ctx->irq_bitmap); 254f204e0b8SIan Munsie ctx->pending_irq = true; 255f204e0b8SIan Munsie spin_unlock(&ctx->lock); 256f204e0b8SIan Munsie 257f204e0b8SIan Munsie wake_up_all(&ctx->wq); 258f204e0b8SIan Munsie 259f204e0b8SIan Munsie return IRQ_HANDLED; 260f204e0b8SIan Munsie } 261f204e0b8SIan Munsie 262f204e0b8SIan Munsie unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq, 26380fa93fcSMichael Neuling irq_handler_t handler, void *cookie, const char *name) 264f204e0b8SIan Munsie { 265f204e0b8SIan Munsie unsigned int virq; 266f204e0b8SIan Munsie int result; 267f204e0b8SIan Munsie 268f204e0b8SIan Munsie /* IRQ Domain? */ 269f204e0b8SIan Munsie virq = irq_create_mapping(NULL, hwirq); 270f204e0b8SIan Munsie if (!virq) { 271f204e0b8SIan Munsie dev_warn(&adapter->dev, "cxl_map_irq: irq_create_mapping failed\n"); 272f204e0b8SIan Munsie return 0; 273f204e0b8SIan Munsie } 274f204e0b8SIan Munsie 275f204e0b8SIan Munsie cxl_setup_irq(adapter, hwirq, virq); 276f204e0b8SIan Munsie 277f204e0b8SIan Munsie pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq); 278f204e0b8SIan Munsie 27980fa93fcSMichael Neuling result = request_irq(virq, handler, 0, name, cookie); 280f204e0b8SIan Munsie if (result) { 281f204e0b8SIan Munsie dev_warn(&adapter->dev, "cxl_map_irq: request_irq failed: %i\n", result); 282f204e0b8SIan Munsie return 0; 283f204e0b8SIan Munsie } 284f204e0b8SIan Munsie 285f204e0b8SIan Munsie return virq; 286f204e0b8SIan Munsie } 287f204e0b8SIan Munsie 288f204e0b8SIan Munsie void cxl_unmap_irq(unsigned int virq, void *cookie) 289f204e0b8SIan Munsie { 290f204e0b8SIan Munsie free_irq(virq, cookie); 291f204e0b8SIan Munsie irq_dispose_mapping(virq); 292f204e0b8SIan Munsie } 293f204e0b8SIan Munsie 294f204e0b8SIan Munsie static int cxl_register_one_irq(struct cxl *adapter, 295f204e0b8SIan Munsie irq_handler_t handler, 296f204e0b8SIan Munsie void *cookie, 297f204e0b8SIan Munsie irq_hw_number_t *dest_hwirq, 29880fa93fcSMichael Neuling unsigned int *dest_virq, 29980fa93fcSMichael Neuling const char *name) 300f204e0b8SIan Munsie { 301f204e0b8SIan Munsie int hwirq, virq; 302f204e0b8SIan Munsie 303f204e0b8SIan Munsie if ((hwirq = cxl_alloc_one_irq(adapter)) < 0) 304f204e0b8SIan Munsie return hwirq; 305f204e0b8SIan Munsie 30680fa93fcSMichael Neuling if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name))) 307f204e0b8SIan Munsie goto err; 308f204e0b8SIan Munsie 309f204e0b8SIan Munsie *dest_hwirq = hwirq; 310f204e0b8SIan Munsie *dest_virq = virq; 311f204e0b8SIan Munsie 312f204e0b8SIan Munsie return 0; 313f204e0b8SIan Munsie 314f204e0b8SIan Munsie err: 315f204e0b8SIan Munsie cxl_release_one_irq(adapter, hwirq); 316f204e0b8SIan Munsie return -ENOMEM; 317f204e0b8SIan Munsie } 318f204e0b8SIan Munsie 319f204e0b8SIan Munsie int cxl_register_psl_err_irq(struct cxl *adapter) 320f204e0b8SIan Munsie { 321f204e0b8SIan Munsie int rc; 322f204e0b8SIan Munsie 32380fa93fcSMichael Neuling adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err", 32480fa93fcSMichael Neuling dev_name(&adapter->dev)); 32580fa93fcSMichael Neuling if (!adapter->irq_name) 32680fa93fcSMichael Neuling return -ENOMEM; 32780fa93fcSMichael Neuling 328f204e0b8SIan Munsie if ((rc = cxl_register_one_irq(adapter, cxl_irq_err, adapter, 329f204e0b8SIan Munsie &adapter->err_hwirq, 33080fa93fcSMichael Neuling &adapter->err_virq, 33180fa93fcSMichael Neuling adapter->irq_name))) { 33280fa93fcSMichael Neuling kfree(adapter->irq_name); 33380fa93fcSMichael Neuling adapter->irq_name = NULL; 334f204e0b8SIan Munsie return rc; 33580fa93fcSMichael Neuling } 336f204e0b8SIan Munsie 337f204e0b8SIan Munsie cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->err_hwirq & 0xffff); 338f204e0b8SIan Munsie 339f204e0b8SIan Munsie return 0; 340f204e0b8SIan Munsie } 341f204e0b8SIan Munsie 342f204e0b8SIan Munsie void cxl_release_psl_err_irq(struct cxl *adapter) 343f204e0b8SIan Munsie { 344f204e0b8SIan Munsie cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000); 345f204e0b8SIan Munsie cxl_unmap_irq(adapter->err_virq, adapter); 346f204e0b8SIan Munsie cxl_release_one_irq(adapter, adapter->err_hwirq); 34780fa93fcSMichael Neuling kfree(adapter->irq_name); 348f204e0b8SIan Munsie } 349f204e0b8SIan Munsie 350f204e0b8SIan Munsie int cxl_register_serr_irq(struct cxl_afu *afu) 351f204e0b8SIan Munsie { 352f204e0b8SIan Munsie u64 serr; 353f204e0b8SIan Munsie int rc; 354f204e0b8SIan Munsie 35580fa93fcSMichael Neuling afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err", 35680fa93fcSMichael Neuling dev_name(&afu->dev)); 35780fa93fcSMichael Neuling if (!afu->err_irq_name) 35880fa93fcSMichael Neuling return -ENOMEM; 35980fa93fcSMichael Neuling 360f204e0b8SIan Munsie if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu, 361f204e0b8SIan Munsie &afu->serr_hwirq, 36280fa93fcSMichael Neuling &afu->serr_virq, afu->err_irq_name))) { 36380fa93fcSMichael Neuling kfree(afu->err_irq_name); 36480fa93fcSMichael Neuling afu->err_irq_name = NULL; 365f204e0b8SIan Munsie return rc; 36680fa93fcSMichael Neuling } 367f204e0b8SIan Munsie 368f204e0b8SIan Munsie serr = cxl_p1n_read(afu, CXL_PSL_SERR_An); 369f204e0b8SIan Munsie serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff); 370f204e0b8SIan Munsie cxl_p1n_write(afu, CXL_PSL_SERR_An, serr); 371f204e0b8SIan Munsie 372f204e0b8SIan Munsie return 0; 373f204e0b8SIan Munsie } 374f204e0b8SIan Munsie 375f204e0b8SIan Munsie void cxl_release_serr_irq(struct cxl_afu *afu) 376f204e0b8SIan Munsie { 377f204e0b8SIan Munsie cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000); 378f204e0b8SIan Munsie cxl_unmap_irq(afu->serr_virq, afu); 379f204e0b8SIan Munsie cxl_release_one_irq(afu->adapter, afu->serr_hwirq); 38080fa93fcSMichael Neuling kfree(afu->err_irq_name); 381f204e0b8SIan Munsie } 382f204e0b8SIan Munsie 383f204e0b8SIan Munsie int cxl_register_psl_irq(struct cxl_afu *afu) 384f204e0b8SIan Munsie { 38580fa93fcSMichael Neuling int rc; 38680fa93fcSMichael Neuling 38780fa93fcSMichael Neuling afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s", 38880fa93fcSMichael Neuling dev_name(&afu->dev)); 38980fa93fcSMichael Neuling if (!afu->psl_irq_name) 39080fa93fcSMichael Neuling return -ENOMEM; 39180fa93fcSMichael Neuling 39280fa93fcSMichael Neuling if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu, 39380fa93fcSMichael Neuling &afu->psl_hwirq, &afu->psl_virq, 39480fa93fcSMichael Neuling afu->psl_irq_name))) { 39580fa93fcSMichael Neuling kfree(afu->psl_irq_name); 39680fa93fcSMichael Neuling afu->psl_irq_name = NULL; 39780fa93fcSMichael Neuling } 39880fa93fcSMichael Neuling return rc; 399f204e0b8SIan Munsie } 400f204e0b8SIan Munsie 401f204e0b8SIan Munsie void cxl_release_psl_irq(struct cxl_afu *afu) 402f204e0b8SIan Munsie { 403f204e0b8SIan Munsie cxl_unmap_irq(afu->psl_virq, afu); 404f204e0b8SIan Munsie cxl_release_one_irq(afu->adapter, afu->psl_hwirq); 40580fa93fcSMichael Neuling kfree(afu->psl_irq_name); 40680fa93fcSMichael Neuling } 40780fa93fcSMichael Neuling 40880fa93fcSMichael Neuling void afu_irq_name_free(struct cxl_context *ctx) 40980fa93fcSMichael Neuling { 41080fa93fcSMichael Neuling struct cxl_irq_name *irq_name, *tmp; 41180fa93fcSMichael Neuling 41280fa93fcSMichael Neuling list_for_each_entry_safe(irq_name, tmp, &ctx->irq_names, list) { 41380fa93fcSMichael Neuling kfree(irq_name->name); 41480fa93fcSMichael Neuling list_del(&irq_name->list); 41580fa93fcSMichael Neuling kfree(irq_name); 41680fa93fcSMichael Neuling } 417f204e0b8SIan Munsie } 418f204e0b8SIan Munsie 419f204e0b8SIan Munsie int afu_register_irqs(struct cxl_context *ctx, u32 count) 420f204e0b8SIan Munsie { 421f204e0b8SIan Munsie irq_hw_number_t hwirq; 42280fa93fcSMichael Neuling int rc, r, i, j = 1; 42380fa93fcSMichael Neuling struct cxl_irq_name *irq_name; 424f204e0b8SIan Munsie 425f204e0b8SIan Munsie if ((rc = cxl_alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter, count))) 426f204e0b8SIan Munsie return rc; 427f204e0b8SIan Munsie 428f204e0b8SIan Munsie /* Multiplexed PSL Interrupt */ 429f204e0b8SIan Munsie ctx->irqs.offset[0] = ctx->afu->psl_hwirq; 430f204e0b8SIan Munsie ctx->irqs.range[0] = 1; 431f204e0b8SIan Munsie 432f204e0b8SIan Munsie ctx->irq_count = count; 433f204e0b8SIan Munsie ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count), 434f204e0b8SIan Munsie sizeof(*ctx->irq_bitmap), GFP_KERNEL); 435f204e0b8SIan Munsie if (!ctx->irq_bitmap) 436f204e0b8SIan Munsie return -ENOMEM; 43780fa93fcSMichael Neuling 43880fa93fcSMichael Neuling /* 43980fa93fcSMichael Neuling * Allocate names first. If any fail, bail out before allocating 44080fa93fcSMichael Neuling * actual hardware IRQs. 44180fa93fcSMichael Neuling */ 44280fa93fcSMichael Neuling INIT_LIST_HEAD(&ctx->irq_names); 44380fa93fcSMichael Neuling for (r = 1; r < CXL_IRQ_RANGES; r++) { 444d3383aaaSColin Ian King for (i = 0; i < ctx->irqs.range[r]; i++) { 44580fa93fcSMichael Neuling irq_name = kmalloc(sizeof(struct cxl_irq_name), 44680fa93fcSMichael Neuling GFP_KERNEL); 44780fa93fcSMichael Neuling if (!irq_name) 44880fa93fcSMichael Neuling goto out; 44980fa93fcSMichael Neuling irq_name->name = kasprintf(GFP_KERNEL, "cxl-%s-pe%i-%i", 45080fa93fcSMichael Neuling dev_name(&ctx->afu->dev), 45180fa93fcSMichael Neuling ctx->pe, j); 45280fa93fcSMichael Neuling if (!irq_name->name) { 45380fa93fcSMichael Neuling kfree(irq_name); 45480fa93fcSMichael Neuling goto out; 45580fa93fcSMichael Neuling } 45680fa93fcSMichael Neuling /* Add to tail so next look get the correct order */ 45780fa93fcSMichael Neuling list_add_tail(&irq_name->list, &ctx->irq_names); 45880fa93fcSMichael Neuling j++; 45980fa93fcSMichael Neuling } 46080fa93fcSMichael Neuling } 46180fa93fcSMichael Neuling 46280fa93fcSMichael Neuling /* We've allocated all memory now, so let's do the irq allocations */ 46380fa93fcSMichael Neuling irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list); 464f204e0b8SIan Munsie for (r = 1; r < CXL_IRQ_RANGES; r++) { 465f204e0b8SIan Munsie hwirq = ctx->irqs.offset[r]; 466f204e0b8SIan Munsie for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) { 467f204e0b8SIan Munsie cxl_map_irq(ctx->afu->adapter, hwirq, 46880fa93fcSMichael Neuling cxl_irq_afu, ctx, irq_name->name); 46980fa93fcSMichael Neuling irq_name = list_next_entry(irq_name, list); 470f204e0b8SIan Munsie } 471f204e0b8SIan Munsie } 472f204e0b8SIan Munsie 473f204e0b8SIan Munsie return 0; 47480fa93fcSMichael Neuling 47580fa93fcSMichael Neuling out: 47680fa93fcSMichael Neuling afu_irq_name_free(ctx); 47780fa93fcSMichael Neuling return -ENOMEM; 478f204e0b8SIan Munsie } 479f204e0b8SIan Munsie 480f204e0b8SIan Munsie void afu_release_irqs(struct cxl_context *ctx) 481f204e0b8SIan Munsie { 482f204e0b8SIan Munsie irq_hw_number_t hwirq; 483f204e0b8SIan Munsie unsigned int virq; 484f204e0b8SIan Munsie int r, i; 485f204e0b8SIan Munsie 486f204e0b8SIan Munsie for (r = 1; r < CXL_IRQ_RANGES; r++) { 487f204e0b8SIan Munsie hwirq = ctx->irqs.offset[r]; 488f204e0b8SIan Munsie for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) { 489f204e0b8SIan Munsie virq = irq_find_mapping(NULL, hwirq); 490f204e0b8SIan Munsie if (virq) 491f204e0b8SIan Munsie cxl_unmap_irq(virq, ctx); 492f204e0b8SIan Munsie } 493f204e0b8SIan Munsie } 494f204e0b8SIan Munsie 49580fa93fcSMichael Neuling afu_irq_name_free(ctx); 496f204e0b8SIan Munsie cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter); 497f204e0b8SIan Munsie } 498