Patchwork [08/25] pci: use helper functions to access pci config space.

login
register
mail settings
Submitter Isaku Yamahata
Date Oct. 2, 2009, 8:16 p.m.
Message ID <1254514577-11896-9-git-send-email-yamahata@valinux.co.jp>
Download mbox | patch
Permalink /patch/34893/
State Superseded
Headers show

Comments

Isaku Yamahata - Oct. 2, 2009, 8:16 p.m.
use pci_[gs]et_{byte, word, long}() to access pci configuration
space.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
---
 hw/pci.c |   38 ++++++++++++++++++--------------------
 1 files changed, 18 insertions(+), 20 deletions(-)
Michael S. Tsirkin - Oct. 4, 2009, 9:47 a.m.
On Sat, Oct 03, 2009 at 05:16:00AM +0900, Isaku Yamahata wrote:
> use pci_[gs]et_{byte, word, long}() to access pci configuration
> space.
> 
> Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>

Acked-by: Michael S. Tsirkin <mst@redhat.com>

> ---
>  hw/pci.c |   38 ++++++++++++++++++--------------------
>  1 files changed, 18 insertions(+), 20 deletions(-)
> 
> diff --git a/hw/pci.c b/hw/pci.c
> index 39791d0..21e23b0 100644
> --- a/hw/pci.c
> +++ b/hw/pci.c
> @@ -426,9 +426,9 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,
>      } else {
>          addr = 0x10 + region_num * 4;
>      }
> -    *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
> -    *(uint32_t *)(pci_dev->wmask + addr) = cpu_to_le32(wmask);
> -    *(uint32_t *)(pci_dev->cmask + addr) = 0xffffffff;
> +    pci_set_long(pci_dev->config + addr, type);
> +    pci_set_long(pci_dev->wmask + addr, wmask);
> +    pci_set_long(pci_dev->cmask + addr, 0xffffffff);
>  }
>  
>  static void pci_update_mappings(PCIDevice *d)
> @@ -437,7 +437,7 @@ static void pci_update_mappings(PCIDevice *d)
>      int cmd, i;
>      uint32_t last_addr, new_addr, config_ofs;
>  
> -    cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
> +    cmd = pci_get_word(d->config + PCI_COMMAND);
>      for(i = 0; i < PCI_NUM_REGIONS; i++) {
>          r = &d->io_regions[i];
>          if (i == PCI_ROM_SLOT) {
> @@ -448,8 +448,7 @@ static void pci_update_mappings(PCIDevice *d)
>          if (r->size != 0) {
>              if (r->type & PCI_ADDRESS_SPACE_IO) {
>                  if (cmd & PCI_COMMAND_IO) {
> -                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
> -                                                         config_ofs));
> +                    new_addr = pci_get_long(d->config + config_ofs);
>                      new_addr = new_addr & ~(r->size - 1);
>                      last_addr = new_addr + r->size - 1;
>                      /* NOTE: we have only 64K ioports on PC */
> @@ -462,8 +461,7 @@ static void pci_update_mappings(PCIDevice *d)
>                  }
>              } else {
>                  if (cmd & PCI_COMMAND_MEMORY) {
> -                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
> -                                                         config_ofs));
> +                    new_addr = pci_get_long(d->config + config_ofs);
>                      /* the ROM slot has a specific enable bit */
>                      if (i == PCI_ROM_SLOT && !(new_addr & 1))
>                          goto no_mem_map;
> @@ -489,7 +487,7 @@ static void pci_update_mappings(PCIDevice *d)
>                          int class;
>                          /* NOTE: specific hack for IDE in PC case:
>                             only one byte must be mapped. */
> -                        class = d->config[0x0a] | (d->config[0x0b] << 8);
> +                        class = pci_get_word(d->config + PCI_CLASS_DEVICE);
>                          if (class == 0x0101 && r->size == 4) {
>                              isa_unassign_ioport(r->addr + 2, 1);
>                          } else {
> @@ -520,18 +518,18 @@ uint32_t pci_default_read_config(PCIDevice *d,
>      default:
>      case 4:
>  	if (address <= 0xfc) {
> -	    val = le32_to_cpu(*(uint32_t *)(d->config + address));
> +            val = pci_get_long(d->config + address);
>  	    break;
>  	}
>  	/* fall through */
>      case 2:
>          if (address <= 0xfe) {
> -	    val = le16_to_cpu(*(uint16_t *)(d->config + address));
> +            val = pci_get_word(d->config + address);
>  	    break;
>  	}
>  	/* fall through */
>      case 1:
> -        val = d->config[address];
> +        val = pci_get_byte(d->config + address);
>          break;
>      }
>      return val;
> @@ -704,7 +702,7 @@ static void pci_info_device(PCIDevice *d)
>  
>      monitor_printf(mon, "  Bus %2d, device %3d, function %d:\n",
>                     d->bus->bus_num, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
> -    class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
> +    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
>      monitor_printf(mon, "    ");
>      desc = pci_class_descriptions;
>      while (desc->desc && class != desc->class)
> @@ -715,8 +713,8 @@ static void pci_info_device(PCIDevice *d)
>          monitor_printf(mon, "Class %04x", class);
>      }
>      monitor_printf(mon, ": PCI device %04x:%04x\n",
> -           le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
> -           le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
> +           pci_get_word(d->config + PCI_VENDOR_ID),
> +           pci_get_word(d->config + PCI_DEVICE_ID));
>  
>      if (d->config[PCI_INTERRUPT_PIN] != 0) {
>          monitor_printf(mon, "      IRQ %d.\n",
> @@ -1026,7 +1024,7 @@ static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
>      PCIIORegion *r;
>      int i, class;
>  
> -    class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
> +    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
>      desc = pci_class_descriptions;
>      while (desc->desc && class != desc->class)
>          desc++;
> @@ -1040,10 +1038,10 @@ static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
>                     "pci id %04x:%04x (sub %04x:%04x)\n",
>                     indent, "", ctxt,
>                     d->bus->bus_num, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
> -                   le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
> -                   le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))),
> -                   le16_to_cpu(*((uint16_t *)(d->config + PCI_SUBSYSTEM_VENDOR_ID))),
> -                   le16_to_cpu(*((uint16_t *)(d->config + PCI_SUBSYSTEM_ID))));
> +                   pci_get_word(d->config + PCI_VENDOR_ID),
> +                   pci_get_word(d->config + PCI_DEVICE_ID),
> +                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
> +                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
>      for (i = 0; i < PCI_NUM_REGIONS; i++) {
>          r = &d->io_regions[i];
>          if (!r->size)
> -- 
> 1.6.0.2
> 
>

Patch

diff --git a/hw/pci.c b/hw/pci.c
index 39791d0..21e23b0 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -426,9 +426,9 @@  void pci_register_bar(PCIDevice *pci_dev, int region_num,
     } else {
         addr = 0x10 + region_num * 4;
     }
-    *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
-    *(uint32_t *)(pci_dev->wmask + addr) = cpu_to_le32(wmask);
-    *(uint32_t *)(pci_dev->cmask + addr) = 0xffffffff;
+    pci_set_long(pci_dev->config + addr, type);
+    pci_set_long(pci_dev->wmask + addr, wmask);
+    pci_set_long(pci_dev->cmask + addr, 0xffffffff);
 }
 
 static void pci_update_mappings(PCIDevice *d)
