blob: 70f6c56c8d0f30b8b635980b6624d42a06b65233 [file] [log] [blame]
Jan Glaubercd248342012-11-29 12:50:30 +01001/*
2 * Copyright IBM Corp. 2012
3 *
4 * Author(s):
5 * Jan Glauber <jang@linux.vnet.ibm.com>
6 *
7 * The System z PCI code is a rewrite from a prototype by
8 * the following people (Kudoz!):
9 * Alexander Schmidt <alexschm@de.ibm.com>
10 * Christoph Raisch <raisch@de.ibm.com>
11 * Hannes Hering <hering2@de.ibm.com>
12 * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
13 * Jan-Bernd Themann <themann@de.ibm.com>
14 * Stefan Roscher <stefan.roscher@de.ibm.com>
15 * Thomas Klein <tklein@de.ibm.com>
16 */
17
18#define COMPONENT "zPCI"
19#define pr_fmt(fmt) COMPONENT ": " fmt
20
21#include <linux/kernel.h>
22#include <linux/slab.h>
23#include <linux/err.h>
24#include <linux/export.h>
25#include <linux/delay.h>
26#include <linux/seq_file.h>
27#include <linux/pci.h>
28#include <linux/msi.h>
29
30#include <asm/facility.h>
31#include <asm/pci_insn.h>
Jan Glaubera755a452012-11-29 12:55:21 +010032#include <asm/pci_clp.h>
Jan Glaubercd248342012-11-29 12:50:30 +010033
34#define DEBUG /* enable pr_debug */
35
36#define ZPCI_NR_DMA_SPACES 1
37#define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
38
39/* list of all detected zpci devices */
40LIST_HEAD(zpci_list);
41DEFINE_MUTEX(zpci_list_lock);
42
43static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
44static DEFINE_SPINLOCK(zpci_domain_lock);
45
46/* I/O Map */
47static DEFINE_SPINLOCK(zpci_iomap_lock);
48static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
49struct zpci_iomap_entry *zpci_iomap_start;
50EXPORT_SYMBOL_GPL(zpci_iomap_start);
51
52struct zpci_dev *get_zdev(struct pci_dev *pdev)
53{
54 return (struct zpci_dev *) pdev->sysdata;
55}
56
57struct zpci_dev *get_zdev_by_fid(u32 fid)
58{
59 struct zpci_dev *tmp, *zdev = NULL;
60
61 mutex_lock(&zpci_list_lock);
62 list_for_each_entry(tmp, &zpci_list, entry) {
63 if (tmp->fid == fid) {
64 zdev = tmp;
65 break;
66 }
67 }
68 mutex_unlock(&zpci_list_lock);
69 return zdev;
70}
71
72bool zpci_fid_present(u32 fid)
73{
74 return (get_zdev_by_fid(fid) != NULL) ? true : false;
75}
76
77static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
78{
79 return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
80}
81
82int pci_domain_nr(struct pci_bus *bus)
83{
84 return ((struct zpci_dev *) bus->sysdata)->domain;
85}
86EXPORT_SYMBOL_GPL(pci_domain_nr);
87
88int pci_proc_domain(struct pci_bus *bus)
89{
90 return pci_domain_nr(bus);
91}
92EXPORT_SYMBOL_GPL(pci_proc_domain);
93
94/* Store PCI function information block */
95static int zpci_store_fib(struct zpci_dev *zdev, u8 *fc)
96{
97 struct zpci_fib *fib;
98 u8 status, cc;
99
100 fib = (void *) get_zeroed_page(GFP_KERNEL);
101 if (!fib)
102 return -ENOMEM;
103
104 do {
105 cc = __stpcifc(zdev->fh, 0, fib, &status);
106 if (cc == 2) {
107 msleep(ZPCI_INSN_BUSY_DELAY);
108 memset(fib, 0, PAGE_SIZE);
109 }
110 } while (cc == 2);
111
112 if (cc)
113 pr_err_once("%s: cc: %u status: %u\n",
114 __func__, cc, status);
115
116 /* Return PCI function controls */
117 *fc = fib->fc;
118
119 free_page((unsigned long) fib);
120 return (cc) ? -EIO : 0;
121}
122
123#define ZPCI_PCIAS_CFGSPC 15
124
125static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
126{
127 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
128 u64 data;
129 int rc;
130
131 rc = pcilg_instr(&data, req, offset);
132 data = data << ((8 - len) * 8);
133 data = le64_to_cpu(data);
134 if (!rc)
135 *val = (u32) data;
136 else
137 *val = 0xffffffff;
138 return rc;
139}
140
141static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
142{
143 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
144 u64 data = val;
145 int rc;
146
147 data = cpu_to_le64(data);
148 data = data >> ((8 - len) * 8);
149 rc = pcistg_instr(data, req, offset);
150 return rc;
151}
152
153void __devinit pcibios_fixup_bus(struct pci_bus *bus)
154{
155}
156
157resource_size_t pcibios_align_resource(void *data, const struct resource *res,
158 resource_size_t size,
159 resource_size_t align)
160{
161 return 0;
162}
163
164/* Create a virtual mapping cookie for a PCI BAR */
165void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
166{
167 struct zpci_dev *zdev = get_zdev(pdev);
168 u64 addr;
169 int idx;
170
171 if ((bar & 7) != bar)
172 return NULL;
173
174 idx = zdev->bars[bar].map_idx;
175 spin_lock(&zpci_iomap_lock);
176 zpci_iomap_start[idx].fh = zdev->fh;
177 zpci_iomap_start[idx].bar = bar;
178 spin_unlock(&zpci_iomap_lock);
179
180 addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
181 return (void __iomem *) addr;
182}
183EXPORT_SYMBOL_GPL(pci_iomap);
184
185void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
186{
187 unsigned int idx;
188
189 idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
190 spin_lock(&zpci_iomap_lock);
191 zpci_iomap_start[idx].fh = 0;
192 zpci_iomap_start[idx].bar = 0;
193 spin_unlock(&zpci_iomap_lock);
194}
195EXPORT_SYMBOL_GPL(pci_iounmap);
196
197static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
198 int size, u32 *val)
199{
200 struct zpci_dev *zdev = get_zdev_by_bus(bus);
201
202 if (!zdev || devfn != ZPCI_DEVFN)
203 return 0;
204 return zpci_cfg_load(zdev, where, val, size);
205}
206
207static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
208 int size, u32 val)
209{
210 struct zpci_dev *zdev = get_zdev_by_bus(bus);
211
212 if (!zdev || devfn != ZPCI_DEVFN)
213 return 0;
214 return zpci_cfg_store(zdev, where, val, size);
215}
216
217static struct pci_ops pci_root_ops = {
218 .read = pci_read,
219 .write = pci_write,
220};
221
222static void zpci_map_resources(struct zpci_dev *zdev)
223{
224 struct pci_dev *pdev = zdev->pdev;
225 resource_size_t len;
226 int i;
227
228 for (i = 0; i < PCI_BAR_COUNT; i++) {
229 len = pci_resource_len(pdev, i);
230 if (!len)
231 continue;
232 pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
233 pdev->resource[i].end = pdev->resource[i].start + len - 1;
234 pr_debug("BAR%i: -> start: %Lx end: %Lx\n",
235 i, pdev->resource[i].start, pdev->resource[i].end);
236 }
237};
238
239static void zpci_unmap_resources(struct pci_dev *pdev)
240{
241 resource_size_t len;
242 int i;
243
244 for (i = 0; i < PCI_BAR_COUNT; i++) {
245 len = pci_resource_len(pdev, i);
246 if (!len)
247 continue;
248 pci_iounmap(pdev, (void *) pdev->resource[i].start);
249 }
250};
251
252struct zpci_dev *zpci_alloc_device(void)
253{
254 struct zpci_dev *zdev;
255
256 /* Alloc memory for our private pci device data */
257 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
258 if (!zdev)
259 return ERR_PTR(-ENOMEM);
260 return zdev;
261}
262
263void zpci_free_device(struct zpci_dev *zdev)
264{
265 kfree(zdev);
266}
267
268/* Called on removal of pci_dev, leaves zpci and bus device */
269static void zpci_remove_device(struct pci_dev *pdev)
270{
271 struct zpci_dev *zdev = get_zdev(pdev);
272
273 dev_info(&pdev->dev, "Removing device %u\n", zdev->domain);
274 zdev->state = ZPCI_FN_STATE_CONFIGURED;
275 zpci_unmap_resources(pdev);
276 list_del(&zdev->entry); /* can be called from init */
277 zdev->pdev = NULL;
278}
279
280static void zpci_scan_devices(void)
281{
282 struct zpci_dev *zdev;
283
284 mutex_lock(&zpci_list_lock);
285 list_for_each_entry(zdev, &zpci_list, entry)
286 if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
287 zpci_scan_device(zdev);
288 mutex_unlock(&zpci_list_lock);
289}
290
291/*
292 * Too late for any s390 specific setup, since interrupts must be set up
293 * already which requires DMA setup too and the pci scan will access the
294 * config space, which only works if the function handle is enabled.
295 */
296int pcibios_enable_device(struct pci_dev *pdev, int mask)
297{
298 struct resource *res;
299 u16 cmd;
300 int i;
301
302 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
303
304 for (i = 0; i < PCI_BAR_COUNT; i++) {
305 res = &pdev->resource[i];
306
307 if (res->flags & IORESOURCE_IO)
308 return -EINVAL;
309
310 if (res->flags & IORESOURCE_MEM)
311 cmd |= PCI_COMMAND_MEMORY;
312 }
313 pci_write_config_word(pdev, PCI_COMMAND, cmd);
314 return 0;
315}
316
317void pcibios_disable_device(struct pci_dev *pdev)
318{
319 zpci_remove_device(pdev);
320 pdev->sysdata = NULL;
321}
322
323static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
324 unsigned long flags, int domain)
325{
326 struct resource *r;
327 char *name;
328 int rc;
329
330 r = kzalloc(sizeof(*r), GFP_KERNEL);
331 if (!r)
332 return ERR_PTR(-ENOMEM);
333 r->start = start;
334 r->end = r->start + size - 1;
335 r->flags = flags;
336 r->parent = &iomem_resource;
337 name = kmalloc(18, GFP_KERNEL);
338 if (!name) {
339 kfree(r);
340 return ERR_PTR(-ENOMEM);
341 }
342 sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
343 r->name = name;
344
345 rc = request_resource(&iomem_resource, r);
346 if (rc)
347 pr_debug("request resource %pR failed\n", r);
348 return r;
349}
350
351static int zpci_alloc_iomap(struct zpci_dev *zdev)
352{
353 int entry;
354
355 spin_lock(&zpci_iomap_lock);
356 entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
357 if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
358 spin_unlock(&zpci_iomap_lock);
359 return -ENOSPC;
360 }
361 set_bit(entry, zpci_iomap);
362 spin_unlock(&zpci_iomap_lock);
363 return entry;
364}
365
366static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
367{
368 spin_lock(&zpci_iomap_lock);
369 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
370 clear_bit(entry, zpci_iomap);
371 spin_unlock(&zpci_iomap_lock);
372}
373
374static int zpci_create_device_bus(struct zpci_dev *zdev)
375{
376 struct resource *res;
377 LIST_HEAD(resources);
378 int i;
379
380 /* allocate mapping entry for each used bar */
381 for (i = 0; i < PCI_BAR_COUNT; i++) {
382 unsigned long addr, size, flags;
383 int entry;
384
385 if (!zdev->bars[i].size)
386 continue;
387 entry = zpci_alloc_iomap(zdev);
388 if (entry < 0)
389 return entry;
390 zdev->bars[i].map_idx = entry;
391
392 /* only MMIO is supported */
393 flags = IORESOURCE_MEM;
394 if (zdev->bars[i].val & 8)
395 flags |= IORESOURCE_PREFETCH;
396 if (zdev->bars[i].val & 4)
397 flags |= IORESOURCE_MEM_64;
398
399 addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
400
401 size = 1UL << zdev->bars[i].size;
402
403 res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
404 if (IS_ERR(res)) {
405 zpci_free_iomap(zdev, entry);
406 return PTR_ERR(res);
407 }
408 pci_add_resource(&resources, res);
409 }
410
411 zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
412 zdev, &resources);
413 if (!zdev->bus)
414 return -EIO;
415
416 zdev->bus->max_bus_speed = zdev->max_bus_speed;
417 return 0;
418}
419
420static int zpci_alloc_domain(struct zpci_dev *zdev)
421{
422 spin_lock(&zpci_domain_lock);
423 zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
424 if (zdev->domain == ZPCI_NR_DEVICES) {
425 spin_unlock(&zpci_domain_lock);
426 return -ENOSPC;
427 }
428 set_bit(zdev->domain, zpci_domain);
429 spin_unlock(&zpci_domain_lock);
430 return 0;
431}
432
433static void zpci_free_domain(struct zpci_dev *zdev)
434{
435 spin_lock(&zpci_domain_lock);
436 clear_bit(zdev->domain, zpci_domain);
437 spin_unlock(&zpci_domain_lock);
438}
439
Jan Glaubera755a452012-11-29 12:55:21 +0100440int zpci_enable_device(struct zpci_dev *zdev)
441{
442 int rc;
443
444 rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
445 if (rc)
446 goto out;
447 pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
448 return 0;
449out:
450 return rc;
451}
452EXPORT_SYMBOL_GPL(zpci_enable_device);
453
Jan Glaubercd248342012-11-29 12:50:30 +0100454int zpci_create_device(struct zpci_dev *zdev)
455{
456 int rc;
457
458 rc = zpci_alloc_domain(zdev);
459 if (rc)
460 goto out;
461
462 rc = zpci_create_device_bus(zdev);
463 if (rc)
464 goto out_bus;
465
466 mutex_lock(&zpci_list_lock);
467 list_add_tail(&zdev->entry, &zpci_list);
468 mutex_unlock(&zpci_list_lock);
469
470 if (zdev->state == ZPCI_FN_STATE_STANDBY)
471 return 0;
472
Jan Glaubera755a452012-11-29 12:55:21 +0100473 rc = zpci_enable_device(zdev);
474 if (rc)
475 goto out_start;
Jan Glaubercd248342012-11-29 12:50:30 +0100476 return 0;
477
Jan Glaubera755a452012-11-29 12:55:21 +0100478out_start:
479 mutex_lock(&zpci_list_lock);
480 list_del(&zdev->entry);
481 mutex_unlock(&zpci_list_lock);
Jan Glaubercd248342012-11-29 12:50:30 +0100482out_bus:
483 zpci_free_domain(zdev);
484out:
485 return rc;
486}
487
488void zpci_stop_device(struct zpci_dev *zdev)
489{
490 /*
491 * Note: SCLP disables fh via set-pci-fn so don't
492 * do that here.
493 */
494}
495EXPORT_SYMBOL_GPL(zpci_stop_device);
496
497int zpci_scan_device(struct zpci_dev *zdev)
498{
499 zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN);
500 if (!zdev->pdev) {
501 pr_err("pci_scan_single_device failed for fid: 0x%x\n",
502 zdev->fid);
503 goto out;
504 }
505
506 zpci_map_resources(zdev);
507 pci_bus_add_devices(zdev->bus);
508
509 /* now that pdev was added to the bus mark it as used */
510 zdev->state = ZPCI_FN_STATE_ONLINE;
511 return 0;
512
513out:
Jan Glaubera755a452012-11-29 12:55:21 +0100514 clp_disable_fh(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100515 return -EIO;
516}
517EXPORT_SYMBOL_GPL(zpci_scan_device);
518
519static inline int barsize(u8 size)
520{
521 return (size) ? (1 << size) >> 10 : 0;
522}
523
524static int zpci_mem_init(void)
525{
526 /* TODO: use realloc */
527 zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
528 GFP_KERNEL);
529 if (!zpci_iomap_start)
530 goto error_zdev;
531 return 0;
532
533error_zdev:
534 return -ENOMEM;
535}
536
537static void zpci_mem_exit(void)
538{
539 kfree(zpci_iomap_start);
540}
541
542unsigned int pci_probe = 1;
543EXPORT_SYMBOL_GPL(pci_probe);
544
545char * __init pcibios_setup(char *str)
546{
547 if (!strcmp(str, "off")) {
548 pci_probe = 0;
549 return NULL;
550 }
551 return str;
552}
553
554static int __init pci_base_init(void)
555{
556 int rc;
557
558 if (!pci_probe)
559 return 0;
560
561 if (!test_facility(2) || !test_facility(69)
562 || !test_facility(71) || !test_facility(72))
563 return 0;
564
565 pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n",
566 test_facility(69), test_facility(70),
567 test_facility(71));
568
569 rc = zpci_mem_init();
570 if (rc)
571 goto out_mem;
572
Jan Glaubera755a452012-11-29 12:55:21 +0100573 rc = clp_find_pci_devices();
574 if (rc)
575 goto out_find;
576
Jan Glaubercd248342012-11-29 12:50:30 +0100577 zpci_scan_devices();
578 return 0;
579
Jan Glaubera755a452012-11-29 12:55:21 +0100580out_find:
Jan Glaubercd248342012-11-29 12:50:30 +0100581 zpci_mem_exit();
582out_mem:
583 return rc;
584}
585subsys_initcall(pci_base_init);