2 * Copyright 2011 Tilera Corporation. All Rights Reserved.
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.
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
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/irq.h>
25 #include <linux/uaccess.h>
26 #include <linux/export.h>
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>
36 * Initialization flow and process
37 * -------------------------------
39 * This files contains the routines to search for PCI buses,
40 * enumerate the buses, and configure any attached devices.
42 * There are two entry points here:
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.
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.
54 static int pci_probe
= 1;
57 * This flag tells if the platform is TILEmpower that needs
58 * special configuration for the PLX switch chip.
60 int __ro_after_init tile_plx_gen1
;
62 static struct pci_controller controllers
[TILE_NUM_PCIE
];
63 static int num_controllers
;
64 static int pci_scan_flags
[TILE_NUM_PCIE
];
66 static struct pci_ops tile_cfg_ops
;
70 * We don't need to worry about the alignment of resources.
72 resource_size_t
pcibios_align_resource(void *data
, const struct resource
*res
,
73 resource_size_t size
, resource_size_t align
)
77 EXPORT_SYMBOL(pcibios_align_resource
);
80 * Open a FD to the hypervisor PCI device.
82 * controller_id is the controller number, config type is 0 or 1 for
83 * config0 or config1 operations.
85 static int tile_pcie_open(int controller_id
, int config_type
)
90 sprintf(filename
, "pcie/%d/config%d", controller_id
, config_type
);
92 fd
= hv_dev_open((HV_VirtAddr
)filename
, 0);
99 * Get the IRQ numbers from the HV and set up the handlers for them.
101 static int tile_init_irqs(int controller_id
, struct pci_controller
*controller
)
107 struct pcie_rc_config rc_config
;
109 sprintf(filename
, "pcie/%d/ctl", controller_id
);
110 fd
= hv_dev_open((HV_VirtAddr
)filename
, 0);
112 pr_err("PCI: hv_dev_open(%s) failed\n", filename
);
115 ret
= hv_dev_pread(fd
, 0, (HV_VirtAddr
)(&rc_config
),
116 sizeof(rc_config
), PCIE_RC_CONFIG_MASK_OFF
);
118 if (ret
!= sizeof(rc_config
)) {
119 pr_err("PCI: wanted %zd bytes, got %d\n",
120 sizeof(rc_config
), ret
);
123 /* Record irq_base so that we can map INTx to IRQ # later. */
124 controller
->irq_base
= rc_config
.intr
;
126 for (x
= 0; x
< 4; x
++)
127 tile_irq_activate(rc_config
.intr
+ x
,
130 if (rc_config
.plx_gen1
)
131 controller
->plx_gen1
= 1;
137 * First initialization entry point, called from setup_arch().
139 * Find valid controllers and fill in pci_controller structs for each
142 * Returns the number of controllers discovered.
144 int __init
tile_pci_init(void)
149 pr_info("PCI: disabled by boot argument\n");
153 pr_info("PCI: Searching for controllers...\n");
155 /* Re-init number of PCIe controllers to support hot-plug feature. */
158 /* Do any configuration we need before using the PCIe */
160 for (i
= 0; i
< TILE_NUM_PCIE
; i
++) {
162 * To see whether we need a real config op based on
163 * the results of pcibios_init(), to support PCIe hot-plug.
165 if (pci_scan_flags
[i
] == 0) {
170 struct pci_controller
*controller
;
173 * Open the fd to the HV. If it fails then this
174 * device doesn't exist.
176 hv_cfg_fd0
= tile_pcie_open(i
, 0);
179 hv_cfg_fd1
= tile_pcie_open(i
, 1);
180 if (hv_cfg_fd1
< 0) {
181 pr_err("PCI: Couldn't open config fd to HV for controller %d\n",
186 sprintf(name
, "pcie/%d/mem", i
);
187 hv_mem_fd
= hv_dev_open((HV_VirtAddr
)name
, 0);
189 pr_err("PCI: Could not open mem fd to HV!\n");
193 pr_info("PCI: Found PCI controller #%d\n", i
);
195 controller
= &controllers
[i
];
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
->last_busno
= 0xff;
202 controller
->ops
= &tile_cfg_ops
;
209 hv_dev_close(hv_cfg_fd0
);
211 hv_dev_close(hv_cfg_fd1
);
213 hv_dev_close(hv_mem_fd
);
219 * Before using the PCIe, see if we need to do any platform-specific
220 * configuration, such as the PLX switch Gen 1 issue on TILEmpower.
222 for (i
= 0; i
< num_controllers
; i
++) {
223 struct pci_controller
*controller
= &controllers
[i
];
225 if (controller
->plx_gen1
)
229 return num_controllers
;
233 * (pin - 1) converts from the PCI standard's [1:4] convention to
234 * a normal [0:3] range.
236 static int tile_map_irq(const struct pci_dev
*dev
, u8 slot
, u8 pin
)
238 struct pci_controller
*controller
=
239 (struct pci_controller
*)dev
->sysdata
;
240 return (pin
- 1) + controller
->irq_base
;
244 static void fixup_read_and_payload_sizes(void)
246 struct pci_dev
*dev
= NULL
;
247 int smallest_max_payload
= 0x1; /* Tile maxes out at 256 bytes. */
248 int max_read_size
= PCI_EXP_DEVCTL_READRQ_512B
;
251 /* Scan for the smallest maximum payload size. */
252 for_each_pci_dev(dev
) {
253 if (!pci_is_pcie(dev
))
256 if (dev
->pcie_mpss
< smallest_max_payload
)
257 smallest_max_payload
= dev
->pcie_mpss
;
260 /* Now, set the max_payload_size for all devices to that value. */
261 new_values
= max_read_size
| (smallest_max_payload
<< 5);
262 for_each_pci_dev(dev
)
263 pcie_capability_clear_and_set_word(dev
, PCI_EXP_DEVCTL
,
264 PCI_EXP_DEVCTL_PAYLOAD
| PCI_EXP_DEVCTL_READRQ
,
270 * Second PCI initialization entry point, called by subsys_initcall.
272 * The controllers have been set up by the time we get here, by a call to
275 int __init
pcibios_init(void)
279 pr_info("PCI: Probing PCI hardware\n");
282 * Delay a bit in case devices aren't ready. Some devices are
283 * known to require at least 20ms here, but we use a more
284 * conservative value.
288 /* Scan all of the recorded PCI controllers. */
289 for (i
= 0; i
< TILE_NUM_PCIE
; i
++) {
291 * Do real pcibios init ops if the controller is initialized
292 * by tile_pci_init() successfully and not initialized by
293 * pcibios_init() yet to support PCIe hot-plug.
295 if (pci_scan_flags
[i
] == 0 && controllers
[i
].ops
!= NULL
) {
296 struct pci_controller
*controller
= &controllers
[i
];
298 LIST_HEAD(resources
);
300 if (tile_init_irqs(i
, controller
)) {
301 pr_err("PCI: Could not initialize IRQs\n");
305 pr_info("PCI: initializing controller #%d\n", i
);
307 pci_add_resource(&resources
, &ioport_resource
);
308 pci_add_resource(&resources
, &iomem_resource
);
309 bus
= pci_scan_root_bus(NULL
, 0, controller
->ops
,
310 controller
, &resources
);
311 controller
->root_bus
= bus
;
312 controller
->last_busno
= bus
->busn_res
.end
;
316 /* Do machine dependent PCI interrupt routing */
317 pci_fixup_irqs(pci_common_swizzle
, tile_map_irq
);
320 * This comes from the generic Linux PCI driver.
322 * It allocates all of the resources (I/O memory, etc)
323 * associated with the devices read in above.
325 pci_assign_unassigned_resources();
327 /* Configure the max_read_size and max_payload_size values. */
328 fixup_read_and_payload_sizes();
330 /* Record the I/O resources in the PCI controller structure. */
331 for (i
= 0; i
< TILE_NUM_PCIE
; i
++) {
333 * Do real pcibios init ops if the controller is initialized
334 * by tile_pci_init() successfully and not initialized by
335 * pcibios_init() yet to support PCIe hot-plug.
337 if (pci_scan_flags
[i
] == 0 && controllers
[i
].ops
!= NULL
) {
338 struct pci_bus
*root_bus
= controllers
[i
].root_bus
;
339 struct pci_bus
*next_bus
;
342 pci_bus_add_devices(root_bus
);
344 list_for_each_entry(dev
, &root_bus
->devices
, bus_list
) {
346 * Find the PCI host controller, ie. the 1st
349 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
&&
350 (PCI_SLOT(dev
->devfn
) == 0)) {
351 next_bus
= dev
->subordinate
;
352 controllers
[i
].mem_resources
[0] =
353 *next_bus
->resource
[0];
354 controllers
[i
].mem_resources
[1] =
355 *next_bus
->resource
[1];
356 controllers
[i
].mem_resources
[2] =
357 *next_bus
->resource
[2];
360 pci_scan_flags
[i
] = 1;
370 subsys_initcall(pcibios_init
);
373 * No bus fixups needed.
375 void pcibios_fixup_bus(struct pci_bus
*bus
)
377 /* Nothing needs to be done. */
380 void pcibios_set_master(struct pci_dev
*dev
)
382 /* No special bus mastering setup handling. */
385 /* Process any "pci=" kernel boot arguments. */
386 char *__init
pcibios_setup(char *str
)
388 if (!strcmp(str
, "off")) {
396 * Enable memory and/or address decoding, as appropriate, for the
397 * device described by the 'dev' struct.
399 * This is called from the generic PCI layer, and can be called
400 * for bridges or endpoints.
402 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
409 pci_read_config_byte(dev
, PCI_HEADER_TYPE
, &header_type
);
411 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
413 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
415 * For bridges, we enable both memory and I/O decoding
418 cmd
|= PCI_COMMAND_IO
;
419 cmd
|= PCI_COMMAND_MEMORY
;
422 * For endpoints, we enable memory and/or I/O decoding
423 * only if they have a memory resource of that type.
425 for (i
= 0; i
< 6; i
++) {
426 r
= &dev
->resource
[i
];
427 if (r
->flags
& IORESOURCE_UNSET
) {
428 pr_err("PCI: Device %s not available because of resource collisions\n",
432 if (r
->flags
& IORESOURCE_IO
)
433 cmd
|= PCI_COMMAND_IO
;
434 if (r
->flags
& IORESOURCE_MEM
)
435 cmd
|= PCI_COMMAND_MEMORY
;
440 * We only write the command if it changed.
443 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
447 /****************************************************************
449 * Tile PCI config space read/write routines
451 ****************************************************************/
454 * These are the normal read and write ops
455 * These are expanded with macros from pci_bus_read_config_byte() etc.
457 * devfn is the combined PCI slot & function.
459 * offset is in bytes, from the start of config space for the
460 * specified bus & slot.
463 static int tile_cfg_read(struct pci_bus
*bus
, unsigned int devfn
, int offset
,
466 struct pci_controller
*controller
= bus
->sysdata
;
467 int busnum
= bus
->number
& 0xff;
468 int slot
= (devfn
>> 3) & 0x1f;
469 int function
= devfn
& 0x7;
474 * There is no bridge between the Tile and bus 0, so we
475 * use config0 to talk to bus 0.
477 * If we're talking to a bus other than zero then we
478 * must have found a bridge.
482 * We fake an empty slot for (busnum == 0) && (slot > 0),
483 * since there is only one slot on bus 0.
492 addr
= busnum
<< 20; /* Bus in 27:20 */
493 addr
|= slot
<< 15; /* Slot (device) in 19:15 */
494 addr
|= function
<< 12; /* Function is in 14:12 */
495 addr
|= (offset
& 0xFFF); /* byte address in 0:11 */
497 return hv_dev_pread(controller
->hv_cfg_fd
[config_mode
], 0,
498 (HV_VirtAddr
)(val
), size
, addr
);
503 * See tile_cfg_read() for relevant comments.
504 * Note that "val" is the value to write, not a pointer to that value.
506 static int tile_cfg_write(struct pci_bus
*bus
, unsigned int devfn
, int offset
,
509 struct pci_controller
*controller
= bus
->sysdata
;
510 int busnum
= bus
->number
& 0xff;
511 int slot
= (devfn
>> 3) & 0x1f;
512 int function
= devfn
& 0x7;
515 HV_VirtAddr valp
= (HV_VirtAddr
)&val
;
518 * For bus 0 slot 0 we use config 0 accesses.
522 * We fake an empty slot for (busnum == 0) && (slot > 0),
523 * since there is only one slot on bus 0.
530 addr
= busnum
<< 20; /* Bus in 27:20 */
531 addr
|= slot
<< 15; /* Slot (device) in 19:15 */
532 addr
|= function
<< 12; /* Function is in 14:12 */
533 addr
|= (offset
& 0xFFF); /* byte address in 0:11 */
536 /* Point to the correct part of the 32-bit "val". */
540 return hv_dev_pwrite(controller
->hv_cfg_fd
[config_mode
], 0,
545 static struct pci_ops tile_cfg_ops
= {
546 .read
= tile_cfg_read
,
547 .write
= tile_cfg_write
,
552 * In the following, each PCI controller's mem_resources[1]
553 * represents its (non-prefetchable) PCI memory resource.
554 * mem_resources[0] and mem_resources[2] refer to its PCI I/O and
555 * prefetchable PCI memory resources, respectively.
556 * For more details, see pci_setup_bridge() in setup-bus.c.
557 * By comparing the target PCI memory address against the
558 * end address of controller 0, we can determine the controller
559 * that should accept the PCI memory access.
561 #define TILE_READ(size, type) \
562 type _tile_read##size(unsigned long addr) \
566 if (addr > controllers[0].mem_resources[1].end && \
567 addr > controllers[0].mem_resources[2].end) \
569 if (hv_dev_pread(controllers[idx].hv_mem_fd, 0, \
570 (HV_VirtAddr)(&val), sizeof(type), addr)) \
571 pr_err("PCI: read %zd bytes at 0x%lX failed\n", \
572 sizeof(type), addr); \
575 EXPORT_SYMBOL(_tile_read##size)
582 #define TILE_WRITE(size, type) \
583 void _tile_write##size(type val, unsigned long addr) \
586 if (addr > controllers[0].mem_resources[1].end && \
587 addr > controllers[0].mem_resources[2].end) \
589 if (hv_dev_pwrite(controllers[idx].hv_mem_fd, 0, \
590 (HV_VirtAddr)(&val), sizeof(type), addr)) \
591 pr_err("PCI: write %zd bytes at 0x%lX failed\n", \
592 sizeof(type), addr); \
594 EXPORT_SYMBOL(_tile_write##size)