@@ -437,7 +437,7 @@  static void pci_update_mappings(PCIDevice *d)
     int cmd, i;
     uint32_t last_addr, new_addr, config_ofs;
 
-    cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
+    cmd = pci_get_word(d->config + PCI_COMMAND);
     for(i = 0; i < PCI_NUM_REGIONS; i++) {
         r = &d->io_regions[i];
         if (i == PCI_ROM_SLOT) {
@@ -448,8 +448,7 @@  static void pci_update_mappings(PCIDevice *d)
         if (r->size != 0) {
             if (r->type & PCI_ADDRESS_SPACE_IO) {
                 if (cmd & PCI_COMMAND_IO) {
-                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
-                                                         config_ofs));
+                    new_addr = pci_get_long(d->config + config_ofs);
                     new_addr = new_addr & ~(r->size - 1);
                     last_addr = new_addr + r->size - 1;
                     /* NOTE: we have only 64K ioports on PC */
@@ -462,8 +461,7 @@  static void pci_update_mappings(PCIDevice *d)
                 }
             } else {
                 if (cmd & PCI_COMMAND_MEMORY) {
-                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
-                                                         config_ofs));
+                    new_addr = pci_get_long(d->config + config_ofs);
                     /* the ROM slot has a specific enable bit */
                     if (i == PCI_ROM_SLOT && !(new_addr & 1))
                         goto no_mem_map;
@@ -489,7 +487,7 @@  static void pci_update_mappings(PCIDevice *d)
                         int class;
                         /* NOTE: specific hack for IDE in PC case:
                            only one byte must be mapped. */
-                        class = d->config[0x0a] | (d->config[0x0b] << 8);
+                        class = pci_get_word(d->config + PCI_CLASS_DEVICE);
                         if (class == 0x0101 && r->size == 4) {
                             isa_unassign_ioport(r->addr + 2, 1);
                         } else {
@@ -520,18 +518,18 @@  uint32_t pci_default_read_config(PCIDevice *d,
     default:
     case 4:
 	if (address <= 0xfc) {
-	    val = le32_to_cpu(*(uint32_t *)(d->config + address));
+            val = pci_get_long(d->config + address);
 	    break;
 	}
 	/* fall through */
     case 2:
         if (address <= 0xfe) {
-	    val = le16_to_cpu(*(uint16_t *)(d->config + address));
+            val = pci_get_word(d->config + address);
 	    break;
 	}
 	/* fall through */
     case 1:
-        val = d->config[address];
+        val = pci_get_byte(d->config + address);
         break;
     }
     return val;
@@ -704,7 +702,7 @@  static void pci_info_device(PCIDevice *d)
 
     monitor_printf(mon, "  Bus %2d, device %3d, function %d:\n",
                    d->bus->bus_num, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
-    class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
+    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
     monitor_printf(mon, "    ");
     desc = pci_class_descriptions;
     while (desc->desc && class != desc->class)
@@ -715,8 +713,8 @@  static void pci_info_device(PCIDevice *d)
         monitor_printf(mon, "Class %04x", class);
     }
     monitor_printf(mon, ": PCI device %04x:%04x\n",
-           le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
-           le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
+           pci_get_word(d->config + PCI_VENDOR_ID),
+           pci_get_word(d->config + PCI_DEVICE_ID));
 
     if (d->config[PCI_INTERRUPT_PIN] != 0) {
         monitor_printf(mon, "      IRQ %d.\n",
@@ -1026,7 +1024,7 @@  static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
     PCIIORegion *r;
     int i, class;
 
-    class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
+    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
     desc = pci_class_descriptions;
     while (desc->desc && class != desc->class)
         desc++;
@@ -1040,10 +1038,10 @@  static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
                    "pci id %04x:%04x (sub %04x:%04x)\n",
                    indent, "", ctxt,
                    d->bus->bus_num, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
-                   le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
-                   le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))),
-                   le16_to_cpu(*((uint16_t *)(d->config + PCI_SUBSYSTEM_VENDOR_ID))),
-                   le16_to_cpu(*((uint16_t *)(d->config + PCI_SUBSYSTEM_ID))));
+                   pci_get_word(d->config + PCI_VENDOR_ID),
+                   pci_get_word(d->config + PCI_DEVICE_ID),
+                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
+                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
     for (i = 0; i < PCI_NUM_REGIONS; i++) {
         r = &d->io_regions[i];
         if (!r->size)