Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel' of ssh://palacios@newskysaw.cs.northwestern.edu/home/palacios...
[palacios.git] / palacios / src / devices / pci.c
index e86af94..1db0134 100644 (file)
@@ -315,6 +315,21 @@ static int data_port_read(ushort_t port, void * dst, uint_t length, struct vm_de
 static inline int is_cfg_reg_writable(uchar_t header_type, int reg_num) {
     if (header_type == 0x00) {
        switch (reg_num) {
+           case 0x00:
+           case 0x01:
+           case 0x02:
+           case 0x03:
+           case 0x08:
+           case 0x09:
+           case 0x0a:
+           case 0x0b:
+           case 0x0e:
+           case 0x3d:
+               return 0;
+                           
+           default:
+               return 1;
            // case (non writable reg list):
            
            default:
@@ -374,6 +389,7 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
                // COMMAND update
            } else if (cur_reg == 0x0f) {
                // BIST update
+               pci_dev->config_header.BIST = 0x00;
            }
        }
     }
@@ -385,10 +401,16 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
     // Scan for BAR updated
     if (pci_dev->bar_update_flag) {
        for (i = 0; i < 6; i++) {
-           if ((pci_dev->bar[i].updated) && (pci_dev->bar[i].bar_update)) {
-               pci_dev->bar[i].bar_update(pci_dev, i);
+           if (pci_dev->bar[i].updated) {
+               int bar_offset = 0x10 + 4 * i;
+
+               *(uint32_t *)pci_dev->config_space + bar_offset) &= pci_dev->bar[i].mask;
+
+               if (pci_dev->bar[i].bar_update) {
+                   pci_dev->bar[i].bar_update(pci_dev, i);
+               }
+               pci_dev->bar[i].updated = 0;
            }
-           pci_dev->bar[i].updated = 0;
        }
        pci_dev->bar_update_flag = 0;
     }
@@ -521,6 +543,37 @@ struct vm_device * v3_create_pci() {
 
 
 
+static inline int init_bars(struct pci_device * pci_dev) {
+    int i = 0;
+
+    for (i = 0; i < 6; i++) {
+       int bar_offset = 0x10 + 4 * i;
+
+       if (pci_dev->bar[i].type == PCI_BAR_IO) {
+           *(uint32_t *)(pci_dev->config_space + bar_offset) = 0x00000001;
+       } else if (pci_dev->bar[i].type == PCI_BAR_MEM32) {
+           pci_dev->bar[i].mask = (pci_dev->bar[i].num_pages << 12) - 1;
+           pci_dev->bar[i].mask |= 0xf; // preserve the configuration flags
+            
+           *(uint32_t *)(pci_dev->config_space + bar_offset) = 0x00000008;
+           
+           if (pci_dev->bar[i].mem_hook) {
+               // clear the prefetchable flag...
+               *(uint8_t *)(pci_dev->config_space + bar_offset) &= ~0x00000008;
+           }
+       } else if (pci_dev->bar[i].type == PCI_BAR_MEM16) {
+           PrintError("16 Bit memory ranges not supported (reg: %d)\n", i);
+       } else if (pci_dev->bar[i].type == PCI_BAR_NONE) {
+           *(uint32_t *)(pci_dev->config_space + bar_offset) = 0x00000000;
+       } else {
+           PrintError("Invalid BAR type for bar #%d\n", i);
+           return -1;
+       }
+           
+
+    }
+}
+
 
 
 // if dev_num == -1, auto assign 
@@ -528,13 +581,16 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
                                           uint_t bus_num,
                                           const char * name,
                                           int dev_num,
+                                          struct v3_pci_bar * bars,
                                           int (*config_update)(struct pci_device * pci_dev, uint_t reg_num, int length),
+                                          int (*cmd_update)(struct pci_device *pci_dev, uchar_t io_enabled, uchar_t mem_enabled),
                                           int (*bar_update)(struct pci_device * pci_dev, uint_t bar),
                                           void * private_data) {
 
     struct pci_internal * pci_state = (struct pci_internal *)pci->private_data;
     struct pci_bus * bus = &(pci_state->bus_list[bus_num]);
     struct pci_device * pci_dev = NULL;
+    int i;
 
     if (dev_num > MAX_BUS_DEVICES) {
        PrintError("Requested Invalid device number (%d)\n", dev_num);
@@ -562,7 +618,7 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
     }
 
     memset(pci_dev, 0, sizeof(struct pci_device));
-       
+    
     
     pci_dev->bus_num = bus_num;
     pci_dev->dev_num = dev_num;
@@ -576,8 +632,25 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
 
     pci_dev->priv_data = private_data;
 
+    
+    //copy bars
+    for (i = 0; i < 6; i ++){
+      pci_dev->bar[i].type = bars[i].type;
+      pci_dev->bar[i].num_resources = bars[i].num_resources;
+      pci_dev->bar[i].bar_update = bars[i].bar_update;
+    }
+
+    if (init_bars(pci_dev) == -1) {
+       PrintError("could not initialize bar registers\n");
+       return NULL;
+    }
+
+    pci_dev->cmd_update = cmd_update;
+    pci_dev->ext_rom_update = ext_rom_update;
+
     // add the device
     add_device_to_bus(bus, pci_dev);
+
     
 #ifdef DEBUG_PCI
     pci_dump_state(pci_state);