~ubuntu-branches/ubuntu/saucy/seabios/saucy-proposed

« back to all changes in this revision

Viewing changes to src/pciinit.c

  • Committer: Package Import Robot
  • Author(s): Michael Tokarev
  • Date: 2013-07-08 21:34:44 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20130708213444-6ed9q23j39x143lu
Tags: 1.7.3-1
Multi-Arch: allowed

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#include "pci_regs.h" // PCI_COMMAND
12
12
#include "ioport.h" // PORT_ATA1_CMD_BASE
13
13
#include "config.h" // CONFIG_*
14
 
#include "xen.h" // usingXen
15
14
#include "memmap.h" // add_e820
16
 
#include "dev-q35.h"
 
15
#include "paravirt.h" // RamSize
 
16
#include "dev-q35.h" // Q35_HOST_BRIDGE_PCIEXBAR_ADDR
 
17
#include "list.h" // struct hlist_node
17
18
 
18
19
/* PM Timer ticks per second (HZ) */
19
20
#define PM_TIMER_FREQUENCY  3579545
47
48
    u64 align;
48
49
    int is64;
49
50
    enum pci_region_type type;
50
 
    struct pci_region_entry *next;
 
51
    struct hlist_node node;
51
52
};
52
53
 
53
54
struct pci_region {
54
55
    /* pci region assignments */
55
56
    u64 base;
56
 
    struct pci_region_entry *list;
 
57
    struct hlist_head list;
57
58
};
58
59
 
59
60
struct pci_bus {
140
141
}
141
142
 
142
143
/* PIIX3/PIIX4 PCI to ISA bridge */
143
 
static void piix_isa_bridge_init(struct pci_device *pci, void *arg)
 
144
static void piix_isa_bridge_setup(struct pci_device *pci, void *arg)
144
145
{
145
146
    int i, irq;
146
147
    u8 elcr[2];
161
162
 
162
163
/* ICH9 LPC PCI to ISA bridge */
163
164
/* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_LPC */
164
 
void mch_isa_bridge_init(struct pci_device *dev, void *arg)
 
165
void mch_isa_bridge_setup(struct pci_device *dev, void *arg)
165
166
{
166
167
    u16 bdf = dev->bdf;
167
168
    int i, irq;
193
194
    /* acpi enable, SCI: IRQ9 000b = irq9*/
194
195
    pci_config_writeb(bdf, ICH9_LPC_ACPI_CTRL, ICH9_LPC_ACPI_CTRL_ACPI_EN);
195
196
 
196
 
    pmtimer_init(PORT_ACPI_PM_BASE + 0x08, PM_TIMER_FREQUENCY / 1000);
 
197
    pmtimer_setup(PORT_ACPI_PM_BASE + 0x08, PM_TIMER_FREQUENCY / 1000);
197
198
}
198
199
 
199
 
static void storage_ide_init(struct pci_device *pci, void *arg)
 
200
static void storage_ide_setup(struct pci_device *pci, void *arg)
200
201
{
201
202
    /* IDE: we map it as in ISA mode */
202
203
    pci_set_io_region_addr(pci, 0, PORT_ATA1_CMD_BASE, 0);
206
207
}
207
208
 
208
209
/* PIIX3/PIIX4 IDE */
209
 
static void piix_ide_init(struct pci_device *pci, void *arg)
 
210
static void piix_ide_setup(struct pci_device *pci, void *arg)
210
211
{
211
212
    u16 bdf = pci->bdf;
212
213
    pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
213
214
    pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
214
215
}
215
216
 
216
 
static void pic_ibm_init(struct pci_device *pci, void *arg)
 
217
static void pic_ibm_setup(struct pci_device *pci, void *arg)
217
218
{
218
219
    /* PIC, IBM, MPIC & MPIC2 */
219
220
    pci_set_io_region_addr(pci, 0, 0x80800000 + 0x00040000, 0);
220
221
}
221
222
 
222
 
static void apple_macio_init(struct pci_device *pci, void *arg)
 
223
static void apple_macio_setup(struct pci_device *pci, void *arg)
223
224
{
224
225
    /* macio bridge */
225
226
    pci_set_io_region_addr(pci, 0, 0x80800000, 0);
226
227
}
227
228
 
228
229
/* PIIX4 Power Management device (for ACPI) */
229
 
static void piix4_pm_init(struct pci_device *pci, void *arg)
 
230
static void piix4_pm_setup(struct pci_device *pci, void *arg)
230
231
{
231
232
    u16 bdf = pci->bdf;
232
233
    // acpi sci is hardwired to 9
237
238
    pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
238
239
    pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
239
240
 
240
 
    pmtimer_init(PORT_ACPI_PM_BASE + 0x08, PM_TIMER_FREQUENCY / 1000);
 
241
    pmtimer_setup(PORT_ACPI_PM_BASE + 0x08, PM_TIMER_FREQUENCY / 1000);
241
242
}
242
243
 
243
244
/* ICH9 SMBUS */
244
245
/* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_SMBUS */
245
 
void ich9_smbus_init(struct pci_device *dev, void *arg)
 
246
void ich9_smbus_setup(struct pci_device *dev, void *arg)
246
247
{
247
248
    u16 bdf = dev->bdf;
248
249
    /* map smbus into io space */
256
257
static const struct pci_device_id pci_device_tbl[] = {
257
258
    /* PIIX3/PIIX4 PCI to ISA bridge */
258
259
    PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0,
259
 
               piix_isa_bridge_init),
 
260
               piix_isa_bridge_setup),
260
261
    PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0,
261
 
               piix_isa_bridge_init),
 
