All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tiejun Chen <tiejun.chen@intel.com>
To: mst@redhat.com, aliguori@amazon.com
Cc: qemu-devel@nongnu.org
Subject: [Qemu-devel] [v3][PATCH 1/1] hw/pci-assign: split pci-assign.c
Date: Mon,  1 Sep 2014 18:36:47 +0800	[thread overview]
Message-ID: <1409567807-18633-2-git-send-email-tiejun.chen@intel.com> (raw)
In-Reply-To: <1409567807-18633-1-git-send-email-tiejun.chen@intel.com>

We will try to reuse assign_dev_load_option_rom in xen side, and
especially its a good beginning to unify pci assign codes both on
kvm and xen in the future.

Signed-off-by: Tiejun Chen <tiejun.chen@intel.com>
---
 hw/i386/kvm/pci-assign.c    | 46 +++++++++++++++++++++++++++++----------------
 include/hw/pci/pci-assign.h | 16 ++++++++++++++++
 2 files changed, 46 insertions(+), 16 deletions(-)
 create mode 100644 include/hw/pci/pci-assign.h

diff --git a/hw/i386/kvm/pci-assign.c b/hw/i386/kvm/pci-assign.c
index 17c7d6dc..fdc7b64 100644
--- a/hw/i386/kvm/pci-assign.c
+++ b/hw/i386/kvm/pci-assign.c
@@ -37,6 +37,7 @@
 #include "hw/pci/pci.h"
 #include "hw/pci/msi.h"
 #include "kvm_i386.h"
+#include "hw/pci/pci-assign.h"
 
 #define MSIX_PAGE_SIZE 0x1000
 
@@ -1896,37 +1897,39 @@ type_init(assign_register_types)
  * load the corresponding ROM data to RAM. If an error occurs while loading an
  * option ROM, we just ignore that option ROM and continue with the next one.
  */
-static void assigned_dev_load_option_rom(AssignedDevice *dev)
+int pci_assign_dev_load_option_rom(PCIDevice *dev, struct Object *owner,
+                                   void *ptr, unsigned int domain,
+                                   unsigned int bus, unsigned int slot,
+                                   unsigned int function)
 {
     char name[32], rom_file[64];
     FILE *fp;
     uint8_t val;
     struct stat st;
-    void *ptr;
+    int size = 0;
 
     /* If loading ROM from file, pci handles it */
-    if (dev->dev.romfile || !dev->dev.rom_bar) {
-        return;
+    if (dev->romfile || !dev->rom_bar) {
+        return -1;
     }
 
     snprintf(rom_file, sizeof(rom_file),
              "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
-             dev->host.domain, dev->host.bus, dev->host.slot,
-             dev->host.function);
+             domain, bus, slot, function);
 
     if (stat(rom_file, &st)) {
-        return;
+        return -1;
     }
 
     if (access(rom_file, F_OK)) {
         error_report("pci-assign: Insufficient privileges for %s", rom_file);
-        return;
+        return -1;
     }
 
     /* Write "1" to the ROM file to enable it */
     fp = fopen(rom_file, "r+");
     if (fp == NULL) {
-        return;
+        return -1;
     }
     val = 1;
     if (fwrite(&val, 1, 1, fp) != 1) {
@@ -1934,11 +1937,10 @@ static void assigned_dev_load_option_rom(AssignedDevice *dev)
     }
     fseek(fp, 0, SEEK_SET);
 
-    snprintf(name, sizeof(name), "%s.rom",
-            object_get_typename(OBJECT(dev)));
-    memory_region_init_ram(&dev->dev.rom, OBJECT(dev), name, st.st_size);
-    vmstate_register_ram(&dev->dev.rom, &dev->dev.qdev);
-    ptr = memory_region_get_ram_ptr(&dev->dev.rom);
+    snprintf(name, sizeof(name), "%s.rom", object_get_typename(owner));
+    memory_region_init_ram(&dev->rom, owner, name, st.st_size);
+    vmstate_register_ram(&dev->rom, &dev->qdev);
+    ptr = memory_region_get_ram_ptr(&dev->rom);
     memset(ptr, 0xff, st.st_size);
 
     if (!fread(ptr, 1, st.st_size, fp)) {
@@ -1949,8 +1951,9 @@ static void assigned_dev_load_option_rom(AssignedDevice *dev)
         goto close_rom;
     }
 
-    pci_register_bar(&dev->dev, PCI_ROM_SLOT, 0, &dev->dev.rom);
-    dev->dev.has_rom = true;
+    pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
+    dev->has_rom = true;
+    size = st.st_size;
 close_rom:
     /* Write "0" to disable ROM */
     fseek(fp, 0, SEEK_SET);
@@ -1959,4 +1962,15 @@ close_rom:
         DEBUG("%s\n", "Failed to disable pci-sysfs rom file");
     }
     fclose(fp);
+
+    return size;
+}
+
+static void assigned_dev_load_option_rom(AssignedDevice *dev)
+{
+    void *ptr = NULL;
+
+    pci_assign_dev_load_option_rom(&dev->dev, OBJECT(dev), ptr,
+                                   dev->host.domain, dev->host.bus,
+                                   dev->host.slot, dev->host.function);
 }
diff --git a/include/hw/pci/pci-assign.h b/include/hw/pci/pci-assign.h
new file mode 100644
index 0000000..6d6bf93
--- /dev/null
+++ b/include/hw/pci/pci-assign.h
@@ -0,0 +1,16 @@
+/*
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * Just split from hw/i386/kvm/pci-assign.c.
+ */
+#ifndef PCI_ASSIGN_H
+#define PCI_ASSIGN_H
+
+#include "hw/pci/pci.h"
+
+int pci_assign_dev_load_option_rom(PCIDevice *dev, struct Object *owner,
+                                   void *ptr, unsigned int domain,
+                                   unsigned int bus, unsigned int slot,
+                                   unsigned int function);
+#endif /* PCI_ASSIGN_H */
-- 
1.9.1

  reply	other threads:[~2014-09-01 10:40 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-01 10:36 [Qemu-devel] [v3][PATCH 0/1] qemu:pci-assign: try to pci-assign.c Tiejun Chen
2014-09-01 10:36 ` Tiejun Chen [this message]
2014-09-01 10:46   ` [Qemu-devel] [v3][PATCH 1/1] hw/pci-assign: split pci-assign.c Michael S. Tsirkin
2014-09-01 10:56     ` Chen, Tiejun
2014-09-01 11:08       ` Michael S. Tsirkin
2014-09-01 11:15         ` Chen, Tiejun
2014-09-01 11:19           ` Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1409567807-18633-2-git-send-email-tiejun.chen@intel.com \
    --to=tiejun.chen@intel.com \
    --cc=aliguori@amazon.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.