1 /* pci_sun4v.c: SUN4V specific PCI controller support.
3 * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/types.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/interrupt.h>
14 #include <asm/iommu.h>
17 #include <asm/pstate.h>
18 #include <asm/oplib.h>
19 #include <asm/hypervisor.h>
22 #include "iommu_common.h"
24 #include "pci_sun4v.h"
26 static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
31 static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
35 static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
40 static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
44 static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
49 static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
53 static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
57 static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
61 struct pci_iommu_ops pci_sun4v_iommu_ops = {
62 .alloc_consistent = pci_4v_alloc_consistent,
63 .free_consistent = pci_4v_free_consistent,
64 .map_single = pci_4v_map_single,
65 .unmap_single = pci_4v_unmap_single,
66 .map_sg = pci_4v_map_sg,
67 .unmap_sg = pci_4v_unmap_sg,
68 .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
69 .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
72 /* SUN4V PCI configuration space accessors. */
74 static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
75 int where, int size, u32 *value)
77 struct pci_pbm_info *pbm = bus_dev->sysdata;
78 unsigned long devhandle = pbm->devhandle;
79 unsigned int bus = bus_dev->number;
80 unsigned int device = PCI_SLOT(devfn);
81 unsigned int func = PCI_FUNC(devfn);
84 ret = pci_sun4v_config_get(devhandle,
85 HV_PCI_DEVICE_BUILD(bus, device, func),
92 *value = ret & 0xffff;
95 *value = ret & 0xffffffff;
100 return PCIBIOS_SUCCESSFUL;
103 static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
104 int where, int size, u32 value)
106 struct pci_pbm_info *pbm = bus_dev->sysdata;
107 unsigned long devhandle = pbm->devhandle;
108 unsigned int bus = bus_dev->number;
109 unsigned int device = PCI_SLOT(devfn);
110 unsigned int func = PCI_FUNC(devfn);
113 ret = pci_sun4v_config_put(devhandle,
114 HV_PCI_DEVICE_BUILD(bus, device, func),
117 return PCIBIOS_SUCCESSFUL;
120 static struct pci_ops pci_sun4v_ops = {
121 .read = pci_sun4v_read_pci_cfg,
122 .write = pci_sun4v_write_pci_cfg,
126 static void pci_sun4v_scan_bus(struct pci_controller_info *p)
128 /* XXX Implement me! XXX */
131 static unsigned int pci_sun4v_irq_build(struct pci_pbm_info *pbm,
132 struct pci_dev *pdev,
135 /* XXX Implement me! XXX */
139 /* XXX correct? XXX */
140 static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
142 struct pcidev_cookie *pcp = pdev->sysdata;
143 struct pci_pbm_info *pbm = pcp->pbm;
144 struct resource *res, *root;
146 int where, size, is_64bit;
148 res = &pdev->resource[resource];
150 where = PCI_BASE_ADDRESS_0 + (resource * 4);
151 } else if (resource == PCI_ROM_RESOURCE) {
152 where = pdev->rom_base_reg;
154 /* Somebody might have asked allocation of a non-standard resource */
159 if (res->flags & IORESOURCE_IO)
160 root = &pbm->io_space;
162 root = &pbm->mem_space;
163 if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
164 == PCI_BASE_ADDRESS_MEM_TYPE_64)
168 size = res->end - res->start;
169 pci_read_config_dword(pdev, where, ®);
170 reg = ((reg & size) |
171 (((u32)(res->start - root->start)) & ~size));
172 if (resource == PCI_ROM_RESOURCE) {
173 reg |= PCI_ROM_ADDRESS_ENABLE;
174 res->flags |= IORESOURCE_ROM_ENABLE;
176 pci_write_config_dword(pdev, where, reg);
178 /* This knows that the upper 32-bits of the address
179 * must be zero. Our PCI common layer enforces this.
182 pci_write_config_dword(pdev, where + 4, 0);
185 /* XXX correct? XXX */
186 static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
187 struct resource *res,
188 struct resource *root)
190 res->start += root->start;
191 res->end += root->start;
194 /* Use ranges property to determine where PCI MEM, I/O, and Config
195 * space are for this PCI bus module.
197 static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
199 int i, saw_cfg, saw_mem, saw_io;
201 saw_cfg = saw_mem = saw_io = 0;
202 for (i = 0; i < pbm->num_pbm_ranges; i++) {
203 struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
207 type = (pr->child_phys_hi >> 24) & 0x3;
208 a = (((unsigned long)pr->parent_phys_hi << 32UL) |
209 ((unsigned long)pr->parent_phys_lo << 0UL));
213 /* PCI config space, 16MB */
214 pbm->config_space = a;
219 /* 16-bit IO space, 16MB */
220 pbm->io_space.start = a;
221 pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
222 pbm->io_space.flags = IORESOURCE_IO;
227 /* 32-bit MEM space, 2GB */
228 pbm->mem_space.start = a;
229 pbm->mem_space.end = a + (0x80000000UL - 1UL);
230 pbm->mem_space.flags = IORESOURCE_MEM;
239 if (!saw_cfg || !saw_io || !saw_mem) {
240 prom_printf("%s: Fatal error, missing %s PBM range.\n",
249 printk("%s: PCI CFG[%lx] IO[%lx] MEM[%lx]\n",
253 pbm->mem_space.start);
256 static void pbm_register_toplevel_resources(struct pci_controller_info *p,
257 struct pci_pbm_info *pbm)
259 pbm->io_space.name = pbm->mem_space.name = pbm->name;
261 request_resource(&ioport_resource, &pbm->io_space);
262 request_resource(&iomem_resource, &pbm->mem_space);
263 pci_register_legacy_regions(&pbm->io_space,
267 static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
269 /* XXX Implement me! XXX */
272 static void pci_sun4v_pbm_init(struct pci_controller_info *p, int prom_node)
274 struct pci_pbm_info *pbm;
275 struct linux_prom64_registers regs;
276 unsigned int busrange[2];
283 pbm->prom_node = prom_node;
284 pbm->pci_first_slot = 1;
286 prom_getproperty(prom_node, "reg", (char *)®s, sizeof(regs));
287 pbm->devhandle = (regs.phys_addr >> 32UL) & 0x0fffffff;
289 sprintf(pbm->name, "SUN4V-PCI%d PBM%c",
290 p->index, (pbm == &p->pbm_A ? 'A' : 'B'));
292 printk("%s: devhandle[%x]\n", pbm->name, pbm->devhandle);
294 prom_getstring(prom_node, "name",
295 pbm->prom_name, sizeof(pbm->prom_name));
297 err = prom_getproperty(prom_node, "ranges",
298 (char *) pbm->pbm_ranges,
299 sizeof(pbm->pbm_ranges));
300 if (err == 0 || err == -1) {
301 prom_printf("%s: Fatal error, no ranges property.\n",
306 pbm->num_pbm_ranges =
307 (err / sizeof(struct linux_prom_pci_ranges));
309 pci_sun4v_determine_mem_io_space(pbm);
310 pbm_register_toplevel_resources(p, pbm);
312 err = prom_getproperty(prom_node, "interrupt-map",
313 (char *)pbm->pbm_intmap,
314 sizeof(pbm->pbm_intmap));
316 pbm->num_pbm_intmap = (err / sizeof(struct linux_prom_pci_intmap));
317 err = prom_getproperty(prom_node, "interrupt-map-mask",
318 (char *)&pbm->pbm_intmask,
319 sizeof(pbm->pbm_intmask));
321 prom_printf("%s: Fatal error, no "
322 "interrupt-map-mask.\n", pbm->name);
326 pbm->num_pbm_intmap = 0;
327 memset(&pbm->pbm_intmask, 0, sizeof(pbm->pbm_intmask));
330 err = prom_getproperty(prom_node, "bus-range",
331 (char *)&busrange[0],
333 if (err == 0 || err == -1) {
334 prom_printf("%s: Fatal error, no bus-range.\n", pbm->name);
337 pbm->pci_first_busno = busrange[0];
338 pbm->pci_last_busno = busrange[1];
340 pci_sun4v_iommu_init(pbm);
343 void sun4v_pci_init(int node, char *model_name)
345 struct pci_controller_info *p;
346 struct pci_iommu *iommu;
348 p = kmalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
350 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
353 memset(p, 0, sizeof(*p));
355 iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
357 prom_printf("SCHIZO: Fatal memory allocation error.\n");
360 memset(iommu, 0, sizeof(*iommu));
361 p->pbm_A.iommu = iommu;
363 iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
365 prom_printf("SCHIZO: Fatal memory allocation error.\n");
368 memset(iommu, 0, sizeof(*iommu));
369 p->pbm_B.iommu = iommu;
371 p->next = pci_controller_root;
372 pci_controller_root = p;
374 p->index = pci_num_controllers++;
375 p->pbms_same_domain = 0;
377 p->scan_bus = pci_sun4v_scan_bus;
378 p->irq_build = pci_sun4v_irq_build;
379 p->base_address_update = pci_sun4v_base_address_update;
380 p->resource_adjust = pci_sun4v_resource_adjust;
381 p->pci_ops = &pci_sun4v_ops;
383 /* Like PSYCHO and SCHIZO we have a 2GB aligned area
386 pci_memspace_mask = 0x7fffffffUL;
388 pci_sun4v_pbm_init(p, node);
390 prom_printf("sun4v_pci_init: Implement me.\n");