262
               piix_isa_bridge_setup),
262
263
    PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_LPC,
263
 
               mch_isa_bridge_init),
 
264
               mch_isa_bridge_setup),
264
265
 
265
266
    /* STORAGE IDE */
266
267
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1,
267
 
                     PCI_CLASS_STORAGE_IDE, piix_ide_init),
 
268
                     PCI_CLASS_STORAGE_IDE, piix_ide_setup),
268
269
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB,
269
 
                     PCI_CLASS_STORAGE_IDE, piix_ide_init),
 
270
                     PCI_CLASS_STORAGE_IDE, piix_ide_setup),
270
271
    PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE,
271
 
                     storage_ide_init),
 
272
                     storage_ide_setup),
272
273
 
273
274
    /* PIC, IBM, MIPC & MPIC2 */
274
275
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC,
275
 
                     pic_ibm_init),
 
276
                     pic_ibm_setup),
276
277
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC,
277
 
                     pic_ibm_init),
 
278
                     pic_ibm_setup),
278
279
 
279
280
    /* PIIX4 Power Management device (for ACPI) */
280
281
    PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
281
 
               piix4_pm_init),
 
282
               piix4_pm_setup),
282
283
    PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_SMBUS,
283
 
               ich9_smbus_init),
 
284
               ich9_smbus_setup),
284
285
 
285
286
    /* 0xff00 */
286
 
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0017, 0xff00, apple_macio_init),
287
 
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0022, 0xff00, apple_macio_init),
 
287
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0017, 0xff00, apple_macio_setup),
 
288
    PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0022, 0xff00, apple_macio_setup),
288
289
 
289
290
    PCI_DEVICE_END,
290
291
};
316
317
    }
317
318
}
318
319
 
 
320
static void pci_enable_default_vga(void)
 
321
{
 
322
    struct pci_device *pci;
 
323
 
 
324
    foreachpci(pci) {
 
325
        if (is_pci_vga(pci)) {
 
326
            dprintf(1, "PCI: Using %02x:%02x.%x for primary VGA\n",
 
327
                    pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf),
 
328
                    pci_bdf_to_fn(pci->bdf));
 
329
            return;
 
330
        }
 
331
    }
 
332
 
 
333
    pci = pci_find_class(PCI_CLASS_DISPLAY_VGA);
 
