~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/s390/pci/pci.c

Version: ~ [ linux-5.6-rc3 ] ~ [ linux-5.5.6 ] ~ [ linux-5.4.22 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.106 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.171 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.214 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.214 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.82 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright IBM Corp. 2012
  3  *
  4  * Author(s):
  5  *   Jan Glauber <jang@linux.vnet.ibm.com>
  6  *
  7  * The System z PCI code is a rewrite from a prototype by
  8  * the following people (Kudoz!):
  9  *   Alexander Schmidt
 10  *   Christoph Raisch
 11  *   Hannes Hering
 12  *   Hoang-Nam Nguyen
 13  *   Jan-Bernd Themann
 14  *   Stefan Roscher
 15  *   Thomas Klein
 16  */
 17 
 18 #define COMPONENT "zPCI"
 19 #define pr_fmt(fmt) COMPONENT ": " fmt
 20 
 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/irq.h>
 27 #include <linux/kernel_stat.h>
 28 #include <linux/seq_file.h>
 29 #include <linux/pci.h>
 30 #include <linux/msi.h>
 31 
 32 #include <asm/isc.h>
 33 #include <asm/airq.h>
 34 #include <asm/facility.h>
 35 #include <asm/pci_insn.h>
 36 #include <asm/pci_clp.h>
 37 #include <asm/pci_dma.h>
 38 
 39 #define DEBUG                           /* enable pr_debug */
 40 
 41 #define SIC_IRQ_MODE_ALL                0
 42 #define SIC_IRQ_MODE_SINGLE             1
 43 
 44 #define ZPCI_NR_DMA_SPACES              1
 45 #define ZPCI_MSI_VEC_BITS               6
 46 #define ZPCI_NR_DEVICES                 CONFIG_PCI_NR_FUNCTIONS
 47 
 48 /* list of all detected zpci devices */
 49 LIST_HEAD(zpci_list);
 50 EXPORT_SYMBOL_GPL(zpci_list);
 51 DEFINE_MUTEX(zpci_list_lock);
 52 EXPORT_SYMBOL_GPL(zpci_list_lock);
 53 
 54 static struct pci_hp_callback_ops *hotplug_ops;
 55 
 56 static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
 57 static DEFINE_SPINLOCK(zpci_domain_lock);
 58 
 59 struct callback {
 60         irq_handler_t   handler;
 61         void            *data;
 62 };
 63 
 64 struct zdev_irq_map {
 65         unsigned long   aibv;           /* AI bit vector */
 66         int             msi_vecs;       /* consecutive MSI-vectors used */
 67         int             __unused;
 68         struct callback cb[ZPCI_NR_MSI_VECS]; /* callback handler array */
 69         spinlock_t      lock;           /* protect callbacks against de-reg */
 70 };
 71 
 72 struct intr_bucket {
 73         /* amap of adapters, one bit per dev, corresponds to one irq nr */
 74         unsigned long   *alloc;
 75         /* AI summary bit, global page for all devices */
 76         unsigned long   *aisb;
 77         /* pointer to aibv and callback data in zdev */
 78         struct zdev_irq_map *imap[ZPCI_NR_DEVICES];
 79         /* protects the whole bucket struct */
 80         spinlock_t      lock;
 81 };
 82 
 83 static struct intr_bucket *bucket;
 84 
 85 /* Adapter local summary indicator */
 86 static u8 *zpci_irq_si;
 87 
 88 static atomic_t irq_retries = ATOMIC_INIT(0);
 89 
 90 /* I/O Map */
 91 static DEFINE_SPINLOCK(zpci_iomap_lock);
 92 static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
 93 struct zpci_iomap_entry *zpci_iomap_start;
 94 EXPORT_SYMBOL_GPL(zpci_iomap_start);
 95 
 96 /* highest irq summary bit */
 97 static int __read_mostly aisb_max;
 98 
 99 static struct kmem_cache *zdev_irq_cache;
100 static struct kmem_cache *zdev_fmb_cache;
101 
102 debug_info_t *pci_debug_msg_id;
103 debug_info_t *pci_debug_err_id;
104 
105 static inline int irq_to_msi_nr(unsigned int irq)
106 {
107         return irq & ZPCI_MSI_MASK;
108 }
109 
110 static inline int irq_to_dev_nr(unsigned int irq)
111 {
112         return irq >> ZPCI_MSI_VEC_BITS;
113 }
114 
115 static inline struct zdev_irq_map *get_imap(unsigned int irq)
116 {
117         return bucket->imap[irq_to_dev_nr(irq)];
118 }
119 
120 struct zpci_dev *get_zdev(struct pci_dev *pdev)
121 {
122         return (struct zpci_dev *) pdev->sysdata;
123 }
124 
125 struct zpci_dev *get_zdev_by_fid(u32 fid)
126 {
127         struct zpci_dev *tmp, *zdev = NULL;
128 
129         mutex_lock(&zpci_list_lock);
130         list_for_each_entry(tmp, &zpci_list, entry) {
131                 if (tmp->fid == fid) {
132                         zdev = tmp;
133                         break;
134                 }
135         }
136         mutex_unlock(&zpci_list_lock);
137         return zdev;
138 }
139 
140 bool zpci_fid_present(u32 fid)
141 {
142         return (get_zdev_by_fid(fid) != NULL) ? true : false;
143 }
144 
145 static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
146 {
147         return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
148 }
149 
150 int pci_domain_nr(struct pci_bus *bus)
151 {
152         return ((struct zpci_dev *) bus->sysdata)->domain;
153 }
154 EXPORT_SYMBOL_GPL(pci_domain_nr);
155 
156 int pci_proc_domain(struct pci_bus *bus)
157 {
158         return pci_domain_nr(bus);
159 }
160 EXPORT_SYMBOL_GPL(pci_proc_domain);
161 
162 /* Modify PCI: Register adapter interruptions */
163 static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb,
164                               u64 aibv)
165 {
166         u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
167         struct zpci_fib *fib;
168         int rc;
169 
170         fib = (void *) get_zeroed_page(GFP_KERNEL);
171         if (!fib)
172                 return -ENOMEM;
173 
174         fib->isc = PCI_ISC;
175         fib->noi = zdev->irq_map->msi_vecs;
176         fib->sum = 1;           /* enable summary notifications */
177         fib->aibv = aibv;
178         fib->aibvo = 0;         /* every function has its own page */
179         fib->aisb = (u64) bucket->aisb + aisb / 8;
180         fib->aisbo = aisb & ZPCI_MSI_MASK;
181 
182         rc = mpcifc_instr(req, fib);
183         pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi);
184 
185         free_page((unsigned long) fib);
186         return rc;
187 }
188 
189 struct mod_pci_args {
190         u64 base;
191         u64 limit;
192         u64 iota;
193         u64 fmb_addr;
194 };
195 
196 static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
197 {
198         u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
199         struct zpci_fib *fib;
200         int rc;
201 
202         /* The FIB must be available even if it's not used */
203         fib = (void *) get_zeroed_page(GFP_KERNEL);
204         if (!fib)
205                 return -ENOMEM;
206 
207         fib->pba = args->base;
208         fib->pal = args->limit;
209         fib->iota = args->iota;
210         fib->fmb_addr = args->fmb_addr;
211 
212         rc = mpcifc_instr(req, fib);
213         free_page((unsigned long) fib);
214         return rc;
215 }
216 
217 /* Modify PCI: Register I/O address translation parameters */
218 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
219                        u64 base, u64 limit, u64 iota)
220 {
221         struct mod_pci_args args = { base, limit, iota, 0 };
222 
223         WARN_ON_ONCE(iota & 0x3fff);
224         args.iota |= ZPCI_IOTA_RTTO_FLAG;
225         return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
226 }
227 
228 /* Modify PCI: Unregister I/O address translation parameters */
229 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
230 {
231         struct mod_pci_args args = { 0, 0, 0, 0 };
232 
233         return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
234 }
235 
236 /* Modify PCI: Unregister adapter interruptions */
237 static int zpci_unregister_airq(struct zpci_dev *zdev)
238 {
239         struct mod_pci_args args = { 0, 0, 0, 0 };
240 
241         return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
242 }
243 
244 /* Modify PCI: Set PCI function measurement parameters */
245 int zpci_fmb_enable_device(struct zpci_dev *zdev)
246 {
247         struct mod_pci_args args = { 0, 0, 0, 0 };
248 
249         if (zdev->fmb)
250                 return -EINVAL;
251 
252         zdev->fmb = kmem_cache_alloc(zdev_fmb_cache, GFP_KERNEL);
253         if (!zdev->fmb)
254                 return -ENOMEM;
255         memset(zdev->fmb, 0, sizeof(*zdev->fmb));
256         WARN_ON((u64) zdev->fmb & 0xf);
257 
258         args.fmb_addr = virt_to_phys(zdev->fmb);
259         return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
260 }
261 
262 /* Modify PCI: Disable PCI function measurement */
263 int zpci_fmb_disable_device(struct zpci_dev *zdev)
264 {
265         struct mod_pci_args args = { 0, 0, 0, 0 };
266         int rc;
267 
268         if (!zdev->fmb)
269                 return -EINVAL;
270 
271         /* Function measurement is disabled if fmb address is zero */
272         rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
273 
274         kmem_cache_free(zdev_fmb_cache, zdev->fmb);
275         zdev->fmb = NULL;
276         return rc;
277 }
278 
279 #define ZPCI_PCIAS_CFGSPC       15
280 
281 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
282 {
283         u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
284         u64 data;
285         int rc;
286 
287         rc = pcilg_instr(&data, req, offset);
288         data = data << ((8 - len) * 8);
289         data = le64_to_cpu(data);
290         if (!rc)
291                 *val = (u32) data;
292         else
293                 *val = 0xffffffff;
294         return rc;
295 }
296 
297 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
298 {
299         u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
300         u64 data = val;
301         int rc;
302 
303         data = cpu_to_le64(data);
304         data = data >> ((8 - len) * 8);
305         rc = pcistg_instr(data, req, offset);
306         return rc;
307 }
308 
309 void enable_irq(unsigned int irq)
310 {
311         struct msi_desc *msi = irq_get_msi_desc(irq);
312 
313         zpci_msi_set_mask_bits(msi, 1, 0);
314 }
315 EXPORT_SYMBOL_GPL(enable_irq);
316 
317 void disable_irq(unsigned int irq)
318 {
319         struct msi_desc *msi = irq_get_msi_desc(irq);
320 
321         zpci_msi_set_mask_bits(msi, 1, 1);
322 }
323 EXPORT_SYMBOL_GPL(disable_irq);
324 
325 void pcibios_fixup_bus(struct pci_bus *bus)
326 {
327 }
328 
329 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
330                                        resource_size_t size,
331                                        resource_size_t align)
332 {
333         return 0;
334 }
335 
336 /* combine single writes by using store-block insn */
337 void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
338 {
339        zpci_memcpy_toio(to, from, count);
340 }
341 
342 /* Create a virtual mapping cookie for a PCI BAR */
343 void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
344 {
345         struct zpci_dev *zdev = get_zdev(pdev);
346         u64 addr;
347         int idx;
348 
349         if ((bar & 7) != bar)
350                 return NULL;
351 
352         idx = zdev->bars[bar].map_idx;
353         spin_lock(&zpci_iomap_lock);
354         zpci_iomap_start[idx].fh = zdev->fh;
355         zpci_iomap_start[idx].bar = bar;
356         spin_unlock(&zpci_iomap_lock);
357 
358         addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
359         return (void __iomem *) addr;
360 }
361 EXPORT_SYMBOL_GPL(pci_iomap);
362 
363 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
364 {
365         unsigned int idx;
366 
367         idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
368         spin_lock(&zpci_iomap_lock);
369         zpci_iomap_start[idx].fh = 0;
370         zpci_iomap_start[idx].bar = 0;
371         spin_unlock(&zpci_iomap_lock);
372 }
373 EXPORT_SYMBOL_GPL(pci_iounmap);
374 
375 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
376                     int size, u32 *val)
377 {
378         struct zpci_dev *zdev = get_zdev_by_bus(bus);
379 
380         if (!zdev || devfn != ZPCI_DEVFN)
381                 return 0;
382         return zpci_cfg_load(zdev, where, val, size);
383 }
384 
385 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
386                      int size, u32 val)
387 {
388         struct zpci_dev *zdev = get_zdev_by_bus(bus);
389 
390         if (!zdev || devfn != ZPCI_DEVFN)
391                 return 0;
392         return zpci_cfg_store(zdev, where, val, size);
393 }
394 
395 static struct pci_ops pci_root_ops = {
396         .read = pci_read,
397         .write = pci_write,
398 };
399 
400 /* store the last handled bit to implement fair scheduling of devices */
401 static DEFINE_PER_CPU(unsigned long, next_sbit);
402 
403 static void zpci_irq_handler(void *dont, void *need)
404 {
405         unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit);
406         int rescan = 0, max = aisb_max;
407         struct zdev_irq_map *imap;
408 
409         inc_irq_stat(IRQIO_PCI);
410         sbit = start;
411 
412 scan:
413         /* find summary_bit */
414         for_each_set_bit_left_cont(sbit, bucket->aisb, max) {
415                 clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6));
416                 last = sbit;
417 
418                 /* find vector bit */
419                 imap = bucket->imap[sbit];
420                 for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) {
421                         inc_irq_stat(IRQIO_MSI);
422                         clear_bit(63 - mbit, &imap->aibv);
423 
424                         spin_lock(&imap->lock);
425                         if (imap->cb[mbit].handler)
426                                 imap->cb[mbit].handler(mbit,
427                                         imap->cb[mbit].data);
428                         spin_unlock(&imap->lock);
429                 }
430         }
431 
432         if (rescan)
433                 goto out;
434 
435         /* scan the skipped bits */
436         if (start > 0) {
437                 sbit = 0;
438                 max = start;
439                 start = 0;
440                 goto scan;
441         }
442 
443         /* enable interrupts again */
444         sic_instr(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
445 
446         /* check again to not lose initiative */
447         rmb();
448         max = aisb_max;
449         sbit = find_first_bit_left(bucket->aisb, max);
450         if (sbit != max) {
451                 atomic_inc(&irq_retries);
452                 rescan++;
453                 goto scan;
454         }
455 out:
456         /* store next device bit to scan */
457         __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last;
458 }
459 
460 /* msi_vecs - number of requested interrupts, 0 place function to error state */
461 static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs)
462 {
463         struct zpci_dev *zdev = get_zdev(pdev);
464         unsigned int aisb, msi_nr;
465         struct msi_desc *msi;
466         int rc;
467 
468         /* store the number of used MSI vectors */
469         zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS);
470 
471         spin_lock(&bucket->lock);
472         aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE);
473         /* alloc map exhausted? */
474         if (aisb == PAGE_SIZE) {
475                 spin_unlock(&bucket->lock);
476                 return -EIO;
477         }
478         set_bit(aisb, bucket->alloc);
479         spin_unlock(&bucket->lock);
480 
481         zdev->aisb = aisb;
482         if (aisb + 1 > aisb_max)
483                 aisb_max = aisb + 1;
484 
485         /* wire up IRQ shortcut pointer */
486         bucket->imap[zdev->aisb] = zdev->irq_map;
487         pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map);
488 
489         /* TODO: irq number 0 wont be found if we return less than requested MSIs.
490          * ignore it for now and fix in common code.
491          */
492         msi_nr = aisb << ZPCI_MSI_VEC_BITS;
493 
494         list_for_each_entry(msi, &pdev->msi_list, list) {
495                 rc = zpci_setup_msi_irq(zdev, msi, msi_nr,
496                                           aisb << ZPCI_MSI_VEC_BITS);
497                 if (rc)
498                         return rc;
499                 msi_nr++;
500         }
501 
502         rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv);
503         if (rc) {
504                 clear_bit(aisb, bucket->alloc);
505                 dev_err(&pdev->dev, "register MSI failed with: %d\n", rc);
506                 return rc;
507         }
508         return (zdev->irq_map->msi_vecs == msi_vecs) ?
509                 0 : zdev->irq_map->msi_vecs;
510 }
511 
512 static void zpci_teardown_msi(struct pci_dev *pdev)
513 {
514         struct zpci_dev *zdev = get_zdev(pdev);
515         struct msi_desc *msi;
516         int aisb, rc;
517 
518         rc = zpci_unregister_airq(zdev);
519         if (rc) {
520                 dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc);
521                 return;
522         }
523 
524         msi = list_first_entry(&pdev->msi_list, struct msi_desc, list);
525         aisb = irq_to_dev_nr(msi->irq);
526 
527         list_for_each_entry(msi, &pdev->msi_list, list)
528                 zpci_teardown_msi_irq(zdev, msi);
529 
530         clear_bit(aisb, bucket->alloc);
531         if (aisb + 1 == aisb_max)
532                 aisb_max--;
533 }
534 
535 int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
536 {
537         pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec);
538         if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI)
539                 return -EINVAL;
540         return zpci_setup_msi(pdev, nvec);
541 }
542 
543 void arch_teardown_msi_irqs(struct pci_dev *pdev)
544 {
545         pr_info("%s: on pdev: %p\n", __func__, pdev);
546         zpci_teardown_msi(pdev);
547 }
548 
549 static void zpci_map_resources(struct zpci_dev *zdev)
550 {
551         struct pci_dev *pdev = zdev->pdev;
552         resource_size_t len;
553         int i;
554 
555         for (i = 0; i < PCI_BAR_COUNT; i++) {
556                 len = pci_resource_len(pdev, i);
557                 if (!len)
558                         continue;
559                 pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
560                 pdev->resource[i].end = pdev->resource[i].start + len - 1;
561                 pr_debug("BAR%i: -> start: %Lx  end: %Lx\n",
562                         i, pdev->resource[i].start, pdev->resource[i].end);
563         }
564 };
565 
566 static void zpci_unmap_resources(struct pci_dev *pdev)
567 {
568         resource_size_t len;
569         int i;
570 
571         for (i = 0; i < PCI_BAR_COUNT; i++) {
572                 len = pci_resource_len(pdev, i);
573                 if (!len)
574                         continue;
575                 pci_iounmap(pdev, (void *) pdev->resource[i].start);
576         }
577 };
578 
579 struct zpci_dev *zpci_alloc_device(void)
580 {
581         struct zpci_dev *zdev;
582 
583         /* Alloc memory for our private pci device data */
584         zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
585         if (!zdev)
586                 return ERR_PTR(-ENOMEM);
587 
588         /* Alloc aibv & callback space */
589         zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL);
590         if (!zdev->irq_map)
591                 goto error;
592         WARN_ON((u64) zdev->irq_map & 0xff);
593         return zdev;
594 
595 error:
596         kfree(zdev);
597         return ERR_PTR(-ENOMEM);
598 }
599 
600 void zpci_free_device(struct zpci_dev *zdev)
601 {
602         kmem_cache_free(zdev_irq_cache, zdev->irq_map);
603         kfree(zdev);
604 }
605 
606 /* Called on removal of pci_dev, leaves zpci and bus device */
607 static void zpci_remove_device(struct pci_dev *pdev)
608 {
609         struct zpci_dev *zdev = get_zdev(pdev);
610 
611         dev_info(&pdev->dev, "Removing device %u\n", zdev->domain);
612         zdev->state = ZPCI_FN_STATE_CONFIGURED;
613         zpci_dma_exit_device(zdev);
614         zpci_fmb_disable_device(zdev);
615         zpci_sysfs_remove_device(&pdev->dev);
616         zpci_unmap_resources(pdev);
617         list_del(&zdev->entry);         /* can be called from init */
618         zdev->pdev = NULL;
619 }
620 
621 static void zpci_scan_devices(void)
622 {
623         struct zpci_dev *zdev;
624 
625         mutex_lock(&zpci_list_lock);
626         list_for_each_entry(zdev, &zpci_list, entry)
627                 if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
628                         zpci_scan_device(zdev);
629         mutex_unlock(&zpci_list_lock);
630 }
631 
632 /*
633  * Too late for any s390 specific setup, since interrupts must be set up
634  * already which requires DMA setup too and the pci scan will access the
635  * config space, which only works if the function handle is enabled.
636  */
637 int pcibios_enable_device(struct pci_dev *pdev, int mask)
638 {
639         struct resource *res;
640         u16 cmd;
641         int i;
642 
643         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
644 
645         for (i = 0; i < PCI_BAR_COUNT; i++) {
646                 res = &pdev->resource[i];
647 
648                 if (res->flags & IORESOURCE_IO)
649                         return -EINVAL;
650 
651                 if (res->flags & IORESOURCE_MEM)
652                         cmd |= PCI_COMMAND_MEMORY;
653         }
654         pci_write_config_word(pdev, PCI_COMMAND, cmd);
655         return 0;
656 }
657 
658 void pcibios_disable_device(struct pci_dev *pdev)
659 {
660         zpci_remove_device(pdev);
661         pdev->sysdata = NULL;
662 }
663 
664 int pcibios_add_platform_entries(struct pci_dev *pdev)
665 {
666         return zpci_sysfs_add_device(&pdev->dev);
667 }
668 
669 int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data)
670 {
671         int msi_nr = irq_to_msi_nr(irq);
672         struct zdev_irq_map *imap;
673         struct msi_desc *msi;
674 
675         msi = irq_get_msi_desc(irq);
676         if (!msi)
677                 return -EIO;
678 
679         imap = get_imap(irq);
680         spin_lock_init(&imap->lock);
681 
682         pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr);
683         imap->cb[msi_nr].handler = handler;
684         imap->cb[msi_nr].data = data;
685 
686         /*
687          * The generic MSI code returns with the interrupt disabled on the
688          * card, using the MSI mask bits. Firmware doesn't appear to unmask
689          * at that level, so we do it here by hand.
690          */
691         zpci_msi_set_mask_bits(msi, 1, 0);
692         return 0;
693 }
694 
695 void zpci_free_irq(unsigned int irq)
696 {
697         struct zdev_irq_map *imap = get_imap(irq);
698         int msi_nr = irq_to_msi_nr(irq);
699         unsigned long flags;
700 
701         pr_debug("%s: for irq: %d\n", __func__, irq);
702 
703         spin_lock_irqsave(&imap->lock, flags);
704         imap->cb[msi_nr].handler = NULL;
705         imap->cb[msi_nr].data = NULL;
706         spin_unlock_irqrestore(&imap->lock, flags);
707 }
708 
709 int request_irq(unsigned int irq, irq_handler_t handler,
710                 unsigned long irqflags, const char *devname, void *dev_id)
711 {
712         pr_debug("%s: irq: %d  handler: %p  flags: %lx  dev: %s\n",
713                 __func__, irq, handler, irqflags, devname);
714 
715         return zpci_request_irq(irq, handler, dev_id);
716 }
717 EXPORT_SYMBOL_GPL(request_irq);
718 
719 void free_irq(unsigned int irq, void *dev_id)
720 {
721         zpci_free_irq(irq);
722 }
723 EXPORT_SYMBOL_GPL(free_irq);
724 
725 static int __init zpci_irq_init(void)
726 {
727         int cpu, rc;
728 
729         bucket = kzalloc(sizeof(*bucket), GFP_KERNEL);
730         if (!bucket)
731                 return -ENOMEM;
732 
733         bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL);
734         if (!bucket->aisb) {
735                 rc = -ENOMEM;
736                 goto out_aisb;
737         }
738 
739         bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL);
740         if (!bucket->alloc) {
741                 rc = -ENOMEM;
742                 goto out_alloc;
743         }
744 
745         isc_register(PCI_ISC);
746         zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC);
747         if (IS_ERR(zpci_irq_si)) {
748                 rc = PTR_ERR(zpci_irq_si);
749                 zpci_irq_si = NULL;
750                 goto out_ai;
751         }
752 
753         for_each_online_cpu(cpu)
754                 per_cpu(next_sbit, cpu) = 0;
755 
756         spin_lock_init(&bucket->lock);
757         /* set summary to 1 to be called every time for the ISC */
758         *zpci_irq_si = 1;
759         sic_instr(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
760         return 0;
761 
762 out_ai:
763         isc_unregister(PCI_ISC);
764         free_page((unsigned long) bucket->alloc);
765 out_alloc:
766         free_page((unsigned long) bucket->aisb);
767 out_aisb:
768         kfree(bucket);
769         return rc;
770 }
771 
772 static void zpci_irq_exit(void)
773 {
774         free_page((unsigned long) bucket->alloc);
775         free_page((unsigned long) bucket->aisb);
776         s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC);
777         isc_unregister(PCI_ISC);
778         kfree(bucket);
779 }
780 
781 void zpci_debug_info(struct zpci_dev *zdev, struct seq_file *m)
782 {
783         if (!zdev)
784                 return;
785 
786         seq_printf(m, "global irq retries: %u\n", atomic_read(&irq_retries));
787         seq_printf(m, "aibv[0]:%016lx  aibv[1]:%016lx  aisb:%016lx\n",
788                    get_imap(0)->aibv, get_imap(1)->aibv, *bucket->aisb);
789 }
790 
791 static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
792                                                 unsigned long flags, int domain)
793 {
794         struct resource *r;
795         char *name;
796         int rc;
797 
798         r = kzalloc(sizeof(*r), GFP_KERNEL);
799         if (!r)
800                 return ERR_PTR(-ENOMEM);
801         r->start = start;
802         r->end = r->start + size - 1;
803         r->flags = flags;
804         r->parent = &iomem_resource;
805         name = kmalloc(18, GFP_KERNEL);
806         if (!name) {
807                 kfree(r);
808                 return ERR_PTR(-ENOMEM);
809         }
810         sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
811         r->name = name;
812 
813         rc = request_resource(&iomem_resource, r);
814         if (rc)
815                 pr_debug("request resource %pR failed\n", r);
816         return r;
817 }
818 
819 static int zpci_alloc_iomap(struct zpci_dev *zdev)
820 {
821         int entry;
822 
823         spin_lock(&zpci_iomap_lock);
824         entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
825         if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
826                 spin_unlock(&zpci_iomap_lock);
827                 return -ENOSPC;
828         }
829         set_bit(entry, zpci_iomap);
830         spin_unlock(&zpci_iomap_lock);
831         return entry;
832 }
833 
834 static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
835 {
836         spin_lock(&zpci_iomap_lock);
837         memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
838         clear_bit(entry, zpci_iomap);
839         spin_unlock(&zpci_iomap_lock);
840 }
841 
842 static int zpci_create_device_bus(struct zpci_dev *zdev)
843 {
844         struct resource *res;
845         LIST_HEAD(resources);
846         int i;
847 
848         /* allocate mapping entry for each used bar */
849         for (i = 0; i < PCI_BAR_COUNT; i++) {
850                 unsigned long addr, size, flags;
851                 int entry;
852 
853                 if (!zdev->bars[i].size)
854                         continue;
855                 entry = zpci_alloc_iomap(zdev);
856                 if (entry < 0)
857                         return entry;
858                 zdev->bars[i].map_idx = entry;
859 
860                 /* only MMIO is supported */
861                 flags = IORESOURCE_MEM;
862                 if (zdev->bars[i].val & 8)
863                         flags |= IORESOURCE_PREFETCH;
864                 if (zdev->bars[i].val & 4)
865                         flags |= IORESOURCE_MEM_64;
866 
867                 addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
868 
869                 size = 1UL << zdev->bars[i].size;
870 
871                 res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
872                 if (IS_ERR(res)) {
873                         zpci_free_iomap(zdev, entry);
874                         return PTR_ERR(res);
875                 }
876                 pci_add_resource(&resources, res);
877         }
878 
879         zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
880                                         zdev, &resources);
881         if (!zdev->bus)
882                 return -EIO;
883 
884         zdev->bus->max_bus_speed = zdev->max_bus_speed;
885         return 0;
886 }
887 
888 static int zpci_alloc_domain(struct zpci_dev *zdev)
889 {
890         spin_lock(&zpci_domain_lock);
891         zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
892         if (zdev->domain == ZPCI_NR_DEVICES) {
893                 spin_unlock(&zpci_domain_lock);
894                 return -ENOSPC;
895         }
896         set_bit(zdev->domain, zpci_domain);
897         spin_unlock(&zpci_domain_lock);
898         return 0;
899 }
900 
901 static void zpci_free_domain(struct zpci_dev *zdev)
902 {
903         spin_lock(&zpci_domain_lock);
904         clear_bit(zdev->domain, zpci_domain);
905         spin_unlock(&zpci_domain_lock);
906 }
907 
908 int zpci_enable_device(struct zpci_dev *zdev)
909 {
910         int rc;
911 
912         rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
913         if (rc)
914                 goto out;
915         pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
916 
917         rc = zpci_dma_init_device(zdev);
918         if (rc)
919                 goto out_dma;
920         return 0;
921 
922 out_dma:
923         clp_disable_fh(zdev);
924 out:
925         return rc;
926 }
927 EXPORT_SYMBOL_GPL(zpci_enable_device);
928 
929 int zpci_create_device(struct zpci_dev *zdev)
930 {
931         int rc;
932 
933         rc = zpci_alloc_domain(zdev);
934         if (rc)
935                 goto out;
936 
937         rc = zpci_create_device_bus(zdev);
938         if (rc)
939                 goto out_bus;
940 
941         mutex_lock(&zpci_list_lock);
942         list_add_tail(&zdev->entry, &zpci_list);
943         if (hotplug_ops)
944                 hotplug_ops->create_slot(zdev);
945         mutex_unlock(&zpci_list_lock);
946 
947         if (zdev->state == ZPCI_FN_STATE_STANDBY)
948                 return 0;
949 
950         rc = zpci_enable_device(zdev);
951         if (rc)
952                 goto out_start;
953         return 0;
954 
955 out_start:
956         mutex_lock(&zpci_list_lock);
957         list_del(&zdev->entry);
958         if (hotplug_ops)
959                 hotplug_ops->remove_slot(zdev);
960         mutex_unlock(&zpci_list_lock);
961 out_bus:
962         zpci_free_domain(zdev);
963 out:
964         return rc;
965 }
966 
967 void zpci_stop_device(struct zpci_dev *zdev)
968 {
969         zpci_dma_exit_device(zdev);
970         /*
971          * Note: SCLP disables fh via set-pci-fn so don't
972          * do that here.
973          */
974 }
975 EXPORT_SYMBOL_GPL(zpci_stop_device);
976 
977 int zpci_scan_device(struct zpci_dev *zdev)
978 {
979         zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN);
980         if (!zdev->pdev) {
981                 pr_err("pci_scan_single_device failed for fid: 0x%x\n",
982                         zdev->fid);
983                 goto out;
984         }
985 
986         zpci_debug_init_device(zdev);
987         zpci_fmb_enable_device(zdev);
988         zpci_map_resources(zdev);
989         pci_bus_add_devices(zdev->bus);
990 
991         /* now that pdev was added to the bus mark it as used */
992         zdev->state = ZPCI_FN_STATE_ONLINE;
993         return 0;
994 
995 out:
996         zpci_dma_exit_device(zdev);
997         clp_disable_fh(zdev);
998         return -EIO;
999 }
1000 EXPORT_SYMBOL_GPL(zpci_scan_device);
1001 
1002 static inline int barsize(u8 size)
1003 {
1004         return (size) ? (1 << size) >> 10 : 0;
1005 }
1006 
1007 static int zpci_mem_init(void)
1008 {
1009         zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map),
1010                                 L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL);
1011         if (!zdev_irq_cache)
1012                 goto error_zdev;
1013 
1014         zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
1015                                 16, 0, NULL);
1016         if (!zdev_fmb_cache)
1017                 goto error_fmb;
1018 
1019         /* TODO: use realloc */
1020         zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
1021                                    GFP_KERNEL);
1022         if (!zpci_iomap_start)
1023                 goto error_iomap;
1024         return 0;
1025 
1026 error_iomap:
1027         kmem_cache_destroy(zdev_fmb_cache);
1028 error_fmb:
1029         kmem_cache_destroy(zdev_irq_cache);
1030 error_zdev:
1031         return -ENOMEM;
1032 }
1033 
1034 static void zpci_mem_exit(void)
1035 {
1036         kfree(zpci_iomap_start);
1037         kmem_cache_destroy(zdev_irq_cache);
1038         kmem_cache_destroy(zdev_fmb_cache);
1039 }
1040 
1041 void zpci_register_hp_ops(struct pci_hp_callback_ops *ops)
1042 {
1043         mutex_lock(&zpci_list_lock);
1044         hotplug_ops = ops;
1045         mutex_unlock(&zpci_list_lock);
1046 }
1047 EXPORT_SYMBOL_GPL(zpci_register_hp_ops);
1048 
1049 void zpci_deregister_hp_ops(void)
1050 {
1051         mutex_lock(&zpci_list_lock);
1052         hotplug_ops = NULL;
1053         mutex_unlock(&zpci_list_lock);
1054 }
1055 EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops);
1056 
1057 unsigned int s390_pci_probe = 1;
1058 EXPORT_SYMBOL_GPL(s390_pci_probe);
1059 
1060 char * __init pcibios_setup(char *str)
1061 {
1062         if (!strcmp(str, "off")) {
1063                 s390_pci_probe = 0;
1064                 return NULL;
1065         }
1066         return str;
1067 }
1068 
1069 static int __init pci_base_init(void)
1070 {
1071         int rc;
1072 
1073         if (!s390_pci_probe)
1074                 return 0;
1075 
1076         if (!test_facility(2) || !test_facility(69)
1077             || !test_facility(71) || !test_facility(72))
1078                 return 0;
1079 
1080         pr_info("Probing PCI hardware: PCI:%d  SID:%d  AEN:%d\n",
1081                 test_facility(69), test_facility(70),
1082                 test_facility(71));
1083 
1084         rc = zpci_debug_init();
1085         if (rc)
1086                 return rc;
1087 
1088         rc = zpci_mem_init();
1089         if (rc)
1090                 goto out_mem;
1091 
1092         rc = zpci_msihash_init();
1093         if (rc)
1094                 goto out_hash;
1095 
1096         rc = zpci_irq_init();
1097         if (rc)
1098                 goto out_irq;
1099 
1100         rc = zpci_dma_init();
1101         if (rc)
1102                 goto out_dma;
1103 
1104         rc = clp_find_pci_devices();
1105         if (rc)
1106                 goto out_find;
1107 
1108         zpci_scan_devices();
1109         return 0;
1110 
1111 out_find:
1112         zpci_dma_exit();
1113 out_dma:
1114         zpci_irq_exit();
1115 out_irq:
1116         zpci_msihash_exit();
1117 out_hash:
1118         zpci_mem_exit();
1119 out_mem:
1120         zpci_debug_exit();
1121         return rc;
1122 }
1123 subsys_initcall(pci_base_init);
1124 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp