All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexey Kardashevskiy <aik@ozlabs.ru>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	jan.kiszka@siemens.com, qemu-devel@nongnu.org,
	qemulist@gmail.com, Avi Kivity <avi.kivity@gmail.com>,
	stefanha@redhat.com, david@gibson.dropbear.id.au
Subject: Re: [Qemu-devel] [PATCH 20/40] pci: use memory core for iommu support
Date: Fri, 10 May 2013 23:07:54 +1000	[thread overview]
Message-ID: <518CF12A.2040305@ozlabs.ru> (raw)
In-Reply-To: <1367936238-12196-21-git-send-email-pbonzini@redhat.com>

On 05/08/2013 12:16 AM, Paolo Bonzini wrote:
> From: Avi Kivity <avi.kivity@gmail.com>
> 
> Use the new iommu support in the memory core for iommu support.  The only
> user, spapr, is also converted, but it still provides a DMAContext
> interface until the non-PCI bits switch to AddressSpace.
> 
> Cc: Michael S. Tsirkin <mst@redhat.com>
> Signed-off-by: Avi Kivity <avi.kivity@gmail.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  hw/pci/pci.c             |   53 ++++++++++++++++++++++++++--------------------
>  hw/ppc/spapr_pci.c       |   12 +++++++---
>  include/hw/pci/pci.h     |    7 ++++-
>  include/hw/pci/pci_bus.h |    5 ++-
>  4 files changed, 46 insertions(+), 31 deletions(-)
> 
> diff --git a/hw/pci/pci.c b/hw/pci/pci.c
> index 16ed118..3eb397c 100644
> --- a/hw/pci/pci.c
> +++ b/hw/pci/pci.c
> @@ -279,6 +279,16 @@ int pci_find_domain(const PCIBus *bus)
>      return -1;
>  }
>  
> +static MemoryRegion *pci_default_iommu(PCIBus *bus, void *opaque, int devfn)
> +{
> +    /* FIXME: inherit memory region from bus creator */
> +    return get_system_memory();
> +}
> +
> +static void pci_default_iommu_dtor(MemoryRegion *mr)
> +{
> +}
> +
>  static void pci_bus_init(PCIBus *bus, DeviceState *parent,
>                           const char *name,
>                           MemoryRegion *address_space_mem,
> @@ -289,6 +299,7 @@ static void pci_bus_init(PCIBus *bus, DeviceState *parent,
>      bus->devfn_min = devfn_min;
>      bus->address_space_mem = address_space_mem;
>      bus->address_space_io = address_space_io;
> +    pci_setup_iommu(bus, pci_default_iommu, NULL, NULL);
>  
>      /* host bridge */
>      QLIST_INIT(&bus->child);
> @@ -801,21 +812,15 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
>                       PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
>          return NULL;
>      }
> +
>      pci_dev->bus = bus;
> -    if (bus->dma_context_fn) {
> -        pci_dev->dma = bus->dma_context_fn(bus, bus->dma_context_opaque, devfn);
> -    } else {
> -        /* FIXME: Make dma_context_fn use MemoryRegions instead, so this path is
> -         * taken unconditionally */
> -        /* FIXME: inherit memory region from bus creator */
> -        memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
> -                                 get_system_memory(), 0,
> -                                 memory_region_size(get_system_memory()));
> -        memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
> -        address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
> -        pci_dev->dma = g_new(DMAContext, 1);
> -        dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
> -    }
> +    pci_dev->iommu = bus->iommu_fn(bus, bus->iommu_opaque, devfn);


I tried to create a PCI bus on the default PHB and put e1000 onto it like this:

-device pci-bridge,chassis_nr=1  -device e1000,bus=pci.0,addr=1.0


And I got a crash as bus->iommu_fn==NULL.

Is it because we need this bridge to use parent's iommu_fn/iommu or
pci-bridge is not supposed to be created this way or ...?



I made small patch just to get one step further, the info qree is below.
Now our system firmware fails (enters infinite loop) but this is a
different story.


diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 64a983c..a156053 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -811,7 +811,11 @@ static PCIDevice *do_pci_register_device(PCIDevice
*pci_dev, PCIBus *bus,
     }

     pci_dev->bus = bus;
-    pci_dev->iommu = bus->iommu_fn(bus, bus->iommu_opaque, devfn);
+    if (bus->iommu_fn)
+        pci_dev->iommu = bus->iommu_fn(bus, bus->iommu_opaque, devfn);
+    else
+        pci_dev->iommu = bus->parent_dev->iommu;
+
     memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
                              pci_dev->iommu, 0,
memory_region_size(pci_dev->iommu));
     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);



  dev: spapr-pci-host-bridge, id ""
    index = 0
    buid = 0x800000020000000
    liobn = 0x80000000
    mem_win_addr = 0x100a0000000
    mem_win_size = 0x20000000
    io_win_addr = 0x10080000000
    io_win_size = 0x10000
    msi_win_addr = 0x10090000000
    irq 0
    bus: pci
      type PCI
      dev: pci-bridge, id ""
        chassis_nr = 1
        msi = on
        addr = 00.0
        romfile = <null>
        rombar = 1
        multifunction = off
        command_serr_enable = on
        class PCI bridge, addr 00:00.0, pci id 1b36:0001 (sub 0000:0000)
        bar 0: mem at 0xffffffffffffffff [0xfe]
        bus: pci.0
          type PCI
          dev: e1000, id ""
            mac = 52:54:00:12:34:57
            vlan = <null>
            netdev = <null>
            bootindex = -1
            autonegotiation = on
            addr = 01.0
            romfile = "efi-e1000.rom"
            rombar = 1
            multifunction = off
            command_serr_enable = on
            class Ethernet controller, addr 00:01.0, pci id 8086:100e (sub
1af4:1100)
            bar 0: mem at 0xffffffffffffffff [0x1fffe]
            bar 1: i/o at 0xffffffffffffffff [0x3e]





> +    memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
> +                             pci_dev->iommu, 0, memory_region_size(pci_dev->iommu));
> +    memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
> +    address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
> +    pci_dev->dma = g_new(DMAContext, 1);
> +    dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
>  
>      pci_dev->devfn = devfn;
>      pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
> @@ -870,12 +875,12 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
>      pci_dev->bus->devices[pci_dev->devfn] = NULL;
>      pci_config_free(pci_dev);
>  
> -    if (!pci_dev->bus->dma_context_fn) {
> -        address_space_destroy(&pci_dev->bus_master_as);
> -        memory_region_destroy(&pci_dev->bus_master_enable_region);
> -        g_free(pci_dev->dma);
> -        pci_dev->dma = NULL;
> -    }
> +    address_space_destroy(&pci_dev->bus_master_as);
> +    memory_region_del_subregion(&pci_dev->bus_master_enable_region, pci_dev->iommu);
> +    pci_dev->bus->iommu_dtor_fn(pci_dev->iommu);
> +    memory_region_destroy(&pci_dev->bus_master_enable_region);
> +    g_free(pci_dev->dma);
> +    pci_dev->dma = NULL;
>  }
>  
>  static void pci_unregister_io_regions(PCIDevice *pci_dev)
> @@ -2234,10 +2239,12 @@ static void pci_device_class_init(ObjectClass *klass, void *data)
>      k->props = pci_props;
>  }
>  
> -void pci_setup_iommu(PCIBus *bus, PCIDMAContextFunc fn, void *opaque)
> +void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, PCIIOMMUDestructorFunc dtor,
> +                     void *opaque)
>  {
> -    bus->dma_context_fn = fn;
> -    bus->dma_context_opaque = opaque;
> +    bus->iommu_fn = fn;
> +    bus->iommu_dtor_fn = dtor ? dtor : pci_default_iommu_dtor;
> +    bus->iommu_opaque = opaque;
>  }
>  
>  static const TypeInfo pci_device_type_info = {
> diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c
> index eb64a8f..ffbb45e 100644
> --- a/hw/ppc/spapr_pci.c
> +++ b/hw/ppc/spapr_pci.c
> @@ -506,12 +506,11 @@ static const MemoryRegionOps spapr_msi_ops = {
>  /*
>   * PHB PCI device
>   */
> -static DMAContext *spapr_pci_dma_context_fn(PCIBus *bus, void *opaque,
> -                                            int devfn)
> +static MemoryRegion *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
>  {
>      sPAPRPHBState *phb = opaque;
>  
> -    return spapr_tce_get_dma(phb->tcet);
> +    return spapr_tce_get_iommu(phb->tcet);
>  }
>  
>  static int spapr_phb_init(SysBusDevice *s)
> @@ -651,7 +655,7 @@ static int spapr_phb_init(SysBusDevice *s)
>          fprintf(stderr, "Unable to create TCE table for %s\n", sphb->dtbusname);
>          return -1;
>      }
> -    pci_setup_iommu(bus, spapr_pci_dma_context_fn, sphb);
> +    pci_setup_iommu(bus, spapr_pci_dma_iommu, NULL, sphb);
>  
>      QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
>  
> diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
> index 8d075ab..7e7b0f4 100644
> --- a/include/hw/pci/pci.h
> +++ b/include/hw/pci/pci.h
> @@ -242,6 +242,7 @@ struct PCIDevice {
>      PCIIORegion io_regions[PCI_NUM_REGIONS];
>      AddressSpace bus_master_as;
>      MemoryRegion bus_master_enable_region;
> +    MemoryRegion *iommu;
>      DMAContext *dma;
>  
>      /* do not access the following fields */
> @@ -401,9 +402,11 @@ int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
>  
>  void pci_device_deassert_intx(PCIDevice *dev);
>  
> -typedef DMAContext *(*PCIDMAContextFunc)(PCIBus *, void *, int);
> +typedef MemoryRegion *(*PCIIOMMUFunc)(PCIBus *, void *, int);
> +typedef void (*PCIIOMMUDestructorFunc)(MemoryRegion *mr);
>  
> -void pci_setup_iommu(PCIBus *bus, PCIDMAContextFunc fn, void *opaque);
> +void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, PCIIOMMUDestructorFunc dtor,
> +                     void *opaque);
>  
>  static inline void
>  pci_set_byte(uint8_t *config, uint8_t val)
> diff --git a/include/hw/pci/pci_bus.h b/include/hw/pci/pci_bus.h
> index 6ee443c..fada8f5 100644
> --- a/include/hw/pci/pci_bus.h
> +++ b/include/hw/pci/pci_bus.h
> @@ -10,8 +10,9 @@
>  
>  struct PCIBus {
>      BusState qbus;
> -    PCIDMAContextFunc dma_context_fn;
> -    void *dma_context_opaque;
> +    PCIIOMMUFunc iommu_fn;
> +    PCIIOMMUDestructorFunc iommu_dtor_fn;
> +    void *iommu_opaque;
>      uint8_t devfn_min;
>      pci_set_irq_fn set_irq;
>      pci_map_irq_fn map_irq;
> 


-- 
Alexey

  parent reply	other threads:[~2013-05-10 13:08 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-07 14:16 [Qemu-devel] [PATCH 00/40] Memory-related changes sneak peek for 1.6 Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 01/40] memory: assert that PhysPageEntry's ptr does not overflow Paolo Bonzini
2013-05-07 15:44   ` Peter Maydell
2013-05-07 16:08     ` Paolo Bonzini
2013-05-07 16:17       ` Peter Maydell
2013-05-09  3:41       ` liu ping fan
2013-05-09 16:46         ` Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 02/40] memory: allow memory_region_find() to run on non-root memory regions Paolo Bonzini
2013-05-07 15:35   ` Peter Maydell
2013-05-09  0:46   ` liu ping fan
2013-05-07 14:16 ` [Qemu-devel] [PATCH 03/40] memory: Replace open-coded memory_region_is_romd Paolo Bonzini
2013-05-07 15:59   ` Peter Maydell
2013-05-07 14:16 ` [Qemu-devel] [PATCH 04/40] memory: Rename readable flag to romd_mode Paolo Bonzini
2013-05-07 16:10   ` Peter Maydell
2013-05-07 17:04     ` [Qemu-devel] [PATCH v2] " Jan Kiszka
2013-05-07 17:07       ` Peter Maydell
2013-05-07 14:16 ` [Qemu-devel] [PATCH 05/40] memory: do not duplicate memory_region_destructor_none Paolo Bonzini
2013-05-07 14:36   ` Peter Maydell
2013-05-07 14:16 ` [Qemu-devel] [PATCH 06/40] memory: make memory_global_sync_dirty_bitmap take an AddressSpace Paolo Bonzini
2013-05-07 14:59   ` Peter Maydell
2013-05-07 14:16 ` [Qemu-devel] [PATCH 07/40] memory: fix address space initialization/destruction Paolo Bonzini
2013-05-07 15:46   ` Peter Maydell
2013-05-07 14:16 ` [Qemu-devel] [PATCH 08/40] memory: limit sections in the radix tree to the actual address space size Paolo Bonzini
2013-05-07 17:13   ` Peter Maydell
2013-05-07 17:24     ` Paolo Bonzini
2013-05-07 17:37       ` Alexander Graf
2013-05-07 14:16 ` [Qemu-devel] [PATCH 09/40] memory: create FlatView for new address spaces Paolo Bonzini
2013-05-07 17:25   ` Peter Maydell
2013-05-08  8:41     ` Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 10/40] exec: remove obsolete comment Paolo Bonzini
2013-05-07 14:25   ` Peter Maydell
2013-05-07 14:16 ` [Qemu-devel] [PATCH 11/40] memory: add address_space_valid Paolo Bonzini
2013-05-07 17:40   ` Peter Maydell
2013-05-13 14:03     ` Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 12/40] memory: add address_space_translate Paolo Bonzini
2013-05-07 18:08   ` Peter Maydell
2013-05-20 10:41     ` Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 13/40] memory: Introduce address_space_lookup_region Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 14/40] memory: iommu support Paolo Bonzini
2013-05-07 18:15   ` Peter Maydell
2013-05-07 14:16 ` [Qemu-devel] [PATCH 15/40] vfio: abort if an emulated iommu is used Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 16/40] spapr: convert TCE API to use an opaque type Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 17/40] spapr: make IOMMU translation go through IOMMUTLBEntry Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 18/40] spapr: use memory core for iommu support Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 19/40] dma: eliminate old-style IOMMU support Paolo Bonzini
2013-05-07 18:20   ` Peter Maydell
2013-05-13 14:04     ` Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 20/40] pci: use memory core for iommu support Paolo Bonzini
2013-05-07 18:30   ` Peter Maydell
2013-05-11  5:09     ` liu ping fan
2013-05-11  8:07       ` Peter Maydell
2013-05-10 13:07   ` Alexey Kardashevskiy [this message]
2013-05-10 13:55     ` Paolo Bonzini
2013-05-07 14:16 ` [Qemu-devel] [PATCH 21/40] spapr_vio: take care of creating our own AddressSpace/DMAContext Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 22/40] dma: eliminate DMAContext Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 23/40] memory: give name to every AddressSpace Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 24/40] memory: add getter/setter for owner Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 25/40] memory: add ref/unref Paolo Bonzini
2013-05-08  9:05   ` Stefan Hajnoczi
2013-05-07 14:17 ` [Qemu-devel] [PATCH 26/40] memory: add ref/unref calls Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 27/40] pci: set owner for BARs Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 28/40] sysbus: set owner for MMIO regions Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 29/40] acpi: add memory_region_set_owner calls Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 30/40] misc: " Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 31/40] isa/portio: allow setting an owner Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 32/40] vga: add memory_region_set_owner calls Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 33/40] pci-assign: " Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 34/40] vfio: " Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 35/40] exec: check MRU in qemu_ram_addr_from_host Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 36/40] memory: return MemoryRegion from qemu_ram_addr_from_host Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 37/40] memory: ref/unref memory across address_space_map/unmap Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 38/40] memory: access FlatView from a local variable Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 39/40] memory: use a new FlatView pointer on every topology update Paolo Bonzini
2013-05-07 14:17 ` [Qemu-devel] [PATCH 40/40] memory: add reference counting to FlatView Paolo Bonzini
2013-05-07 18:00   ` Jan Kiszka
2013-05-07 18:10     ` Jan Kiszka
2013-05-07 19:44     ` Paolo Bonzini
2013-05-08  7:57       ` Jan Kiszka

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=518CF12A.2040305@ozlabs.ru \
    --to=aik@ozlabs.ru \
    --cc=avi.kivity@gmail.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=jan.kiszka@siemens.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemulist@gmail.com \
    --cc=stefanha@redhat.com \
    /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.