blob: 51c3ca86bd05cc2d28fcd7870766981917a71079 [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
Jan Glaubercd248342012-11-29 12:50:30 +010088/* I/O Map */
89static DEFINE_SPINLOCK(zpci_iomap_lock);
90static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
91struct zpci_iomap_entry *zpci_iomap_start;
92EXPORT_SYMBOL_GPL(zpci_iomap_start);
93
Jan Glauber9a4da8a2012-11-29 13:05:05 +010094/* highest irq summary bit */
95static int __read_mostly aisb_max;
96
97static struct kmem_cache *zdev_irq_cache;
Jan Glauberd0b08852012-12-11 14:53:35 +010098static struct kmem_cache *zdev_fmb_cache;
99
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100100static inline int irq_to_msi_nr(unsigned int irq)
101{
102 return irq & ZPCI_MSI_MASK;
103}
104
105static inline int irq_to_dev_nr(unsigned int irq)
106{
107 return irq >> ZPCI_MSI_VEC_BITS;
108}
109
110static inline struct zdev_irq_map *get_imap(unsigned int irq)
111{
112 return bucket->imap[irq_to_dev_nr(irq)];
113}
114
Jan Glaubercd248342012-11-29 12:50:30 +0100115struct zpci_dev *get_zdev(struct pci_dev *pdev)
116{
117 return (struct zpci_dev *) pdev->sysdata;
118}
119
120struct zpci_dev *get_zdev_by_fid(u32 fid)
121{
122 struct zpci_dev *tmp, *zdev = NULL;
123
124 mutex_lock(&zpci_list_lock);
125 list_for_each_entry(tmp, &zpci_list, entry) {
126 if (tmp->fid == fid) {
127 zdev = tmp;
128 break;
129 }
130 }
131 mutex_unlock(&zpci_list_lock);
132 return zdev;
133}
134
135bool zpci_fid_present(u32 fid)
136{
137 return (get_zdev_by_fid(fid) != NULL) ? true : false;
138}
139
140static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
141{
142 return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
143}
144
145int pci_domain_nr(struct pci_bus *bus)
146{
147 return ((struct zpci_dev *) bus->sysdata)->domain;
148}
149EXPORT_SYMBOL_GPL(pci_domain_nr);
150
151int pci_proc_domain(struct pci_bus *bus)
152{
153 return pci_domain_nr(bus);
154}
155EXPORT_SYMBOL_GPL(pci_proc_domain);
156
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100157/* Modify PCI: Register adapter interruptions */
158static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb,
159 u64 aibv)
160{
161 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
162 struct zpci_fib *fib;
163 int rc;
164
165 fib = (void *) get_zeroed_page(GFP_KERNEL);
166 if (!fib)
167 return -ENOMEM;
168
169 fib->isc = PCI_ISC;
170 fib->noi = zdev->irq_map->msi_vecs;
171 fib->sum = 1; /* enable summary notifications */
172 fib->aibv = aibv;
173 fib->aibvo = 0; /* every function has its own page */
174 fib->aisb = (u64) bucket->aisb + aisb / 8;
175 fib->aisbo = aisb & ZPCI_MSI_MASK;
176
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200177 rc = s390pci_mod_fc(req, fib);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100178 pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi);
179
180 free_page((unsigned long) fib);
181 return rc;
182}
183
184struct mod_pci_args {
185 u64 base;
186 u64 limit;
187 u64 iota;
Jan Glauberd0b08852012-12-11 14:53:35 +0100188 u64 fmb_addr;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100189};
190
191static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
192{
193 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
194 struct zpci_fib *fib;
195 int rc;
196
197 /* The FIB must be available even if it's not used */
198 fib = (void *) get_zeroed_page(GFP_KERNEL);
199 if (!fib)
200 return -ENOMEM;
201
202 fib->pba = args->base;
203 fib->pal = args->limit;
204 fib->iota = args->iota;
Jan Glauberd0b08852012-12-11 14:53:35 +0100205 fib->fmb_addr = args->fmb_addr;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100206
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200207 rc = s390pci_mod_fc(req, fib);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100208 free_page((unsigned long) fib);
209 return rc;
210}
211
Jan Glauber828b35f2012-11-29 14:33:30 +0100212/* Modify PCI: Register I/O address translation parameters */
213int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
214 u64 base, u64 limit, u64 iota)
215{
Jan Glauberd0b08852012-12-11 14:53:35 +0100216 struct mod_pci_args args = { base, limit, iota, 0 };
Jan Glauber828b35f2012-11-29 14:33:30 +0100217
218 WARN_ON_ONCE(iota & 0x3fff);
219 args.iota |= ZPCI_IOTA_RTTO_FLAG;
220 return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
221}
222
223/* Modify PCI: Unregister I/O address translation parameters */
224int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
225{
Jan Glauberd0b08852012-12-11 14:53:35 +0100226 struct mod_pci_args args = { 0, 0, 0, 0 };
Jan Glauber828b35f2012-11-29 14:33:30 +0100227
228 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
229}
230
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100231/* Modify PCI: Unregister adapter interruptions */
232static int zpci_unregister_airq(struct zpci_dev *zdev)
233{
Jan Glauberd0b08852012-12-11 14:53:35 +0100234 struct mod_pci_args args = { 0, 0, 0, 0 };
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100235
236 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
237}
238
Jan Glauberd0b08852012-12-11 14:53:35 +0100239/* Modify PCI: Set PCI function measurement parameters */
240int zpci_fmb_enable_device(struct zpci_dev *zdev)
241{
242 struct mod_pci_args args = { 0, 0, 0, 0 };
243
244 if (zdev->fmb)
245 return -EINVAL;
246
Wei Yongjun08b42122013-02-25 22:09:25 +0800247 zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
Jan Glauberd0b08852012-12-11 14:53:35 +0100248 if (!zdev->fmb)
249 return -ENOMEM;
Jan Glauberd0b08852012-12-11 14:53:35 +0100250 WARN_ON((u64) zdev->fmb & 0xf);
251
252 args.fmb_addr = virt_to_phys(zdev->fmb);
253 return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
254}
255
256/* Modify PCI: Disable PCI function measurement */
257int zpci_fmb_disable_device(struct zpci_dev *zdev)
258{
259 struct mod_pci_args args = { 0, 0, 0, 0 };
260 int rc;
261
262 if (!zdev->fmb)
263 return -EINVAL;
264
265 /* Function measurement is disabled if fmb address is zero */
266 rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
267
268 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
269 zdev->fmb = NULL;
270 return rc;
271}
272
Jan Glaubercd248342012-11-29 12:50:30 +0100273#define ZPCI_PCIAS_CFGSPC 15
274
275static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
276{
277 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
278 u64 data;
279 int rc;
280
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200281 rc = s390pci_load(&data, req, offset);
Sebastian Ottb170bad2013-04-16 14:17:15 +0200282 if (!rc) {
283 data = data << ((8 - len) * 8);
284 data = le64_to_cpu(data);
Jan Glaubercd248342012-11-29 12:50:30 +0100285 *val = (u32) data;
Sebastian Ottb170bad2013-04-16 14:17:15 +0200286 } else
Jan Glaubercd248342012-11-29 12:50:30 +0100287 *val = 0xffffffff;
288 return rc;
289}
290
291static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
292{
293 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
294 u64 data = val;
295 int rc;
296
297 data = cpu_to_le64(data);
298 data = data >> ((8 - len) * 8);
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200299 rc = s390pci_store(data, req, offset);
Jan Glaubercd248342012-11-29 12:50:30 +0100300 return rc;
301}
302
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100303void enable_irq(unsigned int irq)
304{
305 struct msi_desc *msi = irq_get_msi_desc(irq);
306
307 zpci_msi_set_mask_bits(msi, 1, 0);
308}
309EXPORT_SYMBOL_GPL(enable_irq);
310
311void disable_irq(unsigned int irq)
312{
313 struct msi_desc *msi = irq_get_msi_desc(irq);
314
315 zpci_msi_set_mask_bits(msi, 1, 1);
316}
317EXPORT_SYMBOL_GPL(disable_irq);
318
Greg Kroah-Hartmanb881bc42012-12-21 14:06:37 -0800319void pcibios_fixup_bus(struct pci_bus *bus)
Jan Glaubercd248342012-11-29 12:50:30 +0100320{
321}
322
323resource_size_t pcibios_align_resource(void *data, const struct resource *res,
324 resource_size_t size,
325 resource_size_t align)
326{
327 return 0;
328}
329
Jan Glauber87bc3592012-12-06 14:30:28 +0100330/* combine single writes by using store-block insn */
331void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
332{
333 zpci_memcpy_toio(to, from, count);
334}
335
Jan Glaubercd248342012-11-29 12:50:30 +0100336/* Create a virtual mapping cookie for a PCI BAR */
337void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
338{
339 struct zpci_dev *zdev = get_zdev(pdev);
340 u64 addr;
341 int idx;
342
343 if ((bar & 7) != bar)
344 return NULL;
345
346 idx = zdev->bars[bar].map_idx;
347 spin_lock(&zpci_iomap_lock);
348 zpci_iomap_start[idx].fh = zdev->fh;
349 zpci_iomap_start[idx].bar = bar;
350 spin_unlock(&zpci_iomap_lock);
351
352 addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
353 return (void __iomem *) addr;
354}
355EXPORT_SYMBOL_GPL(pci_iomap);
356
357void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
358{
359 unsigned int idx;
360
361 idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
362 spin_lock(&zpci_iomap_lock);
363 zpci_iomap_start[idx].fh = 0;
364 zpci_iomap_start[idx].bar = 0;
365 spin_unlock(&zpci_iomap_lock);
366}
367EXPORT_SYMBOL_GPL(pci_iounmap);
368
369static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
370 int size, u32 *val)
371{
372 struct zpci_dev *zdev = get_zdev_by_bus(bus);
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200373 int ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100374
375 if (!zdev || devfn != ZPCI_DEVFN)
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200376 ret = -ENODEV;
377 else
378 ret = zpci_cfg_load(zdev, where, val, size);
379
380 return ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100381}
382
383static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
384 int size, u32 val)
385{
386 struct zpci_dev *zdev = get_zdev_by_bus(bus);
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200387 int ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100388
389 if (!zdev || devfn != ZPCI_DEVFN)
Sebastian Ott2c3700b2013-04-16 14:18:41 +0200390 ret = -ENODEV;
391 else
392 ret = zpci_cfg_store(zdev, where, val, size);
393
394 return ret;
Jan Glaubercd248342012-11-29 12:50:30 +0100395}
396
397static struct pci_ops pci_root_ops = {
398 .read = pci_read,
399 .write = pci_write,
400};
401
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100402/* store the last handled bit to implement fair scheduling of devices */
403static DEFINE_PER_CPU(unsigned long, next_sbit);
404
405static void zpci_irq_handler(void *dont, void *need)
406{
407 unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit);
408 int rescan = 0, max = aisb_max;
409 struct zdev_irq_map *imap;
410
Heiko Carstens420f42e2013-01-02 15:18:18 +0100411 inc_irq_stat(IRQIO_PCI);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100412 sbit = start;
413
414scan:
415 /* find summary_bit */
416 for_each_set_bit_left_cont(sbit, bucket->aisb, max) {
417 clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6));
418 last = sbit;
419
420 /* find vector bit */
421 imap = bucket->imap[sbit];
422 for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) {
Heiko Carstens420f42e2013-01-02 15:18:18 +0100423 inc_irq_stat(IRQIO_MSI);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100424 clear_bit(63 - mbit, &imap->aibv);
425
426 spin_lock(&imap->lock);
427 if (imap->cb[mbit].handler)
428 imap->cb[mbit].handler(mbit,
429 imap->cb[mbit].data);
430 spin_unlock(&imap->lock);
431 }
432 }
433
434 if (rescan)
435 goto out;
436
437 /* scan the skipped bits */
438 if (start > 0) {
439 sbit = 0;
440 max = start;
441 start = 0;
442 goto scan;
443 }
444
445 /* enable interrupts again */
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200446 set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100447
448 /* check again to not lose initiative */
449 rmb();
450 max = aisb_max;
451 sbit = find_first_bit_left(bucket->aisb, max);
452 if (sbit != max) {
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100453 rescan++;
454 goto scan;
455 }
456out:
457 /* store next device bit to scan */
458 __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last;
459}
460
461/* msi_vecs - number of requested interrupts, 0 place function to error state */
462static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs)
463{
464 struct zpci_dev *zdev = get_zdev(pdev);
465 unsigned int aisb, msi_nr;
466 struct msi_desc *msi;
467 int rc;
468
469 /* store the number of used MSI vectors */
470 zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS);
471
472 spin_lock(&bucket->lock);
473 aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE);
474 /* alloc map exhausted? */
475 if (aisb == PAGE_SIZE) {
476 spin_unlock(&bucket->lock);
477 return -EIO;
478 }
479 set_bit(aisb, bucket->alloc);
480 spin_unlock(&bucket->lock);
481
482 zdev->aisb = aisb;
483 if (aisb + 1 > aisb_max)
484 aisb_max = aisb + 1;
485
486 /* wire up IRQ shortcut pointer */
487 bucket->imap[zdev->aisb] = zdev->irq_map;
488 pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map);
489
490 /* TODO: irq number 0 wont be found if we return less than requested MSIs.
491 * ignore it for now and fix in common code.
492 */
493 msi_nr = aisb << ZPCI_MSI_VEC_BITS;
494
495 list_for_each_entry(msi, &pdev->msi_list, list) {
496 rc = zpci_setup_msi_irq(zdev, msi, msi_nr,
497 aisb << ZPCI_MSI_VEC_BITS);
498 if (rc)
499 return rc;
500 msi_nr++;
501 }
502
503 rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv);
504 if (rc) {
505 clear_bit(aisb, bucket->alloc);
506 dev_err(&pdev->dev, "register MSI failed with: %d\n", rc);
507 return rc;
508 }
509 return (zdev->irq_map->msi_vecs == msi_vecs) ?
510 0 : zdev->irq_map->msi_vecs;
511}
512
513static void zpci_teardown_msi(struct pci_dev *pdev)
514{
515 struct zpci_dev *zdev = get_zdev(pdev);
516 struct msi_desc *msi;
517 int aisb, rc;
518
519 rc = zpci_unregister_airq(zdev);
520 if (rc) {
521 dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc);
522 return;
523 }
524
525 msi = list_first_entry(&pdev->msi_list, struct msi_desc, list);
526 aisb = irq_to_dev_nr(msi->irq);
527
528 list_for_each_entry(msi, &pdev->msi_list, list)
529 zpci_teardown_msi_irq(zdev, msi);
530
531 clear_bit(aisb, bucket->alloc);
532 if (aisb + 1 == aisb_max)
533 aisb_max--;
534}
535
536int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
537{
538 pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec);
539 if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI)
540 return -EINVAL;
541 return zpci_setup_msi(pdev, nvec);
542}
543
544void arch_teardown_msi_irqs(struct pci_dev *pdev)
545{
546 pr_info("%s: on pdev: %p\n", __func__, pdev);
547 zpci_teardown_msi(pdev);
548}
549
Jan Glaubercd248342012-11-29 12:50:30 +0100550static void zpci_map_resources(struct zpci_dev *zdev)
551{
552 struct pci_dev *pdev = zdev->pdev;
553 resource_size_t len;
554 int i;
555
556 for (i = 0; i < PCI_BAR_COUNT; i++) {
557 len = pci_resource_len(pdev, i);
558 if (!len)
559 continue;
560 pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
561 pdev->resource[i].end = pdev->resource[i].start + len - 1;
562 pr_debug("BAR%i: -> start: %Lx end: %Lx\n",
563 i, pdev->resource[i].start, pdev->resource[i].end);
564 }
Sebastian Ott944239c2013-06-05 16:06:16 +0200565}
566
567static void zpci_unmap_resources(struct zpci_dev *zdev)
568{
569 struct pci_dev *pdev = zdev->pdev;
570 resource_size_t len;
571 int i;
572
573 for (i = 0; i < PCI_BAR_COUNT; i++) {
574 len = pci_resource_len(pdev, i);
575 if (!len)
576 continue;
577 pci_iounmap(pdev, (void *) pdev->resource[i].start);
578 }
579}
Jan Glaubercd248342012-11-29 12:50:30 +0100580
Jan Glaubercd248342012-11-29 12:50:30 +0100581struct zpci_dev *zpci_alloc_device(void)
582{
583 struct zpci_dev *zdev;
584
585 /* Alloc memory for our private pci device data */
586 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
587 if (!zdev)
588 return ERR_PTR(-ENOMEM);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100589
590 /* Alloc aibv & callback space */
Wei Yongjun4118fee2012-12-03 16:11:34 +0100591 zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100592 if (!zdev->irq_map)
593 goto error;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100594 WARN_ON((u64) zdev->irq_map & 0xff);
Jan Glaubercd248342012-11-29 12:50:30 +0100595 return zdev;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100596
597error:
598 kfree(zdev);
599 return ERR_PTR(-ENOMEM);
Jan Glaubercd248342012-11-29 12:50:30 +0100600}
601
602void zpci_free_device(struct zpci_dev *zdev)
603{
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100604 kmem_cache_free(zdev_irq_cache, zdev->irq_map);
Jan Glaubercd248342012-11-29 12:50:30 +0100605 kfree(zdev);
606}
607
Jan Glaubercd248342012-11-29 12:50:30 +0100608/*
609 * Too late for any s390 specific setup, since interrupts must be set up
610 * already which requires DMA setup too and the pci scan will access the
611 * config space, which only works if the function handle is enabled.
612 */
613int pcibios_enable_device(struct pci_dev *pdev, int mask)
614{
615 struct resource *res;
616 u16 cmd;
617 int i;
618
619 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
620
621 for (i = 0; i < PCI_BAR_COUNT; i++) {
622 res = &pdev->resource[i];
623
624 if (res->flags & IORESOURCE_IO)
625 return -EINVAL;
626
627 if (res->flags & IORESOURCE_MEM)
628 cmd |= PCI_COMMAND_MEMORY;
629 }
630 pci_write_config_word(pdev, PCI_COMMAND, cmd);
631 return 0;
632}
633
Jan Glauber1e8da952012-11-29 14:36:55 +0100634int pcibios_add_platform_entries(struct pci_dev *pdev)
635{
636 return zpci_sysfs_add_device(&pdev->dev);
637}
638
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100639int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data)
640{
641 int msi_nr = irq_to_msi_nr(irq);
642 struct zdev_irq_map *imap;
643 struct msi_desc *msi;
644
645 msi = irq_get_msi_desc(irq);
646 if (!msi)
647 return -EIO;
648
649 imap = get_imap(irq);
650 spin_lock_init(&imap->lock);
651
652 pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr);
653 imap->cb[msi_nr].handler = handler;
654 imap->cb[msi_nr].data = data;
655
656 /*
657 * The generic MSI code returns with the interrupt disabled on the
658 * card, using the MSI mask bits. Firmware doesn't appear to unmask
659 * at that level, so we do it here by hand.
660 */
661 zpci_msi_set_mask_bits(msi, 1, 0);
662 return 0;
663}
664
665void zpci_free_irq(unsigned int irq)
666{
667 struct zdev_irq_map *imap = get_imap(irq);
668 int msi_nr = irq_to_msi_nr(irq);
669 unsigned long flags;
670
671 pr_debug("%s: for irq: %d\n", __func__, irq);
672
673 spin_lock_irqsave(&imap->lock, flags);
674 imap->cb[msi_nr].handler = NULL;
675 imap->cb[msi_nr].data = NULL;
676 spin_unlock_irqrestore(&imap->lock, flags);
677}
678
679int request_irq(unsigned int irq, irq_handler_t handler,
680 unsigned long irqflags, const char *devname, void *dev_id)
681{
682 pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n",
683 __func__, irq, handler, irqflags, devname);
684
685 return zpci_request_irq(irq, handler, dev_id);
686}
687EXPORT_SYMBOL_GPL(request_irq);
688
689void free_irq(unsigned int irq, void *dev_id)
690{
691 zpci_free_irq(irq);
692}
693EXPORT_SYMBOL_GPL(free_irq);
694
695static int __init zpci_irq_init(void)
696{
697 int cpu, rc;
698
699 bucket = kzalloc(sizeof(*bucket), GFP_KERNEL);
700 if (!bucket)
701 return -ENOMEM;
702
703 bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL);
704 if (!bucket->aisb) {
705 rc = -ENOMEM;
706 goto out_aisb;
707 }
708
709 bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL);
710 if (!bucket->alloc) {
711 rc = -ENOMEM;
712 goto out_alloc;
713 }
714
715 isc_register(PCI_ISC);
716 zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC);
717 if (IS_ERR(zpci_irq_si)) {
718 rc = PTR_ERR(zpci_irq_si);
719 zpci_irq_si = NULL;
720 goto out_ai;
721 }
722
723 for_each_online_cpu(cpu)
724 per_cpu(next_sbit, cpu) = 0;
725
726 spin_lock_init(&bucket->lock);
727 /* set summary to 1 to be called every time for the ISC */
728 *zpci_irq_si = 1;
Sebastian Ottb2a9e872013-04-16 14:15:42 +0200729 set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100730 return 0;
731
732out_ai:
733 isc_unregister(PCI_ISC);
734 free_page((unsigned long) bucket->alloc);
735out_alloc:
736 free_page((unsigned long) bucket->aisb);
737out_aisb:
738 kfree(bucket);
739 return rc;
740}
741
742static void zpci_irq_exit(void)
743{
744 free_page((unsigned long) bucket->alloc);
745 free_page((unsigned long) bucket->aisb);
746 s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC);
747 isc_unregister(PCI_ISC);
748 kfree(bucket);
749}
750
Jan Glaubercd248342012-11-29 12:50:30 +0100751static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
752 unsigned long flags, int domain)
753{
754 struct resource *r;
755 char *name;
756 int rc;
757
758 r = kzalloc(sizeof(*r), GFP_KERNEL);
759 if (!r)
760 return ERR_PTR(-ENOMEM);
761 r->start = start;
762 r->end = r->start + size - 1;
763 r->flags = flags;
764 r->parent = &iomem_resource;
765 name = kmalloc(18, GFP_KERNEL);
766 if (!name) {
767 kfree(r);
768 return ERR_PTR(-ENOMEM);
769 }
770 sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
771 r->name = name;
772
773 rc = request_resource(&iomem_resource, r);
774 if (rc)
775 pr_debug("request resource %pR failed\n", r);
776 return r;
777}
778
779static int zpci_alloc_iomap(struct zpci_dev *zdev)
780{
781 int entry;
782
783 spin_lock(&zpci_iomap_lock);
784 entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
785 if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
786 spin_unlock(&zpci_iomap_lock);
787 return -ENOSPC;
788 }
789 set_bit(entry, zpci_iomap);
790 spin_unlock(&zpci_iomap_lock);
791 return entry;
792}
793
794static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
795{
796 spin_lock(&zpci_iomap_lock);
797 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
798 clear_bit(entry, zpci_iomap);
799 spin_unlock(&zpci_iomap_lock);
800}
801
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200802int pcibios_add_device(struct pci_dev *pdev)
803{
804 struct zpci_dev *zdev = get_zdev(pdev);
805
Sebastian Ott1c213512013-04-25 14:49:48 +0200806 zdev->pdev = pdev;
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200807 zpci_debug_init_device(zdev);
808 zpci_fmb_enable_device(zdev);
809 zpci_map_resources(zdev);
810
811 return 0;
812}
813
Sebastian Ott944239c2013-06-05 16:06:16 +0200814void pcibios_release_device(struct pci_dev *pdev)
815{
816 struct zpci_dev *zdev = get_zdev(pdev);
817
818 zpci_unmap_resources(zdev);
819 zpci_fmb_disable_device(zdev);
820 zpci_debug_exit_device(zdev);
821 zdev->pdev = NULL;
822}
823
Sebastian Ott1c213512013-04-25 14:49:48 +0200824static int zpci_scan_bus(struct zpci_dev *zdev)
Jan Glaubercd248342012-11-29 12:50:30 +0100825{
826 struct resource *res;
827 LIST_HEAD(resources);
828 int i;
829
830 /* allocate mapping entry for each used bar */
831 for (i = 0; i < PCI_BAR_COUNT; i++) {
832 unsigned long addr, size, flags;
833 int entry;
834
835 if (!zdev->bars[i].size)
836 continue;
837 entry = zpci_alloc_iomap(zdev);
838 if (entry < 0)
839 return entry;
840 zdev->bars[i].map_idx = entry;
841
842 /* only MMIO is supported */
843 flags = IORESOURCE_MEM;
844 if (zdev->bars[i].val & 8)
845 flags |= IORESOURCE_PREFETCH;
846 if (zdev->bars[i].val & 4)
847 flags |= IORESOURCE_MEM_64;
848
849 addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
850
851 size = 1UL << zdev->bars[i].size;
852
853 res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
854 if (IS_ERR(res)) {
855 zpci_free_iomap(zdev, entry);
856 return PTR_ERR(res);
857 }
858 pci_add_resource(&resources, res);
859 }
860
Sebastian Ott1c213512013-04-25 14:49:48 +0200861 zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
862 zdev, &resources);
Jan Glaubercd248342012-11-29 12:50:30 +0100863 if (!zdev->bus)
864 return -EIO;
865
866 zdev->bus->max_bus_speed = zdev->max_bus_speed;
867 return 0;
868}
869
870static int zpci_alloc_domain(struct zpci_dev *zdev)
871{
872 spin_lock(&zpci_domain_lock);
873 zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
874 if (zdev->domain == ZPCI_NR_DEVICES) {
875 spin_unlock(&zpci_domain_lock);
876 return -ENOSPC;
877 }
878 set_bit(zdev->domain, zpci_domain);
879 spin_unlock(&zpci_domain_lock);
880 return 0;
881}
882
883static void zpci_free_domain(struct zpci_dev *zdev)
884{
885 spin_lock(&zpci_domain_lock);
886 clear_bit(zdev->domain, zpci_domain);
887 spin_unlock(&zpci_domain_lock);
888}
889
Jan Glaubera755a452012-11-29 12:55:21 +0100890int zpci_enable_device(struct zpci_dev *zdev)
891{
892 int rc;
893
894 rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
895 if (rc)
896 goto out;
897 pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
Jan Glauber828b35f2012-11-29 14:33:30 +0100898
899 rc = zpci_dma_init_device(zdev);
900 if (rc)
901 goto out_dma;
Jan Glaubera755a452012-11-29 12:55:21 +0100902 return 0;
Jan Glauber828b35f2012-11-29 14:33:30 +0100903
904out_dma:
905 clp_disable_fh(zdev);
Jan Glaubera755a452012-11-29 12:55:21 +0100906out:
907 return rc;
908}
909EXPORT_SYMBOL_GPL(zpci_enable_device);
910
Sebastian Ottcb65a662013-04-16 14:12:17 +0200911int zpci_disable_device(struct zpci_dev *zdev)
912{
913 zpci_dma_exit_device(zdev);
914 return clp_disable_fh(zdev);
915}
916EXPORT_SYMBOL_GPL(zpci_disable_device);
917
Jan Glaubercd248342012-11-29 12:50:30 +0100918int zpci_create_device(struct zpci_dev *zdev)
919{
920 int rc;
921
922 rc = zpci_alloc_domain(zdev);
923 if (rc)
924 goto out;
925
Sebastian Ott1c213512013-04-25 14:49:48 +0200926 if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
927 rc = zpci_enable_device(zdev);
928 if (rc)
929 goto out_free;
930
931 zdev->state = ZPCI_FN_STATE_ONLINE;
932 }
933 rc = zpci_scan_bus(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100934 if (rc)
Sebastian Ott1c213512013-04-25 14:49:48 +0200935 goto out_disable;
Jan Glaubercd248342012-11-29 12:50:30 +0100936
937 mutex_lock(&zpci_list_lock);
938 list_add_tail(&zdev->entry, &zpci_list);
Sebastian Ott53923352013-01-31 19:55:17 +0100939 if (hotplug_ops)
940 hotplug_ops->create_slot(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100941 mutex_unlock(&zpci_list_lock);
942
Jan Glaubercd248342012-11-29 12:50:30 +0100943 return 0;
944
Sebastian Ott1c213512013-04-25 14:49:48 +0200945out_disable:
946 if (zdev->state == ZPCI_FN_STATE_ONLINE)
947 zpci_disable_device(zdev);
948out_free:
Jan Glaubercd248342012-11-29 12:50:30 +0100949 zpci_free_domain(zdev);
950out:
951 return rc;
952}
953
954void zpci_stop_device(struct zpci_dev *zdev)
955{
Jan Glauber828b35f2012-11-29 14:33:30 +0100956 zpci_dma_exit_device(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100957 /*
958 * Note: SCLP disables fh via set-pci-fn so don't
959 * do that here.
960 */
961}
962EXPORT_SYMBOL_GPL(zpci_stop_device);
963
Jan Glaubercd248342012-11-29 12:50:30 +0100964static inline int barsize(u8 size)
965{
966 return (size) ? (1 << size) >> 10 : 0;
967}
968
969static int zpci_mem_init(void)
970{
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100971 zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map),
972 L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL);
973 if (!zdev_irq_cache)
974 goto error_zdev;
975
Jan Glauberd0b08852012-12-11 14:53:35 +0100976 zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
977 16, 0, NULL);
978 if (!zdev_fmb_cache)
979 goto error_fmb;
980
Jan Glaubercd248342012-11-29 12:50:30 +0100981 /* TODO: use realloc */
982 zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
983 GFP_KERNEL);
984 if (!zpci_iomap_start)
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100985 goto error_iomap;
Jan Glaubercd248342012-11-29 12:50:30 +0100986 return 0;
987
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100988error_iomap:
Jan Glauberd0b08852012-12-11 14:53:35 +0100989 kmem_cache_destroy(zdev_fmb_cache);
990error_fmb:
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100991 kmem_cache_destroy(zdev_irq_cache);
Jan Glaubercd248342012-11-29 12:50:30 +0100992error_zdev:
993 return -ENOMEM;
994}
995
996static void zpci_mem_exit(void)
997{
998 kfree(zpci_iomap_start);
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100999 kmem_cache_destroy(zdev_irq_cache);
Jan Glauberd0b08852012-12-11 14:53:35 +01001000 kmem_cache_destroy(zdev_fmb_cache);
Jan Glaubercd248342012-11-29 12:50:30 +01001001}
1002
Sebastian Ott53923352013-01-31 19:55:17 +01001003void zpci_register_hp_ops(struct pci_hp_callback_ops *ops)
1004{
1005 mutex_lock(&zpci_list_lock);
1006 hotplug_ops = ops;
1007 mutex_unlock(&zpci_list_lock);
1008}
1009EXPORT_SYMBOL_GPL(zpci_register_hp_ops);
1010
1011void zpci_deregister_hp_ops(void)
1012{
1013 mutex_lock(&zpci_list_lock);
1014 hotplug_ops = NULL;
1015 mutex_unlock(&zpci_list_lock);
1016}
1017EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops);
1018
Sebastian Ott89b0dc952013-04-16 14:19:22 +02001019unsigned int s390_pci_probe;
Heiko Carstens1e5635d2013-01-30 15:52:16 +01001020EXPORT_SYMBOL_GPL(s390_pci_probe);
Jan Glaubercd248342012-11-29 12:50:30 +01001021
1022char * __init pcibios_setup(char *str)
1023{
Sebastian Ott89b0dc952013-04-16 14:19:22 +02001024 if (!strcmp(str, "on")) {
1025 s390_pci_probe = 1;
Jan Glaubercd248342012-11-29 12:50:30 +01001026 return NULL;
1027 }
1028 return str;
1029}
1030
1031static int __init pci_base_init(void)
1032{
1033 int rc;
1034
Heiko Carstens1e5635d2013-01-30 15:52:16 +01001035 if (!s390_pci_probe)
Jan Glaubercd248342012-11-29 12:50:30 +01001036 return 0;
1037
1038 if (!test_facility(2) || !test_facility(69)
1039 || !test_facility(71) || !test_facility(72))
1040 return 0;
1041
1042 pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n",
1043 test_facility(69), test_facility(70),
1044 test_facility(71));
1045
Jan Glauberd0b08852012-12-11 14:53:35 +01001046 rc = zpci_debug_init();
1047 if (rc)
1048 return rc;
1049
Jan Glaubercd248342012-11-29 12:50:30 +01001050 rc = zpci_mem_init();
1051 if (rc)
1052 goto out_mem;
1053
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001054 rc = zpci_msihash_init();
1055 if (rc)
1056 goto out_hash;
1057
1058 rc = zpci_irq_init();
1059 if (rc)
1060 goto out_irq;
1061
Jan Glauber828b35f2012-11-29 14:33:30 +01001062 rc = zpci_dma_init();
1063 if (rc)
1064 goto out_dma;
1065
Jan Glaubera755a452012-11-29 12:55:21 +01001066 rc = clp_find_pci_devices();
1067 if (rc)
1068 goto out_find;
1069
Jan Glaubercd248342012-11-29 12:50:30 +01001070 return 0;
1071
Jan Glaubera755a452012-11-29 12:55:21 +01001072out_find:
Jan Glauber828b35f2012-11-29 14:33:30 +01001073 zpci_dma_exit();
1074out_dma:
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001075 zpci_irq_exit();
1076out_irq:
1077 zpci_msihash_exit();
1078out_hash:
Jan Glaubercd248342012-11-29 12:50:30 +01001079 zpci_mem_exit();
1080out_mem:
Jan Glauberd0b08852012-12-11 14:53:35 +01001081 zpci_debug_exit();
Jan Glaubercd248342012-11-29 12:50:30 +01001082 return rc;
1083}
1084subsys_initcall(pci_base_init);