334
    if (!pci) {
 
335
        dprintf(1, "PCI: No VGA devices found\n");
 
336
        return;
 
337
    }
 
338
 
 
339
    dprintf(1, "PCI: Enabling %02x:%02x.%x for primary VGA\n",
 
340
            pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf),
 
341
            pci_bdf_to_fn(pci->bdf));
 
342
 
 
343
    pci_config_maskw(pci->bdf, PCI_COMMAND, 0,
 
344
                     PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
 
345
 
 
346
    while (pci->parent) {
 
347
        pci = pci->parent;
 
348
 
 
349
        dprintf(1, "PCI: Setting VGA enable on bridge %02x:%02x.%x\n",
 
350
                pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf),
 
351
                pci_bdf_to_fn(pci->bdf));
 
352
 
 
353
        pci_config_maskw(pci->bdf, PCI_BRIDGE_CONTROL, 0, PCI_BRIDGE_CTL_VGA);
 
354
        pci_config_maskw(pci->bdf, PCI_COMMAND, 0,
 
355
                         PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
 
356
    }
 
357
}
319
358
 
320
359
/****************************************************************
321
360
 * Platform device initialization
322
361
 ****************************************************************/
323
362
 
324
 
void i440fx_mem_addr_init(struct pci_device *dev, void *arg)
 
363
void i440fx_mem_addr_setup(struct pci_device *dev, void *arg)
325
364
{
326
365
    if (RamSize <= 0x80000000)
327
366
        pcimem_start = 0x80000000;
331
370
    pci_slot_get_irq = piix_pci_slot_get_irq;
332
371
}
333
372
 
334
 
void mch_mem_addr_init(struct pci_device *dev, void *arg)
 
373
void mch_mem_addr_setup(struct pci_device *dev, void *arg)
335
374
{
336
375
    u64 addr = Q35_HOST_BRIDGE_PCIEXBAR_ADDR;
337
376
    u32 size = Q35_HOST_BRIDGE_PCIEXBAR_SIZE;
353
392
 
354
393
static const struct pci_device_id pci_platform_tbl[] = {
355
394
    PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441,
356
 
               i440fx_mem_addr_init),
 
395
               i440fx_mem_addr_setup),
357
396
    PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_Q35_MCH,
358
 
               mch_mem_addr_init),
 
397
               mch_mem_addr_setup),
359
398
    PCI_DEVICE_END
360
399
};
361
400
 
501
540
    }
502
541
    if ((pmem & PCI_PREF_RANGE_TYPE_MASK) != PCI_PREF_RANGE_TYPE_64)
503
542
       return 0;
504
 
    struct pci_region_entry *entry = r->list;
505
 
    while (entry) {
 
543
    struct pci_region_entry *entry;
 
544
    hlist_for_each_entry(entry, &r->list, node) {
506
545
        if (!entry->is64)
507
546
            return 0;
508
 
        entry = entry->next;
509
547
    }
510
548
    return 1;
511
549
}
512
550
 
513
551
static u64 pci_region_align(struct pci_region *r)
514
552
{
515
 
    if (!r->list)
516
 
        return 1;
517
 
    // The first entry in the sorted list has the largest alignment
518
 
    return r->list->align;
 
553
    struct pci_region_entry *entry;
 
554
    hlist_for_each_entry(entry, &r->list, node) {
 
555
        // The first entry in the sorted list has the largest alignment
 
556
        return entry->align;
 
557
    }
 
558
    return 1;
519
559
}
520
560
 
