arch/tile: improve support for PCI hotplug
[deliverable/linux.git] / arch / tile / kernel / pci.c
1 /*
2 * Copyright 2011 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/capability.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/bootmem.h>
24 #include <linux/irq.h>
25 #include <linux/io.h>
26 #include <linux/uaccess.h>
27
28 #include <asm/processor.h>
29 #include <asm/sections.h>
30 #include <asm/byteorder.h>
31 #include <asm/hv_driver.h>
32 #include <hv/drv_pcie_rc_intf.h>
33
34
35 /*
36 * Initialization flow and process
37 * -------------------------------
38 *
39 * This files contains the routines to search for PCI buses,
40 * enumerate the buses, and configure any attached devices.
41 *
42 * There are two entry points here:
43 * 1) tile_pci_init
44 * This sets up the pci_controller structs, and opens the
45 * FDs to the hypervisor. This is called from setup_arch() early
46 * in the boot process.
47 * 2) pcibios_init
48 * This probes the PCI bus(es) for any attached hardware. It's
49 * called by subsys_initcall. All of the real work is done by the
50 * generic Linux PCI layer.
51 *
52 */
53
54 /*
55 * This flag tells if the platform is TILEmpower that needs
56 * special configuration for the PLX switch chip.
57 */
58 int __write_once tile_plx_gen1;
59
60 static struct pci_controller controllers[TILE_NUM_PCIE];
61 static int num_controllers;
62 static int pci_scan_flags[TILE_NUM_PCIE];
63
64 static struct pci_ops tile_cfg_ops;
65
66
67 /*
68 * We don't need to worry about the alignment of resources.
69 */
70 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
71 resource_size_t size, resource_size_t align)
72 {
73 return res->start;
74 }
75 EXPORT_SYMBOL(pcibios_align_resource);
76
77 /*
78 * Open a FD to the hypervisor PCI device.
79 *
80 * controller_id is the controller number, config type is 0 or 1 for
81 * config0 or config1 operations.
82 */
83 static int __devinit tile_pcie_open(int controller_id, int config_type)
84 {
85 char filename[32];
86 int fd;
87
88 sprintf(filename, "pcie/%d/config%d", controller_id, config_type);
89
90 fd = hv_dev_open((HV_VirtAddr)filename, 0);
91
92 return fd;
93 }
94
95
96 /*
97 * Get the IRQ numbers from the HV and set up the handlers for them.
98 */
99 static int __devinit tile_init_irqs(int controller_id,
100 struct pci_controller *controller)
101 {
102 char filename[32];
103 int fd;
104 int ret;
105 int x;
106 struct pcie_rc_config rc_config;
107
108 sprintf(filename, "pcie/%d/ctl", controller_id);
109 fd = hv_dev_open((HV_VirtAddr)filename, 0);
110 if (fd < 0) {
111 pr_err("PCI: hv_dev_open(%s) failed\n", filename);
112 return -1;
113 }
114 ret = hv_dev_pread(fd, 0, (HV_VirtAddr)(&rc_config),
115 sizeof(rc_config), PCIE_RC_CONFIG_MASK_OFF);
116 hv_dev_close(fd);
117 if (ret != sizeof(rc_config)) {
118 pr_err("PCI: wanted %zd bytes, got %d\n",
119 sizeof(rc_config), ret);
120 return -1;
121 }
122 /* Record irq_base so that we can map INTx to IRQ # later. */
123 controller->irq_base = rc_config.intr;
124
125 for (x = 0; x < 4; x++)
126 tile_irq_activate(rc_config.intr + x,
127 TILE_IRQ_HW_CLEAR);
128
129 if (rc_config.plx_gen1)
130 controller->plx_gen1 = 1;
131
132 return 0;
133 }
134
135 /*
136 * First initialization entry point, called from setup_arch().
137 *
138 * Find valid controllers and fill in pci_controller structs for each
139 * of them.
140 *
141 * Returns the number of controllers discovered.
142 */
143 int __devinit tile_pci_init(void)
144 {
145 int i;
146
147 pr_info("PCI: Searching for controllers...\n");
148
149 /* Re-init number of PCIe controllers to support hot-plug feature. */
150 num_controllers = 0;
151
152 /* Do any configuration we need before using the PCIe */
153
154 for (i = 0; i < TILE_NUM_PCIE; i++) {
155 /*
156 * To see whether we need a real config op based on
157 * the results of pcibios_init(), to support PCIe hot-plug.
158 */
159 if (pci_scan_flags[i] == 0) {
160 int hv_cfg_fd0 = -1;
161 int hv_cfg_fd1 = -1;
162 int hv_mem_fd = -1;
163 char name[32];
164 struct pci_controller *controller;
165
166 /*
167 * Open the fd to the HV. If it fails then this
168 * device doesn't exist.
169 */
170 hv_cfg_fd0 = tile_pcie_open(i, 0);
171 if (hv_cfg_fd0 < 0)
172 continue;
173 hv_cfg_fd1 = tile_pcie_open(i, 1);
174 if (hv_cfg_fd1 < 0) {
175 pr_err("PCI: Couldn't open config fd to HV "
176 "for controller %d\n", i);
177 goto err_cont;
178 }
179
180 sprintf(name, "pcie/%d/mem", i);
181 hv_mem_fd = hv_dev_open((HV_VirtAddr)name, 0);
182 if (hv_mem_fd < 0) {
183 pr_err("PCI: Could not open mem fd to HV!\n");
184 goto err_cont;
185 }
186
187 pr_info("PCI: Found PCI controller #%d\n", i);
188
189 controller = &controllers[i];
190
191 if (tile_init_irqs(i, controller)) {
192 pr_err("PCI: Could not initialize "
193 "IRQs, aborting.\n");
194 goto err_cont;
195 }
196
197 controller->index = i;
198 controller->hv_cfg_fd[0] = hv_cfg_fd0;
199 controller->hv_cfg_fd[1] = hv_cfg_fd1;
200 controller->hv_mem_fd = hv_mem_fd;
201 controller->first_busno = 0;
202 controller->last_busno = 0xff;
203 controller->ops = &tile_cfg_ops;
204
205 num_controllers++;
206 continue;
207
208 err_cont:
209 if (hv_cfg_fd0 >= 0)
210 hv_dev_close(hv_cfg_fd0);
211 if (hv_cfg_fd1 >= 0)
212 hv_dev_close(hv_cfg_fd1);
213 if (hv_mem_fd >= 0)
214 hv_dev_close(hv_mem_fd);
215 continue;
216 }
217 }
218
219 /*
220 * Before using the PCIe, see if we need to do any platform-specific
221 * configuration, such as the PLX switch Gen 1 issue on TILEmpower.
222 */
223 for (i = 0; i < num_controllers; i++) {
224 struct pci_controller *controller = &controllers[i];
225
226 if (controller->plx_gen1)
227 tile_plx_gen1 = 1;
228 }
229
230 return num_controllers;
231 }
232
233 /*
234 * (pin - 1) converts from the PCI standard's [1:4] convention to
235 * a normal [0:3] range.
236 */
237 static int tile_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
238 {
239 struct pci_controller *controller =
240 (struct pci_controller *)dev->sysdata;
241 return (pin - 1) + controller->irq_base;
242 }
243
244
245 static void __devinit fixup_read_and_payload_sizes(void)
246 {
247 struct pci_dev *dev = NULL;
248 int smallest_max_payload = 0x1; /* Tile maxes out at 256 bytes. */
249 int max_read_size = 0x2; /* Limit to 512 byte reads. */
250 u16 new_values;
251
252 /* Scan for the smallest maximum payload size. */
253 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
254 int pcie_caps_offset;
255 u32 devcap;
256 int max_payload;
257
258 pcie_caps_offset = pci_find_capability(dev, PCI_CAP_ID_EXP);
259 if (pcie_caps_offset == 0)
260 continue;
261
262 pci_read_config_dword(dev, pcie_caps_offset + PCI_EXP_DEVCAP,
263 &devcap);
264 max_payload = devcap & PCI_EXP_DEVCAP_PAYLOAD;
265 if (max_payload < smallest_max_payload)
266 smallest_max_payload = max_payload;
267 }
268
269 /* Now, set the max_payload_size for all devices to that value. */
270 new_values = (max_read_size << 12) | (smallest_max_payload << 5);
271 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
272 int pcie_caps_offset;
273 u16 devctl;
274
275 pcie_caps_offset = pci_find_capability(dev, PCI_CAP_ID_EXP);
276 if (pcie_caps_offset == 0)
277 continue;
278
279 pci_read_config_word(dev, pcie_caps_offset + PCI_EXP_DEVCTL,
280 &devctl);
281 devctl &= ~(PCI_EXP_DEVCTL_PAYLOAD | PCI_EXP_DEVCTL_READRQ);
282 devctl |= new_values;
283 pci_write_config_word(dev, pcie_caps_offset + PCI_EXP_DEVCTL,
284 devctl);
285 }
286 }
287
288
289 /*
290 * Second PCI initialization entry point, called by subsys_initcall.
291 *
292 * The controllers have been set up by the time we get here, by a call to
293 * tile_pci_init.
294 */
295 int __devinit pcibios_init(void)
296 {
297 int i;
298
299 pr_info("PCI: Probing PCI hardware\n");
300
301 /*
302 * Delay a bit in case devices aren't ready. Some devices are
303 * known to require at least 20ms here, but we use a more
304 * conservative value.
305 */
306 mdelay(250);
307
308 /* Scan all of the recorded PCI controllers. */
309 for (i = 0; i < TILE_NUM_PCIE; i++) {
310 /*
311 * Do real pcibios init ops if the controller is initialized
312 * by tile_pci_init() successfully and not initialized by
313 * pcibios_init() yet to support PCIe hot-plug.
314 */
315 if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
316 struct pci_controller *controller = &controllers[i];
317 struct pci_bus *bus;
318
319 pr_info("PCI: initializing controller #%d\n", i);
320
321 /*
322 * This comes from the generic Linux PCI driver.
323 *
324 * It reads the PCI tree for this bus into the Linux
325 * data structures.
326 *
327 * This is inlined in linux/pci.h and calls into
328 * pci_scan_bus_parented() in probe.c.
329 */
330 bus = pci_scan_bus(0, controller->ops, controller);
331 controller->root_bus = bus;
332 controller->last_busno = bus->subordinate;
333 }
334 }
335
336 /* Do machine dependent PCI interrupt routing */
337 pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
338
339 /*
340 * This comes from the generic Linux PCI driver.
341 *
342 * It allocates all of the resources (I/O memory, etc)
343 * associated with the devices read in above.
344 */
345 pci_assign_unassigned_resources();
346
347 /* Configure the max_read_size and max_payload_size values. */
348 fixup_read_and_payload_sizes();
349
350 /* Record the I/O resources in the PCI controller structure. */
351 for (i = 0; i < TILE_NUM_PCIE; i++) {
352 /*
353 * Do real pcibios init ops if the controller is initialized
354 * by tile_pci_init() successfully and not initialized by
355 * pcibios_init() yet to support PCIe hot-plug.
356 */
357 if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
358 struct pci_bus *root_bus = controllers[i].root_bus;
359 struct pci_bus *next_bus;
360 struct pci_dev *dev;
361
362 list_for_each_entry(dev, &root_bus->devices, bus_list) {
363 /*
364 * Find the PCI host controller, ie. the 1st
365 * bridge.
366 */
367 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
368 (PCI_SLOT(dev->devfn) == 0)) {
369 next_bus = dev->subordinate;
370 controllers[i].mem_resources[0] =
371 *next_bus->resource[0];
372 controllers[i].mem_resources[1] =
373 *next_bus->resource[1];
374 controllers[i].mem_resources[2] =
375 *next_bus->resource[2];
376
377 /* Setup flags. */
378 pci_scan_flags[i] = 1;
379
380 break;
381 }
382 }
383 }
384 }
385
386 return 0;
387 }
388 subsys_initcall(pcibios_init);
389
390 /*
391 * No bus fixups needed.
392 */
393 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
394 {
395 /* Nothing needs to be done. */
396 }
397
398 /*
399 * This can be called from the generic PCI layer, but doesn't need to
400 * do anything.
401 */
402 char __devinit *pcibios_setup(char *str)
403 {
404 /* Nothing needs to be done. */
405 return str;
406 }
407
408 /*
409 * This is called from the generic Linux layer.
410 */
411 void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
412 {
413 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
414 }
415
416 /*
417 * Enable memory and/or address decoding, as appropriate, for the
418 * device described by the 'dev' struct.
419 *
420 * This is called from the generic PCI layer, and can be called
421 * for bridges or endpoints.
422 */
423 int pcibios_enable_device(struct pci_dev *dev, int mask)
424 {
425 u16 cmd, old_cmd;
426 u8 header_type;
427 int i;
428 struct resource *r;
429
430 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
431
432 pci_read_config_word(dev, PCI_COMMAND, &cmd);
433 old_cmd = cmd;
434 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
435 /*
436 * For bridges, we enable both memory and I/O decoding
437 * in call cases.
438 */
439 cmd |= PCI_COMMAND_IO;
440 cmd |= PCI_COMMAND_MEMORY;
441 } else {
442 /*
443 * For endpoints, we enable memory and/or I/O decoding
444 * only if they have a memory resource of that type.
445 */
446 for (i = 0; i < 6; i++) {
447 r = &dev->resource[i];
448 if (r->flags & IORESOURCE_UNSET) {
449 pr_err("PCI: Device %s not available "
450 "because of resource collisions\n",
451 pci_name(dev));
452 return -EINVAL;
453 }
454 if (r->flags & IORESOURCE_IO)
455 cmd |= PCI_COMMAND_IO;
456 if (r->flags & IORESOURCE_MEM)
457 cmd |= PCI_COMMAND_MEMORY;
458 }
459 }
460
461 /*
462 * We only write the command if it changed.
463 */
464 if (cmd != old_cmd)
465 pci_write_config_word(dev, PCI_COMMAND, cmd);
466 return 0;
467 }
468
469 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
470 {
471 unsigned long start = pci_resource_start(dev, bar);
472 unsigned long len = pci_resource_len(dev, bar);
473 unsigned long flags = pci_resource_flags(dev, bar);
474
475 if (!len)
476 return NULL;
477 if (max && len > max)
478 len = max;
479
480 if (!(flags & IORESOURCE_MEM)) {
481 pr_info("PCI: Trying to map invalid resource %#lx\n", flags);
482 start = 0;
483 }
484
485 return (void __iomem *)start;
486 }
487 EXPORT_SYMBOL(pci_iomap);
488
489
490 /****************************************************************
491 *
492 * Tile PCI config space read/write routines
493 *
494 ****************************************************************/
495
496 /*
497 * These are the normal read and write ops
498 * These are expanded with macros from pci_bus_read_config_byte() etc.
499 *
500 * devfn is the combined PCI slot & function.
501 *
502 * offset is in bytes, from the start of config space for the
503 * specified bus & slot.
504 */
505
506 static int __devinit tile_cfg_read(struct pci_bus *bus,
507 unsigned int devfn,
508 int offset,
509 int size,
510 u32 *val)
511 {
512 struct pci_controller *controller = bus->sysdata;
513 int busnum = bus->number & 0xff;
514 int slot = (devfn >> 3) & 0x1f;
515 int function = devfn & 0x7;
516 u32 addr;
517 int config_mode = 1;
518
519 /*
520 * There is no bridge between the Tile and bus 0, so we
521 * use config0 to talk to bus 0.
522 *
523 * If we're talking to a bus other than zero then we
524 * must have found a bridge.
525 */
526 if (busnum == 0) {
527 /*
528 * We fake an empty slot for (busnum == 0) && (slot > 0),
529 * since there is only one slot on bus 0.
530 */
531 if (slot) {
532 *val = 0xFFFFFFFF;
533 return 0;
534 }
535 config_mode = 0;
536 }
537
538 addr = busnum << 20; /* Bus in 27:20 */
539 addr |= slot << 15; /* Slot (device) in 19:15 */
540 addr |= function << 12; /* Function is in 14:12 */
541 addr |= (offset & 0xFFF); /* byte address in 0:11 */
542
543 return hv_dev_pread(controller->hv_cfg_fd[config_mode], 0,
544 (HV_VirtAddr)(val), size, addr);
545 }
546
547
548 /*
549 * See tile_cfg_read() for relevant comments.
550 * Note that "val" is the value to write, not a pointer to that value.
551 */
552 static int __devinit tile_cfg_write(struct pci_bus *bus,
553 unsigned int devfn,
554 int offset,
555 int size,
556 u32 val)
557 {
558 struct pci_controller *controller = bus->sysdata;
559 int busnum = bus->number & 0xff;
560 int slot = (devfn >> 3) & 0x1f;
561 int function = devfn & 0x7;
562 u32 addr;
563 int config_mode = 1;
564 HV_VirtAddr valp = (HV_VirtAddr)&val;
565
566 /*
567 * For bus 0 slot 0 we use config 0 accesses.
568 */
569 if (busnum == 0) {
570 /*
571 * We fake an empty slot for (busnum == 0) && (slot > 0),
572 * since there is only one slot on bus 0.
573 */
574 if (slot)
575 return 0;
576 config_mode = 0;
577 }
578
579 addr = busnum << 20; /* Bus in 27:20 */
580 addr |= slot << 15; /* Slot (device) in 19:15 */
581 addr |= function << 12; /* Function is in 14:12 */
582 addr |= (offset & 0xFFF); /* byte address in 0:11 */
583
584 #ifdef __BIG_ENDIAN
585 /* Point to the correct part of the 32-bit "val". */
586 valp += 4 - size;
587 #endif
588
589 return hv_dev_pwrite(controller->hv_cfg_fd[config_mode], 0,
590 valp, size, addr);
591 }
592
593
594 static struct pci_ops tile_cfg_ops = {
595 .read = tile_cfg_read,
596 .write = tile_cfg_write,
597 };
598
599
600 /*
601 * In the following, each PCI controller's mem_resources[1]
602 * represents its (non-prefetchable) PCI memory resource.
603 * mem_resources[0] and mem_resources[2] refer to its PCI I/O and
604 * prefetchable PCI memory resources, respectively.
605 * For more details, see pci_setup_bridge() in setup-bus.c.
606 * By comparing the target PCI memory address against the
607 * end address of controller 0, we can determine the controller
608 * that should accept the PCI memory access.
609 */
610 #define TILE_READ(size, type) \
611 type _tile_read##size(unsigned long addr) \
612 { \
613 type val; \
614 int idx = 0; \
615 if (addr > controllers[0].mem_resources[1].end && \
616 addr > controllers[0].mem_resources[2].end) \
617 idx = 1; \
618 if (hv_dev_pread(controllers[idx].hv_mem_fd, 0, \
619 (HV_VirtAddr)(&val), sizeof(type), addr)) \
620 pr_err("PCI: read %zd bytes at 0x%lX failed\n", \
621 sizeof(type), addr); \
622 return val; \
623 } \
624 EXPORT_SYMBOL(_tile_read##size)
625
626 TILE_READ(b, u8);
627 TILE_READ(w, u16);
628 TILE_READ(l, u32);
629 TILE_READ(q, u64);
630
631 #define TILE_WRITE(size, type) \
632 void _tile_write##size(type val, unsigned long addr) \
633 { \
634 int idx = 0; \
635 if (addr > controllers[0].mem_resources[1].end && \
636 addr > controllers[0].mem_resources[2].end) \
637 idx = 1; \
638 if (hv_dev_pwrite(controllers[idx].hv_mem_fd, 0, \
639 (HV_VirtAddr)(&val), sizeof(type), addr)) \
640 pr_err("PCI: write %zd bytes at 0x%lX failed\n", \
641 sizeof(type), addr); \
642 } \
643 EXPORT_SYMBOL(_tile_write##size)
644
645 TILE_WRITE(b, u8);
646 TILE_WRITE(w, u16);
647 TILE_WRITE(l, u32);
648 TILE_WRITE(q, u64);
This page took 0.048717 seconds and 5 git commands to generate.