2 * Copyright IBM Corp. 2012
5 * Jan Glauber <jang@linux.vnet.ibm.com>
7 * The System z PCI code is a rewrite from a prototype by
8 * the following people (Kudoz!):
9 * Alexander Schmidt <alexschm@de.ibm.com>
10 * Christoph Raisch <raisch@de.ibm.com>
11 * Hannes Hering <hering2@de.ibm.com>
12 * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
13 * Jan-Bernd Themann <themann@de.ibm.com>
14 * Stefan Roscher <stefan.roscher@de.ibm.com>
15 * Thomas Klein <tklein@de.ibm.com>
18 #define COMPONENT "zPCI"
19 #define pr_fmt(fmt) COMPONENT ": " fmt
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/export.h>
25 #include <linux/delay.h>
26 #include <linux/seq_file.h>
27 #include <linux/pci.h>
28 #include <linux/msi.h>
30 #include <asm/facility.h>
31 #include <asm/pci_insn.h>
32 #include <asm/pci_clp.h>
34 #define DEBUG /* enable pr_debug */
36 #define ZPCI_NR_DMA_SPACES 1
37 #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
39 /* list of all detected zpci devices */
41 DEFINE_MUTEX(zpci_list_lock);
43 static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
44 static DEFINE_SPINLOCK(zpci_domain_lock);
47 static DEFINE_SPINLOCK(zpci_iomap_lock);
48 static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
49 struct zpci_iomap_entry *zpci_iomap_start;
50 EXPORT_SYMBOL_GPL(zpci_iomap_start);
52 struct zpci_dev *get_zdev(struct pci_dev *pdev)
54 return (struct zpci_dev *) pdev->sysdata;
57 struct zpci_dev *get_zdev_by_fid(u32 fid)
59 struct zpci_dev *tmp, *zdev = NULL;
61 mutex_lock(&zpci_list_lock);
62 list_for_each_entry(tmp, &zpci_list, entry) {
63 if (tmp->fid == fid) {
68 mutex_unlock(&zpci_list_lock);
72 bool zpci_fid_present(u32 fid)
74 return (get_zdev_by_fid(fid) != NULL) ? true : false;
77 static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
79 return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
82 int pci_domain_nr(struct pci_bus *bus)
84 return ((struct zpci_dev *) bus->sysdata)->domain;
86 EXPORT_SYMBOL_GPL(pci_domain_nr);
88 int pci_proc_domain(struct pci_bus *bus)
90 return pci_domain_nr(bus);
92 EXPORT_SYMBOL_GPL(pci_proc_domain);
94 /* Store PCI function information block */
95 static int zpci_store_fib(struct zpci_dev *zdev, u8 *fc)
100 fib = (void *) get_zeroed_page(GFP_KERNEL);
105 cc = __stpcifc(zdev->fh, 0, fib, &status);
107 msleep(ZPCI_INSN_BUSY_DELAY);
108 memset(fib, 0, PAGE_SIZE);
113 pr_err_once("%s: cc: %u status: %u\n",
114 __func__, cc, status);
116 /* Return PCI function controls */
119 free_page((unsigned long) fib);
120 return (cc) ? -EIO : 0;
123 #define ZPCI_PCIAS_CFGSPC 15
125 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
127 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
131 rc = pcilg_instr(&data, req, offset);
132 data = data << ((8 - len) * 8);
133 data = le64_to_cpu(data);
141 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
143 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
147 data = cpu_to_le64(data);
148 data = data >> ((8 - len) * 8);
149 rc = pcistg_instr(data, req, offset);
153 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
157 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
158 resource_size_t size,
159 resource_size_t align)
164 /* Create a virtual mapping cookie for a PCI BAR */
165 void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
167 struct zpci_dev *zdev = get_zdev(pdev);
171 if ((bar & 7) != bar)
174 idx = zdev->bars[bar].map_idx;
175 spin_lock(&zpci_iomap_lock);
176 zpci_iomap_start[idx].fh = zdev->fh;
177 zpci_iomap_start[idx].bar = bar;
178 spin_unlock(&zpci_iomap_lock);
180 addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
181 return (void __iomem *) addr;
183 EXPORT_SYMBOL_GPL(pci_iomap);
185 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
189 idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
190 spin_lock(&zpci_iomap_lock);
191 zpci_iomap_start[idx].fh = 0;
192 zpci_iomap_start[idx].bar = 0;
193 spin_unlock(&zpci_iomap_lock);
195 EXPORT_SYMBOL_GPL(pci_iounmap);
197 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
200 struct zpci_dev *zdev = get_zdev_by_bus(bus);
202 if (!zdev || devfn != ZPCI_DEVFN)
204 return zpci_cfg_load(zdev, where, val, size);
207 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
210 struct zpci_dev *zdev = get_zdev_by_bus(bus);
212 if (!zdev || devfn != ZPCI_DEVFN)
214 return zpci_cfg_store(zdev, where, val, size);
217 static struct pci_ops pci_root_ops = {
222 static void zpci_map_resources(struct zpci_dev *zdev)
224 struct pci_dev *pdev = zdev->pdev;
228 for (i = 0; i < PCI_BAR_COUNT; i++) {
229 len = pci_resource_len(pdev, i);
232 pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
233 pdev->resource[i].end = pdev->resource[i].start + len - 1;
234 pr_debug("BAR%i: -> start: %Lx end: %Lx\n",
235 i, pdev->resource[i].start, pdev->resource[i].end);
239 static void zpci_unmap_resources(struct pci_dev *pdev)
244 for (i = 0; i < PCI_BAR_COUNT; i++) {
245 len = pci_resource_len(pdev, i);
248 pci_iounmap(pdev, (void *) pdev->resource[i].start);
252 struct zpci_dev *zpci_alloc_device(void)
254 struct zpci_dev *zdev;
256 /* Alloc memory for our private pci device data */
257 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
259 return ERR_PTR(-ENOMEM);
263 void zpci_free_device(struct zpci_dev *zdev)
268 /* Called on removal of pci_dev, leaves zpci and bus device */
269 static void zpci_remove_device(struct pci_dev *pdev)
271 struct zpci_dev *zdev = get_zdev(pdev);
273 dev_info(&pdev->dev, "Removing device %u\n", zdev->domain);
274 zdev->state = ZPCI_FN_STATE_CONFIGURED;
275 zpci_unmap_resources(pdev);
276 list_del(&zdev->entry); /* can be called from init */
280 static void zpci_scan_devices(void)
282 struct zpci_dev *zdev;
284 mutex_lock(&zpci_list_lock);
285 list_for_each_entry(zdev, &zpci_list, entry)
286 if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
287 zpci_scan_device(zdev);
288 mutex_unlock(&zpci_list_lock);
292 * Too late for any s390 specific setup, since interrupts must be set up
293 * already which requires DMA setup too and the pci scan will access the
294 * config space, which only works if the function handle is enabled.
296 int pcibios_enable_device(struct pci_dev *pdev, int mask)
298 struct resource *res;
302 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
304 for (i = 0; i < PCI_BAR_COUNT; i++) {
305 res = &pdev->resource[i];
307 if (res->flags & IORESOURCE_IO)
310 if (res->flags & IORESOURCE_MEM)
311 cmd |= PCI_COMMAND_MEMORY;
313 pci_write_config_word(pdev, PCI_COMMAND, cmd);
317 void pcibios_disable_device(struct pci_dev *pdev)
319 zpci_remove_device(pdev);
320 pdev->sysdata = NULL;
323 static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
324 unsigned long flags, int domain)
330 r = kzalloc(sizeof(*r), GFP_KERNEL);
332 return ERR_PTR(-ENOMEM);
334 r->end = r->start + size - 1;
336 r->parent = &iomem_resource;
337 name = kmalloc(18, GFP_KERNEL);
340 return ERR_PTR(-ENOMEM);
342 sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
345 rc = request_resource(&iomem_resource, r);
347 pr_debug("request resource %pR failed\n", r);
351 static int zpci_alloc_iomap(struct zpci_dev *zdev)
355 spin_lock(&zpci_iomap_lock);
356 entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
357 if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
358 spin_unlock(&zpci_iomap_lock);
361 set_bit(entry, zpci_iomap);
362 spin_unlock(&zpci_iomap_lock);
366 static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
368 spin_lock(&zpci_iomap_lock);
369 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
370 clear_bit(entry, zpci_iomap);
371 spin_unlock(&zpci_iomap_lock);
374 static int zpci_create_device_bus(struct zpci_dev *zdev)
376 struct resource *res;
377 LIST_HEAD(resources);
380 /* allocate mapping entry for each used bar */
381 for (i = 0; i < PCI_BAR_COUNT; i++) {
382 unsigned long addr, size, flags;
385 if (!zdev->bars[i].size)
387 entry = zpci_alloc_iomap(zdev);
390 zdev->bars[i].map_idx = entry;
392 /* only MMIO is supported */
393 flags = IORESOURCE_MEM;
394 if (zdev->bars[i].val & 8)
395 flags |= IORESOURCE_PREFETCH;
396 if (zdev->bars[i].val & 4)
397 flags |= IORESOURCE_MEM_64;
399 addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
401 size = 1UL << zdev->bars[i].size;
403 res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
405 zpci_free_iomap(zdev, entry);
408 pci_add_resource(&resources, res);
411 zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
416 zdev->bus->max_bus_speed = zdev->max_bus_speed;
420 static int zpci_alloc_domain(struct zpci_dev *zdev)
422 spin_lock(&zpci_domain_lock);
423 zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
424 if (zdev->domain == ZPCI_NR_DEVICES) {
425 spin_unlock(&zpci_domain_lock);
428 set_bit(zdev->domain, zpci_domain);
429 spin_unlock(&zpci_domain_lock);
433 static void zpci_free_domain(struct zpci_dev *zdev)
435 spin_lock(&zpci_domain_lock);
436 clear_bit(zdev->domain, zpci_domain);
437 spin_unlock(&zpci_domain_lock);
440 int zpci_enable_device(struct zpci_dev *zdev)
444 rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
447 pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
452 EXPORT_SYMBOL_GPL(zpci_enable_device);
454 int zpci_create_device(struct zpci_dev *zdev)
458 rc = zpci_alloc_domain(zdev);
462 rc = zpci_create_device_bus(zdev);
466 mutex_lock(&zpci_list_lock);
467 list_add_tail(&zdev->entry, &zpci_list);
468 mutex_unlock(&zpci_list_lock);
470 if (zdev->state == ZPCI_FN_STATE_STANDBY)
473 rc = zpci_enable_device(zdev);
479 mutex_lock(&zpci_list_lock);
480 list_del(&zdev->entry);
481 mutex_unlock(&zpci_list_lock);
483 zpci_free_domain(zdev);
488 void zpci_stop_device(struct zpci_dev *zdev)
491 * Note: SCLP disables fh via set-pci-fn so don't
495 EXPORT_SYMBOL_GPL(zpci_stop_device);
497 int zpci_scan_device(struct zpci_dev *zdev)
499 zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN);
501 pr_err("pci_scan_single_device failed for fid: 0x%x\n",
506 zpci_map_resources(zdev);
507 pci_bus_add_devices(zdev->bus);
509 /* now that pdev was added to the bus mark it as used */
510 zdev->state = ZPCI_FN_STATE_ONLINE;
514 clp_disable_fh(zdev);
517 EXPORT_SYMBOL_GPL(zpci_scan_device);
519 static inline int barsize(u8 size)
521 return (size) ? (1 << size) >> 10 : 0;
524 static int zpci_mem_init(void)
526 /* TODO: use realloc */
527 zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
529 if (!zpci_iomap_start)
537 static void zpci_mem_exit(void)
539 kfree(zpci_iomap_start);
542 unsigned int pci_probe = 1;
543 EXPORT_SYMBOL_GPL(pci_probe);
545 char * __init pcibios_setup(char *str)
547 if (!strcmp(str, "off")) {
554 static int __init pci_base_init(void)
561 if (!test_facility(2) || !test_facility(69)
562 || !test_facility(71) || !test_facility(72))
565 pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n",
566 test_facility(69), test_facility(70),
569 rc = zpci_mem_init();
573 rc = clp_find_pci_devices();
585 subsys_initcall(pci_base_init);