521
561
static u64 pci_region_sum(struct pci_region *r)
522
562
{
523
 
    struct pci_region_entry *entry = r->list;
524
563
    u64 sum = 0;
525
 
    while (entry) {
 
564
    struct pci_region_entry *entry;
 
565
    hlist_for_each_entry(entry, &r->list, node) {
526
566
        sum += entry->size;
527
 
        entry = entry->next;
528
567
    }
529
568
    return sum;
530
569
}
532
571
static void pci_region_migrate_64bit_entries(struct pci_region *from,
533
572
                                             struct pci_region *to)
534
573
{
535
 
    struct pci_region_entry **pprev = &from->list, **last = &to->list;
536
 
    while (*pprev) {
537
 
        struct pci_region_entry *entry = *pprev;
538
 
        if (!entry->is64) {
539
 
            pprev = &entry->next;
 
574
    struct hlist_node *n, **last = &to->list.first;
 
575
    struct pci_region_entry *entry;
 
576
    hlist_for_each_entry_safe(entry, n, &from->list, node) {
 
577
        if (!entry->is64)
540
578
            continue;
541
 
        }
542
579
        // Move from source list to destination list.
543
 
        *pprev = entry->next;
544
 
        entry->next = NULL;
545
 
        *last = entry;
546
 
        last = &entry->next;
 
580
        hlist_del(&entry->node);
 
581
        hlist_add(&entry->node, last);
547
582
    }
548
583
}
549
584
 
564
599
    entry->is64 = is64;
565
600
    entry->type = type;
566
601
    // Insert into list in sorted order.
567
 
    struct pci_region_entry **pprev;
568
 
    for (pprev = &bus->r[type].list; *pprev; pprev = &(*pprev)->next) {
569
 
        struct pci_region_entry *pos = *pprev;
 
602
    struct hlist_node **pprev;
 
603
    struct pci_region_entry *pos;
 
604
    hlist_for_each_entry_pprev(pos, pprev, &bus->r[type].list, node) {
570
605
        if (pos->align < align || (pos->align == align && pos->size < size))
571
606
            break;
572
607
    }
573
 
    entry->next = *pprev;
574
 
    *pprev = entry;
 
608
    hlist_add(&entry->node, pprev);
575
609
    return entry;
576
610
}
577
611
 
710
744
 
711
745
static void pci_region_map_entries(struct pci_bus *busses, struct pci_region *r)
712
746
{
713
 
    struct pci_region_entry *entry = r->list;
714
 
    while (entry) {
 
747
    struct hlist_node *n;
 
748
    struct pci_region_entry *entry;
 
749
    hlist_for_each_entry_safe(entry, n, &r->list, node) {
715
750
        u64 addr = r->base;
716
751
        r->base += entry->size;
717
752
        if (entry->bar == -1)
718
753
            // Update bus base address if entry is a bridge region
719
754
            busses[entry->dev->secondary_bus].r[entry->type].base = addr;
720
755
        pci_region_map_one_entry(entry, addr);
721
 
        struct pci_region_entry *next = entry->next;
 
756
        hlist_del(&entry->node);
722
757
        free(entry);
723
 
        entry = next;
724
758
    }
725
759
}
726
760
 
728
762
{
729
763
    if (pci_bios_init_root_regions(busses)) {
730
764
        struct pci_region r64_mem, r64_pref;
731
 
        r64_mem.list = NULL;
732
 
        r64_pref.list = NULL;
 
765
        r64_mem.list.first = NULL;
 
766
        r64_pref.list.first = NULL;
733
767
        pci_region_migrate_64bit_entries(&busses[0].r[PCI_REGION_TYPE_MEM],
734
768
                                         &r64_mem);
735
769
        pci_region_migrate_64bit_entries(&busses[0].r[PCI_REGION_TYPE_PREFMEM],
771
805
void
772
806
pci_setup(void)
773
807
{
774
 
    if (CONFIG_COREBOOT || usingXen()) {
775
 
        // PCI setup already done by coreboot or Xen - just do probe.
776
 
        pci_probe_devices();
 
808
    if (!CONFIG_QEMU)
777
809
        return;
778
 
    }
779
810
 
780
811
    dprintf(3, "pci setup\n");
781
812
 
807
838
    pci_bios_init_devices();
808
839
 
809
840
    free(busses);
 
841
 
 
842
    pci_enable_default_vga();
810
843
}