Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2015 IBM Corp.
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/slab.h>
0010 #include <linux/of_address.h>
0011 #include <linux/of_platform.h>
0012 
0013 #include "cxl.h"
0014 
0015 
0016 static const __be32 *read_prop_string(const struct device_node *np,
0017                 const char *prop_name)
0018 {
0019     const __be32 *prop;
0020 
0021     prop = of_get_property(np, prop_name, NULL);
0022     if (cxl_verbose && prop)
0023         pr_info("%s: %s\n", prop_name, (char *) prop);
0024     return prop;
0025 }
0026 
0027 static const __be32 *read_prop_dword(const struct device_node *np,
0028                 const char *prop_name, u32 *val)
0029 {
0030     const __be32 *prop;
0031 
0032     prop = of_get_property(np, prop_name, NULL);
0033     if (prop)
0034         *val = be32_to_cpu(prop[0]);
0035     if (cxl_verbose && prop)
0036         pr_info("%s: %#x (%u)\n", prop_name, *val, *val);
0037     return prop;
0038 }
0039 
0040 static const __be64 *read_prop64_dword(const struct device_node *np,
0041                 const char *prop_name, u64 *val)
0042 {
0043     const __be64 *prop;
0044 
0045     prop = of_get_property(np, prop_name, NULL);
0046     if (prop)
0047         *val = be64_to_cpu(prop[0]);
0048     if (cxl_verbose && prop)
0049         pr_info("%s: %#llx (%llu)\n", prop_name, *val, *val);
0050     return prop;
0051 }
0052 
0053 
0054 static int read_handle(struct device_node *np, u64 *handle)
0055 {
0056     const __be32 *prop;
0057     u64 size;
0058 
0059     /* Get address and size of the node */
0060     prop = of_get_address(np, 0, &size, NULL);
0061     if (size)
0062         return -EINVAL;
0063 
0064     /* Helper to read a big number; size is in cells (not bytes) */
0065     *handle = of_read_number(prop, of_n_addr_cells(np));
0066     return 0;
0067 }
0068 
0069 static int read_phys_addr(struct device_node *np, char *prop_name,
0070             struct cxl_afu *afu)
0071 {
0072     int i, len, entry_size, naddr, nsize, type;
0073     u64 addr, size;
0074     const __be32 *prop;
0075 
0076     naddr = of_n_addr_cells(np);
0077     nsize = of_n_size_cells(np);
0078 
0079     prop = of_get_property(np, prop_name, &len);
0080     if (prop) {
0081         entry_size = naddr + nsize;
0082         for (i = 0; i < (len / 4); i += entry_size, prop += entry_size) {
0083             type = be32_to_cpu(prop[0]);
0084             addr = of_read_number(prop, naddr);
0085             size = of_read_number(&prop[naddr], nsize);
0086             switch (type) {
0087             case 0: /* unit address */
0088                 afu->guest->handle = addr;
0089                 break;
0090             case 1: /* p2 area */
0091                 afu->guest->p2n_phys += addr;
0092                 afu->guest->p2n_size = size;
0093                 break;
0094             case 2: /* problem state area */
0095                 afu->psn_phys += addr;
0096                 afu->adapter->ps_size = size;
0097                 break;
0098             default:
0099                 pr_err("Invalid address type %d found in %s property of AFU\n",
0100                     type, prop_name);
0101                 return -EINVAL;
0102             }
0103             if (cxl_verbose)
0104                 pr_info("%s: %#x %#llx (size %#llx)\n",
0105                     prop_name, type, addr, size);
0106         }
0107     }
0108     return 0;
0109 }
0110 
0111 static int read_vpd(struct cxl *adapter, struct cxl_afu *afu)
0112 {
0113     char vpd[256];
0114     int rc;
0115     size_t len = sizeof(vpd);
0116 
0117     memset(vpd, 0, len);
0118 
0119     if (adapter)
0120         rc = cxl_guest_read_adapter_vpd(adapter, vpd, len);
0121     else
0122         rc = cxl_guest_read_afu_vpd(afu, vpd, len);
0123 
0124     if (rc > 0) {
0125         cxl_dump_debug_buffer(vpd, rc);
0126         rc = 0;
0127     }
0128     return rc;
0129 }
0130 
0131 int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node *afu_np)
0132 {
0133     if (read_handle(afu_np, &afu->guest->handle))
0134         return -EINVAL;
0135     pr_devel("AFU handle: 0x%.16llx\n", afu->guest->handle);
0136 
0137     return 0;
0138 }
0139 
0140 int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *np)
0141 {
0142     int i, len, rc;
0143     char *p;
0144     const __be32 *prop;
0145     u16 device_id, vendor_id;
0146     u32 val = 0, class_code;
0147 
0148     /* Properties are read in the same order as listed in PAPR */
0149 
0150     if (cxl_verbose) {
0151         pr_info("Dump of the 'ibm,coherent-platform-function' node properties:\n");
0152 
0153         prop = of_get_property(np, "compatible", &len);
0154         i = 0;
0155         while (i < len) {
0156             p = (char *) prop + i;
0157             pr_info("compatible: %s\n", p);
0158             i += strlen(p) + 1;
0159         }
0160         read_prop_string(np, "name");
0161     }
0162 
0163     rc = read_phys_addr(np, "reg", afu);
0164     if (rc)
0165         return rc;
0166 
0167     rc = read_phys_addr(np, "assigned-addresses", afu);
0168     if (rc)
0169         return rc;
0170 
0171     if (afu->psn_phys == 0)
0172         afu->psa = false;
0173     else
0174         afu->psa = true;
0175 
0176     if (cxl_verbose) {
0177         read_prop_string(np, "ibm,loc-code");
0178         read_prop_string(np, "device_type");
0179     }
0180 
0181     read_prop_dword(np, "ibm,#processes", &afu->max_procs_virtualised);
0182 
0183     if (cxl_verbose) {
0184         read_prop_dword(np, "ibm,scratchpad-size", &val);
0185         read_prop_dword(np, "ibm,programmable", &val);
0186         read_prop_string(np, "ibm,phandle");
0187         read_vpd(NULL, afu);
0188     }
0189 
0190     read_prop_dword(np, "ibm,max-ints-per-process", &afu->guest->max_ints);
0191     afu->irqs_max = afu->guest->max_ints;
0192 
0193     prop = read_prop_dword(np, "ibm,min-ints-per-process", &afu->pp_irqs);
0194     if (prop) {
0195         /* One extra interrupt for the PSL interrupt is already
0196          * included. Remove it now to keep only AFU interrupts and
0197          * match the native case.
0198          */
0199         afu->pp_irqs--;
0200     }
0201 
0202     if (cxl_verbose) {
0203         read_prop_dword(np, "ibm,max-ints", &val);
0204         read_prop_dword(np, "ibm,vpd-size", &val);
0205     }
0206 
0207     read_prop64_dword(np, "ibm,error-buffer-size", &afu->eb_len);
0208     afu->eb_offset = 0;
0209 
0210     if (cxl_verbose)
0211         read_prop_dword(np, "ibm,config-record-type", &val);
0212 
0213     read_prop64_dword(np, "ibm,config-record-size", &afu->crs_len);
0214     afu->crs_offset = 0;
0215 
0216     read_prop_dword(np, "ibm,#config-records", &afu->crs_num);
0217 
0218     if (cxl_verbose) {
0219         for (i = 0; i < afu->crs_num; i++) {
0220             rc = cxl_ops->afu_cr_read16(afu, i, PCI_DEVICE_ID,
0221                         &device_id);
0222             if (!rc)
0223                 pr_info("record %d - device-id: %#x\n",
0224                     i, device_id);
0225             rc = cxl_ops->afu_cr_read16(afu, i, PCI_VENDOR_ID,
0226                         &vendor_id);
0227             if (!rc)
0228                 pr_info("record %d - vendor-id: %#x\n",
0229                     i, vendor_id);
0230             rc = cxl_ops->afu_cr_read32(afu, i, PCI_CLASS_REVISION,
0231                         &class_code);
0232             if (!rc) {
0233                 class_code >>= 8;
0234                 pr_info("record %d - class-code: %#x\n",
0235                     i, class_code);
0236             }
0237         }
0238 
0239         read_prop_dword(np, "ibm,function-number", &val);
0240         read_prop_dword(np, "ibm,privileged-function", &val);
0241         read_prop_dword(np, "vendor-id", &val);
0242         read_prop_dword(np, "device-id", &val);
0243         read_prop_dword(np, "revision-id", &val);
0244         read_prop_dword(np, "class-code", &val);
0245         read_prop_dword(np, "subsystem-vendor-id", &val);
0246         read_prop_dword(np, "subsystem-id", &val);
0247     }
0248     /*
0249      * if "ibm,process-mmio" doesn't exist then per-process mmio is
0250      * not supported
0251      */
0252     val = 0;
0253     prop = read_prop_dword(np, "ibm,process-mmio", &val);
0254     if (prop && val == 1)
0255         afu->pp_psa = true;
0256     else
0257         afu->pp_psa = false;
0258 
0259     if (cxl_verbose) {
0260         read_prop_dword(np, "ibm,supports-aur", &val);
0261         read_prop_dword(np, "ibm,supports-csrp", &val);
0262         read_prop_dword(np, "ibm,supports-prr", &val);
0263     }
0264 
0265     prop = read_prop_dword(np, "ibm,function-error-interrupt", &val);
0266     if (prop)
0267         afu->serr_hwirq = val;
0268 
0269     pr_devel("AFU handle: %#llx\n", afu->guest->handle);
0270     pr_devel("p2n_phys: %#llx (size %#llx)\n",
0271         afu->guest->p2n_phys, afu->guest->p2n_size);
0272     pr_devel("psn_phys: %#llx (size %#llx)\n",
0273         afu->psn_phys, afu->adapter->ps_size);
0274     pr_devel("Max number of processes virtualised=%i\n",
0275         afu->max_procs_virtualised);
0276     pr_devel("Per-process irqs min=%i, max=%i\n", afu->pp_irqs,
0277          afu->irqs_max);
0278     pr_devel("Slice error interrupt=%#lx\n", afu->serr_hwirq);
0279 
0280     return 0;
0281 }
0282 
0283 static int read_adapter_irq_config(struct cxl *adapter, struct device_node *np)
0284 {
0285     const __be32 *ranges;
0286     int len, nranges, i;
0287     struct irq_avail *cur;
0288 
0289     ranges = of_get_property(np, "interrupt-ranges", &len);
0290     if (ranges == NULL || len < (2 * sizeof(int)))
0291         return -EINVAL;
0292 
0293     /*
0294      * encoded array of two cells per entry, each cell encoded as
0295      * with encode-int
0296      */
0297     nranges = len / (2 * sizeof(int));
0298     if (nranges == 0 || (nranges * 2 * sizeof(int)) != len)
0299         return -EINVAL;
0300 
0301     adapter->guest->irq_avail = kcalloc(nranges, sizeof(struct irq_avail),
0302                         GFP_KERNEL);
0303     if (adapter->guest->irq_avail == NULL)
0304         return -ENOMEM;
0305 
0306     adapter->guest->irq_base_offset = be32_to_cpu(ranges[0]);
0307     for (i = 0; i < nranges; i++) {
0308         cur = &adapter->guest->irq_avail[i];
0309         cur->offset = be32_to_cpu(ranges[i * 2]);
0310         cur->range  = be32_to_cpu(ranges[i * 2 + 1]);
0311         cur->bitmap = bitmap_zalloc(cur->range, GFP_KERNEL);
0312         if (cur->bitmap == NULL)
0313             goto err;
0314         if (cur->offset < adapter->guest->irq_base_offset)
0315             adapter->guest->irq_base_offset = cur->offset;
0316         if (cxl_verbose)
0317             pr_info("available IRQ range: %#lx-%#lx (%lu)\n",
0318                 cur->offset, cur->offset + cur->range - 1,
0319                 cur->range);
0320     }
0321     adapter->guest->irq_nranges = nranges;
0322     spin_lock_init(&adapter->guest->irq_alloc_lock);
0323 
0324     return 0;
0325 err:
0326     for (i--; i >= 0; i--) {
0327         cur = &adapter->guest->irq_avail[i];
0328         bitmap_free(cur->bitmap);
0329     }
0330     kfree(adapter->guest->irq_avail);
0331     adapter->guest->irq_avail = NULL;
0332     return -ENOMEM;
0333 }
0334 
0335 int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node *np)
0336 {
0337     if (read_handle(np, &adapter->guest->handle))
0338         return -EINVAL;
0339     pr_devel("Adapter handle: 0x%.16llx\n", adapter->guest->handle);
0340 
0341     return 0;
0342 }
0343 
0344 int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np)
0345 {
0346     int rc, len, naddr, i;
0347     char *p;
0348     const __be32 *prop;
0349     u32 val = 0;
0350 
0351     /* Properties are read in the same order as listed in PAPR */
0352 
0353     naddr = of_n_addr_cells(np);
0354 
0355     if (cxl_verbose) {
0356         pr_info("Dump of the 'ibm,coherent-platform-facility' node properties:\n");
0357 
0358         read_prop_dword(np, "#address-cells", &val);
0359         read_prop_dword(np, "#size-cells", &val);
0360 
0361         prop = of_get_property(np, "compatible", &len);
0362         i = 0;
0363         while (i < len) {
0364             p = (char *) prop + i;
0365             pr_info("compatible: %s\n", p);
0366             i += strlen(p) + 1;
0367         }
0368         read_prop_string(np, "name");
0369         read_prop_string(np, "model");
0370 
0371         prop = of_get_property(np, "reg", NULL);
0372         if (prop) {
0373             pr_info("reg: addr:%#llx size:%#x\n",
0374                 of_read_number(prop, naddr),
0375                 be32_to_cpu(prop[naddr]));
0376         }
0377 
0378         read_prop_string(np, "ibm,loc-code");
0379     }
0380 
0381     if ((rc = read_adapter_irq_config(adapter, np)))
0382         return rc;
0383 
0384     if (cxl_verbose) {
0385         read_prop_string(np, "device_type");
0386         read_prop_string(np, "ibm,phandle");
0387     }
0388 
0389     prop = read_prop_dword(np, "ibm,caia-version", &val);
0390     if (prop) {
0391         adapter->caia_major = (val & 0xFF00) >> 8;
0392         adapter->caia_minor = val & 0xFF;
0393     }
0394 
0395     prop = read_prop_dword(np, "ibm,psl-revision", &val);
0396     if (prop)
0397         adapter->psl_rev = val;
0398 
0399     prop = read_prop_string(np, "status");
0400     if (prop) {
0401         adapter->guest->status = kasprintf(GFP_KERNEL, "%s", (char *) prop);
0402         if (adapter->guest->status == NULL)
0403             return -ENOMEM;
0404     }
0405 
0406     prop = read_prop_dword(np, "vendor-id", &val);
0407     if (prop)
0408         adapter->guest->vendor = val;
0409 
0410     prop = read_prop_dword(np, "device-id", &val);
0411     if (prop)
0412         adapter->guest->device = val;
0413 
0414     if (cxl_verbose) {
0415         read_prop_dword(np, "ibm,privileged-facility", &val);
0416         read_prop_dword(np, "revision-id", &val);
0417         read_prop_dword(np, "class-code", &val);
0418     }
0419 
0420     prop = read_prop_dword(np, "subsystem-vendor-id", &val);
0421     if (prop)
0422         adapter->guest->subsystem_vendor = val;
0423 
0424     prop = read_prop_dword(np, "subsystem-id", &val);
0425     if (prop)
0426         adapter->guest->subsystem = val;
0427 
0428     if (cxl_verbose)
0429         read_vpd(adapter, NULL);
0430 
0431     return 0;
0432 }
0433 
0434 static int cxl_of_remove(struct platform_device *pdev)
0435 {
0436     struct cxl *adapter;
0437     int afu;
0438 
0439     adapter = dev_get_drvdata(&pdev->dev);
0440     for (afu = 0; afu < adapter->slices; afu++)
0441         cxl_guest_remove_afu(adapter->afu[afu]);
0442 
0443     cxl_guest_remove_adapter(adapter);
0444     return 0;
0445 }
0446 
0447 static void cxl_of_shutdown(struct platform_device *pdev)
0448 {
0449     cxl_of_remove(pdev);
0450 }
0451 
0452 int cxl_of_probe(struct platform_device *pdev)
0453 {
0454     struct device_node *np = NULL;
0455     struct device_node *afu_np = NULL;
0456     struct cxl *adapter = NULL;
0457     int ret;
0458     int slice = 0, slice_ok = 0;
0459 
0460     pr_devel("in %s\n", __func__);
0461 
0462     np = pdev->dev.of_node;
0463     if (np == NULL)
0464         return -ENODEV;
0465 
0466     /* init adapter */
0467     adapter = cxl_guest_init_adapter(np, pdev);
0468     if (IS_ERR(adapter)) {
0469         dev_err(&pdev->dev, "guest_init_adapter failed: %li\n", PTR_ERR(adapter));
0470         return PTR_ERR(adapter);
0471     }
0472 
0473     /* init afu */
0474     for_each_child_of_node(np, afu_np) {
0475         if ((ret = cxl_guest_init_afu(adapter, slice, afu_np)))
0476             dev_err(&pdev->dev, "AFU %i failed to initialise: %i\n",
0477                 slice, ret);
0478         else
0479             slice_ok++;
0480         slice++;
0481     }
0482 
0483     if (slice_ok == 0) {
0484         dev_info(&pdev->dev, "No active AFU");
0485         adapter->slices = 0;
0486     }
0487 
0488     return 0;
0489 }
0490 
0491 static const struct of_device_id cxl_of_match[] = {
0492     { .compatible = "ibm,coherent-platform-facility",},
0493     {},
0494 };
0495 MODULE_DEVICE_TABLE(of, cxl_of_match);
0496 
0497 struct platform_driver cxl_of_driver = {
0498     .driver = {
0499         .name = "cxl_of",
0500         .of_match_table = cxl_of_match,
0501         .owner = THIS_MODULE
0502     },
0503     .probe = cxl_of_probe,
0504     .remove = cxl_of_remove,
0505     .shutdown = cxl_of_shutdown,
0506 };