1/**
2 * \file PCI VM-to-host interface
3 */
4
5/*
6 * Copyright (c) 2009, ETH Zurich.
7 * All rights reserved.
8 *
9 * This file is distributed under the terms in the attached LICENSE file.
10 * If you do not find this file, copies can be found by writing to:
11 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
12 */
13
14#include <stdlib.h>
15
16#include "vmkitmon.h"
17#include "pci.h"
18#include "pci_host.h"
19#include <dev/pci_hdr0_mem_dev.h>
20
21#define INVALID         0xffffffff
22
23#define VMKIT_PCI_HOST_DEBUG_SWITCH
24
25#if defined(VMKIT_PCI_HOST_DEBUG_SWITCH)
26#define VMKIT_PCI_HOST_DEBUG(x...) printf("VMKit PCI host: " x)
27#else
28#define VMKIT_PCI_HOST_DEBUG(x...) ((void)0)
29#endif
30
31struct pci_host {
32    pci_hdr0_mem_t      ph;
33    uint32_t            pci_header[0x40];
34};
35
36static void confspace_write(struct pci_device *dev,
37                            union pci_config_address_word addr,
38                            enum opsize size, uint32_t val)
39{
40}
41
42static void confspace_read(struct pci_device *dev,
43                           union pci_config_address_word addr,
44                           enum opsize size, uint32_t *val)
45{
46    struct pci_host *h = dev->state;
47
48    if(addr.d.fnct_nr != 0) {
49        *val = INVALID;
50        return;
51    }
52
53    if(addr.d.doubleword < 0x40) {
54        *val = h->pci_header[addr.d.doubleword];
55        VMKIT_PCI_HOST_DEBUG("reading register %d, opsize %d: %x\n",
56                             addr.d.doubleword, size, *val);
57
58        if(size == 1) {
59            *val = (*val) & 0xffff;
60        }
61    } else {
62        *val = INVALID;
63    }
64}
65
66void init_host_devices(struct pci *pci)
67{
68    struct pci_device *dev = calloc(1, sizeof(struct pci_device));
69    struct pci_host *host = calloc(1, sizeof(struct pci_host));
70    pci_hdr0_mem_t *ph = &host->ph;
71
72    dev->confspace_write = confspace_write;
73    dev->confspace_read = confspace_read;
74    dev->state = host;
75
76    pci_hdr0_mem_initialize(ph, (mackerel_addr_t)host->pci_header);
77
78    int r = pci_attach_device(pci, 0, 1, dev);
79    assert(r == 0);
80}
81