1 /*
2 * Copyright IBM Corp. 2012
3 *
4 * Author(s):
5 * Jan Glauber <jang@linux.vnet.ibm.com>
6 *
7 * The System z PCI code is a rewrite from a prototype by
8 * the following people (Kudoz!):
9 * Alexander Schmidt
10 * Christoph Raisch
11 * Hannes Hering
12 * Hoang-Nam Nguyen
13 * Jan-Bernd Themann
14 * Stefan Roscher
15 * Thomas Klein
16 */
17
18 #define KMSG_COMPONENT "zpci"
19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/export.h>
25 #include <linux/delay.h>
26 #include <linux/irq.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/seq_file.h>
29 #include <linux/pci.h>
30 #include <linux/msi.h>
31
32 #include <asm/isc.h>
33 #include <asm/airq.h>
34 #include <asm/facility.h>
35 #include <asm/pci_insn.h>
36 #include <asm/pci_clp.h>
37 #include <asm/pci_dma.h>
38
39 #define DEBUG /* enable pr_debug */
40
41 #define SIC_IRQ_MODE_ALL 0
42 #define SIC_IRQ_MODE_SINGLE 1
43
44 #define ZPCI_NR_DMA_SPACES 1
45 #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
46
47 /* list of all detected zpci devices */
48 static LIST_HEAD(zpci_list);
49 static DEFINE_SPINLOCK(zpci_list_lock);
50
51 static struct irq_chip zpci_irq_chip = {
52 .name = "zPCI",
53 .irq_unmask = unmask_msi_irq,
54 .irq_mask = mask_msi_irq,
55 };
56
57 static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
58 static DEFINE_SPINLOCK(zpci_domain_lock);
59
60 static struct airq_iv *zpci_aisb_iv;
61 static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES];
62
63 /* Adapter interrupt definitions */
64 static void zpci_irq_handler(struct airq_struct *airq);
65
66 static struct airq_struct zpci_airq = {
67 .handler = zpci_irq_handler,
68 .isc = PCI_ISC,
69 };
70
71 /* I/O Map */
72 static DEFINE_SPINLOCK(zpci_iomap_lock);
73 static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
74 struct zpci_iomap_entry *zpci_iomap_start;
75 EXPORT_SYMBOL_GPL(zpci_iomap_start);
76
77 static struct kmem_cache *zdev_fmb_cache;
78
get_zdev(struct pci_dev * pdev)79 struct zpci_dev *get_zdev(struct pci_dev *pdev)
80 {
81 return (struct zpci_dev *) pdev->sysdata;
82 }
83
get_zdev_by_fid(u32 fid)84 struct zpci_dev *get_zdev_by_fid(u32 fid)
85 {
86 struct zpci_dev *tmp, *zdev = NULL;
87
88 spin_lock(&zpci_list_lock);
89 list_for_each_entry(tmp, &zpci_list, entry) {
90 if (tmp->fid == fid) {
91 zdev = tmp;
92 break;
93 }
94 }
95 spin_unlock(&zpci_list_lock);
96 return zdev;
97 }
98
get_zdev_by_bus(struct pci_bus * bus)99 static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
100 {
101 return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
102 }
103
pci_domain_nr(struct pci_bus * bus)104 int pci_domain_nr(struct pci_bus *bus)
105 {
106 return ((struct zpci_dev *) bus->sysdata)->domain;
107 }
108 EXPORT_SYMBOL_GPL(pci_domain_nr);
109
pci_proc_domain(struct pci_bus * bus)110 int pci_proc_domain(struct pci_bus *bus)
111 {
112 return pci_domain_nr(bus);
113 }
114 EXPORT_SYMBOL_GPL(pci_proc_domain);
115
116 /* Modify PCI: Register adapter interruptions */
zpci_set_airq(struct zpci_dev * zdev)117 static int zpci_set_airq(struct zpci_dev *zdev)
118 {
119 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
120 struct zpci_fib fib = {0};
121
122 fib.isc = PCI_ISC;
123 fib.sum = 1; /* enable summary notifications */
124 fib.noi = airq_iv_end(zdev->aibv);
125 fib.aibv = (unsigned long) zdev->aibv->vector;
126 fib.aibvo = 0; /* each zdev has its own interrupt vector */
127 fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8;
128 fib.aisbo = zdev->aisb & 63;
129
130 return zpci_mod_fc(req, &fib);
131 }
132
133 struct mod_pci_args {
134 u64 base;
135 u64 limit;
136 u64 iota;
137 u64 fmb_addr;
138 };
139
mod_pci(struct zpci_dev * zdev,int fn,u8 dmaas,struct mod_pci_args * args)140 static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
141 {
142 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
143 struct zpci_fib fib = {0};
144
145 fib.pba = args->base;
146 fib.pal = args->limit;
147 fib.iota = args->iota;
148 fib.fmb_addr = args->fmb_addr;
149
150 return zpci_mod_fc(req, &fib);
151 }
152
153 /* Modify PCI: Register I/O address translation parameters */
zpci_register_ioat(struct zpci_dev * zdev,u8 dmaas,u64 base,u64 limit,u64 iota)154 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
155 u64 base, u64 limit, u64 iota)
156 {
157 struct mod_pci_args args = { base, limit, iota, 0 };
158
159 WARN_ON_ONCE(iota & 0x3fff);
160 args.iota |= ZPCI_IOTA_RTTO_FLAG;
161 return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
162 }
163
164 /* Modify PCI: Unregister I/O address translation parameters */
zpci_unregister_ioat(struct zpci_dev * zdev,u8 dmaas)165 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
166 {
167 struct mod_pci_args args = { 0, 0, 0, 0 };
168
169 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
170 }
171
172 /* Modify PCI: Unregister adapter interruptions */
zpci_clear_airq(struct zpci_dev * zdev)173 static int zpci_clear_airq(struct zpci_dev *zdev)
174 {
175 struct mod_pci_args args = { 0, 0, 0, 0 };
176
177 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
178 }
179
180 /* Modify PCI: Set PCI function measurement parameters */
zpci_fmb_enable_device(struct zpci_dev * zdev)181 int zpci_fmb_enable_device(struct zpci_dev *zdev)
182 {
183 struct mod_pci_args args = { 0, 0, 0, 0 };
184
185 if (zdev->fmb)
186 return -EINVAL;
187
188 zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
189 if (!zdev->fmb)
190 return -ENOMEM;
191 WARN_ON((u64) zdev->fmb & 0xf);
192
193 /* reset software counters */
194 atomic64_set(&zdev->allocated_pages, 0);
195 atomic64_set(&zdev->mapped_pages, 0);
196 atomic64_set(&zdev->unmapped_pages, 0);
197
198 args.fmb_addr = virt_to_phys(zdev->fmb);
199 return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
200 }
201
202 /* Modify PCI: Disable PCI function measurement */
zpci_fmb_disable_device(struct zpci_dev * zdev)203 int zpci_fmb_disable_device(struct zpci_dev *zdev)
204 {
205 struct mod_pci_args args = { 0, 0, 0, 0 };
206 int rc;
207
208 if (!zdev->fmb)
209 return -EINVAL;
210
211 /* Function measurement is disabled if fmb address is zero */
212 rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
213
214 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
215 zdev->fmb = NULL;
216 return rc;
217 }
218
219 #define ZPCI_PCIAS_CFGSPC 15
220
zpci_cfg_load(struct zpci_dev * zdev,int offset,u32 * val,u8 len)221 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
222 {
223 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
224 u64 data;
225 int rc;
226
227 rc = zpci_load(&data, req, offset);
228 if (!rc) {
229 data = data << ((8 - len) * 8);
230 data = le64_to_cpu(data);
231 *val = (u32) data;
232 } else
233 *val = 0xffffffff;
234 return rc;
235 }
236
zpci_cfg_store(struct zpci_dev * zdev,int offset,u32 val,u8 len)237 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
238 {
239 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
240 u64 data = val;
241 int rc;
242
243 data = cpu_to_le64(data);
244 data = data >> ((8 - len) * 8);
245 rc = zpci_store(data, req, offset);
246 return rc;
247 }
248
pcibios_fixup_bus(struct pci_bus * bus)249 void pcibios_fixup_bus(struct pci_bus *bus)
250 {
251 }
252
pcibios_align_resource(void * data,const struct resource * res,resource_size_t size,resource_size_t align)253 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
254 resource_size_t size,
255 resource_size_t align)
256 {
257 return 0;
258 }
259
260 /* combine single writes by using store-block insn */
__iowrite64_copy(void __iomem * to,const void * from,size_t count)261 void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
262 {
263 zpci_memcpy_toio(to, from, count);
264 }
265
266 /* Create a virtual mapping cookie for a PCI BAR */
pci_iomap(struct pci_dev * pdev,int bar,unsigned long max)267 void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
268 {
269 struct zpci_dev *zdev = get_zdev(pdev);
270 u64 addr;
271 int idx;
272
273 if ((bar & 7) != bar)
274 return NULL;
275
276 idx = zdev->bars[bar].map_idx;
277 spin_lock(&zpci_iomap_lock);
278 zpci_iomap_start[idx].fh = zdev->fh;
279 zpci_iomap_start[idx].bar = bar;
280 spin_unlock(&zpci_iomap_lock);
281
282 addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
283 return (void __iomem *) addr;
284 }
285 EXPORT_SYMBOL_GPL(pci_iomap);
286
pci_iounmap(struct pci_dev * pdev,void __iomem * addr)287 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
288 {
289 unsigned int idx;
290
291 idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
292 spin_lock(&zpci_iomap_lock);
293 zpci_iomap_start[idx].fh = 0;
294 zpci_iomap_start[idx].bar = 0;
295 spin_unlock(&zpci_iomap_lock);
296 }
297 EXPORT_SYMBOL_GPL(pci_iounmap);
298
pci_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)299 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
300 int size, u32 *val)
301 {
302 struct zpci_dev *zdev = get_zdev_by_bus(bus);
303 int ret;
304
305 if (!zdev || devfn != ZPCI_DEVFN)
306 ret = -ENODEV;
307 else
308 ret = zpci_cfg_load(zdev, where, val, size);
309
310 return ret;
311 }
312
pci_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)313 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
314 int size, u32 val)
315 {
316 struct zpci_dev *zdev = get_zdev_by_bus(bus);
317 int ret;
318
319 if (!zdev || devfn != ZPCI_DEVFN)
320 ret = -ENODEV;
321 else
322 ret = zpci_cfg_store(zdev, where, val, size);
323
324 return ret;
325 }
326
327 static struct pci_ops pci_root_ops = {
328 .read = pci_read,
329 .write = pci_write,
330 };
331
zpci_irq_handler(struct airq_struct * airq)332 static void zpci_irq_handler(struct airq_struct *airq)
333 {
334 unsigned long si, ai;
335 struct airq_iv *aibv;
336 int irqs_on = 0;
337
338 inc_irq_stat(IRQIO_PCI);
339 for (si = 0;;) {
340 /* Scan adapter summary indicator bit vector */
341 si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv));
342 if (si == -1UL) {
343 if (irqs_on++)
344 /* End of second scan with interrupts on. */
345 break;
346 /* First scan complete, reenable interrupts. */
347 zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
348 si = 0;
349 continue;
350 }
351
352 /* Scan the adapter interrupt vector for this device. */
353 aibv = zpci_aibv[si];
354 for (ai = 0;;) {
355 ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv));
356 if (ai == -1UL)
357 break;
358 inc_irq_stat(IRQIO_MSI);
359 airq_iv_lock(aibv, ai);
360 generic_handle_irq(airq_iv_get_data(aibv, ai));
361 airq_iv_unlock(aibv, ai);
362 }
363 }
364 }
365
arch_setup_msi_irqs(struct pci_dev * pdev,int nvec,int type)366 int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
367 {
368 struct zpci_dev *zdev = get_zdev(pdev);
369 unsigned int hwirq, msi_vecs;
370 unsigned long aisb;
371 struct msi_desc *msi;
372 struct msi_msg msg;
373 int rc, irq;
374
375 if (type == PCI_CAP_ID_MSI && nvec > 1)
376 return 1;
377 msi_vecs = min(nvec, ZPCI_MSI_VEC_MAX);
378 msi_vecs = min_t(unsigned int, msi_vecs, CONFIG_PCI_NR_MSI);
379
380 /* Allocate adapter summary indicator bit */
381 rc = -EIO;
382 aisb = airq_iv_alloc_bit(zpci_aisb_iv);
383 if (aisb == -1UL)
384 goto out;
385 zdev->aisb = aisb;
386
387 /* Create adapter interrupt vector */
388 rc = -ENOMEM;
389 zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK);
390 if (!zdev->aibv)
391 goto out_si;
392
393 /* Wire up shortcut pointer */
394 zpci_aibv[aisb] = zdev->aibv;
395
396 /* Request MSI interrupts */
397 hwirq = 0;
398 list_for_each_entry(msi, &pdev->msi_list, list) {
399 rc = -EIO;
400 irq = irq_alloc_desc(0); /* Alloc irq on node 0 */
401 if (irq < 0)
402 goto out_msi;
403 rc = irq_set_msi_desc(irq, msi);
404 if (rc)
405 goto out_msi;
406 irq_set_chip_and_handler(irq, &zpci_irq_chip,
407 handle_simple_irq);
408 msg.data = hwirq;
409 msg.address_lo = zdev->msi_addr & 0xffffffff;
410 msg.address_hi = zdev->msi_addr >> 32;
411 write_msi_msg(irq, &msg);
412 airq_iv_set_data(zdev->aibv, hwirq, irq);
413 hwirq++;
414 }
415
416 /* Enable adapter interrupts */
417 rc = zpci_set_airq(zdev);
418 if (rc)
419 goto out_msi;
420
421 return (msi_vecs == nvec) ? 0 : msi_vecs;
422
423 out_msi:
424 list_for_each_entry(msi, &pdev->msi_list, list) {
425 if (hwirq-- == 0)
426 break;
427 irq_set_msi_desc(msi->irq, NULL);
428 irq_free_desc(msi->irq);
429 msi->msg.address_lo = 0;
430 msi->msg.address_hi = 0;
431 msi->msg.data = 0;
432 msi->irq = 0;
433 }
434 zpci_aibv[aisb] = NULL;
435 airq_iv_release(zdev->aibv);
436 out_si:
437 airq_iv_free_bit(zpci_aisb_iv, aisb);
438 out:
439 return rc;
440 }
441
arch_teardown_msi_irqs(struct pci_dev * pdev)442 void arch_teardown_msi_irqs(struct pci_dev *pdev)
443 {
444 struct zpci_dev *zdev = get_zdev(pdev);
445 struct msi_desc *msi;
446 int rc;
447
448 /* Disable adapter interrupts */
449 rc = zpci_clear_airq(zdev);
450 if (rc)
451 return;
452
453 /* Release MSI interrupts */
454 list_for_each_entry(msi, &pdev->msi_list, list) {
455 if (msi->msi_attrib.is_msix)
456 default_msix_mask_irq(msi, 1);
457 else
458 default_msi_mask_irq(msi, 1, 1);
459 irq_set_msi_desc(msi->irq, NULL);
460 irq_free_desc(msi->irq);
461 msi->msg.address_lo = 0;
462 msi->msg.address_hi = 0;
463 msi->msg.data = 0;
464 msi->irq = 0;
465 }
466
467 zpci_aibv[zdev->aisb] = NULL;
468 airq_iv_release(zdev->aibv);
469 airq_iv_free_bit(zpci_aisb_iv, zdev->aisb);
470 }
471
zpci_map_resources(struct zpci_dev * zdev)472 static void zpci_map_resources(struct zpci_dev *zdev)
473 {
474 struct pci_dev *pdev = zdev->pdev;
475 resource_size_t len;
476 int i;
477
478 for (i = 0; i < PCI_BAR_COUNT; i++) {
479 len = pci_resource_len(pdev, i);
480 if (!len)
481 continue;
482 pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
483 pdev->resource[i].end = pdev->resource[i].start + len - 1;
484 }
485 }
486
zpci_unmap_resources(struct zpci_dev * zdev)487 static void zpci_unmap_resources(struct zpci_dev *zdev)
488 {
489 struct pci_dev *pdev = zdev->pdev;
490 resource_size_t len;
491 int i;
492
493 for (i = 0; i < PCI_BAR_COUNT; i++) {
494 len = pci_resource_len(pdev, i);
495 if (!len)
496 continue;
497 pci_iounmap(pdev, (void *) pdev->resource[i].start);
498 }
499 }
500
zpci_irq_init(void)501 static int __init zpci_irq_init(void)
502 {
503 int rc;
504
505 rc = register_adapter_interrupt(&zpci_airq);
506 if (rc)
507 goto out;
508 /* Set summary to 1 to be called every time for the ISC. */
509 *zpci_airq.lsi_ptr = 1;
510
511 rc = -ENOMEM;
512 zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC);
513 if (!zpci_aisb_iv)
514 goto out_airq;
515
516 zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
517 return 0;
518
519 out_airq:
520 unregister_adapter_interrupt(&zpci_airq);
521 out:
522 return rc;
523 }
524
zpci_irq_exit(void)525 static void zpci_irq_exit(void)
526 {
527 airq_iv_release(zpci_aisb_iv);
528 unregister_adapter_interrupt(&zpci_airq);
529 }
530
zpci_alloc_iomap(struct zpci_dev * zdev)531 static int zpci_alloc_iomap(struct zpci_dev *zdev)
532 {
533 int entry;
534
535 spin_lock(&zpci_iomap_lock);
536 entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
537 if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
538 spin_unlock(&zpci_iomap_lock);
539 return -ENOSPC;
540 }
541 set_bit(entry, zpci_iomap);
542 spin_unlock(&zpci_iomap_lock);
543 return entry;
544 }
545
zpci_free_iomap(struct zpci_dev * zdev,int entry)546 static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
547 {
548 spin_lock(&zpci_iomap_lock);
549 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
550 clear_bit(entry, zpci_iomap);
551 spin_unlock(&zpci_iomap_lock);
552 }
553
__alloc_res(struct zpci_dev * zdev,unsigned long start,unsigned long size,unsigned long flags)554 static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
555 unsigned long size, unsigned long flags)
556 {
557 struct resource *r;
558
559 r = kzalloc(sizeof(*r), GFP_KERNEL);
560 if (!r)
561 return NULL;
562
563 r->start = start;
564 r->end = r->start + size - 1;
565 r->flags = flags;
566 r->name = zdev->res_name;
567
568 if (request_resource(&iomem_resource, r)) {
569 kfree(r);
570 return NULL;
571 }
572 return r;
573 }
574
zpci_setup_bus_resources(struct zpci_dev * zdev,struct list_head * resources)575 static int zpci_setup_bus_resources(struct zpci_dev *zdev,
576 struct list_head *resources)
577 {
578 unsigned long addr, size, flags;
579 struct resource *res;
580 int i, entry;
581
582 snprintf(zdev->res_name, sizeof(zdev->res_name),
583 "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);
584
585 for (i = 0; i < PCI_BAR_COUNT; i++) {
586 if (!zdev->bars[i].size)
587 continue;
588 entry = zpci_alloc_iomap(zdev);
589 if (entry < 0)
590 return entry;
591 zdev->bars[i].map_idx = entry;
592
593 /* only MMIO is supported */
594 flags = IORESOURCE_MEM;
595 if (zdev->bars[i].val & 8)
596 flags |= IORESOURCE_PREFETCH;
597 if (zdev->bars[i].val & 4)
598 flags |= IORESOURCE_MEM_64;
599
600 addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
601
602 size = 1UL << zdev->bars[i].size;
603
604 res = __alloc_res(zdev, addr, size, flags);
605 if (!res) {
606 zpci_free_iomap(zdev, entry);
607 return -ENOMEM;
608 }
609 zdev->bars[i].res = res;
610 pci_add_resource(resources, res);
611 }
612
613 return 0;
614 }
615
zpci_cleanup_bus_resources(struct zpci_dev * zdev)616 static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
617 {
618 int i;
619
620 for (i = 0; i < PCI_BAR_COUNT; i++) {
621 if (!zdev->bars[i].size)
622 continue;
623
624 zpci_free_iomap(zdev, zdev->bars[i].map_idx);
625 release_resource(zdev->bars[i].res);
626 kfree(zdev->bars[i].res);
627 }
628 }
629
pcibios_add_device(struct pci_dev * pdev)630 int pcibios_add_device(struct pci_dev *pdev)
631 {
632 struct zpci_dev *zdev = get_zdev(pdev);
633 struct resource *res;
634 int i;
635
636 zdev->pdev = pdev;
637 pdev->dev.groups = zpci_attr_groups;
638 zpci_map_resources(zdev);
639
640 for (i = 0; i < PCI_BAR_COUNT; i++) {
641 res = &pdev->resource[i];
642 if (res->parent || !res->flags)
643 continue;
644 pci_claim_resource(pdev, i);
645 }
646
647 return 0;
648 }
649
pcibios_enable_device(struct pci_dev * pdev,int mask)650 int pcibios_enable_device(struct pci_dev *pdev, int mask)
651 {
652 struct zpci_dev *zdev = get_zdev(pdev);
653
654 zdev->pdev = pdev;
655 zpci_debug_init_device(zdev);
656 zpci_fmb_enable_device(zdev);
657 zpci_map_resources(zdev);
658
659 return pci_enable_resources(pdev, mask);
660 }
661
pcibios_disable_device(struct pci_dev * pdev)662 void pcibios_disable_device(struct pci_dev *pdev)
663 {
664 struct zpci_dev *zdev = get_zdev(pdev);
665
666 zpci_unmap_resources(zdev);
667 zpci_fmb_disable_device(zdev);
668 zpci_debug_exit_device(zdev);
669 zdev->pdev = NULL;
670 }
671
672 #ifdef CONFIG_HIBERNATE_CALLBACKS
zpci_restore(struct device * dev)673 static int zpci_restore(struct device *dev)
674 {
675 struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
676 int ret = 0;
677
678 if (zdev->state != ZPCI_FN_STATE_ONLINE)
679 goto out;
680
681 ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
682 if (ret)
683 goto out;
684
685 zpci_map_resources(zdev);
686 zpci_register_ioat(zdev, 0, zdev->start_dma + PAGE_OFFSET,
687 zdev->start_dma + zdev->iommu_size - 1,
688 (u64) zdev->dma_table);
689
690 out:
691 return ret;
692 }
693
zpci_freeze(struct device * dev)694 static int zpci_freeze(struct device *dev)
695 {
696 struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
697
698 if (zdev->state != ZPCI_FN_STATE_ONLINE)
699 return 0;
700
701 zpci_unregister_ioat(zdev, 0);
702 return clp_disable_fh(zdev);
703 }
704
705 struct dev_pm_ops pcibios_pm_ops = {
706 .thaw_noirq = zpci_restore,
707 .freeze_noirq = zpci_freeze,
708 .restore_noirq = zpci_restore,
709 .poweroff_noirq = zpci_freeze,
710 };
711 #endif /* CONFIG_HIBERNATE_CALLBACKS */
712
zpci_alloc_domain(struct zpci_dev * zdev)713 static int zpci_alloc_domain(struct zpci_dev *zdev)
714 {
715 spin_lock(&zpci_domain_lock);
716 zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
717 if (zdev->domain == ZPCI_NR_DEVICES) {
718 spin_unlock(&zpci_domain_lock);
719 return -ENOSPC;
720 }
721 set_bit(zdev->domain, zpci_domain);
722 spin_unlock(&zpci_domain_lock);
723 return 0;
724 }
725
zpci_free_domain(struct zpci_dev * zdev)726 static void zpci_free_domain(struct zpci_dev *zdev)
727 {
728 spin_lock(&zpci_domain_lock);
729 clear_bit(zdev->domain, zpci_domain);
730 spin_unlock(&zpci_domain_lock);
731 }
732
pcibios_remove_bus(struct pci_bus * bus)733 void pcibios_remove_bus(struct pci_bus *bus)
734 {
735 struct zpci_dev *zdev = get_zdev_by_bus(bus);
736
737 zpci_exit_slot(zdev);
738 zpci_cleanup_bus_resources(zdev);
739 zpci_free_domain(zdev);
740
741 spin_lock(&zpci_list_lock);
742 list_del(&zdev->entry);
743 spin_unlock(&zpci_list_lock);
744
745 kfree(zdev);
746 }
747
zpci_scan_bus(struct zpci_dev * zdev)748 static int zpci_scan_bus(struct zpci_dev *zdev)
749 {
750 LIST_HEAD(resources);
751 int ret;
752
753 ret = zpci_setup_bus_resources(zdev, &resources);
754 if (ret)
755 return ret;
756
757 zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
758 zdev, &resources);
759 if (!zdev->bus) {
760 zpci_cleanup_bus_resources(zdev);
761 return -EIO;
762 }
763
764 zdev->bus->max_bus_speed = zdev->max_bus_speed;
765 return 0;
766 }
767
zpci_enable_device(struct zpci_dev * zdev)768 int zpci_enable_device(struct zpci_dev *zdev)
769 {
770 int rc;
771
772 rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
773 if (rc)
774 goto out;
775
776 rc = zpci_dma_init_device(zdev);
777 if (rc)
778 goto out_dma;
779
780 zdev->state = ZPCI_FN_STATE_ONLINE;
781 return 0;
782
783 out_dma:
784 clp_disable_fh(zdev);
785 out:
786 return rc;
787 }
788 EXPORT_SYMBOL_GPL(zpci_enable_device);
789
zpci_disable_device(struct zpci_dev * zdev)790 int zpci_disable_device(struct zpci_dev *zdev)
791 {
792 zpci_dma_exit_device(zdev);
793 return clp_disable_fh(zdev);
794 }
795 EXPORT_SYMBOL_GPL(zpci_disable_device);
796
zpci_create_device(struct zpci_dev * zdev)797 int zpci_create_device(struct zpci_dev *zdev)
798 {
799 int rc;
800
801 rc = zpci_alloc_domain(zdev);
802 if (rc)
803 goto out;
804
805 if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
806 rc = zpci_enable_device(zdev);
807 if (rc)
808 goto out_free;
809 }
810 rc = zpci_scan_bus(zdev);
811 if (rc)
812 goto out_disable;
813
814 spin_lock(&zpci_list_lock);
815 list_add_tail(&zdev->entry, &zpci_list);
816 spin_unlock(&zpci_list_lock);
817
818 zpci_init_slot(zdev);
819
820 return 0;
821
822 out_disable:
823 if (zdev->state == ZPCI_FN_STATE_ONLINE)
824 zpci_disable_device(zdev);
825 out_free:
826 zpci_free_domain(zdev);
827 out:
828 return rc;
829 }
830
zpci_stop_device(struct zpci_dev * zdev)831 void zpci_stop_device(struct zpci_dev *zdev)
832 {
833 zpci_dma_exit_device(zdev);
834 /*
835 * Note: SCLP disables fh via set-pci-fn so don't
836 * do that here.
837 */
838 }
839 EXPORT_SYMBOL_GPL(zpci_stop_device);
840
barsize(u8 size)841 static inline int barsize(u8 size)
842 {
843 return (size) ? (1 << size) >> 10 : 0;
844 }
845
zpci_mem_init(void)846 static int zpci_mem_init(void)
847 {
848 BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
849 __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
850
851 zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
852 __alignof__(struct zpci_fmb), 0, NULL);
853 if (!zdev_fmb_cache)
854 goto error_zdev;
855
856 /* TODO: use realloc */
857 zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
858 GFP_KERNEL);
859 if (!zpci_iomap_start)
860 goto error_iomap;
861 return 0;
862
863 error_iomap:
864 kmem_cache_destroy(zdev_fmb_cache);
865 error_zdev:
866 return -ENOMEM;
867 }
868
zpci_mem_exit(void)869 static void zpci_mem_exit(void)
870 {
871 kfree(zpci_iomap_start);
872 kmem_cache_destroy(zdev_fmb_cache);
873 }
874
875 static unsigned int s390_pci_probe = 1;
876 static unsigned int s390_pci_initialized;
877
pcibios_setup(char * str)878 char * __init pcibios_setup(char *str)
879 {
880 if (!strcmp(str, "off")) {
881 s390_pci_probe = 0;
882 return NULL;
883 }
884 return str;
885 }
886
zpci_is_enabled(void)887 bool zpci_is_enabled(void)
888 {
889 return s390_pci_initialized;
890 }
891
pci_base_init(void)892 static int __init pci_base_init(void)
893 {
894 int rc;
895
896 if (!s390_pci_probe)
897 return 0;
898
899 if (!test_facility(2) || !test_facility(69)
900 || !test_facility(71) || !test_facility(72))
901 return 0;
902
903 rc = zpci_debug_init();
904 if (rc)
905 goto out;
906
907 rc = zpci_mem_init();
908 if (rc)
909 goto out_mem;
910
911 rc = zpci_irq_init();
912 if (rc)
913 goto out_irq;
914
915 rc = zpci_dma_init();
916 if (rc)
917 goto out_dma;
918
919 rc = clp_scan_pci_devices();
920 if (rc)
921 goto out_find;
922
923 s390_pci_initialized = 1;
924 return 0;
925
926 out_find:
927 zpci_dma_exit();
928 out_dma:
929 zpci_irq_exit();
930 out_irq:
931 zpci_mem_exit();
932 out_mem:
933 zpci_debug_exit();
934 out:
935 return rc;
936 }
937 subsys_initcall_sync(pci_base_init);
938
zpci_rescan(void)939 void zpci_rescan(void)
940 {
941 if (zpci_is_enabled())
942 clp_rescan_pci_devices_simple();
943 }
944