114baf4d9SChristophe Lombard /* 214baf4d9SChristophe Lombard * Copyright 2015 IBM Corp. 314baf4d9SChristophe Lombard * 414baf4d9SChristophe Lombard * This program is free software; you can redistribute it and/or 514baf4d9SChristophe Lombard * modify it under the terms of the GNU General Public License 614baf4d9SChristophe Lombard * as published by the Free Software Foundation; either version 714baf4d9SChristophe Lombard * 2 of the License, or (at your option) any later version. 814baf4d9SChristophe Lombard */ 914baf4d9SChristophe Lombard 1014baf4d9SChristophe Lombard #include <linux/spinlock.h> 1114baf4d9SChristophe Lombard #include <linux/uaccess.h> 1214baf4d9SChristophe Lombard #include <linux/delay.h> 1314baf4d9SChristophe Lombard 1414baf4d9SChristophe Lombard #include "cxl.h" 1514baf4d9SChristophe Lombard #include "hcalls.h" 1614baf4d9SChristophe Lombard #include "trace.h" 1714baf4d9SChristophe Lombard 180d400f77SChristophe Lombard #define CXL_ERROR_DETECTED_EVENT 1 190d400f77SChristophe Lombard #define CXL_SLOT_RESET_EVENT 2 200d400f77SChristophe Lombard #define CXL_RESUME_EVENT 3 210d400f77SChristophe Lombard 220d400f77SChristophe Lombard static void pci_error_handlers(struct cxl_afu *afu, 230d400f77SChristophe Lombard int bus_error_event, 240d400f77SChristophe Lombard pci_channel_state_t state) 250d400f77SChristophe Lombard { 260d400f77SChristophe Lombard struct pci_dev *afu_dev; 270d400f77SChristophe Lombard 280d400f77SChristophe Lombard if (afu->phb == NULL) 290d400f77SChristophe Lombard return; 300d400f77SChristophe Lombard 310d400f77SChristophe Lombard list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) { 320d400f77SChristophe Lombard if (!afu_dev->driver) 330d400f77SChristophe Lombard continue; 340d400f77SChristophe Lombard 350d400f77SChristophe Lombard switch (bus_error_event) { 360d400f77SChristophe Lombard case CXL_ERROR_DETECTED_EVENT: 370d400f77SChristophe Lombard afu_dev->error_state = state; 380d400f77SChristophe Lombard 390d400f77SChristophe Lombard if (afu_dev->driver->err_handler && 400d400f77SChristophe Lombard afu_dev->driver->err_handler->error_detected) 410d400f77SChristophe Lombard afu_dev->driver->err_handler->error_detected(afu_dev, state); 420d400f77SChristophe Lombard break; 430d400f77SChristophe Lombard case CXL_SLOT_RESET_EVENT: 440d400f77SChristophe Lombard afu_dev->error_state = state; 450d400f77SChristophe Lombard 460d400f77SChristophe Lombard if (afu_dev->driver->err_handler && 470d400f77SChristophe Lombard afu_dev->driver->err_handler->slot_reset) 480d400f77SChristophe Lombard afu_dev->driver->err_handler->slot_reset(afu_dev); 490d400f77SChristophe Lombard break; 500d400f77SChristophe Lombard case CXL_RESUME_EVENT: 510d400f77SChristophe Lombard if (afu_dev->driver->err_handler && 520d400f77SChristophe Lombard afu_dev->driver->err_handler->resume) 530d400f77SChristophe Lombard afu_dev->driver->err_handler->resume(afu_dev); 540d400f77SChristophe Lombard break; 550d400f77SChristophe Lombard } 560d400f77SChristophe Lombard } 570d400f77SChristophe Lombard } 5814baf4d9SChristophe Lombard 5914baf4d9SChristophe Lombard static irqreturn_t guest_handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr, 6014baf4d9SChristophe Lombard u64 errstat) 6114baf4d9SChristophe Lombard { 6214baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 6314baf4d9SChristophe Lombard dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat); 6414baf4d9SChristophe Lombard 6514baf4d9SChristophe Lombard return cxl_ops->ack_irq(ctx, 0, errstat); 6614baf4d9SChristophe Lombard } 6714baf4d9SChristophe Lombard 6814baf4d9SChristophe Lombard static ssize_t guest_collect_vpd(struct cxl *adapter, struct cxl_afu *afu, 6914baf4d9SChristophe Lombard void *buf, size_t len) 7014baf4d9SChristophe Lombard { 7114baf4d9SChristophe Lombard unsigned int entries, mod; 7214baf4d9SChristophe Lombard unsigned long **vpd_buf = NULL; 7314baf4d9SChristophe Lombard struct sg_list *le; 7414baf4d9SChristophe Lombard int rc = 0, i, tocopy; 7514baf4d9SChristophe Lombard u64 out = 0; 7614baf4d9SChristophe Lombard 7714baf4d9SChristophe Lombard if (buf == NULL) 7814baf4d9SChristophe Lombard return -EINVAL; 7914baf4d9SChristophe Lombard 8014baf4d9SChristophe Lombard /* number of entries in the list */ 8114baf4d9SChristophe Lombard entries = len / SG_BUFFER_SIZE; 8214baf4d9SChristophe Lombard mod = len % SG_BUFFER_SIZE; 8314baf4d9SChristophe Lombard if (mod) 8414baf4d9SChristophe Lombard entries++; 8514baf4d9SChristophe Lombard 8614baf4d9SChristophe Lombard if (entries > SG_MAX_ENTRIES) { 8714baf4d9SChristophe Lombard entries = SG_MAX_ENTRIES; 8814baf4d9SChristophe Lombard len = SG_MAX_ENTRIES * SG_BUFFER_SIZE; 8914baf4d9SChristophe Lombard mod = 0; 9014baf4d9SChristophe Lombard } 9114baf4d9SChristophe Lombard 9214baf4d9SChristophe Lombard vpd_buf = kzalloc(entries * sizeof(unsigned long *), GFP_KERNEL); 9314baf4d9SChristophe Lombard if (!vpd_buf) 9414baf4d9SChristophe Lombard return -ENOMEM; 9514baf4d9SChristophe Lombard 9614baf4d9SChristophe Lombard le = (struct sg_list *)get_zeroed_page(GFP_KERNEL); 9714baf4d9SChristophe Lombard if (!le) { 9814baf4d9SChristophe Lombard rc = -ENOMEM; 9914baf4d9SChristophe Lombard goto err1; 10014baf4d9SChristophe Lombard } 10114baf4d9SChristophe Lombard 10214baf4d9SChristophe Lombard for (i = 0; i < entries; i++) { 10314baf4d9SChristophe Lombard vpd_buf[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL); 10414baf4d9SChristophe Lombard if (!vpd_buf[i]) { 10514baf4d9SChristophe Lombard rc = -ENOMEM; 10614baf4d9SChristophe Lombard goto err2; 10714baf4d9SChristophe Lombard } 10814baf4d9SChristophe Lombard le[i].phys_addr = cpu_to_be64(virt_to_phys(vpd_buf[i])); 10914baf4d9SChristophe Lombard le[i].len = cpu_to_be64(SG_BUFFER_SIZE); 11014baf4d9SChristophe Lombard if ((i == (entries - 1)) && mod) 11114baf4d9SChristophe Lombard le[i].len = cpu_to_be64(mod); 11214baf4d9SChristophe Lombard } 11314baf4d9SChristophe Lombard 11414baf4d9SChristophe Lombard if (adapter) 11514baf4d9SChristophe Lombard rc = cxl_h_collect_vpd_adapter(adapter->guest->handle, 11614baf4d9SChristophe Lombard virt_to_phys(le), entries, &out); 11714baf4d9SChristophe Lombard else 11814baf4d9SChristophe Lombard rc = cxl_h_collect_vpd(afu->guest->handle, 0, 11914baf4d9SChristophe Lombard virt_to_phys(le), entries, &out); 12014baf4d9SChristophe Lombard pr_devel("length of available (entries: %i), vpd: %#llx\n", 12114baf4d9SChristophe Lombard entries, out); 12214baf4d9SChristophe Lombard 12314baf4d9SChristophe Lombard if (!rc) { 12414baf4d9SChristophe Lombard /* 12514baf4d9SChristophe Lombard * hcall returns in 'out' the size of available VPDs. 12614baf4d9SChristophe Lombard * It fills the buffer with as much data as possible. 12714baf4d9SChristophe Lombard */ 12814baf4d9SChristophe Lombard if (out < len) 12914baf4d9SChristophe Lombard len = out; 13014baf4d9SChristophe Lombard rc = len; 13114baf4d9SChristophe Lombard if (out) { 13214baf4d9SChristophe Lombard for (i = 0; i < entries; i++) { 13314baf4d9SChristophe Lombard if (len < SG_BUFFER_SIZE) 13414baf4d9SChristophe Lombard tocopy = len; 13514baf4d9SChristophe Lombard else 13614baf4d9SChristophe Lombard tocopy = SG_BUFFER_SIZE; 13714baf4d9SChristophe Lombard memcpy(buf, vpd_buf[i], tocopy); 13814baf4d9SChristophe Lombard buf += tocopy; 13914baf4d9SChristophe Lombard len -= tocopy; 14014baf4d9SChristophe Lombard } 14114baf4d9SChristophe Lombard } 14214baf4d9SChristophe Lombard } 14314baf4d9SChristophe Lombard err2: 14414baf4d9SChristophe Lombard for (i = 0; i < entries; i++) { 14514baf4d9SChristophe Lombard if (vpd_buf[i]) 14614baf4d9SChristophe Lombard free_page((unsigned long) vpd_buf[i]); 14714baf4d9SChristophe Lombard } 14814baf4d9SChristophe Lombard free_page((unsigned long) le); 14914baf4d9SChristophe Lombard err1: 15014baf4d9SChristophe Lombard kfree(vpd_buf); 15114baf4d9SChristophe Lombard return rc; 15214baf4d9SChristophe Lombard } 15314baf4d9SChristophe Lombard 15414baf4d9SChristophe Lombard static int guest_get_irq_info(struct cxl_context *ctx, struct cxl_irq_info *info) 15514baf4d9SChristophe Lombard { 15614baf4d9SChristophe Lombard return cxl_h_collect_int_info(ctx->afu->guest->handle, ctx->process_token, info); 15714baf4d9SChristophe Lombard } 15814baf4d9SChristophe Lombard 15914baf4d9SChristophe Lombard static irqreturn_t guest_psl_irq(int irq, void *data) 16014baf4d9SChristophe Lombard { 16114baf4d9SChristophe Lombard struct cxl_context *ctx = data; 16214baf4d9SChristophe Lombard struct cxl_irq_info irq_info; 16314baf4d9SChristophe Lombard int rc; 16414baf4d9SChristophe Lombard 16514baf4d9SChristophe Lombard pr_devel("%d: received PSL interrupt %i\n", ctx->pe, irq); 16614baf4d9SChristophe Lombard rc = guest_get_irq_info(ctx, &irq_info); 16714baf4d9SChristophe Lombard if (rc) { 16814baf4d9SChristophe Lombard WARN(1, "Unable to get IRQ info: %i\n", rc); 16914baf4d9SChristophe Lombard return IRQ_HANDLED; 17014baf4d9SChristophe Lombard } 17114baf4d9SChristophe Lombard 172*64663f37SChristophe Lombard rc = cxl_irq_psl8(irq, ctx, &irq_info); 17314baf4d9SChristophe Lombard return rc; 17414baf4d9SChristophe Lombard } 17514baf4d9SChristophe Lombard 1760d400f77SChristophe Lombard static int afu_read_error_state(struct cxl_afu *afu, int *state_out) 1770d400f77SChristophe Lombard { 1780d400f77SChristophe Lombard u64 state; 1790d400f77SChristophe Lombard int rc = 0; 1800d400f77SChristophe Lombard 181266eab8fSChristophe Lombard if (!afu) 182266eab8fSChristophe Lombard return -EIO; 183266eab8fSChristophe Lombard 1840d400f77SChristophe Lombard rc = cxl_h_read_error_state(afu->guest->handle, &state); 1850d400f77SChristophe Lombard if (!rc) { 1860d400f77SChristophe Lombard WARN_ON(state != H_STATE_NORMAL && 1870d400f77SChristophe Lombard state != H_STATE_DISABLE && 1880d400f77SChristophe Lombard state != H_STATE_TEMP_UNAVAILABLE && 1890d400f77SChristophe Lombard state != H_STATE_PERM_UNAVAILABLE); 1900d400f77SChristophe Lombard *state_out = state & 0xffffffff; 1910d400f77SChristophe Lombard } 1920d400f77SChristophe Lombard return rc; 1930d400f77SChristophe Lombard } 1940d400f77SChristophe Lombard 19514baf4d9SChristophe Lombard static irqreturn_t guest_slice_irq_err(int irq, void *data) 19614baf4d9SChristophe Lombard { 19714baf4d9SChristophe Lombard struct cxl_afu *afu = data; 19814baf4d9SChristophe Lombard int rc; 1996e0c50f9SPhilippe Bergheaud u64 serr, afu_error, dsisr; 20014baf4d9SChristophe Lombard 20114baf4d9SChristophe Lombard rc = cxl_h_get_fn_error_interrupt(afu->guest->handle, &serr); 20214baf4d9SChristophe Lombard if (rc) { 20314baf4d9SChristophe Lombard dev_crit(&afu->dev, "Couldn't read PSL_SERR_An: %d\n", rc); 20414baf4d9SChristophe Lombard return IRQ_HANDLED; 20514baf4d9SChristophe Lombard } 2066e0c50f9SPhilippe Bergheaud afu_error = cxl_p2n_read(afu, CXL_AFU_ERR_An); 2076e0c50f9SPhilippe Bergheaud dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An); 2086e0c50f9SPhilippe Bergheaud cxl_afu_decode_psl_serr(afu, serr); 2096e0c50f9SPhilippe Bergheaud dev_crit(&afu->dev, "AFU_ERR_An: 0x%.16llx\n", afu_error); 2106e0c50f9SPhilippe Bergheaud dev_crit(&afu->dev, "PSL_DSISR_An: 0x%.16llx\n", dsisr); 21114baf4d9SChristophe Lombard 21214baf4d9SChristophe Lombard rc = cxl_h_ack_fn_error_interrupt(afu->guest->handle, serr); 21314baf4d9SChristophe Lombard if (rc) 21414baf4d9SChristophe Lombard dev_crit(&afu->dev, "Couldn't ack slice error interrupt: %d\n", 21514baf4d9SChristophe Lombard rc); 21614baf4d9SChristophe Lombard 21714baf4d9SChristophe Lombard return IRQ_HANDLED; 21814baf4d9SChristophe Lombard } 21914baf4d9SChristophe Lombard 22014baf4d9SChristophe Lombard 22114baf4d9SChristophe Lombard static int irq_alloc_range(struct cxl *adapter, int len, int *irq) 22214baf4d9SChristophe Lombard { 22314baf4d9SChristophe Lombard int i, n; 22414baf4d9SChristophe Lombard struct irq_avail *cur; 22514baf4d9SChristophe Lombard 22614baf4d9SChristophe Lombard for (i = 0; i < adapter->guest->irq_nranges; i++) { 22714baf4d9SChristophe Lombard cur = &adapter->guest->irq_avail[i]; 22814baf4d9SChristophe Lombard n = bitmap_find_next_zero_area(cur->bitmap, cur->range, 22914baf4d9SChristophe Lombard 0, len, 0); 23014baf4d9SChristophe Lombard if (n < cur->range) { 23114baf4d9SChristophe Lombard bitmap_set(cur->bitmap, n, len); 23214baf4d9SChristophe Lombard *irq = cur->offset + n; 23314baf4d9SChristophe Lombard pr_devel("guest: allocate IRQs %#x->%#x\n", 23414baf4d9SChristophe Lombard *irq, *irq + len - 1); 23514baf4d9SChristophe Lombard 23614baf4d9SChristophe Lombard return 0; 23714baf4d9SChristophe Lombard } 23814baf4d9SChristophe Lombard } 23914baf4d9SChristophe Lombard return -ENOSPC; 24014baf4d9SChristophe Lombard } 24114baf4d9SChristophe Lombard 24214baf4d9SChristophe Lombard static int irq_free_range(struct cxl *adapter, int irq, int len) 24314baf4d9SChristophe Lombard { 24414baf4d9SChristophe Lombard int i, n; 24514baf4d9SChristophe Lombard struct irq_avail *cur; 24614baf4d9SChristophe Lombard 24714baf4d9SChristophe Lombard if (len == 0) 24814baf4d9SChristophe Lombard return -ENOENT; 24914baf4d9SChristophe Lombard 25014baf4d9SChristophe Lombard for (i = 0; i < adapter->guest->irq_nranges; i++) { 25114baf4d9SChristophe Lombard cur = &adapter->guest->irq_avail[i]; 25214baf4d9SChristophe Lombard if (irq >= cur->offset && 25314baf4d9SChristophe Lombard (irq + len) <= (cur->offset + cur->range)) { 25414baf4d9SChristophe Lombard n = irq - cur->offset; 25514baf4d9SChristophe Lombard bitmap_clear(cur->bitmap, n, len); 25614baf4d9SChristophe Lombard pr_devel("guest: release IRQs %#x->%#x\n", 25714baf4d9SChristophe Lombard irq, irq + len - 1); 25814baf4d9SChristophe Lombard return 0; 25914baf4d9SChristophe Lombard } 26014baf4d9SChristophe Lombard } 26114baf4d9SChristophe Lombard return -ENOENT; 26214baf4d9SChristophe Lombard } 26314baf4d9SChristophe Lombard 26414baf4d9SChristophe Lombard static int guest_reset(struct cxl *adapter) 26514baf4d9SChristophe Lombard { 2660d400f77SChristophe Lombard struct cxl_afu *afu = NULL; 2670d400f77SChristophe Lombard int i, rc; 26814baf4d9SChristophe Lombard 26914baf4d9SChristophe Lombard pr_devel("Adapter reset request\n"); 2700d400f77SChristophe Lombard for (i = 0; i < adapter->slices; i++) { 2710d400f77SChristophe Lombard if ((afu = adapter->afu[i])) { 2720d400f77SChristophe Lombard pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT, 2730d400f77SChristophe Lombard pci_channel_io_frozen); 2740d400f77SChristophe Lombard cxl_context_detach_all(afu); 2750d400f77SChristophe Lombard } 2760d400f77SChristophe Lombard } 2770d400f77SChristophe Lombard 27814baf4d9SChristophe Lombard rc = cxl_h_reset_adapter(adapter->guest->handle); 2790d400f77SChristophe Lombard for (i = 0; i < adapter->slices; i++) { 2800d400f77SChristophe Lombard if (!rc && (afu = adapter->afu[i])) { 2810d400f77SChristophe Lombard pci_error_handlers(afu, CXL_SLOT_RESET_EVENT, 2820d400f77SChristophe Lombard pci_channel_io_normal); 2830d400f77SChristophe Lombard pci_error_handlers(afu, CXL_RESUME_EVENT, 0); 2840d400f77SChristophe Lombard } 2850d400f77SChristophe Lombard } 28614baf4d9SChristophe Lombard return rc; 28714baf4d9SChristophe Lombard } 28814baf4d9SChristophe Lombard 28914baf4d9SChristophe Lombard static int guest_alloc_one_irq(struct cxl *adapter) 29014baf4d9SChristophe Lombard { 29114baf4d9SChristophe Lombard int irq; 29214baf4d9SChristophe Lombard 29314baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 29414baf4d9SChristophe Lombard if (irq_alloc_range(adapter, 1, &irq)) 29514baf4d9SChristophe Lombard irq = -ENOSPC; 29614baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 29714baf4d9SChristophe Lombard return irq; 29814baf4d9SChristophe Lombard } 29914baf4d9SChristophe Lombard 30014baf4d9SChristophe Lombard static void guest_release_one_irq(struct cxl *adapter, int irq) 30114baf4d9SChristophe Lombard { 30214baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 30314baf4d9SChristophe Lombard irq_free_range(adapter, irq, 1); 30414baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 30514baf4d9SChristophe Lombard } 30614baf4d9SChristophe Lombard 30714baf4d9SChristophe Lombard static int guest_alloc_irq_ranges(struct cxl_irq_ranges *irqs, 30814baf4d9SChristophe Lombard struct cxl *adapter, unsigned int num) 30914baf4d9SChristophe Lombard { 31014baf4d9SChristophe Lombard int i, try, irq; 31114baf4d9SChristophe Lombard 31214baf4d9SChristophe Lombard memset(irqs, 0, sizeof(struct cxl_irq_ranges)); 31314baf4d9SChristophe Lombard 31414baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 31514baf4d9SChristophe Lombard for (i = 0; i < CXL_IRQ_RANGES && num; i++) { 31614baf4d9SChristophe Lombard try = num; 31714baf4d9SChristophe Lombard while (try) { 31814baf4d9SChristophe Lombard if (irq_alloc_range(adapter, try, &irq) == 0) 31914baf4d9SChristophe Lombard break; 32014baf4d9SChristophe Lombard try /= 2; 32114baf4d9SChristophe Lombard } 32214baf4d9SChristophe Lombard if (!try) 32314baf4d9SChristophe Lombard goto error; 32414baf4d9SChristophe Lombard irqs->offset[i] = irq; 32514baf4d9SChristophe Lombard irqs->range[i] = try; 32614baf4d9SChristophe Lombard num -= try; 32714baf4d9SChristophe Lombard } 32814baf4d9SChristophe Lombard if (num) 32914baf4d9SChristophe Lombard goto error; 33014baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 33114baf4d9SChristophe Lombard return 0; 33214baf4d9SChristophe Lombard 33314baf4d9SChristophe Lombard error: 33414baf4d9SChristophe Lombard for (i = 0; i < CXL_IRQ_RANGES; i++) 33514baf4d9SChristophe Lombard irq_free_range(adapter, irqs->offset[i], irqs->range[i]); 33614baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 33714baf4d9SChristophe Lombard return -ENOSPC; 33814baf4d9SChristophe Lombard } 33914baf4d9SChristophe Lombard 34014baf4d9SChristophe Lombard static void guest_release_irq_ranges(struct cxl_irq_ranges *irqs, 34114baf4d9SChristophe Lombard struct cxl *adapter) 34214baf4d9SChristophe Lombard { 34314baf4d9SChristophe Lombard int i; 34414baf4d9SChristophe Lombard 34514baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 34614baf4d9SChristophe Lombard for (i = 0; i < CXL_IRQ_RANGES; i++) 34714baf4d9SChristophe Lombard irq_free_range(adapter, irqs->offset[i], irqs->range[i]); 34814baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 34914baf4d9SChristophe Lombard } 35014baf4d9SChristophe Lombard 35114baf4d9SChristophe Lombard static int guest_register_serr_irq(struct cxl_afu *afu) 35214baf4d9SChristophe Lombard { 35314baf4d9SChristophe Lombard afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err", 35414baf4d9SChristophe Lombard dev_name(&afu->dev)); 35514baf4d9SChristophe Lombard if (!afu->err_irq_name) 35614baf4d9SChristophe Lombard return -ENOMEM; 35714baf4d9SChristophe Lombard 35814baf4d9SChristophe Lombard if (!(afu->serr_virq = cxl_map_irq(afu->adapter, afu->serr_hwirq, 35914baf4d9SChristophe Lombard guest_slice_irq_err, afu, afu->err_irq_name))) { 36014baf4d9SChristophe Lombard kfree(afu->err_irq_name); 36114baf4d9SChristophe Lombard afu->err_irq_name = NULL; 36214baf4d9SChristophe Lombard return -ENOMEM; 36314baf4d9SChristophe Lombard } 36414baf4d9SChristophe Lombard 36514baf4d9SChristophe Lombard return 0; 36614baf4d9SChristophe Lombard } 36714baf4d9SChristophe Lombard 36814baf4d9SChristophe Lombard static void guest_release_serr_irq(struct cxl_afu *afu) 36914baf4d9SChristophe Lombard { 37014baf4d9SChristophe Lombard cxl_unmap_irq(afu->serr_virq, afu); 37114baf4d9SChristophe Lombard cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq); 37214baf4d9SChristophe Lombard kfree(afu->err_irq_name); 37314baf4d9SChristophe Lombard } 37414baf4d9SChristophe Lombard 37514baf4d9SChristophe Lombard static int guest_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask) 37614baf4d9SChristophe Lombard { 37714baf4d9SChristophe Lombard return cxl_h_control_faults(ctx->afu->guest->handle, ctx->process_token, 37814baf4d9SChristophe Lombard tfc >> 32, (psl_reset_mask != 0)); 37914baf4d9SChristophe Lombard } 38014baf4d9SChristophe Lombard 38114baf4d9SChristophe Lombard static void disable_afu_irqs(struct cxl_context *ctx) 38214baf4d9SChristophe Lombard { 38314baf4d9SChristophe Lombard irq_hw_number_t hwirq; 38414baf4d9SChristophe Lombard unsigned int virq; 38514baf4d9SChristophe Lombard int r, i; 38614baf4d9SChristophe Lombard 38714baf4d9SChristophe Lombard pr_devel("Disabling AFU(%d) interrupts\n", ctx->afu->slice); 38814baf4d9SChristophe Lombard for (r = 0; r < CXL_IRQ_RANGES; r++) { 38914baf4d9SChristophe Lombard hwirq = ctx->irqs.offset[r]; 39014baf4d9SChristophe Lombard for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) { 39114baf4d9SChristophe Lombard virq = irq_find_mapping(NULL, hwirq); 39214baf4d9SChristophe Lombard disable_irq(virq); 39314baf4d9SChristophe Lombard } 39414baf4d9SChristophe Lombard } 39514baf4d9SChristophe Lombard } 39614baf4d9SChristophe Lombard 39714baf4d9SChristophe Lombard static void enable_afu_irqs(struct cxl_context *ctx) 39814baf4d9SChristophe Lombard { 39914baf4d9SChristophe Lombard irq_hw_number_t hwirq; 40014baf4d9SChristophe Lombard unsigned int virq; 40114baf4d9SChristophe Lombard int r, i; 40214baf4d9SChristophe Lombard 40314baf4d9SChristophe Lombard pr_devel("Enabling AFU(%d) interrupts\n", ctx->afu->slice); 40414baf4d9SChristophe Lombard for (r = 0; r < CXL_IRQ_RANGES; r++) { 40514baf4d9SChristophe Lombard hwirq = ctx->irqs.offset[r]; 40614baf4d9SChristophe Lombard for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) { 40714baf4d9SChristophe Lombard virq = irq_find_mapping(NULL, hwirq); 40814baf4d9SChristophe Lombard enable_irq(virq); 40914baf4d9SChristophe Lombard } 41014baf4d9SChristophe Lombard } 41114baf4d9SChristophe Lombard } 41214baf4d9SChristophe Lombard 41314baf4d9SChristophe Lombard static int _guest_afu_cr_readXX(int sz, struct cxl_afu *afu, int cr_idx, 41414baf4d9SChristophe Lombard u64 offset, u64 *val) 41514baf4d9SChristophe Lombard { 41614baf4d9SChristophe Lombard unsigned long cr; 41714baf4d9SChristophe Lombard char c; 41814baf4d9SChristophe Lombard int rc = 0; 41914baf4d9SChristophe Lombard 42014baf4d9SChristophe Lombard if (afu->crs_len < sz) 42114baf4d9SChristophe Lombard return -ENOENT; 42214baf4d9SChristophe Lombard 42314baf4d9SChristophe Lombard if (unlikely(offset >= afu->crs_len)) 42414baf4d9SChristophe Lombard return -ERANGE; 42514baf4d9SChristophe Lombard 42614baf4d9SChristophe Lombard cr = get_zeroed_page(GFP_KERNEL); 42714baf4d9SChristophe Lombard if (!cr) 42814baf4d9SChristophe Lombard return -ENOMEM; 42914baf4d9SChristophe Lombard 43014baf4d9SChristophe Lombard rc = cxl_h_get_config(afu->guest->handle, cr_idx, offset, 43114baf4d9SChristophe Lombard virt_to_phys((void *)cr), sz); 43214baf4d9SChristophe Lombard if (rc) 43314baf4d9SChristophe Lombard goto err; 43414baf4d9SChristophe Lombard 43514baf4d9SChristophe Lombard switch (sz) { 43614baf4d9SChristophe Lombard case 1: 43714baf4d9SChristophe Lombard c = *((char *) cr); 43814baf4d9SChristophe Lombard *val = c; 43914baf4d9SChristophe Lombard break; 44014baf4d9SChristophe Lombard case 2: 44114baf4d9SChristophe Lombard *val = in_le16((u16 *)cr); 44214baf4d9SChristophe Lombard break; 44314baf4d9SChristophe Lombard case 4: 44414baf4d9SChristophe Lombard *val = in_le32((unsigned *)cr); 44514baf4d9SChristophe Lombard break; 44614baf4d9SChristophe Lombard case 8: 44714baf4d9SChristophe Lombard *val = in_le64((u64 *)cr); 44814baf4d9SChristophe Lombard break; 44914baf4d9SChristophe Lombard default: 45014baf4d9SChristophe Lombard WARN_ON(1); 45114baf4d9SChristophe Lombard } 45214baf4d9SChristophe Lombard err: 45314baf4d9SChristophe Lombard free_page(cr); 45414baf4d9SChristophe Lombard return rc; 45514baf4d9SChristophe Lombard } 45614baf4d9SChristophe Lombard 45714baf4d9SChristophe Lombard static int guest_afu_cr_read32(struct cxl_afu *afu, int cr_idx, u64 offset, 45814baf4d9SChristophe Lombard u32 *out) 45914baf4d9SChristophe Lombard { 46014baf4d9SChristophe Lombard int rc; 46114baf4d9SChristophe Lombard u64 val; 46214baf4d9SChristophe Lombard 46314baf4d9SChristophe Lombard rc = _guest_afu_cr_readXX(4, afu, cr_idx, offset, &val); 46414baf4d9SChristophe Lombard if (!rc) 46514baf4d9SChristophe Lombard *out = (u32) val; 46614baf4d9SChristophe Lombard return rc; 46714baf4d9SChristophe Lombard } 46814baf4d9SChristophe Lombard 46914baf4d9SChristophe Lombard static int guest_afu_cr_read16(struct cxl_afu *afu, int cr_idx, u64 offset, 47014baf4d9SChristophe Lombard u16 *out) 47114baf4d9SChristophe Lombard { 47214baf4d9SChristophe Lombard int rc; 47314baf4d9SChristophe Lombard u64 val; 47414baf4d9SChristophe Lombard 47514baf4d9SChristophe Lombard rc = _guest_afu_cr_readXX(2, afu, cr_idx, offset, &val); 47614baf4d9SChristophe Lombard if (!rc) 47714baf4d9SChristophe Lombard *out = (u16) val; 47814baf4d9SChristophe Lombard return rc; 47914baf4d9SChristophe Lombard } 48014baf4d9SChristophe Lombard 48114baf4d9SChristophe Lombard static int guest_afu_cr_read8(struct cxl_afu *afu, int cr_idx, u64 offset, 48214baf4d9SChristophe Lombard u8 *out) 48314baf4d9SChristophe Lombard { 48414baf4d9SChristophe Lombard int rc; 48514baf4d9SChristophe Lombard u64 val; 48614baf4d9SChristophe Lombard 48714baf4d9SChristophe Lombard rc = _guest_afu_cr_readXX(1, afu, cr_idx, offset, &val); 48814baf4d9SChristophe Lombard if (!rc) 48914baf4d9SChristophe Lombard *out = (u8) val; 49014baf4d9SChristophe Lombard return rc; 49114baf4d9SChristophe Lombard } 49214baf4d9SChristophe Lombard 49314baf4d9SChristophe Lombard static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx, u64 offset, 49414baf4d9SChristophe Lombard u64 *out) 49514baf4d9SChristophe Lombard { 49614baf4d9SChristophe Lombard return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out); 49714baf4d9SChristophe Lombard } 49814baf4d9SChristophe Lombard 499d601ea91SFrederic Barrat static int guest_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in) 500d601ea91SFrederic Barrat { 501d601ea91SFrederic Barrat /* config record is not writable from guest */ 502d601ea91SFrederic Barrat return -EPERM; 503d601ea91SFrederic Barrat } 504d601ea91SFrederic Barrat 505d601ea91SFrederic Barrat static int guest_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in) 506d601ea91SFrederic Barrat { 507d601ea91SFrederic Barrat /* config record is not writable from guest */ 508d601ea91SFrederic Barrat return -EPERM; 509d601ea91SFrederic Barrat } 510d601ea91SFrederic Barrat 511d601ea91SFrederic Barrat static int guest_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in) 512d601ea91SFrederic Barrat { 513d601ea91SFrederic Barrat /* config record is not writable from guest */ 514d601ea91SFrederic Barrat return -EPERM; 515d601ea91SFrederic Barrat } 516d601ea91SFrederic Barrat 51714baf4d9SChristophe Lombard static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr) 51814baf4d9SChristophe Lombard { 51914baf4d9SChristophe Lombard struct cxl_process_element_hcall *elem; 52014baf4d9SChristophe Lombard struct cxl *adapter = ctx->afu->adapter; 52114baf4d9SChristophe Lombard const struct cred *cred; 52214baf4d9SChristophe Lombard u32 pid, idx; 52314baf4d9SChristophe Lombard int rc, r, i; 52414baf4d9SChristophe Lombard u64 mmio_addr, mmio_size; 52514baf4d9SChristophe Lombard __be64 flags = 0; 52614baf4d9SChristophe Lombard 52714baf4d9SChristophe Lombard /* Must be 8 byte aligned and cannot cross a 4096 byte boundary */ 52814baf4d9SChristophe Lombard if (!(elem = (struct cxl_process_element_hcall *) 52914baf4d9SChristophe Lombard get_zeroed_page(GFP_KERNEL))) 53014baf4d9SChristophe Lombard return -ENOMEM; 53114baf4d9SChristophe Lombard 53214baf4d9SChristophe Lombard elem->version = cpu_to_be64(CXL_PROCESS_ELEMENT_VERSION); 53314baf4d9SChristophe Lombard if (ctx->kernel) { 53414baf4d9SChristophe Lombard pid = 0; 53514baf4d9SChristophe Lombard flags |= CXL_PE_TRANSLATION_ENABLED; 53614baf4d9SChristophe Lombard flags |= CXL_PE_PRIVILEGED_PROCESS; 53714baf4d9SChristophe Lombard if (mfmsr() & MSR_SF) 53814baf4d9SChristophe Lombard flags |= CXL_PE_64_BIT; 53914baf4d9SChristophe Lombard } else { 54014baf4d9SChristophe Lombard pid = current->pid; 54114baf4d9SChristophe Lombard flags |= CXL_PE_PROBLEM_STATE; 54214baf4d9SChristophe Lombard flags |= CXL_PE_TRANSLATION_ENABLED; 54314baf4d9SChristophe Lombard if (!test_tsk_thread_flag(current, TIF_32BIT)) 54414baf4d9SChristophe Lombard flags |= CXL_PE_64_BIT; 54514baf4d9SChristophe Lombard cred = get_current_cred(); 54614baf4d9SChristophe Lombard if (uid_eq(cred->euid, GLOBAL_ROOT_UID)) 54714baf4d9SChristophe Lombard flags |= CXL_PE_PRIVILEGED_PROCESS; 54814baf4d9SChristophe Lombard put_cred(cred); 54914baf4d9SChristophe Lombard } 55014baf4d9SChristophe Lombard elem->flags = cpu_to_be64(flags); 55114baf4d9SChristophe Lombard elem->common.tid = cpu_to_be32(0); /* Unused */ 55214baf4d9SChristophe Lombard elem->common.pid = cpu_to_be32(pid); 55314baf4d9SChristophe Lombard elem->common.csrp = cpu_to_be64(0); /* disable */ 55414baf4d9SChristophe Lombard elem->common.aurp0 = cpu_to_be64(0); /* disable */ 55514baf4d9SChristophe Lombard elem->common.aurp1 = cpu_to_be64(0); /* disable */ 55614baf4d9SChristophe Lombard 55714baf4d9SChristophe Lombard cxl_prefault(ctx, wed); 55814baf4d9SChristophe Lombard 55914baf4d9SChristophe Lombard elem->common.sstp0 = cpu_to_be64(ctx->sstp0); 56014baf4d9SChristophe Lombard elem->common.sstp1 = cpu_to_be64(ctx->sstp1); 5613c206fa7SIan Munsie 5623c206fa7SIan Munsie /* 5633c206fa7SIan Munsie * Ensure we have at least one interrupt allocated to take faults for 5643c206fa7SIan Munsie * kernel contexts that may not have allocated any AFU IRQs at all: 5653c206fa7SIan Munsie */ 5663c206fa7SIan Munsie if (ctx->irqs.range[0] == 0) { 5673c206fa7SIan Munsie rc = afu_register_irqs(ctx, 0); 5683c206fa7SIan Munsie if (rc) 5693c206fa7SIan Munsie goto out_free; 5703c206fa7SIan Munsie } 5713c206fa7SIan Munsie 57214baf4d9SChristophe Lombard for (r = 0; r < CXL_IRQ_RANGES; r++) { 57314baf4d9SChristophe Lombard for (i = 0; i < ctx->irqs.range[r]; i++) { 57414baf4d9SChristophe Lombard if (r == 0 && i == 0) { 57514baf4d9SChristophe Lombard elem->pslVirtualIsn = cpu_to_be32(ctx->irqs.offset[0]); 57614baf4d9SChristophe Lombard } else { 57714baf4d9SChristophe Lombard idx = ctx->irqs.offset[r] + i - adapter->guest->irq_base_offset; 57814baf4d9SChristophe Lombard elem->applicationVirtualIsnBitmap[idx / 8] |= 0x80 >> (idx % 8); 57914baf4d9SChristophe Lombard } 58014baf4d9SChristophe Lombard } 58114baf4d9SChristophe Lombard } 58214baf4d9SChristophe Lombard elem->common.amr = cpu_to_be64(amr); 58314baf4d9SChristophe Lombard elem->common.wed = cpu_to_be64(wed); 58414baf4d9SChristophe Lombard 58514baf4d9SChristophe Lombard disable_afu_irqs(ctx); 58614baf4d9SChristophe Lombard 58714baf4d9SChristophe Lombard rc = cxl_h_attach_process(ctx->afu->guest->handle, elem, 58814baf4d9SChristophe Lombard &ctx->process_token, &mmio_addr, &mmio_size); 58914baf4d9SChristophe Lombard if (rc == H_SUCCESS) { 59014baf4d9SChristophe Lombard if (ctx->master || !ctx->afu->pp_psa) { 59114baf4d9SChristophe Lombard ctx->psn_phys = ctx->afu->psn_phys; 59214baf4d9SChristophe Lombard ctx->psn_size = ctx->afu->adapter->ps_size; 59314baf4d9SChristophe Lombard } else { 59414baf4d9SChristophe Lombard ctx->psn_phys = mmio_addr; 59514baf4d9SChristophe Lombard ctx->psn_size = mmio_size; 59614baf4d9SChristophe Lombard } 59714baf4d9SChristophe Lombard if (ctx->afu->pp_psa && mmio_size && 59814baf4d9SChristophe Lombard ctx->afu->pp_size == 0) { 59914baf4d9SChristophe Lombard /* 60014baf4d9SChristophe Lombard * There's no property in the device tree to read the 60114baf4d9SChristophe Lombard * pp_size. We only find out at the 1st attach. 60214baf4d9SChristophe Lombard * Compared to bare-metal, it is too late and we 60314baf4d9SChristophe Lombard * should really lock here. However, on powerVM, 60414baf4d9SChristophe Lombard * pp_size is really only used to display in /sys. 60514baf4d9SChristophe Lombard * Being discussed with pHyp for their next release. 60614baf4d9SChristophe Lombard */ 60714baf4d9SChristophe Lombard ctx->afu->pp_size = mmio_size; 60814baf4d9SChristophe Lombard } 60914baf4d9SChristophe Lombard /* from PAPR: process element is bytes 4-7 of process token */ 61014baf4d9SChristophe Lombard ctx->external_pe = ctx->process_token & 0xFFFFFFFF; 61114baf4d9SChristophe Lombard pr_devel("CXL pe=%i is known as %i for pHyp, mmio_size=%#llx", 61214baf4d9SChristophe Lombard ctx->pe, ctx->external_pe, ctx->psn_size); 61314baf4d9SChristophe Lombard ctx->pe_inserted = true; 61414baf4d9SChristophe Lombard enable_afu_irqs(ctx); 61514baf4d9SChristophe Lombard } 61614baf4d9SChristophe Lombard 6173c206fa7SIan Munsie out_free: 61814baf4d9SChristophe Lombard free_page((u64)elem); 61914baf4d9SChristophe Lombard return rc; 62014baf4d9SChristophe Lombard } 62114baf4d9SChristophe Lombard 62214baf4d9SChristophe Lombard static int guest_attach_process(struct cxl_context *ctx, bool kernel, u64 wed, u64 amr) 62314baf4d9SChristophe Lombard { 62414baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 62514baf4d9SChristophe Lombard 6267a0d85d3SIan Munsie if (ctx->real_mode) 6277a0d85d3SIan Munsie return -EPERM; 6287a0d85d3SIan Munsie 62914baf4d9SChristophe Lombard ctx->kernel = kernel; 63014baf4d9SChristophe Lombard if (ctx->afu->current_mode == CXL_MODE_DIRECTED) 63114baf4d9SChristophe Lombard return attach_afu_directed(ctx, wed, amr); 63214baf4d9SChristophe Lombard 63314baf4d9SChristophe Lombard /* dedicated mode not supported on FW840 */ 63414baf4d9SChristophe Lombard 63514baf4d9SChristophe Lombard return -EINVAL; 63614baf4d9SChristophe Lombard } 63714baf4d9SChristophe Lombard 63814baf4d9SChristophe Lombard static int detach_afu_directed(struct cxl_context *ctx) 63914baf4d9SChristophe Lombard { 64014baf4d9SChristophe Lombard if (!ctx->pe_inserted) 64114baf4d9SChristophe Lombard return 0; 64214baf4d9SChristophe Lombard if (cxl_h_detach_process(ctx->afu->guest->handle, ctx->process_token)) 64314baf4d9SChristophe Lombard return -1; 64414baf4d9SChristophe Lombard return 0; 64514baf4d9SChristophe Lombard } 64614baf4d9SChristophe Lombard 64714baf4d9SChristophe Lombard static int guest_detach_process(struct cxl_context *ctx) 64814baf4d9SChristophe Lombard { 64914baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 65014baf4d9SChristophe Lombard trace_cxl_detach(ctx); 65114baf4d9SChristophe Lombard 6520d400f77SChristophe Lombard if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) 65314baf4d9SChristophe Lombard return -EIO; 65414baf4d9SChristophe Lombard 65514baf4d9SChristophe Lombard if (ctx->afu->current_mode == CXL_MODE_DIRECTED) 65614baf4d9SChristophe Lombard return detach_afu_directed(ctx); 65714baf4d9SChristophe Lombard 65814baf4d9SChristophe Lombard return -EINVAL; 65914baf4d9SChristophe Lombard } 66014baf4d9SChristophe Lombard 66114baf4d9SChristophe Lombard static void guest_release_afu(struct device *dev) 66214baf4d9SChristophe Lombard { 66314baf4d9SChristophe Lombard struct cxl_afu *afu = to_cxl_afu(dev); 66414baf4d9SChristophe Lombard 66514baf4d9SChristophe Lombard pr_devel("%s\n", __func__); 66614baf4d9SChristophe Lombard 66714baf4d9SChristophe Lombard idr_destroy(&afu->contexts_idr); 66814baf4d9SChristophe Lombard 66914baf4d9SChristophe Lombard kfree(afu->guest); 67014baf4d9SChristophe Lombard kfree(afu); 67114baf4d9SChristophe Lombard } 67214baf4d9SChristophe Lombard 67314baf4d9SChristophe Lombard ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len) 67414baf4d9SChristophe Lombard { 67514baf4d9SChristophe Lombard return guest_collect_vpd(NULL, afu, buf, len); 67614baf4d9SChristophe Lombard } 67714baf4d9SChristophe Lombard 67814baf4d9SChristophe Lombard #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE 67914baf4d9SChristophe Lombard static ssize_t guest_afu_read_err_buffer(struct cxl_afu *afu, char *buf, 68014baf4d9SChristophe Lombard loff_t off, size_t count) 68114baf4d9SChristophe Lombard { 68214baf4d9SChristophe Lombard void *tbuf = NULL; 68314baf4d9SChristophe Lombard int rc = 0; 68414baf4d9SChristophe Lombard 68514baf4d9SChristophe Lombard tbuf = (void *) get_zeroed_page(GFP_KERNEL); 68614baf4d9SChristophe Lombard if (!tbuf) 68714baf4d9SChristophe Lombard return -ENOMEM; 68814baf4d9SChristophe Lombard 68914baf4d9SChristophe Lombard rc = cxl_h_get_afu_err(afu->guest->handle, 69014baf4d9SChristophe Lombard off & 0x7, 69114baf4d9SChristophe Lombard virt_to_phys(tbuf), 69214baf4d9SChristophe Lombard count); 69314baf4d9SChristophe Lombard if (rc) 69414baf4d9SChristophe Lombard goto err; 69514baf4d9SChristophe Lombard 69614baf4d9SChristophe Lombard if (count > ERR_BUFF_MAX_COPY_SIZE) 69714baf4d9SChristophe Lombard count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7); 69814baf4d9SChristophe Lombard memcpy(buf, tbuf, count); 69914baf4d9SChristophe Lombard err: 70014baf4d9SChristophe Lombard free_page((u64)tbuf); 70114baf4d9SChristophe Lombard 70214baf4d9SChristophe Lombard return rc; 70314baf4d9SChristophe Lombard } 70414baf4d9SChristophe Lombard 70514baf4d9SChristophe Lombard static int guest_afu_check_and_enable(struct cxl_afu *afu) 70614baf4d9SChristophe Lombard { 70714baf4d9SChristophe Lombard return 0; 70814baf4d9SChristophe Lombard } 70914baf4d9SChristophe Lombard 7104752876cSChristophe Lombard static bool guest_support_attributes(const char *attr_name, 7114752876cSChristophe Lombard enum cxl_attrs type) 7124752876cSChristophe Lombard { 7134752876cSChristophe Lombard switch (type) { 7144752876cSChristophe Lombard case CXL_ADAPTER_ATTRS: 7154752876cSChristophe Lombard if ((strcmp(attr_name, "base_image") == 0) || 7164752876cSChristophe Lombard (strcmp(attr_name, "load_image_on_perst") == 0) || 7174752876cSChristophe Lombard (strcmp(attr_name, "perst_reloads_same_image") == 0) || 7184752876cSChristophe Lombard (strcmp(attr_name, "image_loaded") == 0)) 7194752876cSChristophe Lombard return false; 7204752876cSChristophe Lombard break; 7214752876cSChristophe Lombard case CXL_AFU_MASTER_ATTRS: 7224752876cSChristophe Lombard if ((strcmp(attr_name, "pp_mmio_off") == 0)) 7234752876cSChristophe Lombard return false; 7244752876cSChristophe Lombard break; 7254752876cSChristophe Lombard case CXL_AFU_ATTRS: 7264752876cSChristophe Lombard break; 7274752876cSChristophe Lombard default: 7284752876cSChristophe Lombard break; 7294752876cSChristophe Lombard } 7304752876cSChristophe Lombard 7314752876cSChristophe Lombard return true; 7324752876cSChristophe Lombard } 7334752876cSChristophe Lombard 73414baf4d9SChristophe Lombard static int activate_afu_directed(struct cxl_afu *afu) 73514baf4d9SChristophe Lombard { 73614baf4d9SChristophe Lombard int rc; 73714baf4d9SChristophe Lombard 73814baf4d9SChristophe Lombard dev_info(&afu->dev, "Activating AFU(%d) directed mode\n", afu->slice); 73914baf4d9SChristophe Lombard 74014baf4d9SChristophe Lombard afu->current_mode = CXL_MODE_DIRECTED; 74114baf4d9SChristophe Lombard 74214baf4d9SChristophe Lombard afu->num_procs = afu->max_procs_virtualised; 74314baf4d9SChristophe Lombard 74414baf4d9SChristophe Lombard if ((rc = cxl_chardev_m_afu_add(afu))) 74514baf4d9SChristophe Lombard return rc; 74614baf4d9SChristophe Lombard 74714baf4d9SChristophe Lombard if ((rc = cxl_sysfs_afu_m_add(afu))) 74814baf4d9SChristophe Lombard goto err; 74914baf4d9SChristophe Lombard 75014baf4d9SChristophe Lombard if ((rc = cxl_chardev_s_afu_add(afu))) 75114baf4d9SChristophe Lombard goto err1; 75214baf4d9SChristophe Lombard 75314baf4d9SChristophe Lombard return 0; 75414baf4d9SChristophe Lombard err1: 75514baf4d9SChristophe Lombard cxl_sysfs_afu_m_remove(afu); 75614baf4d9SChristophe Lombard err: 75714baf4d9SChristophe Lombard cxl_chardev_afu_remove(afu); 75814baf4d9SChristophe Lombard return rc; 75914baf4d9SChristophe Lombard } 76014baf4d9SChristophe Lombard 76114baf4d9SChristophe Lombard static int guest_afu_activate_mode(struct cxl_afu *afu, int mode) 76214baf4d9SChristophe Lombard { 76314baf4d9SChristophe Lombard if (!mode) 76414baf4d9SChristophe Lombard return 0; 76514baf4d9SChristophe Lombard if (!(mode & afu->modes_supported)) 76614baf4d9SChristophe Lombard return -EINVAL; 76714baf4d9SChristophe Lombard 76814baf4d9SChristophe Lombard if (mode == CXL_MODE_DIRECTED) 76914baf4d9SChristophe Lombard return activate_afu_directed(afu); 77014baf4d9SChristophe Lombard 77114baf4d9SChristophe Lombard if (mode == CXL_MODE_DEDICATED) 77214baf4d9SChristophe Lombard dev_err(&afu->dev, "Dedicated mode not supported\n"); 77314baf4d9SChristophe Lombard 77414baf4d9SChristophe Lombard return -EINVAL; 77514baf4d9SChristophe Lombard } 77614baf4d9SChristophe Lombard 77714baf4d9SChristophe Lombard static int deactivate_afu_directed(struct cxl_afu *afu) 77814baf4d9SChristophe Lombard { 77914baf4d9SChristophe Lombard dev_info(&afu->dev, "Deactivating AFU(%d) directed mode\n", afu->slice); 78014baf4d9SChristophe Lombard 78114baf4d9SChristophe Lombard afu->current_mode = 0; 78214baf4d9SChristophe Lombard afu->num_procs = 0; 78314baf4d9SChristophe Lombard 78414baf4d9SChristophe Lombard cxl_sysfs_afu_m_remove(afu); 78514baf4d9SChristophe Lombard cxl_chardev_afu_remove(afu); 78614baf4d9SChristophe Lombard 78714baf4d9SChristophe Lombard cxl_ops->afu_reset(afu); 78814baf4d9SChristophe Lombard 78914baf4d9SChristophe Lombard return 0; 79014baf4d9SChristophe Lombard } 79114baf4d9SChristophe Lombard 79214baf4d9SChristophe Lombard static int guest_afu_deactivate_mode(struct cxl_afu *afu, int mode) 79314baf4d9SChristophe Lombard { 79414baf4d9SChristophe Lombard if (!mode) 79514baf4d9SChristophe Lombard return 0; 79614baf4d9SChristophe Lombard if (!(mode & afu->modes_supported)) 79714baf4d9SChristophe Lombard return -EINVAL; 79814baf4d9SChristophe Lombard 79914baf4d9SChristophe Lombard if (mode == CXL_MODE_DIRECTED) 80014baf4d9SChristophe Lombard return deactivate_afu_directed(afu); 80114baf4d9SChristophe Lombard return 0; 80214baf4d9SChristophe Lombard } 80314baf4d9SChristophe Lombard 80414baf4d9SChristophe Lombard static int guest_afu_reset(struct cxl_afu *afu) 80514baf4d9SChristophe Lombard { 80614baf4d9SChristophe Lombard pr_devel("AFU(%d) reset request\n", afu->slice); 80714baf4d9SChristophe Lombard return cxl_h_reset_afu(afu->guest->handle); 80814baf4d9SChristophe Lombard } 80914baf4d9SChristophe Lombard 81014baf4d9SChristophe Lombard static int guest_map_slice_regs(struct cxl_afu *afu) 81114baf4d9SChristophe Lombard { 81214baf4d9SChristophe Lombard if (!(afu->p2n_mmio = ioremap(afu->guest->p2n_phys, afu->guest->p2n_size))) { 81314baf4d9SChristophe Lombard dev_err(&afu->dev, "Error mapping AFU(%d) MMIO regions\n", 81414baf4d9SChristophe Lombard afu->slice); 81514baf4d9SChristophe Lombard return -ENOMEM; 81614baf4d9SChristophe Lombard } 81714baf4d9SChristophe Lombard return 0; 81814baf4d9SChristophe Lombard } 81914baf4d9SChristophe Lombard 82014baf4d9SChristophe Lombard static void guest_unmap_slice_regs(struct cxl_afu *afu) 82114baf4d9SChristophe Lombard { 82214baf4d9SChristophe Lombard if (afu->p2n_mmio) 82314baf4d9SChristophe Lombard iounmap(afu->p2n_mmio); 82414baf4d9SChristophe Lombard } 82514baf4d9SChristophe Lombard 8260d400f77SChristophe Lombard static int afu_update_state(struct cxl_afu *afu) 82714baf4d9SChristophe Lombard { 8280d400f77SChristophe Lombard int rc, cur_state; 8290d400f77SChristophe Lombard 8300d400f77SChristophe Lombard rc = afu_read_error_state(afu, &cur_state); 8310d400f77SChristophe Lombard if (rc) 8320d400f77SChristophe Lombard return rc; 8330d400f77SChristophe Lombard 8340d400f77SChristophe Lombard if (afu->guest->previous_state == cur_state) 8350d400f77SChristophe Lombard return 0; 8360d400f77SChristophe Lombard 8370d400f77SChristophe Lombard pr_devel("AFU(%d) update state to %#x\n", afu->slice, cur_state); 8380d400f77SChristophe Lombard 8390d400f77SChristophe Lombard switch (cur_state) { 8400d400f77SChristophe Lombard case H_STATE_NORMAL: 8410d400f77SChristophe Lombard afu->guest->previous_state = cur_state; 8420d400f77SChristophe Lombard break; 8430d400f77SChristophe Lombard 8440d400f77SChristophe Lombard case H_STATE_DISABLE: 8450d400f77SChristophe Lombard pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT, 8460d400f77SChristophe Lombard pci_channel_io_frozen); 8470d400f77SChristophe Lombard 8480d400f77SChristophe Lombard cxl_context_detach_all(afu); 8490d400f77SChristophe Lombard if ((rc = cxl_ops->afu_reset(afu))) 8500d400f77SChristophe Lombard pr_devel("reset hcall failed %d\n", rc); 8510d400f77SChristophe Lombard 8520d400f77SChristophe Lombard rc = afu_read_error_state(afu, &cur_state); 8530d400f77SChristophe Lombard if (!rc && cur_state == H_STATE_NORMAL) { 8540d400f77SChristophe Lombard pci_error_handlers(afu, CXL_SLOT_RESET_EVENT, 8550d400f77SChristophe Lombard pci_channel_io_normal); 8560d400f77SChristophe Lombard pci_error_handlers(afu, CXL_RESUME_EVENT, 0); 8570d400f77SChristophe Lombard } 8580d400f77SChristophe Lombard afu->guest->previous_state = 0; 8590d400f77SChristophe Lombard break; 8600d400f77SChristophe Lombard 8610d400f77SChristophe Lombard case H_STATE_TEMP_UNAVAILABLE: 8620d400f77SChristophe Lombard afu->guest->previous_state = cur_state; 8630d400f77SChristophe Lombard break; 8640d400f77SChristophe Lombard 8650d400f77SChristophe Lombard case H_STATE_PERM_UNAVAILABLE: 8660d400f77SChristophe Lombard dev_err(&afu->dev, "AFU is in permanent error state\n"); 8670d400f77SChristophe Lombard pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT, 8680d400f77SChristophe Lombard pci_channel_io_perm_failure); 8690d400f77SChristophe Lombard afu->guest->previous_state = cur_state; 8700d400f77SChristophe Lombard break; 8710d400f77SChristophe Lombard 8720d400f77SChristophe Lombard default: 8730d400f77SChristophe Lombard pr_err("Unexpected AFU(%d) error state: %#x\n", 8740d400f77SChristophe Lombard afu->slice, cur_state); 8750d400f77SChristophe Lombard return -EINVAL; 8760d400f77SChristophe Lombard } 8770d400f77SChristophe Lombard 8780d400f77SChristophe Lombard return rc; 8790d400f77SChristophe Lombard } 8800d400f77SChristophe Lombard 881266eab8fSChristophe Lombard static void afu_handle_errstate(struct work_struct *work) 8820d400f77SChristophe Lombard { 883266eab8fSChristophe Lombard struct cxl_afu_guest *afu_guest = 884266eab8fSChristophe Lombard container_of(to_delayed_work(work), struct cxl_afu_guest, work_err); 8850d400f77SChristophe Lombard 886266eab8fSChristophe Lombard if (!afu_update_state(afu_guest->parent) && 887266eab8fSChristophe Lombard afu_guest->previous_state == H_STATE_PERM_UNAVAILABLE) 888266eab8fSChristophe Lombard return; 889266eab8fSChristophe Lombard 8903382a622SAndrew Donnellan if (afu_guest->handle_err) 891266eab8fSChristophe Lombard schedule_delayed_work(&afu_guest->work_err, 892266eab8fSChristophe Lombard msecs_to_jiffies(3000)); 8930d400f77SChristophe Lombard } 8940d400f77SChristophe Lombard 8950d400f77SChristophe Lombard static bool guest_link_ok(struct cxl *cxl, struct cxl_afu *afu) 8960d400f77SChristophe Lombard { 8970d400f77SChristophe Lombard int state; 8980d400f77SChristophe Lombard 899266eab8fSChristophe Lombard if (afu && (!afu_read_error_state(afu, &state))) { 900266eab8fSChristophe Lombard if (state == H_STATE_NORMAL) 9010d400f77SChristophe Lombard return true; 9020d400f77SChristophe Lombard } 9030d400f77SChristophe Lombard 904266eab8fSChristophe Lombard return false; 90514baf4d9SChristophe Lombard } 90614baf4d9SChristophe Lombard 90714baf4d9SChristophe Lombard static int afu_properties_look_ok(struct cxl_afu *afu) 90814baf4d9SChristophe Lombard { 90914baf4d9SChristophe Lombard if (afu->pp_irqs < 0) { 91014baf4d9SChristophe Lombard dev_err(&afu->dev, "Unexpected per-process minimum interrupt value\n"); 91114baf4d9SChristophe Lombard return -EINVAL; 91214baf4d9SChristophe Lombard } 91314baf4d9SChristophe Lombard 91414baf4d9SChristophe Lombard if (afu->max_procs_virtualised < 1) { 91514baf4d9SChristophe Lombard dev_err(&afu->dev, "Unexpected max number of processes virtualised value\n"); 91614baf4d9SChristophe Lombard return -EINVAL; 91714baf4d9SChristophe Lombard } 91814baf4d9SChristophe Lombard 91914baf4d9SChristophe Lombard if (afu->crs_len < 0) { 92014baf4d9SChristophe Lombard dev_err(&afu->dev, "Unexpected configuration record size value\n"); 92114baf4d9SChristophe Lombard return -EINVAL; 92214baf4d9SChristophe Lombard } 92314baf4d9SChristophe Lombard 92414baf4d9SChristophe Lombard return 0; 92514baf4d9SChristophe Lombard } 92614baf4d9SChristophe Lombard 92714baf4d9SChristophe Lombard int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np) 92814baf4d9SChristophe Lombard { 92914baf4d9SChristophe Lombard struct cxl_afu *afu; 93014baf4d9SChristophe Lombard bool free = true; 93114baf4d9SChristophe Lombard int rc; 93214baf4d9SChristophe Lombard 93314baf4d9SChristophe Lombard pr_devel("in %s - AFU(%d)\n", __func__, slice); 93414baf4d9SChristophe Lombard if (!(afu = cxl_alloc_afu(adapter, slice))) 93514baf4d9SChristophe Lombard return -ENOMEM; 93614baf4d9SChristophe Lombard 93714baf4d9SChristophe Lombard if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest), GFP_KERNEL))) { 93814baf4d9SChristophe Lombard kfree(afu); 93914baf4d9SChristophe Lombard return -ENOMEM; 94014baf4d9SChristophe Lombard } 94114baf4d9SChristophe Lombard 94214baf4d9SChristophe Lombard if ((rc = dev_set_name(&afu->dev, "afu%i.%i", 94314baf4d9SChristophe Lombard adapter->adapter_num, 94414baf4d9SChristophe Lombard slice))) 94514baf4d9SChristophe Lombard goto err1; 94614baf4d9SChristophe Lombard 94714baf4d9SChristophe Lombard adapter->slices++; 94814baf4d9SChristophe Lombard 94914baf4d9SChristophe Lombard if ((rc = cxl_of_read_afu_handle(afu, afu_np))) 95014baf4d9SChristophe Lombard goto err1; 95114baf4d9SChristophe Lombard 95214baf4d9SChristophe Lombard if ((rc = cxl_ops->afu_reset(afu))) 95314baf4d9SChristophe Lombard goto err1; 95414baf4d9SChristophe Lombard 95514baf4d9SChristophe Lombard if ((rc = cxl_of_read_afu_properties(afu, afu_np))) 95614baf4d9SChristophe Lombard goto err1; 95714baf4d9SChristophe Lombard 95814baf4d9SChristophe Lombard if ((rc = afu_properties_look_ok(afu))) 95914baf4d9SChristophe Lombard goto err1; 96014baf4d9SChristophe Lombard 96114baf4d9SChristophe Lombard if ((rc = guest_map_slice_regs(afu))) 96214baf4d9SChristophe Lombard goto err1; 96314baf4d9SChristophe Lombard 96414baf4d9SChristophe Lombard if ((rc = guest_register_serr_irq(afu))) 96514baf4d9SChristophe Lombard goto err2; 96614baf4d9SChristophe Lombard 96714baf4d9SChristophe Lombard /* 96814baf4d9SChristophe Lombard * After we call this function we must not free the afu directly, even 96914baf4d9SChristophe Lombard * if it returns an error! 97014baf4d9SChristophe Lombard */ 97114baf4d9SChristophe Lombard if ((rc = cxl_register_afu(afu))) 97214baf4d9SChristophe Lombard goto err_put1; 97314baf4d9SChristophe Lombard 97414baf4d9SChristophe Lombard if ((rc = cxl_sysfs_afu_add(afu))) 97514baf4d9SChristophe Lombard goto err_put1; 97614baf4d9SChristophe Lombard 97714baf4d9SChristophe Lombard /* 97814baf4d9SChristophe Lombard * pHyp doesn't expose the programming models supported by the 97914baf4d9SChristophe Lombard * AFU. pHyp currently only supports directed mode. If it adds 98014baf4d9SChristophe Lombard * dedicated mode later, this version of cxl has no way to 98114baf4d9SChristophe Lombard * detect it. So we'll initialize the driver, but the first 98214baf4d9SChristophe Lombard * attach will fail. 98314baf4d9SChristophe Lombard * Being discussed with pHyp to do better (likely new property) 98414baf4d9SChristophe Lombard */ 98514baf4d9SChristophe Lombard if (afu->max_procs_virtualised == 1) 98614baf4d9SChristophe Lombard afu->modes_supported = CXL_MODE_DEDICATED; 98714baf4d9SChristophe Lombard else 98814baf4d9SChristophe Lombard afu->modes_supported = CXL_MODE_DIRECTED; 98914baf4d9SChristophe Lombard 99014baf4d9SChristophe Lombard if ((rc = cxl_afu_select_best_mode(afu))) 99114baf4d9SChristophe Lombard goto err_put2; 99214baf4d9SChristophe Lombard 99314baf4d9SChristophe Lombard adapter->afu[afu->slice] = afu; 99414baf4d9SChristophe Lombard 99514baf4d9SChristophe Lombard afu->enabled = true; 99614baf4d9SChristophe Lombard 997266eab8fSChristophe Lombard /* 998266eab8fSChristophe Lombard * wake up the cpu periodically to check the state 999266eab8fSChristophe Lombard * of the AFU using "afu" stored in the guest structure. 1000266eab8fSChristophe Lombard */ 1001266eab8fSChristophe Lombard afu->guest->parent = afu; 1002266eab8fSChristophe Lombard afu->guest->handle_err = true; 1003266eab8fSChristophe Lombard INIT_DELAYED_WORK(&afu->guest->work_err, afu_handle_errstate); 1004266eab8fSChristophe Lombard schedule_delayed_work(&afu->guest->work_err, msecs_to_jiffies(1000)); 1005266eab8fSChristophe Lombard 1006d601ea91SFrederic Barrat if ((rc = cxl_pci_vphb_add(afu))) 1007d601ea91SFrederic Barrat dev_info(&afu->dev, "Can't register vPHB\n"); 1008d601ea91SFrederic Barrat 100914baf4d9SChristophe Lombard return 0; 101014baf4d9SChristophe Lombard 101114baf4d9SChristophe Lombard err_put2: 101214baf4d9SChristophe Lombard cxl_sysfs_afu_remove(afu); 101314baf4d9SChristophe Lombard err_put1: 101414baf4d9SChristophe Lombard device_unregister(&afu->dev); 101514baf4d9SChristophe Lombard free = false; 101614baf4d9SChristophe Lombard guest_release_serr_irq(afu); 101714baf4d9SChristophe Lombard err2: 101814baf4d9SChristophe Lombard guest_unmap_slice_regs(afu); 101914baf4d9SChristophe Lombard err1: 102014baf4d9SChristophe Lombard if (free) { 102114baf4d9SChristophe Lombard kfree(afu->guest); 102214baf4d9SChristophe Lombard kfree(afu); 102314baf4d9SChristophe Lombard } 102414baf4d9SChristophe Lombard return rc; 102514baf4d9SChristophe Lombard } 102614baf4d9SChristophe Lombard 102714baf4d9SChristophe Lombard void cxl_guest_remove_afu(struct cxl_afu *afu) 102814baf4d9SChristophe Lombard { 102914baf4d9SChristophe Lombard pr_devel("in %s - AFU(%d)\n", __func__, afu->slice); 103014baf4d9SChristophe Lombard 103114baf4d9SChristophe Lombard if (!afu) 103214baf4d9SChristophe Lombard return; 103314baf4d9SChristophe Lombard 1034266eab8fSChristophe Lombard /* flush and stop pending job */ 1035266eab8fSChristophe Lombard afu->guest->handle_err = false; 1036266eab8fSChristophe Lombard flush_delayed_work(&afu->guest->work_err); 1037266eab8fSChristophe Lombard 1038d601ea91SFrederic Barrat cxl_pci_vphb_remove(afu); 103914baf4d9SChristophe Lombard cxl_sysfs_afu_remove(afu); 104014baf4d9SChristophe Lombard 104114baf4d9SChristophe Lombard spin_lock(&afu->adapter->afu_list_lock); 104214baf4d9SChristophe Lombard afu->adapter->afu[afu->slice] = NULL; 104314baf4d9SChristophe Lombard spin_unlock(&afu->adapter->afu_list_lock); 104414baf4d9SChristophe Lombard 104514baf4d9SChristophe Lombard cxl_context_detach_all(afu); 104614baf4d9SChristophe Lombard cxl_ops->afu_deactivate_mode(afu, afu->current_mode); 104714baf4d9SChristophe Lombard guest_release_serr_irq(afu); 104814baf4d9SChristophe Lombard guest_unmap_slice_regs(afu); 104914baf4d9SChristophe Lombard 105014baf4d9SChristophe Lombard device_unregister(&afu->dev); 105114baf4d9SChristophe Lombard } 105214baf4d9SChristophe Lombard 105314baf4d9SChristophe Lombard static void free_adapter(struct cxl *adapter) 105414baf4d9SChristophe Lombard { 105514baf4d9SChristophe Lombard struct irq_avail *cur; 105614baf4d9SChristophe Lombard int i; 105714baf4d9SChristophe Lombard 10588fbaa51dSAndrew Donnellan if (adapter->guest) { 105914baf4d9SChristophe Lombard if (adapter->guest->irq_avail) { 106014baf4d9SChristophe Lombard for (i = 0; i < adapter->guest->irq_nranges; i++) { 106114baf4d9SChristophe Lombard cur = &adapter->guest->irq_avail[i]; 106214baf4d9SChristophe Lombard kfree(cur->bitmap); 106314baf4d9SChristophe Lombard } 106414baf4d9SChristophe Lombard kfree(adapter->guest->irq_avail); 106514baf4d9SChristophe Lombard } 106614baf4d9SChristophe Lombard kfree(adapter->guest->status); 106714baf4d9SChristophe Lombard kfree(adapter->guest); 10688fbaa51dSAndrew Donnellan } 10698fbaa51dSAndrew Donnellan cxl_remove_adapter_nr(adapter); 107014baf4d9SChristophe Lombard kfree(adapter); 107114baf4d9SChristophe Lombard } 107214baf4d9SChristophe Lombard 107314baf4d9SChristophe Lombard static int properties_look_ok(struct cxl *adapter) 107414baf4d9SChristophe Lombard { 107514baf4d9SChristophe Lombard /* The absence of this property means that the operational 107614baf4d9SChristophe Lombard * status is unknown or okay 107714baf4d9SChristophe Lombard */ 107814baf4d9SChristophe Lombard if (strlen(adapter->guest->status) && 107914baf4d9SChristophe Lombard strcmp(adapter->guest->status, "okay")) { 108014baf4d9SChristophe Lombard pr_err("ABORTING:Bad operational status of the device\n"); 108114baf4d9SChristophe Lombard return -EINVAL; 108214baf4d9SChristophe Lombard } 108314baf4d9SChristophe Lombard 108414baf4d9SChristophe Lombard return 0; 108514baf4d9SChristophe Lombard } 108614baf4d9SChristophe Lombard 108714baf4d9SChristophe Lombard ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len) 108814baf4d9SChristophe Lombard { 108914baf4d9SChristophe Lombard return guest_collect_vpd(adapter, NULL, buf, len); 109014baf4d9SChristophe Lombard } 109114baf4d9SChristophe Lombard 109214baf4d9SChristophe Lombard void cxl_guest_remove_adapter(struct cxl *adapter) 109314baf4d9SChristophe Lombard { 109414baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 109514baf4d9SChristophe Lombard 109614baf4d9SChristophe Lombard cxl_sysfs_adapter_remove(adapter); 109714baf4d9SChristophe Lombard 1098594ff7d0SChristophe Lombard cxl_guest_remove_chardev(adapter); 109914baf4d9SChristophe Lombard device_unregister(&adapter->dev); 110014baf4d9SChristophe Lombard } 110114baf4d9SChristophe Lombard 110214baf4d9SChristophe Lombard static void release_adapter(struct device *dev) 110314baf4d9SChristophe Lombard { 110414baf4d9SChristophe Lombard free_adapter(to_cxl_adapter(dev)); 110514baf4d9SChristophe Lombard } 110614baf4d9SChristophe Lombard 110714baf4d9SChristophe Lombard struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *pdev) 110814baf4d9SChristophe Lombard { 110914baf4d9SChristophe Lombard struct cxl *adapter; 111014baf4d9SChristophe Lombard bool free = true; 111114baf4d9SChristophe Lombard int rc; 111214baf4d9SChristophe Lombard 111314baf4d9SChristophe Lombard if (!(adapter = cxl_alloc_adapter())) 111414baf4d9SChristophe Lombard return ERR_PTR(-ENOMEM); 111514baf4d9SChristophe Lombard 111614baf4d9SChristophe Lombard if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest), GFP_KERNEL))) { 111714baf4d9SChristophe Lombard free_adapter(adapter); 111814baf4d9SChristophe Lombard return ERR_PTR(-ENOMEM); 111914baf4d9SChristophe Lombard } 112014baf4d9SChristophe Lombard 112114baf4d9SChristophe Lombard adapter->slices = 0; 112214baf4d9SChristophe Lombard adapter->guest->pdev = pdev; 112314baf4d9SChristophe Lombard adapter->dev.parent = &pdev->dev; 112414baf4d9SChristophe Lombard adapter->dev.release = release_adapter; 112514baf4d9SChristophe Lombard dev_set_drvdata(&pdev->dev, adapter); 112614baf4d9SChristophe Lombard 1127e009a7e8SFrederic Barrat /* 1128e009a7e8SFrederic Barrat * Hypervisor controls PSL timebase initialization (p1 register). 1129e009a7e8SFrederic Barrat * On FW840, PSL is initialized. 1130e009a7e8SFrederic Barrat */ 1131e009a7e8SFrederic Barrat adapter->psl_timebase_synced = true; 1132e009a7e8SFrederic Barrat 113314baf4d9SChristophe Lombard if ((rc = cxl_of_read_adapter_handle(adapter, np))) 113414baf4d9SChristophe Lombard goto err1; 113514baf4d9SChristophe Lombard 113614baf4d9SChristophe Lombard if ((rc = cxl_of_read_adapter_properties(adapter, np))) 113714baf4d9SChristophe Lombard goto err1; 113814baf4d9SChristophe Lombard 113914baf4d9SChristophe Lombard if ((rc = properties_look_ok(adapter))) 114014baf4d9SChristophe Lombard goto err1; 114114baf4d9SChristophe Lombard 1142594ff7d0SChristophe Lombard if ((rc = cxl_guest_add_chardev(adapter))) 1143594ff7d0SChristophe Lombard goto err1; 1144594ff7d0SChristophe Lombard 114514baf4d9SChristophe Lombard /* 114614baf4d9SChristophe Lombard * After we call this function we must not free the adapter directly, 114714baf4d9SChristophe Lombard * even if it returns an error! 114814baf4d9SChristophe Lombard */ 114914baf4d9SChristophe Lombard if ((rc = cxl_register_adapter(adapter))) 115014baf4d9SChristophe Lombard goto err_put1; 115114baf4d9SChristophe Lombard 115214baf4d9SChristophe Lombard if ((rc = cxl_sysfs_adapter_add(adapter))) 115314baf4d9SChristophe Lombard goto err_put1; 115414baf4d9SChristophe Lombard 115570b565bbSVaibhav Jain /* release the context lock as the adapter is configured */ 115670b565bbSVaibhav Jain cxl_adapter_context_unlock(adapter); 115770b565bbSVaibhav Jain 115814baf4d9SChristophe Lombard return adapter; 115914baf4d9SChristophe Lombard 116014baf4d9SChristophe Lombard err_put1: 116114baf4d9SChristophe Lombard device_unregister(&adapter->dev); 116214baf4d9SChristophe Lombard free = false; 1163594ff7d0SChristophe Lombard cxl_guest_remove_chardev(adapter); 116414baf4d9SChristophe Lombard err1: 116514baf4d9SChristophe Lombard if (free) 116614baf4d9SChristophe Lombard free_adapter(adapter); 116714baf4d9SChristophe Lombard return ERR_PTR(rc); 116814baf4d9SChristophe Lombard } 116914baf4d9SChristophe Lombard 1170594ff7d0SChristophe Lombard void cxl_guest_reload_module(struct cxl *adapter) 1171594ff7d0SChristophe Lombard { 1172594ff7d0SChristophe Lombard struct platform_device *pdev; 1173594ff7d0SChristophe Lombard 1174594ff7d0SChristophe Lombard pdev = adapter->guest->pdev; 1175594ff7d0SChristophe Lombard cxl_guest_remove_adapter(adapter); 1176594ff7d0SChristophe Lombard 1177594ff7d0SChristophe Lombard cxl_of_probe(pdev); 1178594ff7d0SChristophe Lombard } 1179594ff7d0SChristophe Lombard 118014baf4d9SChristophe Lombard const struct cxl_backend_ops cxl_guest_ops = { 118114baf4d9SChristophe Lombard .module = THIS_MODULE, 118214baf4d9SChristophe Lombard .adapter_reset = guest_reset, 118314baf4d9SChristophe Lombard .alloc_one_irq = guest_alloc_one_irq, 118414baf4d9SChristophe Lombard .release_one_irq = guest_release_one_irq, 118514baf4d9SChristophe Lombard .alloc_irq_ranges = guest_alloc_irq_ranges, 118614baf4d9SChristophe Lombard .release_irq_ranges = guest_release_irq_ranges, 118714baf4d9SChristophe Lombard .setup_irq = NULL, 118814baf4d9SChristophe Lombard .handle_psl_slice_error = guest_handle_psl_slice_error, 118914baf4d9SChristophe Lombard .psl_interrupt = guest_psl_irq, 119014baf4d9SChristophe Lombard .ack_irq = guest_ack_irq, 119114baf4d9SChristophe Lombard .attach_process = guest_attach_process, 119214baf4d9SChristophe Lombard .detach_process = guest_detach_process, 1193292841b0SIan Munsie .update_ivtes = NULL, 11944752876cSChristophe Lombard .support_attributes = guest_support_attributes, 119514baf4d9SChristophe Lombard .link_ok = guest_link_ok, 119614baf4d9SChristophe Lombard .release_afu = guest_release_afu, 119714baf4d9SChristophe Lombard .afu_read_err_buffer = guest_afu_read_err_buffer, 119814baf4d9SChristophe Lombard .afu_check_and_enable = guest_afu_check_and_enable, 119914baf4d9SChristophe Lombard .afu_activate_mode = guest_afu_activate_mode, 120014baf4d9SChristophe Lombard .afu_deactivate_mode = guest_afu_deactivate_mode, 120114baf4d9SChristophe Lombard .afu_reset = guest_afu_reset, 120214baf4d9SChristophe Lombard .afu_cr_read8 = guest_afu_cr_read8, 120314baf4d9SChristophe Lombard .afu_cr_read16 = guest_afu_cr_read16, 120414baf4d9SChristophe Lombard .afu_cr_read32 = guest_afu_cr_read32, 120514baf4d9SChristophe Lombard .afu_cr_read64 = guest_afu_cr_read64, 1206d601ea91SFrederic Barrat .afu_cr_write8 = guest_afu_cr_write8, 1207d601ea91SFrederic Barrat .afu_cr_write16 = guest_afu_cr_write16, 1208d601ea91SFrederic Barrat .afu_cr_write32 = guest_afu_cr_write32, 1209d601ea91SFrederic Barrat .read_adapter_vpd = cxl_guest_read_adapter_vpd, 121014baf4d9SChristophe Lombard }; 1211