1 /*
2 * pci.c - Low-Level PCI Access in IA-64
3 *
4 * Derived from bios32.c of i386 tree.
5 * Copyright (C) 2002, 2003 Hewlett-Packard Co
6 * David Mosberger-Tang <davidm@hpl.hp.com>
7 * Bjorn Helgaas <bjorn_helgaas@hp.com>
8 *
9 * Note: Above list of copyright holders is incomplete...
10 */
11 #include <linux/config.h>
12
13 #include <linux/acpi.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/init.h>
18 #include <linux/ioport.h>
19 #include <linux/slab.h>
20 #include <linux/smp_lock.h>
21 #include <linux/spinlock.h>
22
23 #include <asm/machvec.h>
24 #include <asm/mca.h>
25 #include <asm/page.h>
26 #include <asm/segment.h>
27 #include <asm/system.h>
28 #include <asm/io.h>
29
30 #include <asm/sal.h>
31
32
33 #ifdef CONFIG_SMP
34 # include <asm/smp.h>
35 #endif
36 #include <asm/irq.h>
37
38
39 #undef DEBUG
40 #define DEBUG
41
42 #ifdef DEBUG
43 #define DBG(x...) printk(x)
44 #else
45 #define DBG(x...)
46 #endif
47
48 struct pci_fixup pcibios_fixups[1];
49
50 struct pci_ops *pci_root_ops;
51
52 int (*pci_config_read)(int seg, int bus, int dev, int fn, int reg, int len, u32 *value);
53 int (*pci_config_write)(int seg, int bus, int dev, int fn, int reg, int len, u32 value);
54
55
56 /*
57 * Low-level SAL-based PCI configuration access functions. Note that SAL
58 * calls are already serialized (via sal_lock), so we don't need another
59 * synchronization mechanism here.
60 */
61
62 #define PCI_SAL_ADDRESS(seg, bus, dev, fn, reg) \
63 ((u64)(seg << 24) | (u64)(bus << 16) | \
64 (u64)(dev << 11) | (u64)(fn << 8) | (u64)(reg))
65
66 static int
pci_sal_read(int seg,int bus,int dev,int fn,int reg,int len,u32 * value)67 pci_sal_read (int seg, int bus, int dev, int fn, int reg, int len, u32 *value)
68 {
69 int result = 0;
70 u64 data = 0;
71
72 if (!value || (seg > 255) || (bus > 255) || (dev > 31) || (fn > 7) || (reg > 255))
73 return -EINVAL;
74
75 result = ia64_sal_pci_config_read(PCI_SAL_ADDRESS(seg, bus, dev, fn, reg), len, &data);
76
77 *value = (u32) data;
78
79 return result;
80 }
81
82 static int
pci_sal_write(int seg,int bus,int dev,int fn,int reg,int len,u32 value)83 pci_sal_write (int seg, int bus, int dev, int fn, int reg, int len, u32 value)
84 {
85 if ((seg > 255) || (bus > 255) || (dev > 31) || (fn > 7) || (reg > 255))
86 return -EINVAL;
87
88 return ia64_sal_pci_config_write(PCI_SAL_ADDRESS(seg, bus, dev, fn, reg), len, value);
89 }
90
91
92 static int
pci_sal_read_config_byte(struct pci_dev * dev,int where,u8 * value)93 pci_sal_read_config_byte (struct pci_dev *dev, int where, u8 *value)
94 {
95 int result = 0;
96 u32 data = 0;
97
98 if (!value)
99 return -EINVAL;
100
101 result = pci_sal_read(PCI_SEGMENT(dev), dev->bus->number, PCI_SLOT(dev->devfn),
102 PCI_FUNC(dev->devfn), where, 1, &data);
103
104 *value = (u8) data;
105
106 return result;
107 }
108
109 static int
pci_sal_read_config_word(struct pci_dev * dev,int where,u16 * value)110 pci_sal_read_config_word (struct pci_dev *dev, int where, u16 *value)
111 {
112 int result = 0;
113 u32 data = 0;
114
115 if (!value)
116 return -EINVAL;
117
118 result = pci_sal_read(PCI_SEGMENT(dev), dev->bus->number, PCI_SLOT(dev->devfn),
119 PCI_FUNC(dev->devfn), where, 2, &data);
120
121 *value = (u16) data;
122
123 return result;
124 }
125
126 static int
pci_sal_read_config_dword(struct pci_dev * dev,int where,u32 * value)127 pci_sal_read_config_dword (struct pci_dev *dev, int where, u32 *value)
128 {
129 if (!value)
130 return -EINVAL;
131
132 return pci_sal_read(PCI_SEGMENT(dev), dev->bus->number, PCI_SLOT(dev->devfn),
133 PCI_FUNC(dev->devfn), where, 4, value);
134 }
135
136 static int
pci_sal_write_config_byte(struct pci_dev * dev,int where,u8 value)137 pci_sal_write_config_byte (struct pci_dev *dev, int where, u8 value)
138 {
139 return pci_sal_write(PCI_SEGMENT(dev), dev->bus->number, PCI_SLOT(dev->devfn),
140 PCI_FUNC(dev->devfn), where, 1, value);
141 }
142
143 static int
pci_sal_write_config_word(struct pci_dev * dev,int where,u16 value)144 pci_sal_write_config_word (struct pci_dev *dev, int where, u16 value)
145 {
146 return pci_sal_write(PCI_SEGMENT(dev), dev->bus->number, PCI_SLOT(dev->devfn),
147 PCI_FUNC(dev->devfn), where, 2, value);
148 }
149
150 static int
pci_sal_write_config_dword(struct pci_dev * dev,int where,u32 value)151 pci_sal_write_config_dword (struct pci_dev *dev, int where, u32 value)
152 {
153 return pci_sal_write(PCI_SEGMENT(dev), dev->bus->number, PCI_SLOT(dev->devfn),
154 PCI_FUNC(dev->devfn), where, 4, value);
155 }
156
157 struct pci_ops pci_sal_ops = {
158 pci_sal_read_config_byte,
159 pci_sal_read_config_word,
160 pci_sal_read_config_dword,
161 pci_sal_write_config_byte,
162 pci_sal_write_config_word,
163 pci_sal_write_config_dword
164 };
165
166
167 /*
168 * Initialization. Uses the SAL interface
169 */
170
171 static struct pci_controller *
alloc_pci_controller(int seg)172 alloc_pci_controller (int seg)
173 {
174 struct pci_controller *controller;
175
176 controller = kmalloc(sizeof(*controller), GFP_KERNEL);
177 if (!controller)
178 return NULL;
179
180 memset(controller, 0, sizeof(*controller));
181 controller->segment = seg;
182 return controller;
183 }
184
185 static struct pci_bus *
scan_root_bus(int bus,struct pci_ops * ops,void * sysdata)186 scan_root_bus (int bus, struct pci_ops *ops, void *sysdata)
187 {
188 struct pci_bus *b;
189
190 /*
191 * We know this is a new root bus we haven't seen before, so
192 * scan it, even if we've seen the same bus number in a different
193 * segment.
194 */
195 b = kmalloc(sizeof(*b), GFP_KERNEL);
196 if (!b)
197 return NULL;
198
199 memset(b, 0, sizeof(*b));
200 INIT_LIST_HEAD(&b->children);
201 INIT_LIST_HEAD(&b->devices);
202
203 list_add_tail(&b->node, &pci_root_buses);
204
205 b->number = b->secondary = bus;
206 b->resource[0] = &ioport_resource;
207 b->resource[1] = &iomem_resource;
208
209 b->sysdata = sysdata;
210 b->ops = ops;
211 b->subordinate = pci_do_scan_bus(b);
212
213 return b;
214 }
215
216 static int
alloc_resource(char * name,struct resource * root,unsigned long start,unsigned long end,unsigned long flags)217 alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end, unsigned long flags)
218 {
219 struct resource *res;
220
221 res = kmalloc(sizeof(*res), GFP_KERNEL);
222 if (!res)
223 return -ENOMEM;
224
225 memset(res, 0, sizeof(*res));
226 res->name = name;
227 res->start = start;
228 res->end = end;
229 res->flags = flags;
230
231 if (request_resource(root, res))
232 return -EBUSY;
233
234 return 0;
235 }
236
237 static u64
add_io_space(struct acpi_resource_address64 * addr)238 add_io_space (struct acpi_resource_address64 *addr)
239 {
240 u64 offset;
241 int sparse = 0;
242 int i;
243
244 if (addr->address_translation_offset == 0)
245 return IO_SPACE_BASE(0); /* part of legacy IO space */
246
247 if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
248 sparse = 1;
249
250 offset = (u64) ioremap(addr->address_translation_offset, 0);
251 for (i = 0; i < num_io_spaces; i++)
252 if (io_space[i].mmio_base == offset &&
253 io_space[i].sparse == sparse)
254 return IO_SPACE_BASE(i);
255
256 if (num_io_spaces == MAX_IO_SPACES) {
257 printk("Too many IO port spaces\n");
258 return ~0;
259 }
260
261 i = num_io_spaces++;
262 io_space[i].mmio_base = offset;
263 io_space[i].sparse = sparse;
264
265 return IO_SPACE_BASE(i);
266 }
267
268 static acpi_status
count_window(struct acpi_resource * resource,void * data)269 count_window (struct acpi_resource *resource, void *data)
270 {
271 unsigned int *windows = (unsigned int *) data;
272 struct acpi_resource_address64 addr;
273 acpi_status status;
274
275 status = acpi_resource_to_address64(resource, &addr);
276 if (ACPI_SUCCESS(status))
277 if (addr.resource_type == ACPI_MEMORY_RANGE ||
278 addr.resource_type == ACPI_IO_RANGE)
279 (*windows)++;
280
281 return AE_OK;
282 }
283
284 struct pci_root_info {
285 struct pci_controller *controller;
286 char *name;
287 };
288
289 static acpi_status
add_window(struct acpi_resource * res,void * data)290 add_window (struct acpi_resource *res, void *data)
291 {
292 struct pci_root_info *info = (struct pci_root_info *) data;
293 struct pci_window *window;
294 struct acpi_resource_address64 addr;
295 acpi_status status;
296 unsigned long flags, offset = 0;
297 struct resource *root;
298
299 status = acpi_resource_to_address64(res, &addr);
300 if (ACPI_SUCCESS(status)) {
301 if (!addr.address_length)
302 return AE_OK;
303
304 if (addr.resource_type == ACPI_MEMORY_RANGE) {
305 flags = IORESOURCE_MEM;
306 root = &iomem_resource;
307 offset = addr.address_translation_offset;
308 } else if (addr.resource_type == ACPI_IO_RANGE) {
309 flags = IORESOURCE_IO;
310 root = &ioport_resource;
311 offset = add_io_space(&addr);
312 if (offset == ~0)
313 return AE_OK;
314 } else
315 return AE_OK;
316
317 window = &info->controller->window[info->controller->windows++];
318 window->resource.flags |= flags;
319 window->resource.start = addr.min_address_range;
320 window->resource.end = addr.max_address_range;
321 window->offset = offset;
322
323 if (alloc_resource(info->name, root, addr.min_address_range + offset,
324 addr.max_address_range + offset, flags))
325 printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
326 addr.min_address_range + offset, addr.max_address_range + offset,
327 root->name, info->name);
328 }
329
330 return AE_OK;
331 }
332
333 struct pci_bus *
pcibios_scan_root(void * handle,int seg,int bus)334 pcibios_scan_root (void *handle, int seg, int bus)
335 {
336 struct pci_root_info info;
337 struct pci_controller *controller;
338 unsigned int windows = 0;
339 char *name;
340
341 controller = alloc_pci_controller(seg);
342 if (!controller)
343 goto out1;
344
345 controller->acpi_handle = handle;
346
347 acpi_walk_resources(handle, METHOD_NAME__CRS, count_window, &windows);
348 controller->window = kmalloc(sizeof(*controller->window) * windows, GFP_KERNEL);
349 if (!controller->window)
350 goto out2;
351
352 name = kmalloc(16, GFP_KERNEL);
353 if (!name)
354 goto out3;
355
356 sprintf(name, "PCI Bus %02x:%02x", seg, bus);
357 info.controller = controller;
358 info.name = name;
359 acpi_walk_resources(handle, METHOD_NAME__CRS, add_window, &info);
360
361 return scan_root_bus(bus, pci_root_ops, controller);
362
363 out3:
364 kfree(controller->window);
365 out2:
366 kfree(controller);
367 out1:
368 return NULL;
369 }
370
371 void __init
pcibios_config_init(void)372 pcibios_config_init (void)
373 {
374 if (pci_root_ops)
375 return;
376
377 printk("PCI: Using SAL to access configuration space\n");
378
379 pci_root_ops = &pci_sal_ops;
380 pci_config_read = pci_sal_read;
381 pci_config_write = pci_sal_write;
382
383 return;
384 }
385
386 void __init
pcibios_init(void)387 pcibios_init (void)
388 {
389 pcibios_config_init();
390
391 platform_pci_fixup(0); /* phase 0 fixups (before buses scanned) */
392
393 platform_pci_fixup(1); /* phase 1 fixups (after buses scanned) */
394
395 return;
396 }
397
398 void __init
pcibios_fixup_device_resources(struct pci_dev * dev,struct pci_bus * bus)399 pcibios_fixup_device_resources (struct pci_dev *dev, struct pci_bus *bus)
400 {
401 struct pci_controller *controller = PCI_CONTROLLER(dev);
402 struct pci_window *window;
403 int i, j;
404
405 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
406 if (!dev->resource[i].start)
407 continue;
408
409 #define contains(win, res) ((res)->start >= (win)->start && \
410 (res)->end <= (win)->end)
411
412 for (j = 0; j < controller->windows; j++) {
413 window = &controller->window[j];
414 if (((dev->resource[i].flags & IORESOURCE_MEM &&
415 window->resource.flags & IORESOURCE_MEM) ||
416 (dev->resource[i].flags & IORESOURCE_IO &&
417 window->resource.flags & IORESOURCE_IO)) &&
418 contains(&window->resource, &dev->resource[i])) {
419 dev->resource[i].start += window->offset;
420 dev->resource[i].end += window->offset;
421 }
422 }
423 }
424 }
425
426 /*
427 * Called after each bus is probed, but before its children are examined.
428 */
429 void __devinit
pcibios_fixup_bus(struct pci_bus * b)430 pcibios_fixup_bus (struct pci_bus *b)
431 {
432 struct list_head *ln;
433
434 for (ln = b->devices.next; ln != &b->devices; ln = ln->next)
435 pcibios_fixup_device_resources(pci_dev_b(ln), b);
436 }
437
438 void __devinit
pcibios_update_resource(struct pci_dev * dev,struct resource * root,struct resource * res,int resource)439 pcibios_update_resource (struct pci_dev *dev, struct resource *root,
440 struct resource *res, int resource)
441 {
442 unsigned long where, size;
443 u32 reg;
444
445 where = PCI_BASE_ADDRESS_0 + (resource * 4);
446 size = res->end - res->start;
447 pci_read_config_dword(dev, where, ®);
448 reg = (reg & size) | (((u32)(res->start - root->start)) & ~size);
449 pci_write_config_dword(dev, where, reg);
450
451 /* ??? FIXME -- record old value for shutdown. */
452 }
453
454 void __devinit
pcibios_update_irq(struct pci_dev * dev,int irq)455 pcibios_update_irq (struct pci_dev *dev, int irq)
456 {
457 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
458
459 /* ??? FIXME -- record old value for shutdown. */
460 }
461
462 void __devinit
pcibios_fixup_pbus_ranges(struct pci_bus * bus,struct pbus_set_ranges_data * ranges)463 pcibios_fixup_pbus_ranges (struct pci_bus * bus, struct pbus_set_ranges_data * ranges)
464 {
465 ranges->io_start -= bus->resource[0]->start;
466 ranges->io_end -= bus->resource[0]->start;
467 ranges->mem_start -= bus->resource[1]->start;
468 ranges->mem_end -= bus->resource[1]->start;
469 }
470
471 static inline int
pcibios_enable_resources(struct pci_dev * dev,int mask)472 pcibios_enable_resources (struct pci_dev *dev, int mask)
473 {
474 u16 cmd, old_cmd;
475 int idx;
476 struct resource *r;
477
478 if (!dev)
479 return -EINVAL;
480
481 pci_read_config_word(dev, PCI_COMMAND, &cmd);
482 old_cmd = cmd;
483 for (idx=0; idx<6; idx++) {
484 /* Only set up the desired resources. */
485 if (!(mask & (1 << idx)))
486 continue;
487
488 r = &dev->resource[idx];
489 if (!r->start && r->end) {
490 printk(KERN_ERR
491 "PCI: Device %s not available because of resource collisions\n",
492 dev->slot_name);
493 return -EINVAL;
494 }
495 if (r->flags & IORESOURCE_IO)
496 cmd |= PCI_COMMAND_IO;
497 if (r->flags & IORESOURCE_MEM)
498 cmd |= PCI_COMMAND_MEMORY;
499 }
500 if (dev->resource[PCI_ROM_RESOURCE].start)
501 cmd |= PCI_COMMAND_MEMORY;
502 if (cmd != old_cmd) {
503 printk("PCI: Enabling device %s (%04x -> %04x)\n", dev->slot_name, old_cmd, cmd);
504 pci_write_config_word(dev, PCI_COMMAND, cmd);
505 }
506 return 0;
507 }
508
509 int
pcibios_enable_device(struct pci_dev * dev,int mask)510 pcibios_enable_device (struct pci_dev *dev, int mask)
511 {
512 int ret;
513
514 ret = pcibios_enable_resources(dev, mask);
515 if (ret < 0)
516 return ret;
517
518 printk(KERN_INFO "PCI: Found IRQ %d for device %s\n", dev->irq, dev->slot_name);
519
520 return 0;
521 }
522
523 void
pcibios_align_resource(void * data,struct resource * res,unsigned long size,unsigned long align)524 pcibios_align_resource (void *data, struct resource *res,
525 unsigned long size, unsigned long align)
526 {
527 }
528
529 /*
530 * PCI BIOS setup, always defaults to SAL interface
531 */
532 char * __init
pcibios_setup(char * str)533 pcibios_setup (char *str)
534 {
535 return NULL;
536 }
537
538 int
pci_mmap_page_range(struct pci_dev * dev,struct vm_area_struct * vma,enum pci_mmap_state mmap_state,int write_combine)539 pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
540 enum pci_mmap_state mmap_state, int write_combine)
541 {
542 /*
543 * I/O space cannot be accessed via normal processor loads and stores on this
544 * platform.
545 */
546 if (mmap_state == pci_mmap_io)
547 /*
548 * XXX we could relax this for I/O spaces for which ACPI indicates that
549 * the space is 1-to-1 mapped. But at the moment, we don't support
550 * multiple PCI address spaces and the legacy I/O space is not 1-to-1
551 * mapped, so this is moot.
552 */
553 return -EINVAL;
554
555 /*
556 * Leave vm_pgoff as-is, the PCI space address is the physical address on this
557 * platform.
558 */
559 vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO);
560
561 if (write_combine)
562 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
563 else
564 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
565
566 if (remap_page_range(vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
567 vma->vm_end - vma->vm_start, vma->vm_page_prot))
568 return -EAGAIN;
569
570 return 0;
571 }
572
573 /**
574 * pci_cacheline_size - determine cacheline size for PCI devices
575 * @dev: void
576 *
577 * We want to use the line-size of the outer-most cache. We assume
578 * that this line-size is the same for all CPUs.
579 *
580 * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
581 *
582 * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
583 */
584 static unsigned long
pci_cacheline_size(void)585 pci_cacheline_size (void)
586 {
587 u64 levels, unique_caches;
588 s64 status;
589 pal_cache_config_info_t cci;
590 static u8 cacheline_size;
591
592 if (cacheline_size)
593 return cacheline_size;
594
595 status = ia64_pal_cache_summary(&levels, &unique_caches);
596 if (status != 0) {
597 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
598 __FUNCTION__, status);
599 return SMP_CACHE_BYTES;
600 }
601
602 status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
603 &cci);
604 if (status != 0) {
605 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
606 __FUNCTION__, status);
607 return SMP_CACHE_BYTES;
608 }
609 cacheline_size = 1 << cci.pcci_line_size;
610 return cacheline_size;
611 }
612
613 /**
614 * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
615 * @dev: the PCI device for which MWI is enabled
616 *
617 * For ia64, we can get the cacheline sizes from PAL.
618 *
619 * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
620 */
621 int
pcibios_set_mwi(struct pci_dev * dev)622 pcibios_set_mwi (struct pci_dev *dev)
623 {
624 unsigned long desired_linesize, current_linesize;
625 int rc = 0;
626 u8 pci_linesize;
627
628 desired_linesize = pci_cacheline_size();
629
630 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
631 current_linesize = 4 * pci_linesize;
632 if (desired_linesize != current_linesize) {
633 printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
634 dev->slot_name, current_linesize);
635 if (current_linesize > desired_linesize) {
636 printk(" expected %lu bytes instead\n", desired_linesize);
637 rc = -EINVAL;
638 } else {
639 printk(" correcting to %lu\n", desired_linesize);
640 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
641 }
642 }
643 return rc;
644 }
645