blob: 295cbb18a4f20829e08df0078a2ef724da490655 [file] [log] [blame]
Kumar Gala5516b542007-06-27 01:17:57 -05001/*
2 * Contains common pci routines for ALL ppc platform
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; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#undef DEBUG
11
12#include <linux/kernel.h>
13#include <linux/pci.h>
14#include <linux/string.h>
15#include <linux/init.h>
16#include <linux/bootmem.h>
17#include <linux/mm.h>
18#include <linux/list.h>
19#include <linux/syscalls.h>
20#include <linux/irq.h>
21#include <linux/vmalloc.h>
22
23#include <asm/processor.h>
24#include <asm/io.h>
25#include <asm/prom.h>
26#include <asm/pci-bridge.h>
27#include <asm/byteorder.h>
28#include <asm/machdep.h>
29#include <asm/ppc-pci.h>
30#include <asm/firmware.h>
31
32#ifdef DEBUG
33#include <asm/udbg.h>
34#define DBG(fmt...) printk(fmt)
35#else
36#define DBG(fmt...)
37#endif
38
Kumar Galaa4c9e322007-06-27 13:09:43 -050039static DEFINE_SPINLOCK(hose_spinlock);
40
41/* XXX kill that some day ... */
42int global_phb_number; /* Global phb counter */
43
44extern struct list_head hose_list;
45
46/*
47 * pci_controller(phb) initialized common variables.
48 */
49static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
50{
51 memset(hose, 0, sizeof(struct pci_controller));
52
53 spin_lock(&hose_spinlock);
54 hose->global_number = global_phb_number++;
55 list_add_tail(&hose->list_node, &hose_list);
56 spin_unlock(&hose_spinlock);
57}
58
59struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
60{
61 struct pci_controller *phb;
62
63 if (mem_init_done)
64 phb = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
65 else
66 phb = alloc_bootmem(sizeof (struct pci_controller));
67 if (phb == NULL)
68 return NULL;
69 pci_setup_pci_controller(phb);
70 phb->arch_data = dev;
71 phb->is_dynamic = mem_init_done;
72#ifdef CONFIG_PPC64
73 if (dev) {
74 int nid = of_node_to_nid(dev);
75
76 if (nid < 0 || !node_online(nid))
77 nid = -1;
78
79 PHB_SET_NODE(phb, nid);
80 }
81#endif
82 return phb;
83}
84
85void pcibios_free_controller(struct pci_controller *phb)
86{
87 spin_lock(&hose_spinlock);
88 list_del(&phb->list_node);
89 spin_unlock(&hose_spinlock);
90
91 if (phb->is_dynamic)
92 kfree(phb);
93}
94
Kumar Gala5516b542007-06-27 01:17:57 -050095/*
96 * Return the domain number for this bus.
97 */
98int pci_domain_nr(struct pci_bus *bus)
99{
100 if (firmware_has_feature(FW_FEATURE_ISERIES))
101 return 0;
102 else {
103 struct pci_controller *hose = pci_bus_to_host(bus);
104
105 return hose->global_number;
106 }
107}
108
109EXPORT_SYMBOL(pci_domain_nr);
Kumar Gala58083da2007-06-27 11:07:51 -0500110
111#ifdef CONFIG_PPC_OF
Kumar Galaa4c9e322007-06-27 13:09:43 -0500112
113/* This routine is meant to be used early during boot, when the
114 * PCI bus numbers have not yet been assigned, and you need to
115 * issue PCI config cycles to an OF device.
116 * It could also be used to "fix" RTAS config cycles if you want
117 * to set pci_assign_all_buses to 1 and still use RTAS for PCI
118 * config cycles.
119 */
120struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
121{
122 if (!have_of)
123 return NULL;
124 while(node) {
125 struct pci_controller *hose, *tmp;
126 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
127 if (hose->arch_data == node)
128 return hose;
129 node = node->parent;
130 }
131 return NULL;
132}
133
Kumar Gala58083da2007-06-27 11:07:51 -0500134static ssize_t pci_show_devspec(struct device *dev,
135 struct device_attribute *attr, char *buf)
136{
137 struct pci_dev *pdev;
138 struct device_node *np;
139
140 pdev = to_pci_dev (dev);
141 np = pci_device_to_OF_node(pdev);
142 if (np == NULL || np->full_name == NULL)
143 return 0;
144 return sprintf(buf, "%s", np->full_name);
145}
146static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
147#endif /* CONFIG_PPC_OF */
148
149/* Add sysfs properties */
150void pcibios_add_platform_entries(struct pci_dev *pdev)
151{
152#ifdef CONFIG_PPC_OF
153 device_create_file(&pdev->dev, &dev_attr_devspec);
154#endif /* CONFIG_PPC_OF */
155}
156
157char __init *pcibios_setup(char *str)
158{
159 return str;
160}
161
162/*
163 * Reads the interrupt pin to determine if interrupt is use by card.
164 * If the interrupt is used, then gets the interrupt line from the
165 * openfirmware and sets it in the pci_dev and pci_config line.
166 */
167int pci_read_irq_line(struct pci_dev *pci_dev)
168{
169 struct of_irq oirq;
170 unsigned int virq;
171
172 DBG("Try to map irq for %s...\n", pci_name(pci_dev));
173
174#ifdef DEBUG
175 memset(&oirq, 0xff, sizeof(oirq));
176#endif
177 /* Try to get a mapping from the device-tree */
178 if (of_irq_map_pci(pci_dev, &oirq)) {
179 u8 line, pin;
180
181 /* If that fails, lets fallback to what is in the config
182 * space and map that through the default controller. We
183 * also set the type to level low since that's what PCI
184 * interrupts are. If your platform does differently, then
185 * either provide a proper interrupt tree or don't use this
186 * function.
187 */
188 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
189 return -1;
190 if (pin == 0)
191 return -1;
192 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
193 line == 0xff) {
194 return -1;
195 }
196 DBG(" -> no map ! Using irq line %d from PCI config\n", line);
197
198 virq = irq_create_mapping(NULL, line);
199 if (virq != NO_IRQ)
200 set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
201 } else {
202 DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
203 oirq.size, oirq.specifier[0], oirq.specifier[1],
204 oirq.controller->full_name);
205
206 virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
207 oirq.size);
208 }
209 if(virq == NO_IRQ) {
210 DBG(" -> failed to map !\n");
211 return -1;
212 }
213
214 DBG(" -> mapped to linux irq %d\n", virq);
215
216 pci_dev->irq = virq;
217
218 return 0;
219}
220EXPORT_SYMBOL(pci_read_irq_line);
221
222/*
223 * Platform support for /proc/bus/pci/X/Y mmap()s,
224 * modelled on the sparc64 implementation by Dave Miller.
225 * -- paulus.
226 */
227
228/*
229 * Adjust vm_pgoff of VMA such that it is the physical page offset
230 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
231 *
232 * Basically, the user finds the base address for his device which he wishes
233 * to mmap. They read the 32-bit value from the config space base register,
234 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
235 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
236 *
237 * Returns negative error code on failure, zero on success.
238 */
239static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
240 resource_size_t *offset,
241 enum pci_mmap_state mmap_state)
242{
243 struct pci_controller *hose = pci_bus_to_host(dev->bus);
244 unsigned long io_offset = 0;
245 int i, res_bit;
246
247 if (hose == 0)
248 return NULL; /* should never happen */
249
250 /* If memory, add on the PCI bridge address offset */
251 if (mmap_state == pci_mmap_mem) {
252#if 0 /* See comment in pci_resource_to_user() for why this is disabled */
253 *offset += hose->pci_mem_offset;
254#endif
255 res_bit = IORESOURCE_MEM;
256 } else {
257 io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
258 *offset += io_offset;
259 res_bit = IORESOURCE_IO;
260 }
261
262 /*
263 * Check that the offset requested corresponds to one of the
264 * resources of the device.
265 */
266 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
267 struct resource *rp = &dev->resource[i];
268 int flags = rp->flags;
269
270 /* treat ROM as memory (should be already) */
271 if (i == PCI_ROM_RESOURCE)
272 flags |= IORESOURCE_MEM;
273
274 /* Active and same type? */
275 if ((flags & res_bit) == 0)
276 continue;
277
278 /* In the range of this resource? */
279 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
280 continue;
281
282 /* found it! construct the final physical address */
283 if (mmap_state == pci_mmap_io)
284 *offset += hose->io_base_phys - io_offset;
285 return rp;
286 }
287
288 return NULL;
289}
290
291/*
292 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
293 * device mapping.
294 */
295static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
296 pgprot_t protection,
297 enum pci_mmap_state mmap_state,
298 int write_combine)
299{
300 unsigned long prot = pgprot_val(protection);
301
302 /* Write combine is always 0 on non-memory space mappings. On
303 * memory space, if the user didn't pass 1, we check for a
304 * "prefetchable" resource. This is a bit hackish, but we use
305 * this to workaround the inability of /sysfs to provide a write
306 * combine bit
307 */
308 if (mmap_state != pci_mmap_mem)
309 write_combine = 0;
310 else if (write_combine == 0) {
311 if (rp->flags & IORESOURCE_PREFETCH)
312 write_combine = 1;
313 }
314
315 /* XXX would be nice to have a way to ask for write-through */
316 prot |= _PAGE_NO_CACHE;
317 if (write_combine)
318 prot &= ~_PAGE_GUARDED;
319 else
320 prot |= _PAGE_GUARDED;
321
322 return __pgprot(prot);
323}
324
325/*
326 * This one is used by /dev/mem and fbdev who have no clue about the
327 * PCI device, it tries to find the PCI device first and calls the
328 * above routine
329 */
330pgprot_t pci_phys_mem_access_prot(struct file *file,
331 unsigned long pfn,
332 unsigned long size,
333 pgprot_t protection)
334{
335 struct pci_dev *pdev = NULL;
336 struct resource *found = NULL;
337 unsigned long prot = pgprot_val(protection);
338 unsigned long offset = pfn << PAGE_SHIFT;
339 int i;
340
341 if (page_is_ram(pfn))
342 return __pgprot(prot);
343
344 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
345
346 for_each_pci_dev(pdev) {
347 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
348 struct resource *rp = &pdev->resource[i];
349 int flags = rp->flags;
350
351 /* Active and same type? */
352 if ((flags & IORESOURCE_MEM) == 0)
353 continue;
354 /* In the range of this resource? */
355 if (offset < (rp->start & PAGE_MASK) ||
356 offset > rp->end)
357 continue;
358 found = rp;
359 break;
360 }
361 if (found)
362 break;
363 }
364 if (found) {
365 if (found->flags & IORESOURCE_PREFETCH)
366 prot &= ~_PAGE_GUARDED;
367 pci_dev_put(pdev);
368 }
369
370 DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
371
372 return __pgprot(prot);
373}
374
375
376/*
377 * Perform the actual remap of the pages for a PCI device mapping, as
378 * appropriate for this architecture. The region in the process to map
379 * is described by vm_start and vm_end members of VMA, the base physical
380 * address is found in vm_pgoff.
381 * The pci device structure is provided so that architectures may make mapping
382 * decisions on a per-device or per-bus basis.
383 *
384 * Returns a negative error code on failure, zero on success.
385 */
386int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
387 enum pci_mmap_state mmap_state, int write_combine)
388{
389 resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
390 struct resource *rp;
391 int ret;
392
393 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
394 if (rp == NULL)
395 return -EINVAL;
396
397 vma->vm_pgoff = offset >> PAGE_SHIFT;
398 vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
399 vma->vm_page_prot,
400 mmap_state, write_combine);
401
402 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
403 vma->vm_end - vma->vm_start, vma->vm_page_prot);
404
405 return ret;
406}
407
408void pci_resource_to_user(const struct pci_dev *dev, int bar,
409 const struct resource *rsrc,
410 resource_size_t *start, resource_size_t *end)
411{
412 struct pci_controller *hose = pci_bus_to_host(dev->bus);
413 resource_size_t offset = 0;
414
415 if (hose == NULL)
416 return;
417
418 if (rsrc->flags & IORESOURCE_IO)
419 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
420
421 /* We pass a fully fixed up address to userland for MMIO instead of
422 * a BAR value because X is lame and expects to be able to use that
423 * to pass to /dev/mem !
424 *
425 * That means that we'll have potentially 64 bits values where some
426 * userland apps only expect 32 (like X itself since it thinks only
427 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
428 * 32 bits CHRPs :-(
429 *
430 * Hopefully, the sysfs insterface is immune to that gunk. Once X
431 * has been fixed (and the fix spread enough), we can re-enable the
432 * 2 lines below and pass down a BAR value to userland. In that case
433 * we'll also have to re-enable the matching code in
434 * __pci_mmap_make_offset().
435 *
436 * BenH.
437 */
438#if 0
439 else if (rsrc->flags & IORESOURCE_MEM)
440 offset = hose->pci_mem_offset;
441#endif
442
443 *start = rsrc->start - offset;
444 *end = rsrc->end - offset;
445}