|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +#include <linux/init.h> |
| 3 | +#include <linux/kernel.h> |
| 4 | +#include <linux/pci.h> |
| 5 | +#include <linux/slab.h> |
| 6 | +#include <linux/interrupt.h> |
| 7 | +#include <linux/types.h> |
| 8 | +#include <linux/io.h> |
| 9 | +#include <linux/platform_device.h> |
| 10 | +#include <linux/dma-mapping.h> |
| 11 | +#include <linux/scatterlist.h> |
| 12 | +#include <linux/mm.h> |
| 13 | +#include <asm/host_ops.h> |
| 14 | + |
| 15 | +static int lkl_pci_generic_read(struct pci_bus *bus, unsigned int devfn, |
| 16 | + int where, int size, u32 *val) |
| 17 | +{ |
| 18 | + if (devfn == 0 && |
| 19 | + lkl_ops->pci_ops->read(bus->sysdata, where, size, val) == size) |
| 20 | + return PCIBIOS_SUCCESSFUL; |
| 21 | + else |
| 22 | + return PCIBIOS_FUNC_NOT_SUPPORTED; |
| 23 | +} |
| 24 | + |
| 25 | +static int lkl_pci_generic_write(struct pci_bus *bus, unsigned int devfn, |
| 26 | + int where, int size, u32 val) |
| 27 | +{ |
| 28 | + if (devfn == 0 && |
| 29 | + lkl_ops->pci_ops->write(bus->sysdata, where, size, &val) == size) |
| 30 | + return PCIBIOS_SUCCESSFUL; |
| 31 | + else |
| 32 | + return PCIBIOS_FUNC_NOT_SUPPORTED; |
| 33 | +} |
| 34 | + |
| 35 | +void __iomem *__pci_ioport_map(struct pci_dev *dev, unsigned long port, |
| 36 | + unsigned int nr) |
| 37 | +{ |
| 38 | + panic("%s is not supported\n", __func__); |
| 39 | + return NULL; |
| 40 | +} |
| 41 | + |
| 42 | +static int lkl_pci_override_resource(struct pci_dev *dev, void *data) |
| 43 | +{ |
| 44 | + int i; |
| 45 | + struct resource *r; |
| 46 | + resource_size_t start, size; |
| 47 | + void *remapped_start = NULL; |
| 48 | + |
| 49 | + if (dev->devfn != 0) |
| 50 | + return 0; |
| 51 | + |
| 52 | + for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 53 | + r = &dev->resource[i]; |
| 54 | + |
| 55 | + if (!r->parent && r->start && r->flags) { |
| 56 | + dev_info(&dev->dev, "claiming resource %s/%d\n", |
| 57 | + pci_name(dev), i); |
| 58 | + if (pci_claim_resource(dev, i)) { |
| 59 | + dev_err(&dev->dev, |
| 60 | + "Could not claim resource %s/%d!", |
| 61 | + pci_name(dev), i); |
| 62 | + } |
| 63 | + |
| 64 | + size = pci_resource_len(dev, i); |
| 65 | + |
| 66 | + if (pci_resource_flags(dev, i) & IORESOURCE_MEM) { |
| 67 | + remapped_start = |
| 68 | + lkl_ops->pci_ops->resource_alloc( |
| 69 | + dev->sysdata, size, i); |
| 70 | + } |
| 71 | + |
| 72 | + if (remapped_start) { |
| 73 | + /* override values */ |
| 74 | + start = (resource_size_t)remapped_start; |
| 75 | + pci_resource_start(dev, i) = start; |
| 76 | + pci_resource_end(dev, i) = start + size - 1; |
| 77 | + } else { |
| 78 | + /* |
| 79 | + * A host library or the application could |
| 80 | + * not handle the resource. Disable it |
| 81 | + * not to be touched by drivers. |
| 82 | + */ |
| 83 | + pci_resource_flags(dev, i) |= |
| 84 | + IORESOURCE_DISABLED; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + dev->irq = lkl_get_free_irq("pci"); |
| 90 | + |
| 91 | + if (lkl_ops->pci_ops->irq_init(dev->sysdata, dev->irq) < 0) |
| 92 | + return -ENOMEM; |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +static int lkl_pci_remove_devices(struct pci_dev *dev, void *data) |
| 98 | +{ |
| 99 | + lkl_ops->pci_ops->remove(dev->sysdata); |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +static struct pci_ops lkl_pci_root_ops = { |
| 104 | + .read = lkl_pci_generic_read, |
| 105 | + .write = lkl_pci_generic_write, |
| 106 | +}; |
| 107 | + |
| 108 | +static void *lkl_dma_alloc(struct device *dev, size_t size, |
| 109 | + dma_addr_t *dma_handle, gfp_t gfp, |
| 110 | + unsigned long attrs) |
| 111 | +{ |
| 112 | + void *vaddr = page_to_virt(alloc_pages(gfp, get_order(size))); |
| 113 | + *dma_handle = (dma_addr_t)lkl_ops->pci_ops->map_page( |
| 114 | + to_pci_dev(dev)->sysdata, vaddr, size); |
| 115 | + return vaddr; |
| 116 | +} |
| 117 | + |
| 118 | +static void lkl_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 119 | + dma_addr_t dma_addr, unsigned long attrs) |
| 120 | +{ |
| 121 | + lkl_ops->pci_ops->unmap_page(to_pci_dev(dev)->sysdata, dma_addr, size); |
| 122 | + __free_pages(cpu_addr, get_order(size)); |
| 123 | +} |
| 124 | + |
| 125 | +static dma_addr_t lkl_dma_map_page(struct device *dev, struct page *page, |
| 126 | + unsigned long offset, size_t size, |
| 127 | + enum dma_data_direction dir, |
| 128 | + unsigned long attrs) |
| 129 | +{ |
| 130 | + dma_addr_t dma_handle = (dma_addr_t)lkl_ops->pci_ops->map_page( |
| 131 | + to_pci_dev(dev)->sysdata, page_to_virt(page) + offset, size); |
| 132 | + if (dma_handle == 0) |
| 133 | + return DMA_MAPPING_ERROR; |
| 134 | + |
| 135 | + return dma_handle; |
| 136 | +} |
| 137 | + |
| 138 | +static void lkl_dma_unmap_page(struct device *dev, dma_addr_t dma_addr, |
| 139 | + size_t size, enum dma_data_direction dir, |
| 140 | + unsigned long attrs) |
| 141 | +{ |
| 142 | + lkl_ops->pci_ops->unmap_page(to_pci_dev(dev)->sysdata, dma_addr, size); |
| 143 | +} |
| 144 | + |
| 145 | +static int lkl_dma_map_sg(struct device *dev, struct scatterlist *sgl, |
| 146 | + int nents, enum dma_data_direction dir, |
| 147 | + unsigned long attrs) |
| 148 | +{ |
| 149 | + int i; |
| 150 | + struct scatterlist *sg; |
| 151 | + |
| 152 | + for_each_sg(sgl, sg, nents, i) { |
| 153 | + void *va; |
| 154 | + |
| 155 | + WARN_ON(!sg_page(sg)); |
| 156 | + va = sg_virt(sg); |
| 157 | + sg_dma_address(sg) = (dma_addr_t)lkl_dma_map_page( |
| 158 | + dev, sg_page(sg), sg->offset, sg->length, dir, attrs); |
| 159 | + sg_dma_len(sg) = sg->length; |
| 160 | + } |
| 161 | + return nents; |
| 162 | +} |
| 163 | + |
| 164 | +static void lkl_dma_unmap_sg(struct device *dev, struct scatterlist *sgl, |
| 165 | + int nents, enum dma_data_direction dir, |
| 166 | + unsigned long attrs) |
| 167 | +{ |
| 168 | + int i; |
| 169 | + struct scatterlist *sg; |
| 170 | + |
| 171 | + for_each_sg(sgl, sg, nents, i) |
| 172 | + lkl_dma_unmap_page(dev, sg_dma_address(sg), sg_dma_len(sg), dir, |
| 173 | + attrs); |
| 174 | +} |
| 175 | + |
| 176 | +static int lkl_dma_supported(struct device *dev, u64 mask) |
| 177 | +{ |
| 178 | + return 1; |
| 179 | +} |
| 180 | + |
| 181 | +static char *pcidev_name; |
| 182 | + |
| 183 | +static int __init setup_pci_device(char *str) |
| 184 | +{ |
| 185 | + if (pcidev_name) { |
| 186 | + pr_info("The PCI driver supports only one PCI device."); |
| 187 | + pr_info("'%s' will be discarded.", str); |
| 188 | + return -1; |
| 189 | + } |
| 190 | + pcidev_name = str; |
| 191 | + return 0; |
| 192 | +} |
| 193 | + |
| 194 | +early_param("lkl_pci", setup_pci_device); |
| 195 | + |
| 196 | +const struct dma_map_ops lkl_dma_ops = { |
| 197 | + .alloc = lkl_dma_alloc, |
| 198 | + .free = lkl_dma_free, |
| 199 | + .map_sg = lkl_dma_map_sg, |
| 200 | + .unmap_sg = lkl_dma_unmap_sg, |
| 201 | + .map_page = lkl_dma_map_page, |
| 202 | + .unmap_page = lkl_dma_unmap_page, |
| 203 | + .dma_supported = lkl_dma_supported, |
| 204 | +}; |
| 205 | + |
| 206 | +static int lkl_pci_probe(struct platform_device *pdev) |
| 207 | +{ |
| 208 | + struct lkl_pci_dev *dev; |
| 209 | + struct pci_bus *bus; |
| 210 | + |
| 211 | + if (!lkl_ops->pci_ops || !pcidev_name) |
| 212 | + return -1; |
| 213 | + |
| 214 | + dev = lkl_ops->pci_ops->add(pcidev_name, (void *)memory_start, |
| 215 | + memory_end - memory_start); |
| 216 | + if (!dev) |
| 217 | + return -1; |
| 218 | + |
| 219 | + bus = pci_scan_bus(0, &lkl_pci_root_ops, (void *)dev); |
| 220 | + if (!bus) { |
| 221 | + lkl_ops->pci_ops->remove(dev); |
| 222 | + return -1; |
| 223 | + } |
| 224 | + pci_walk_bus(bus, lkl_pci_override_resource, NULL); |
| 225 | + pci_bus_add_devices(bus); |
| 226 | + dev_set_drvdata(&pdev->dev, bus); |
| 227 | + |
| 228 | + return 0; |
| 229 | +} |
| 230 | + |
| 231 | +static void lkl_pci_shutdown(struct platform_device *pdev) |
| 232 | +{ |
| 233 | + struct pci_bus *bus = (struct pci_bus *)dev_get_drvdata(&pdev->dev); |
| 234 | + |
| 235 | + if (bus) |
| 236 | + pci_walk_bus(bus, lkl_pci_remove_devices, NULL); |
| 237 | +} |
| 238 | + |
| 239 | +static struct platform_driver lkl_pci_driver = { |
| 240 | + .driver = { |
| 241 | + .name = "lkl_pci", |
| 242 | + }, |
| 243 | + .probe = lkl_pci_probe, |
| 244 | + .shutdown = lkl_pci_shutdown, |
| 245 | +}; |
| 246 | + |
| 247 | +static int __init lkl_pci_init(void) |
| 248 | +{ |
| 249 | + int ret; |
| 250 | + struct platform_device *dev; |
| 251 | + |
| 252 | + /*register a platform driver*/ |
| 253 | + ret = platform_driver_register(&lkl_pci_driver); |
| 254 | + if (ret != 0) |
| 255 | + return ret; |
| 256 | + |
| 257 | + dev = platform_device_alloc("lkl_pci", -1); |
| 258 | + if (!dev) |
| 259 | + return -ENOMEM; |
| 260 | + |
| 261 | + ret = platform_device_add(dev); |
| 262 | + if (ret != 0) |
| 263 | + goto error; |
| 264 | + |
| 265 | + return 0; |
| 266 | +error: |
| 267 | + platform_device_put(dev); |
| 268 | + return ret; |
| 269 | +} |
| 270 | + |
| 271 | +subsys_initcall(lkl_pci_init); |
0 commit comments