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.


updates to the PCI configuration for the southbridge and northbridge
[palacios.git] / palacios / src / devices / pci.c
index ad53670..6b5472d 100644 (file)
@@ -154,9 +154,9 @@ struct pci_device * __add_device_to_bus(struct pci_bus * bus, struct pci_device
     parent = *p;
     tmp_dev = rb_entry(parent, struct pci_device, dev_tree_node);
 
-    if (dev->dev_num < tmp_dev->dev_num) {
+    if (dev->devfn < tmp_dev->devfn) {
       p = &(*p)->rb_left;
-    } else if (dev->dev_num > tmp_dev->dev_num) {
+    } else if (dev->devfn > tmp_dev->devfn) {
       p = &(*p)->rb_right;
     } else {
       return tmp_dev;
@@ -186,16 +186,17 @@ struct pci_device * add_device_to_bus(struct pci_bus * bus, struct pci_device *
 }
 
 
-static struct pci_device * get_device(struct pci_bus * bus, int dev_num) {
+static struct pci_device * get_device(struct pci_bus * bus, uint8_t dev_num, uint8_t fn_num) {
     struct rb_node * n = bus->devices.rb_node;
     struct pci_device * dev = NULL;
+    uint8_t devfn = ((dev_num & 0x1f) << 3) | (fn_num & 0x7);
 
     while (n) {
        dev = rb_entry(n, struct pci_device, dev_tree_node);
        
-       if (dev_num < dev->dev_num) {
+       if (devfn < dev->devfn) {
            n = n->rb_left;
-       } else if (dev_num > dev->dev_num) {
+       } else if (devfn > dev->devfn) {
            n = n->rb_right;
        } else {
            return dev;
@@ -301,7 +302,7 @@ static int data_port_read(ushort_t port, void * dst, uint_t length, struct vm_de
               reg_num, reg_num, 
               pci_state->addr_reg.val);
 
-    pci_dev = get_device(&(pci_state->bus_list[0]), pci_state->addr_reg.dev_num);
+    pci_dev = get_device(&(pci_state->bus_list[0]), pci_state->addr_reg.dev_num, pci_state->addr_reg.fn_num);
     
     if (pci_dev == NULL) {
        for (i = 0; i < length; i++) {
@@ -340,6 +341,24 @@ static inline int is_cfg_reg_writable(uchar_t header_type, int reg_num) {
                return 1;
  
        }
+    } else if (header_type == 0x80) {
+       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;
+       }
     } else {
        // PCI to PCI Bridge = 0x01
        // CardBus Bridge = 0x02
@@ -352,9 +371,43 @@ static inline int is_cfg_reg_writable(uchar_t header_type, int reg_num) {
 }
 
 
-static int bar_update(struct pci_device * pci, int bar_num) {
-    PrintError("Bar Updates not handled (bar=%d)\n", bar_num);
-    return -1;
+static int bar_update(struct pci_device * pci, int bar_num, uint32_t new_val) {
+    struct v3_pci_bar * bar = &(pci->bar[bar_num]);
+
+    PrintDebug("Updating BAR Register  (Dev=%s) (bar=%d) (old_val=%x) (new_val=%x)\n", 
+              pci->name, bar_num, bar->val, new_val);
+
+    switch (bar->type) {
+       case PCI_BAR_IO: {
+           int i = 0;
+
+               PrintDebug("\tRehooking %d IO ports from base %x to %x\n",
+                          bar->num_ports, PCI_IO_BASE(bar->val), PCI_IO_BASE(new_val));
+               
+           // only do this if pci device is enabled....
+           for (i = 0; i < bar->num_ports; i++) {
+
+               v3_dev_unhook_io(pci->vm_dev, PCI_IO_BASE(bar->val) + i);
+
+               v3_dev_hook_io(pci->vm_dev, PCI_IO_BASE(new_val) + i, 
+                              bar->io_read, bar->io_write);
+           }
+
+           bar->val = new_val;
+
+           break;
+       }
+       case PCI_BAR_NONE: {
+           PrintDebug("Reprogramming an unsupported BAR register (Dev=%s) (bar=%d) (val=%x)\n", 
+                      pci->name, bar_num, new_val);
+           break;
+       }
+       default:
+           PrintError("Invalid Bar Reg updated (bar=%d)\n", bar_num);
+           return -1;
+    }
+
+    return 0;
 }
 
 
@@ -377,7 +430,7 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
               *(uint32_t *)src, length);
 
 
-    pci_dev = get_device(&(pci_state->bus_list[0]), pci_state->addr_reg.dev_num);
+    pci_dev = get_device(&(pci_state->bus_list[0]), pci_state->addr_reg.dev_num, pci_state->addr_reg.fn_num);
     
     if (pci_dev == NULL) {
        PrintError("Writing configuration space for non-present device (dev_num=%d)\n", 
@@ -388,27 +441,39 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
 
     for (i = 0; i < length; i++) {
        uint_t cur_reg = reg_num + i;
+       int writable = is_cfg_reg_writable(pci_dev->config_header.header_type, cur_reg);
        
-       if (is_cfg_reg_writable(pci_dev->config_header.header_type, cur_reg)) {
+       if (writable == -1) {
+           PrintError("Invalid PCI configuration space\n");
+           return -1;
+       }
+
+       if (writable) {
            pci_dev->config_space[cur_reg] = *(uint8_t *)((uint8_t *)src + i);
 
            if ((cur_reg >= 0x10) && (cur_reg < 0x28)) {
-               // BAR Reg
-               int bar_reg = (cur_reg & ~0x3) - 0x10;
+               // BAR Register Update
+               int bar_reg = ((cur_reg & ~0x3) - 0x10) / 4;
                
                pci_dev->bar_update_flag = 1;
                pci_dev->bar[bar_reg].updated = 1;
                
-               PrintDebug("Updating BAR register\n");
+               // PrintDebug("Updating BAR register %d\n", bar_reg);
 
            } else if ((cur_reg >= 0x30) && (cur_reg < 0x34)) {
+               // Extension ROM update
+
                pci_dev->ext_rom_update_flag = 1;
            } else if (cur_reg == 0x04) {
                // COMMAND update            
                uint8_t command = *((uint8_t *)src + i);
                
+               PrintError("command update for %s old=%x new=%x\n",
+                          pci_dev->name, 
+                          pci_dev->config_space[cur_reg],command);
+
                pci_dev->config_space[cur_reg] = command;             
-               
+
                if (pci_dev->cmd_update) {
                    pci_dev->cmd_update(pci_dev, (command & 0x01), (command & 0x02));
                }
@@ -431,9 +496,10 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
                int bar_offset = 0x10 + 4 * i;
 
                *(uint32_t *)(pci_dev->config_space + bar_offset) &= pci_dev->bar[i].mask;
+               // check special flags....
 
                // bar_update
-               if (bar_update(pci_dev, i) == -1) {
+               if (bar_update(pci_dev, i, *(uint32_t *)(pci_dev->config_space + bar_offset)) == -1) {
                    PrintError("PCI Device %s: Bar update Error Bar=%d\n", pci_dev->name, i);
                    return -1;
                }
@@ -487,36 +553,6 @@ static int pci_deinit_device(struct vm_device * dev) {
 
 
 
-
-static int init_i440fx(struct vm_device * dev) {
-    struct pci_device * pci_dev = NULL;
-    struct v3_pci_bar bars[6];
-    int i;
-    
-    for (i = 0; i < 6; i++) {
-       bars[i].type = PCI_BAR_NONE;
-    }    
-
-    pci_dev = v3_pci_register_device(dev, PCI_STD_DEVICE, 0, "i440FX", 0, bars,
-                                    NULL, NULL, NULL, NULL);
-    
-    if (!pci_dev) {
-       return -1;
-    }
-    
-    pci_dev->config_header.vendor_id = 0x8086;
-    pci_dev->config_header.device_id = 0x1237;
-    pci_dev->config_header.revision = 0x0002;
-    pci_dev->config_header.subclass = 0x00; //  SubClass: host2pci
-    pci_dev->config_header.class = 0x06;    // Class: PCI bridge
-
-    pci_dev->bus_num = 0;
-    return 0;
-}
-
-
-
-
 static void init_pci_busses(struct pci_internal * pci_state) {
     int i;
 
@@ -541,11 +577,6 @@ static int pci_init_device(struct vm_device * dev) {
     pci_state->addr_reg.val = 0; 
 
     init_pci_busses(pci_state);
-
-    if (init_i440fx(dev) == -1) {
-       PrintError("Could not intialize i440fx\n");
-       return -1;
-    }
     
     PrintDebug("Sizeof config header=%d\n", (int)sizeof(struct pci_config_header));
     
@@ -588,9 +619,9 @@ static inline int init_bars(struct pci_device * pci_dev) {
        if (pci_dev->bar[i].type == PCI_BAR_IO) {
            int j = 0;
            pci_dev->bar[i].mask = (~((pci_dev->bar[i].num_ports) - 1)) | 0x01;
-           
-           *(uint32_t *)(pci_dev->config_space + bar_offset) = pci_dev->bar[i].default_base_port & pci_dev->bar[i].mask;
-           *(uint32_t *)(pci_dev->config_space + bar_offset) |= 0x00000001;
+
+           pci_dev->bar[i].val = pci_dev->bar[i].default_base_port & pci_dev->bar[i].mask;
+           pci_dev->bar[i].val |= 0x00000001;
 
            for (j = 0; j < pci_dev->bar[i].num_ports; j++) {
                // hook IO
@@ -601,11 +632,13 @@ static inline int init_bars(struct pci_device * pci_dev) {
                }
            }
 
+           *(uint32_t *)(pci_dev->config_space + bar_offset) = pci_dev->bar[i].val;
+
        } 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) = pci_dev->bar[i].default_base_addr & pci_dev->bar[i].mask;
+           pci_dev->bar[i].val = pci_dev->bar[i].default_base_addr & pci_dev->bar[i].mask;
 
            // hook memory
            if (pci_dev->bar[i].mem_read) {
@@ -624,14 +657,19 @@ static inline int init_bars(struct pci_device * pci_dev) {
                */
            } else {
                // set the prefetchable flag...
-               *(uint8_t *)(pci_dev->config_space + bar_offset) |= 0x00000008;
+               pci_dev->bar[i].val |= 0x00000008;
            }
 
+
+           *(uint32_t *)(pci_dev->config_space + bar_offset) = pci_dev->bar[i].val;
+
        } else if (pci_dev->bar[i].type == PCI_BAR_MEM16) {
            PrintError("16 Bit memory ranges not supported (reg: %d)\n", i);
            return -1;
        } else if (pci_dev->bar[i].type == PCI_BAR_NONE) {
-           *(uint32_t *)(pci_dev->config_space + bar_offset) = 0x00000000;
+           pci_dev->bar[i].val = 0x00000000;
+           pci_dev->bar[i].mask = 0x00000000; // This ensures that all updates will be dropped
+           *(uint32_t *)(pci_dev->config_space + bar_offset) = pci_dev->bar[i].val;
        } else {
            PrintError("Invalid BAR type for bar #%d\n", i);
            return -1;
@@ -645,14 +683,15 @@ static inline int init_bars(struct pci_device * pci_dev) {
 // if dev_num == -1, auto assign 
 struct pci_device * v3_pci_register_device(struct vm_device * pci,
                                           pci_device_type_t dev_type, 
-                                          uint_t bus_num,
-                                          const char * name,
+                                          int bus_num,
                                           int dev_num,
+                                          int fn_num,
+                                          const char * name,
                                           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 (*ext_rom_update)(struct pci_device * pci_dev),
-                                          void * private_data) {
+                                          struct vm_device * dev) {
 
     struct pci_internal * pci_state = (struct pci_internal *)pci->private_data;
     struct pci_bus * bus = &(pci_state->bus_list[bus_num]);
@@ -671,7 +710,7 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
        }
     }
     
-    if (get_device(bus, dev_num) != NULL) {
+    if (get_device(bus, dev_num, fn_num) != NULL) {
        PrintError("PCI Device already registered at slot %d on bus %d\n", 
                   dev_num, bus->bus_num);
        return NULL;
@@ -681,16 +720,20 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
     pci_dev = (struct pci_device *)V3_Malloc(sizeof(struct pci_device));
 
     if (pci_dev == NULL) {
+       PrintError("Could not allocate pci device\n");
        return NULL;
     }
 
     memset(pci_dev, 0, sizeof(struct pci_device));
-    
+
     
     switch (dev_type) {
        case PCI_STD_DEVICE:
            pci_dev->config_header.header_type = 0x00;
            break;
+       case PCI_MULTIFUNCTION:
+           pci_dev->config_header.header_type = 0x80;
+           break;
        default:
            PrintError("Unhandled PCI Device Type: %d\n", dev_type);
            return NULL;
@@ -698,20 +741,19 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
 
     pci_dev->bus_num = bus_num;
     pci_dev->dev_num = dev_num;
+    pci_dev->fn_num = fn_num;
 
     strncpy(pci_dev->name, name, sizeof(pci_dev->name));
-    pci_dev->vm_dev = pci;
+    pci_dev->vm_dev = dev;
 
     // register update callbacks
     pci_dev->config_update = config_update;
     pci_dev->cmd_update = cmd_update;
     pci_dev->ext_rom_update = ext_rom_update;
 
-    pci_dev->priv_data = private_data;
-
 
     //copy bars
-    for (i = 0; i < 6; i ++){
+    for (i = 0; i < 6; i ++) {
        pci_dev->bar[i].type = bars[i].type;
 
        if (pci_dev->bar[i].type == PCI_BAR_IO) {