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