blob: a56fce445c391beafeba7ab13049899a1973a999 [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!):
Jan Glauberbedef752012-12-06 14:06:28 +01009 * Alexander Schmidt
10 * Christoph Raisch
11 * Hannes Hering
12 * Hoang-Nam Nguyen
13 * Jan-Bernd Themann
14 * Stefan Roscher
15 * Thomas Klein
Jan Glaubercd248342012-11-29 12:50:30 +010016 */
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 Glauber828b35f2012-11-29 14:33:30 +010037#include <asm/pci_dma.h>
Jan Glaubercd248342012-11-29 12:50:30 +010038
39#define DEBUG /* enable pr_debug */
40
Jan Glauber9a4da8a2012-11-29 13:05:05 +010041#define SIC_IRQ_MODE_ALL 0
42#define SIC_IRQ_MODE_SINGLE 1
43
Jan Glaubercd248342012-11-29 12:50:30 +010044#define ZPCI_NR_DMA_SPACES 1
Jan Glauber9a4da8a2012-11-29 13:05:05 +010045#define ZPCI_MSI_VEC_BITS 6
Jan Glaubercd248342012-11-29 12:50:30 +010046#define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
47
48/* list of all detected zpci devices */
49LIST_HEAD(zpci_list);
Jan Glauber7441b062012-11-29 14:35:47 +010050EXPORT_SYMBOL_GPL(zpci_list);
Jan Glaubercd248342012-11-29 12:50:30 +010051DEFINE_MUTEX(zpci_list_lock);
Jan Glauber7441b062012-11-29 14:35:47 +010052EXPORT_SYMBOL_GPL(zpci_list_lock);
53
Sebastian Ott53923352013-01-31 19:55:17 +010054static struct pci_hp_callback_ops *hotplug_ops;
Jan Glaubercd248342012-11-29 12:50:30 +010055
56static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
57static DEFINE_SPINLOCK(zpci_domain_lock);
58
Jan Glauber9a4da8a2012-11-29 13:05:05 +010059struct callback {
60 irq_handler_t handler;
61 void *data;
62};
63
64struct 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
72struct 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
83static struct intr_bucket *bucket;
84
85/* Adapter local summary indicator */
86static u8 *zpci_irq_si;
87
88static atomic_t irq_retries = ATOMIC_INIT(0);
89
Jan Glaubercd248342012-11-29 12:50:30 +010090/* I/O Map */
91static DEFINE_SPINLOCK(zpci_iomap_lock);
92static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
93struct zpci_iomap_entry *zpci_iomap_start;
94EXPORT_SYMBOL_GPL(zpci_iomap_start);
95
Jan Glauber9a4da8a2012-11-29 13:05:05 +010096/* highest irq summary bit */
97static int __read_mostly aisb_max;
98
99static struct kmem_cache *zdev_irq_cache;
Jan Glauberd0b08852012-12-11 14:53:35 +0100100static struct kmem_cache *zdev_fmb_cache;
101
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100102static inline int irq_to_msi_nr(unsigned int irq)
103{
104 return irq & ZPCI_MSI_MASK;
105}
106
107static inline int irq_to_dev_nr(unsigned int irq)
108{
109 return irq >> ZPCI_MSI_VEC_BITS;
110}
111
112static inline struct zdev_irq_map *get_imap(unsigned int irq)
113{
114 return bucket->imap[irq_to_dev_nr(irq)];
115}
116
Jan Glaubercd248342012-11-29 12:50:30 +0100117struct zpci_dev *get_zdev(struct pci_dev *pdev)
118{
119 return (struct zpci_dev *) pdev->sysdata;
120}
121
122struct zpci_dev *get_zdev_by_fid(u32 fid)
123{
124 struct zpci_dev *tmp, *zdev = NULL;
125
126 mutex_lock(&zpci_list_lock);
127 list_for_each_entry(tmp, &zpci_list, entry) {
128 if (tmp->fid == fid) {
129 zdev = tmp;
130 break;
131 }
132 }
133 mutex_unlock(&zpci_list_lock);
134 return zdev;
135}
136
137bool zpci_fid_present(u32 fid)
138{
139 return (get_zdev_by_fid(fid) != NULL) ? true : false;
140}
141
142static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
143{
144 return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
145}
146
147int pci_domain_nr(struct pci_bus *bus)
148{
149 return ((struct zpci_dev *) bus->sysdata)->domain;
150}
151EXPORT_SYMBOL_GPL(pci_domain_nr);
152
153int pci_proc_domain(struct pci_bus *bus)
154{
155 return pci_domain_nr(bus);
156}
157EXPORT_SYMBOL_GPL(pci_proc_domain);
158
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100159/* Modify PCI: Register adapter interruptions */
160static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb,
161 u64 aibv)
162{
163 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
164 struct zpci_fib *fib;
165 int rc;
166
167 fib = (void *) get_zeroed_page(GFP_KERNEL);
168 if (!fib)
169 return -ENOMEM;
170
171 fib->isc = PCI_ISC;
172 fib->noi = zdev->irq_map->msi_vecs;
173 fib->sum = 1; /* enable summary notifications */
174 fib->aibv = aibv;
175 fib->aibvo = 0; /* every function has its own page */
176 fib->aisb = (u64) bucket->aisb + aisb / 8;
177 fib->aisbo = aisb & ZPCI_MSI_MASK;
178
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200179 rc = s390pci_mod_fc(req, fib);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100180 pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi);
181
182 free_page((unsigned long) fib);
183 return rc;
184}
185
186struct mod_pci_args {
187 u64 base;
188 u64 limit;
189 u64 iota;
Jan Glauberd0b08852012-12-11 14:53:35 +0100190 u64 fmb_addr;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100191};
192
193static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
194{
195 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
196 struct zpci_fib *fib;
197 int rc;
198
199 /* The FIB must be available even if it's not used */
200 fib = (void *) get_zeroed_page(GFP_KERNEL);
201 if (!fib)
202 return -ENOMEM;
203
204 fib->pba = args->base;
205 fib->pal = args->limit;
206 fib->iota = args->iota;
Jan Glauberd0b08852012-12-11 14:53:35 +0100207 fib->fmb_addr = args->fmb_addr;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100208
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200209 rc = s390pci_mod_fc(req, fib);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100210 free_page((unsigned long) fib);
211 return rc;
212}
213
Jan Glauber828b35f2012-11-29 14:33:30 +0100214/* Modify PCI: Register I/O address translation parameters */
215int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
216 u64 base, u64 limit, u64 iota)
217{
Jan Glauberd0b08852012-12-11 14:53:35 +0100218 struct mod_pci_args args = { base, limit, iota, 0 };
Jan Glauber828b35f2012-11-29 14:33:30 +0100219
220 WARN_ON_ONCE(iota & 0x3fff);
221 args.iota |= ZPCI_IOTA_RTTO_FLAG;
222 return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
223}
224
225/* Modify PCI: Unregister I/O address translation parameters */
226int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
227{
Jan Glauberd0b08852012-12-11 14:53:35 +0100228 struct mod_pci_args args = { 0, 0, 0, 0 };
Jan Glauber828b35f2012-11-29 14:33:30 +0100229
230 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
231}
232
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100233/* Modify PCI: Unregister adapter interruptions */
234static int zpci_unregister_airq(struct zpci_dev *zdev)
235{
Jan Glauberd0b08852012-12-11 14:53:35 +0100236 struct mod_pci_args args = { 0, 0, 0, 0 };
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100237
238 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
239}
240
Jan Glauberd0b08852012-12-11 14:53:35 +0100241/* Modify PCI: Set PCI function measurement parameters */
242int zpci_fmb_enable_device(struct zpci_dev *zdev)
243{
244 struct mod_pci_args args = { 0, 0, 0, 0 };
245
246 if (zdev->fmb)
247 return -EINVAL;
248
Wei Yongjun08b42122013-02-25 22:09:25 +0800249 zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
Jan Glauberd0b08852012-12-11 14:53:35 +0100250 if (!zdev->fmb)
251 return -ENOMEM;
Jan Glauberd0b08852012-12-11 14:53:35 +0100252 WARN_ON((u64) zdev->fmb & 0xf);
253
254 args.fmb_addr = virt_to_phys(zdev->fmb);
255 return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
256}
257
258/* Modify PCI: Disable PCI function measurement */
259int zpci_fmb_disable_device(struct zpci_dev *zdev)
260{
261 struct mod_pci_args args = { 0, 0, 0, 0 };
262 int rc;
263
264 if (!zdev->fmb)
265 return -EINVAL;
266
267 /* Function measurement is disabled if fmb address is zero */
268 rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
269
270 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
271 zdev->fmb = NULL;
272 return rc;
273}
274
Jan Glaubercd248342012-11-29 12:50:30 +0100275#define ZPCI_PCIAS_CFGSPC 15
276
277static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
278{
279 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
280 u64 data;
281 int rc;
282
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200283 rc = s390pci_load(&data, req, offset);
Sebastian Ottb170bad2013-04-16 14:17:15 +0200284 if (!rc) {
285 data = data << ((8 - len) * 8);
286 data = le64_to_cpu(data);
Jan Glaubercd248342012-11-29 12:50:30 +0100287 *val = (u32) data;
Sebastian Ottb170bad2013-04-16 14:17:15 +0200288 } else
Jan Glaubercd248342012-11-29 12:50:30 +0100289 *val = 0xffffffff;
290 return rc;
291}
292
293static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
294{
295 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
296 u64 data = val;
297 int rc;
298
299 data = cpu_to_le64(data);
300 data = data >> ((8 - len) * 8);
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200301 rc = s390pci_store(data, req, offset);
Jan Glaubercd248342012-11-29 12:50:30 +0100302 return rc;
303}
304
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100305void enable_irq(unsigned int irq)
306{
307 struct msi_desc *msi = irq_get_msi_desc(irq);
308
309 zpci_msi_set_mask_bits(msi, 1, 0);
310}
311EXPORT_SYMBOL_GPL(enable_irq);
312
313void disable_irq(unsigned int irq)
314{
315 struct msi_desc *msi = irq_get_msi_desc(irq);
316
317 zpci_msi_set_mask_bits(msi, 1, 1);
318}
319EXPORT_SYMBOL_GPL(disable_irq);
320
Greg Kroah-Hartmanb881bc42012-12-21 14:06:37 -0800321void pcibios_fixup_bus(struct pci_bus *bus)
Jan Glaubercd248342012-11-29 12:50:30 +0100322{
323}
324
325resource_size_t pcibios_align_resource(void *data, const struct resource *res,
326 resource_size_t size,
327 resource_size_t align)
328{
329 return 0;
330}
331
Jan Glauber87bc3592012-12-06 14:30:28 +0100332/* combine single writes by using store-block insn */
333void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
334{
335 zpci_memcpy_toio(to, from, count);
336}
337
Jan Glaubercd248342012-11-29 12:50:30 +0100338/* Create a virtual mapping cookie for a PCI BAR */
339void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
340{
341 struct zpci_dev *zdev = get_zdev(pdev);
342 u64 addr;
343 int idx;
344
345 if ((bar & 7) != bar)
346 return NULL;
347
348 idx = zdev->bars[bar].map_idx;
349 spin_lock(&zpci_iomap_lock);
350 zpci_iomap_start[idx].fh = zdev->fh;
351 zpci_iomap_start[idx].bar = bar;
352 spin_unlock(&zpci_iomap_lock);
353
354 addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
355 return (void __iomem *) addr;
356}
357EXPORT_SYMBOL_GPL(pci_iomap);
358
359void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
360{
361 unsigned int idx;
362
363 idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
364 spin_lock(&zpci_iomap_lock);
365 zpci_iomap_start[idx].fh = 0;
366 zpci_iomap_start[idx].bar = 0;
367 spin_unlock(&zpci_iomap_lock);
368}
369EXPORT_SYMBOL_GPL(pci_iounmap);
370
371static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
372 int size, u32 *val)
373{
374 struct zpci_dev *zdev = get_zdev_by_bus(bus);
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200375 int ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100376
377 if (!zdev || devfn != ZPCI_DEVFN)
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200378 ret = -ENODEV;
379 else
380 ret = zpci_cfg_load(zdev, where, val, size);
381
382 return ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100383}
384
385static 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);
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200389 int ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100390
391 if (!zdev || devfn != ZPCI_DEVFN)
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200392 ret = -ENODEV;
393 else
394 ret = zpci_cfg_store(zdev, where, val, size);
395
396 return ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100397}
398
399static struct pci_ops pci_root_ops = {
400 .read = pci_read,
401 .write = pci_write,
402};
403
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100404/* store the last handled bit to implement fair scheduling of devices */
405static DEFINE_PER_CPU(unsigned long, next_sbit);
406
407static void zpci_irq_handler(void *dont, void *need)
408{
409 unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit);
410 int rescan = 0, max = aisb_max;
411 struct zdev_irq_map *imap;
412
Heiko Carstens420f42e2013-01-02 15:18:18 +0100413 inc_irq_stat(IRQIO_PCI);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100414 sbit = start;
415
416scan:
417 /* find summary_bit */
418 for_each_set_bit_left_cont(sbit, bucket->aisb, max) {
419 clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6));
420 last = sbit;
421
422 /* find vector bit */
423 imap = bucket->imap[sbit];
424 for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) {
Heiko Carstens420f42e2013-01-02 15:18:18 +0100425 inc_irq_stat(IRQIO_MSI);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100426 clear_bit(63 - mbit, &imap->aibv);
427
428 spin_lock(&imap->lock);
429 if (imap->cb[mbit].handler)
430 imap->cb[mbit].handler(mbit,
431 imap->cb[mbit].data);
432 spin_unlock(&imap->lock);
433 }
434 }
435
436 if (rescan)
437 goto out;
438
439 /* scan the skipped bits */
440 if (start > 0) {
441 sbit = 0;
442 max = start;
443 start = 0;
444 goto scan;
445 }
446
447 /* enable interrupts again */
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200448 set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100449
450 /* check again to not lose initiative */
451 rmb();
452 max = aisb_max;
453 sbit = find_first_bit_left(bucket->aisb, max);
454 if (sbit != max) {
455 atomic_inc(&irq_retries);
456 rescan++;
457 goto scan;
458 }
459out:
460 /* store next device bit to scan */
461 __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last;
462}
463
464/* msi_vecs - number of requested interrupts, 0 place function to error state */
465static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs)
466{
467 struct zpci_dev *zdev = get_zdev(pdev);
468 unsigned int aisb, msi_nr;
469 struct msi_desc *msi;
470 int rc;
471
472 /* store the number of used MSI vectors */
473 zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS);
474
475 spin_lock(&bucket->lock);
476 aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE);
477 /* alloc map exhausted? */
478 if (aisb == PAGE_SIZE) {
479 spin_unlock(&bucket->lock);
480 return -EIO;
481 }
482 set_bit(aisb, bucket->alloc);
483 spin_unlock(&bucket->lock);
484
485 zdev->aisb = aisb;
486 if (aisb + 1 > aisb_max)
487 aisb_max = aisb + 1;
488
489 /* wire up IRQ shortcut pointer */
490 bucket->imap[zdev->aisb] = zdev->irq_map;
491 pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map);
492
493 /* TODO: irq number 0 wont be found if we return less than requested MSIs.
494 * ignore it for now and fix in common code.
495 */
496 msi_nr = aisb << ZPCI_MSI_VEC_BITS;
497
498 list_for_each_entry(msi, &pdev->msi_list, list) {
499 rc = zpci_setup_msi_irq(zdev, msi, msi_nr,
500 aisb << ZPCI_MSI_VEC_BITS);
501 if (rc)
502 return rc;
503 msi_nr++;
504 }
505
506 rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv);
507 if (rc) {
508 clear_bit(aisb, bucket->alloc);
509 dev_err(&pdev->dev, "register MSI failed with: %d\n", rc);
510 return rc;
511 }
512 return (zdev->irq_map->msi_vecs == msi_vecs) ?
513 0 : zdev->irq_map->msi_vecs;
514}
515
516static void zpci_teardown_msi(struct pci_dev *pdev)
517{
518 struct zpci_dev *zdev = get_zdev(pdev);
519 struct msi_desc *msi;
520 int aisb, rc;
521
522 rc = zpci_unregister_airq(zdev);
523 if (rc) {
524 dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc);
525 return;
526 }
527
528 msi = list_first_entry(&pdev->msi_list, struct msi_desc, list);
529 aisb = irq_to_dev_nr(msi->irq);
530
531 list_for_each_entry(msi, &pdev->msi_list, list)
532 zpci_teardown_msi_irq(zdev, msi);
533
534 clear_bit(aisb, bucket->alloc);
535 if (aisb + 1 == aisb_max)
536 aisb_max--;
537}
538
539int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
540{
541 pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec);
542 if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI)
543 return -EINVAL;
544 return zpci_setup_msi(pdev, nvec);
545}
546
547void arch_teardown_msi_irqs(struct pci_dev *pdev)
548{
549 pr_info("%s: on pdev: %p\n", __func__, pdev);
550 zpci_teardown_msi(pdev);
551}
552
Jan Glaubercd248342012-11-29 12:50:30 +0100553static void zpci_map_resources(struct zpci_dev *zdev)
554{
555 struct pci_dev *pdev = zdev->pdev;
556 resource_size_t len;
557 int i;
558
559 for (i = 0; i < PCI_BAR_COUNT; i++) {
560 len = pci_resource_len(pdev, i);
561 if (!len)
562 continue;
563 pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
564 pdev->resource[i].end = pdev->resource[i].start + len - 1;
565 pr_debug("BAR%i: -> start: %Lx end: %Lx\n",
566 i, pdev->resource[i].start, pdev->resource[i].end);
567 }
Sebastian Ott944239c2013-06-05 16:06:16 +0200568}
569
570static void zpci_unmap_resources(struct zpci_dev *zdev)
571{
572 struct pci_dev *pdev = zdev->pdev;
573 resource_size_t len;
574 int i;
575
576 for (i = 0; i < PCI_BAR_COUNT; i++) {
577 len = pci_resource_len(pdev, i);
578 if (!len)
579 continue;
580 pci_iounmap(pdev, (void *) pdev->resource[i].start);
581 }
582}
Jan Glaubercd248342012-11-29 12:50:30 +0100583
Jan Glaubercd248342012-11-29 12:50:30 +0100584struct zpci_dev *zpci_alloc_device(void)
585{
586 struct zpci_dev *zdev;
587
588 /* Alloc memory for our private pci device data */
589 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
590 if (!zdev)
591 return ERR_PTR(-ENOMEM);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100592
593 /* Alloc aibv & callback space */
Wei Yongjun4118fee2012-12-03 16:11:34 +0100594 zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100595 if (!zdev->irq_map)
596 goto error;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100597 WARN_ON((u64) zdev->irq_map & 0xff);
Jan Glaubercd248342012-11-29 12:50:30 +0100598 return zdev;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100599
600error:
601 kfree(zdev);
602 return ERR_PTR(-ENOMEM);
Jan Glaubercd248342012-11-29 12:50:30 +0100603}
604
605void zpci_free_device(struct zpci_dev *zdev)
606{
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100607 kmem_cache_free(zdev_irq_cache, zdev->irq_map);
Jan Glaubercd248342012-11-29 12:50:30 +0100608 kfree(zdev);
609}
610
Jan Glaubercd248342012-11-29 12:50:30 +0100611/*
612 * Too late for any s390 specific setup, since interrupts must be set up
613 * already which requires DMA setup too and the pci scan will access the
614 * config space, which only works if the function handle is enabled.
615 */
616int pcibios_enable_device(struct pci_dev *pdev, int mask)
617{
618 struct resource *res;
619 u16 cmd;
620 int i;
621
622 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
623
624 for (i = 0; i < PCI_BAR_COUNT; i++) {
625 res = &pdev->resource[i];
626
627 if (res->flags & IORESOURCE_IO)
628 return -EINVAL;
629
630 if (res->flags & IORESOURCE_MEM)
631 cmd |= PCI_COMMAND_MEMORY;
632 }
633 pci_write_config_word(pdev, PCI_COMMAND, cmd);
634 return 0;
635}
636
Jan Glauber1e8da952012-11-29 14:36:55 +0100637int pcibios_add_platform_entries(struct pci_dev *pdev)
638{
639 return zpci_sysfs_add_device(&pdev->dev);
640}
641
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100642int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data)
643{
644 int msi_nr = irq_to_msi_nr(irq);
645 struct zdev_irq_map *imap;
646 struct msi_desc *msi;
647
648 msi = irq_get_msi_desc(irq);
649 if (!msi)
650 return -EIO;
651
652 imap = get_imap(irq);
653 spin_lock_init(&imap->lock);
654
655 pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr);
656 imap->cb[msi_nr].handler = handler;
657 imap->cb[msi_nr].data = data;
658
659 /*
660 * The generic MSI code returns with the interrupt disabled on the
661 * card, using the MSI mask bits. Firmware doesn't appear to unmask
662 * at that level, so we do it here by hand.
663 */
664 zpci_msi_set_mask_bits(msi, 1, 0);
665 return 0;
666}
667
668void zpci_free_irq(unsigned int irq)
669{
670 struct zdev_irq_map *imap = get_imap(irq);
671 int msi_nr = irq_to_msi_nr(irq);
672 unsigned long flags;
673
674 pr_debug("%s: for irq: %d\n", __func__, irq);
675
676 spin_lock_irqsave(&imap->lock, flags);
677 imap->cb[msi_nr].handler = NULL;
678 imap->cb[msi_nr].data = NULL;
679 spin_unlock_irqrestore(&imap->lock, flags);
680}
681
682int request_irq(unsigned int irq, irq_handler_t handler,
683 unsigned long irqflags, const char *devname, void *dev_id)
684{
685 pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n",
686 __func__, irq, handler, irqflags, devname);
687
688 return zpci_request_irq(irq, handler, dev_id);
689}
690EXPORT_SYMBOL_GPL(request_irq);
691
692void free_irq(unsigned int irq, void *dev_id)
693{
694 zpci_free_irq(irq);
695}
696EXPORT_SYMBOL_GPL(free_irq);
697
698static int __init zpci_irq_init(void)
699{
700 int cpu, rc;
701
702 bucket = kzalloc(sizeof(*bucket), GFP_KERNEL);
703 if (!bucket)
704 return -ENOMEM;
705
706 bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL);
707 if (!bucket->aisb) {
708 rc = -ENOMEM;
709 goto out_aisb;
710 }
711
712 bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL);
713 if (!bucket->alloc) {
714 rc = -ENOMEM;
715 goto out_alloc;
716 }
717
718 isc_register(PCI_ISC);
719 zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC);
720 if (IS_ERR(zpci_irq_si)) {
721 rc = PTR_ERR(zpci_irq_si);
722 zpci_irq_si = NULL;
723 goto out_ai;
724 }
725
726 for_each_online_cpu(cpu)
727 per_cpu(next_sbit, cpu) = 0;
728
729 spin_lock_init(&bucket->lock);
730 /* set summary to 1 to be called every time for the ISC */
731 *zpci_irq_si = 1;
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200732 set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100733 return 0;
734
735out_ai:
736 isc_unregister(PCI_ISC);
737 free_page((unsigned long) bucket->alloc);
738out_alloc:
739 free_page((unsigned long) bucket->aisb);
740out_aisb:
741 kfree(bucket);
742 return rc;
743}
744
745static void zpci_irq_exit(void)
746{
747 free_page((unsigned long) bucket->alloc);
748 free_page((unsigned long) bucket->aisb);
749 s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC);
750 isc_unregister(PCI_ISC);
751 kfree(bucket);
752}
753
Jan Glauberd0b08852012-12-11 14:53:35 +0100754void zpci_debug_info(struct zpci_dev *zdev, struct seq_file *m)
755{
756 if (!zdev)
757 return;
758
759 seq_printf(m, "global irq retries: %u\n", atomic_read(&irq_retries));
760 seq_printf(m, "aibv[0]:%016lx aibv[1]:%016lx aisb:%016lx\n",
761 get_imap(0)->aibv, get_imap(1)->aibv, *bucket->aisb);
762}
763
Jan Glaubercd248342012-11-29 12:50:30 +0100764static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
765 unsigned long flags, int domain)
766{
767 struct resource *r;
768 char *name;
769 int rc;
770
771 r = kzalloc(sizeof(*r), GFP_KERNEL);
772 if (!r)
773 return ERR_PTR(-ENOMEM);
774 r->start = start;
775 r->end = r->start + size - 1;
776 r->flags = flags;
777 r->parent = &iomem_resource;
778 name = kmalloc(18, GFP_KERNEL);
779 if (!name) {
780 kfree(r);
781 return ERR_PTR(-ENOMEM);
782 }
783 sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
784 r->name = name;
785
786 rc = request_resource(&iomem_resource, r);
787 if (rc)
788 pr_debug("request resource %pR failed\n", r);
789 return r;
790}
791
792static int zpci_alloc_iomap(struct zpci_dev *zdev)
793{
794 int entry;
795
796 spin_lock(&zpci_iomap_lock);
797 entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
798 if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
799 spin_unlock(&zpci_iomap_lock);
800 return -ENOSPC;
801 }
802 set_bit(entry, zpci_iomap);
803 spin_unlock(&zpci_iomap_lock);
804 return entry;
805}
806
807static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
808{
809 spin_lock(&zpci_iomap_lock);
810 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
811 clear_bit(entry, zpci_iomap);
812 spin_unlock(&zpci_iomap_lock);
813}
814
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200815int pcibios_add_device(struct pci_dev *pdev)
816{
817 struct zpci_dev *zdev = get_zdev(pdev);
818
Sebastian Ott1c213512013-04-25 14:49:48 +0200819 zdev->pdev = pdev;
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200820 zpci_debug_init_device(zdev);
821 zpci_fmb_enable_device(zdev);
822 zpci_map_resources(zdev);
823
824 return 0;
825}
826
Sebastian Ott944239c2013-06-05 16:06:16 +0200827void pcibios_release_device(struct pci_dev *pdev)
828{
829 struct zpci_dev *zdev = get_zdev(pdev);
830
831 zpci_unmap_resources(zdev);
832 zpci_fmb_disable_device(zdev);
833 zpci_debug_exit_device(zdev);
834 zdev->pdev = NULL;
835}
836
Sebastian Ott1c213512013-04-25 14:49:48 +0200837static int zpci_scan_bus(struct zpci_dev *zdev)
Jan Glaubercd248342012-11-29 12:50:30 +0100838{
839 struct resource *res;
840 LIST_HEAD(resources);
841 int i;
842
843 /* allocate mapping entry for each used bar */
844 for (i = 0; i < PCI_BAR_COUNT; i++) {
845 unsigned long addr, size, flags;
846 int entry;
847
848 if (!zdev->bars[i].size)
849 continue;
850 entry = zpci_alloc_iomap(zdev);
851 if (entry < 0)
852 return entry;
853 zdev->bars[i].map_idx = entry;
854
855 /* only MMIO is supported */
856 flags = IORESOURCE_MEM;
857 if (zdev->bars[i].val & 8)
858 flags |= IORESOURCE_PREFETCH;
859 if (zdev->bars[i].val & 4)
860 flags |= IORESOURCE_MEM_64;
861
862 addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
863
864 size = 1UL << zdev->bars[i].size;
865
866 res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
867 if (IS_ERR(res)) {
868 zpci_free_iomap(zdev, entry);
869 return PTR_ERR(res);
870 }
871 pci_add_resource(&resources, res);
872 }
873
Sebastian Ott1c213512013-04-25 14:49:48 +0200874 zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
875 zdev, &resources);
Jan Glaubercd248342012-11-29 12:50:30 +0100876 if (!zdev->bus)
877 return -EIO;
878
879 zdev->bus->max_bus_speed = zdev->max_bus_speed;
880 return 0;
881}
882
883static int zpci_alloc_domain(struct zpci_dev *zdev)
884{
885 spin_lock(&zpci_domain_lock);
886 zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
887 if (zdev->domain == ZPCI_NR_DEVICES) {
888 spin_unlock(&zpci_domain_lock);
889 return -ENOSPC;
890 }
891 set_bit(zdev->domain, zpci_domain);
892 spin_unlock(&zpci_domain_lock);
893 return 0;
894}
895
896static void zpci_free_domain(struct zpci_dev *zdev)
897{
898 spin_lock(&zpci_domain_lock);
899 clear_bit(zdev->domain, zpci_domain);
900 spin_unlock(&zpci_domain_lock);
901}
902
Jan Glaubera755a452012-11-29 12:55:21 +0100903int zpci_enable_device(struct zpci_dev *zdev)
904{
905 int rc;
906
907 rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
908 if (rc)
909 goto out;
910 pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
Jan Glauber828b35f2012-11-29 14:33:30 +0100911
912 rc = zpci_dma_init_device(zdev);
913 if (rc)
914 goto out_dma;
Jan Glaubera755a452012-11-29 12:55:21 +0100915 return 0;
Jan Glauber828b35f2012-11-29 14:33:30 +0100916
917out_dma:
918 clp_disable_fh(zdev);
Jan Glaubera755a452012-11-29 12:55:21 +0100919out:
920 return rc;
921}
922EXPORT_SYMBOL_GPL(zpci_enable_device);
923
Sebastian Ottcb65a662013-04-16 14:12:17 +0200924int zpci_disable_device(struct zpci_dev *zdev)
925{
926 zpci_dma_exit_device(zdev);
927 return clp_disable_fh(zdev);
928}
929EXPORT_SYMBOL_GPL(zpci_disable_device);
930
Jan Glaubercd248342012-11-29 12:50:30 +0100931int zpci_create_device(struct zpci_dev *zdev)
932{
933 int rc;
934
935 rc = zpci_alloc_domain(zdev);
936 if (rc)
937 goto out;
938
Sebastian Ott1c213512013-04-25 14:49:48 +0200939 if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
940 rc = zpci_enable_device(zdev);
941 if (rc)
942 goto out_free;
943
944 zdev->state = ZPCI_FN_STATE_ONLINE;
945 }
946 rc = zpci_scan_bus(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100947 if (rc)
Sebastian Ott1c213512013-04-25 14:49:48 +0200948 goto out_disable;
Jan Glaubercd248342012-11-29 12:50:30 +0100949
950 mutex_lock(&zpci_list_lock);
951 list_add_tail(&zdev->entry, &zpci_list);
Sebastian Ott53923352013-01-31 19:55:17 +0100952 if (hotplug_ops)
953 hotplug_ops->create_slot(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100954 mutex_unlock(&zpci_list_lock);
955
Jan Glaubercd248342012-11-29 12:50:30 +0100956 return 0;
957
Sebastian Ott1c213512013-04-25 14:49:48 +0200958out_disable:
959 if (zdev->state == ZPCI_FN_STATE_ONLINE)
960 zpci_disable_device(zdev);
961out_free:
Jan Glaubercd248342012-11-29 12:50:30 +0100962 zpci_free_domain(zdev);
963out:
964 return rc;
965}
966
967void zpci_stop_device(struct zpci_dev *zdev)
968{
Jan Glauber828b35f2012-11-29 14:33:30 +0100969 zpci_dma_exit_device(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100970 /*
971 * Note: SCLP disables fh via set-pci-fn so don't
972 * do that here.
973 */
974}
975EXPORT_SYMBOL_GPL(zpci_stop_device);
976
977int 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
Jan Glaubercd248342012-11-29 12:50:30 +0100986 pci_bus_add_devices(zdev->bus);
987
Jan Glaubercd248342012-11-29 12:50:30 +0100988 return 0;
Jan Glaubercd248342012-11-29 12:50:30 +0100989out:
Jan Glauber828b35f2012-11-29 14:33:30 +0100990 zpci_dma_exit_device(zdev);
Jan Glaubera755a452012-11-29 12:55:21 +0100991 clp_disable_fh(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100992 return -EIO;
993}
994EXPORT_SYMBOL_GPL(zpci_scan_device);
995
996static inline int barsize(u8 size)
997{
998 return (size) ? (1 << size) >> 10 : 0;
999}
1000
1001static int zpci_mem_init(void)
1002{
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001003 zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map),
1004 L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL);
1005 if (!zdev_irq_cache)
1006 goto error_zdev;
1007
Jan Glauberd0b08852012-12-11 14:53:35 +01001008 zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
1009 16, 0, NULL);
1010 if (!zdev_fmb_cache)
1011 goto error_fmb;
1012
Jan Glaubercd248342012-11-29 12:50:30 +01001013 /* TODO: use realloc */
1014 zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
1015 GFP_KERNEL);
1016 if (!zpci_iomap_start)
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001017 goto error_iomap;
Jan Glaubercd248342012-11-29 12:50:30 +01001018 return 0;
1019
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001020error_iomap:
Jan Glauberd0b08852012-12-11 14:53:35 +01001021 kmem_cache_destroy(zdev_fmb_cache);
1022error_fmb:
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001023 kmem_cache_destroy(zdev_irq_cache);
Jan Glaubercd248342012-11-29 12:50:30 +01001024error_zdev:
1025 return -ENOMEM;
1026}
1027
1028static void zpci_mem_exit(void)
1029{
1030 kfree(zpci_iomap_start);
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001031 kmem_cache_destroy(zdev_irq_cache);
Jan Glauberd0b08852012-12-11 14:53:35 +01001032 kmem_cache_destroy(zdev_fmb_cache);
Jan Glaubercd248342012-11-29 12:50:30 +01001033}
1034
Sebastian Ott53923352013-01-31 19:55:17 +01001035void zpci_register_hp_ops(struct pci_hp_callback_ops *ops)
1036{
1037 mutex_lock(&zpci_list_lock);
1038 hotplug_ops = ops;
1039 mutex_unlock(&zpci_list_lock);
1040}
1041EXPORT_SYMBOL_GPL(zpci_register_hp_ops);
1042
1043void zpci_deregister_hp_ops(void)
1044{
1045 mutex_lock(&zpci_list_lock);
1046 hotplug_ops = NULL;
1047 mutex_unlock(&zpci_list_lock);
1048}
1049EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops);
1050
Sebastian Ott89b0dc952013-04-16 14:19:22 +02001051unsigned int s390_pci_probe;
Heiko Carstens1e5635d2013-01-30 15:52:16 +01001052EXPORT_SYMBOL_GPL(s390_pci_probe);
Jan Glaubercd248342012-11-29 12:50:30 +01001053
1054char * __init pcibios_setup(char *str)
1055{
Sebastian Ott89b0dc952013-04-16 14:19:22 +02001056 if (!strcmp(str, "on")) {
1057 s390_pci_probe = 1;
Jan Glaubercd248342012-11-29 12:50:30 +01001058 return NULL;
1059 }
1060 return str;
1061}
1062
1063static int __init pci_base_init(void)
1064{
1065 int rc;
1066
Heiko Carstens1e5635d2013-01-30 15:52:16 +01001067 if (!s390_pci_probe)
Jan Glaubercd248342012-11-29 12:50:30 +01001068 return 0;
1069
1070 if (!test_facility(2) || !test_facility(69)
1071 || !test_facility(71) || !test_facility(72))
1072 return 0;
1073
1074 pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n",
1075 test_facility(69), test_facility(70),
1076 test_facility(71));
1077
Jan Glauberd0b08852012-12-11 14:53:35 +01001078 rc = zpci_debug_init();
1079 if (rc)
1080 return rc;
1081
Jan Glaubercd248342012-11-29 12:50:30 +01001082 rc = zpci_mem_init();
1083 if (rc)
1084 goto out_mem;
1085
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001086 rc = zpci_msihash_init();
1087 if (rc)
1088 goto out_hash;
1089
1090 rc = zpci_irq_init();
1091 if (rc)
1092 goto out_irq;
1093
Jan Glauber828b35f2012-11-29 14:33:30 +01001094 rc = zpci_dma_init();
1095 if (rc)
1096 goto out_dma;
1097
Jan Glaubera755a452012-11-29 12:55:21 +01001098 rc = clp_find_pci_devices();
1099 if (rc)
1100 goto out_find;
1101
Jan Glaubercd248342012-11-29 12:50:30 +01001102 return 0;
1103
Jan Glaubera755a452012-11-29 12:55:21 +01001104out_find:
Jan Glauber828b35f2012-11-29 14:33:30 +01001105 zpci_dma_exit();
1106out_dma:
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001107 zpci_irq_exit();
1108out_irq:
1109 zpci_msihash_exit();
1110out_hash:
Jan Glaubercd248342012-11-29 12:50:30 +01001111 zpci_mem_exit();
1112out_mem:
Jan Glauberd0b08852012-12-11 14:53:35 +01001113 zpci_debug_exit();
Jan Glaubercd248342012-11-29 12:50:30 +01001114 return rc;
1115}
1116subsys_initcall(pci_base_init);