Make AGP work with IOMMU
Subsystems (2)
RFC 1/1 Make AGP work with IOMMU
Based on some patches from Zhenyu that I found at
http://people.freedesktop.org/~zhen/agp-mm-*, this set of patches stops the Intel graphics drivers from being utterly broken when the IOMMU is enabled...
http://git.infradead.org/users/dwmw2/iommu-agp.git
I'm still seeing write faults to seemingly random addresses at startup when I first enable the IOMMU (and before the gfx driver actually starts). Those happen even when I don't use any graphics drivers at all, though.
And I'm slightly confused about the way we use the scratch page without mask_memory().
commit d8450c7be46afd0b51d2dc171c9d2eb60366ce05
Author: Zhenyu Wang <[email protected]> Date: Mon Jul 27 12:59:57 2009 +0100
intel_agp: Use PCI DMA API correctly on chipsets new enough to have IOMMU
When graphics dma remapping engine is active, we must fill gart table with dma address from dmar engine, as now graphics device access to graphics memory must go through dma remapping table to get real physical address.
Add this support to all drivers which use intel_i915_insert_entries()
commit ca8cdb1f983c508f586b90870703e85aab87659a
Author: Zhenyu Wang <[email protected]> Date: Thu Jul 23 17:25:49 2009 +0100
agp: Add generic support for graphics dma remapping
New driver hooks for support graphics memory dma remapping are introduced in this patch. It makes generic code can tell if current device needs dma remapping, then call driver provided interfaces for mapping and unmapping. Change has also been made to handle scratch_page in remapping case.
commit 42a509b9cd513ddd536c38cc302c7457a3c811d7
Author: David Woodhouse <[email protected]> Date: Mon Jul 27 10:27:29 2009 +0100
agp: Switch mask_memory() method to take address argument again, not page
In commit 07613ba2 ("agp: switch AGP to use page array instead of unsigned long array") we switched the mask_memory() method to take a 'struct page *' instead of an address. This is painful, because in some cases it has to be an IOMMU-mapped virtual bus address (in fact, shouldn't it _always_ be a dma_addr_t returned from pci_map_xxx(), and we just happen to get lucky most of the time?)
drivers/char/agp/agp.h +8 -2
@@ -107,7 +107,7 @@ struct agp_bridge_driver {void (*agp_enable)(struct agp_bridge_data *,u32 );void (*cleanup)(void );void (*tlb_flush)(struct agp_memory *); -unsigned long (*mask_memory)(struct agp_bridge_data *,struct page *,int ); +unsigned long (*mask_memory)(struct agp_bridge_data *,dma_addr_t ,int );void (*cache_flush)(void );int (*create_gatt_table)(struct agp_bridge_data *);int (*free_gatt_table)(struct agp_bridge_data *);
@@ -121,6 +121,11 @@ struct agp_bridge_driver {void (*agp_destroy_pages)(struct agp_memory *);int (*agp_type_to_mask_type) (struct agp_bridge_data *,int );void (*chipset_flush)(struct agp_bridge_data *); + +int (*agp_map_page)(void *addr,dma_addr_t *ret); +void (*agp_unmap_page)(void *addr,dma_addr_t dma); +int (*agp_map_memory)(struct agp_memory *mem); +void (*agp_unmap_memory)(struct agp_memory *mem); };struct agp_bridge_data {
@@ -135,6 +140,7 @@ struct agp_bridge_data {u32 *gatt_table_real;unsigned long scratch_page;unsigned long scratch_page_real; +dma_addr_t scratch_page_dma;unsigned long gart_bus_addr;unsigned long gatt_bus_addr;u32 mode;
@@ -291,7 +297,7 @@ int agp_3_5_enable(struct agp_bridge_data *bridge);void global_cache_flush (void );void get_agp_version (struct agp_bridge_data *bridge);unsigned long agp_generic_mask_memory (struct agp_bridge_data *bridge, -struct page *page,int type); +dma_addr_t phys,int type);int agp_generic_type_to_mask_type (struct agp_bridge_data *bridge,int type);struct agp_bridge_data *agp_generic_find_bridge (struct pci_dev *pdev);
drivers/char/agp/amd-k7-agp.c +3 -1
@@ -325,7 +325,9 @@ static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type) addr = (j *PAGE_SIZE ) + agp_bridge->gart_bus_addr; cur_gatt =GET_GATT (addr);writel (agp_generic_mask_memory (agp_bridge, - mem->pages[i], mem->type), cur_gatt+GET_GATT_OFF (addr)); +phys_to_gart (page_to_phys (mem->pages[i])), + mem->type), + cur_gatt+GET_GATT_OFF (addr));readl (cur_gatt+GET_GATT_OFF (addr));/* PCI Posting. */ }amd_irongate_tlbflush (mem);
drivers/char/agp/amd64-agp.c +2 -1
@@ -79,7 +79,8 @@ static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type)for (i =0 , j = pg_start; i < mem->page_count; i++, j++) { tmp = agp_bridge->driver->mask_memory (agp_bridge, - mem->pages[i], mask_type); +phys_to_gart (page_to_phys (mem->pages[i])), + mask_type);BUG_ON (tmp &0xffffff0000000ffcULL ); pte = (tmp &0x000000ff00000000ULL ) >>28 ;
drivers/char/agp/ati-agp.c +2 -1
@@ -302,7 +302,8 @@ static int ati_insert_memory(struct agp_memory * mem, addr = (j *PAGE_SIZE ) + agp_bridge->gart_bus_addr; cur_gatt =GET_GATT (addr);writel (agp_bridge->driver->mask_memory (agp_bridge, - mem->pages[i], mem->type), +phys_to_gart (page_to_phys (mem->pages[i])), + mem->type), cur_gatt+GET_GATT_OFF (addr)); }readl (GET_GATT (agp_bridge->gart_bus_addr));/* PCI posting */
drivers/char/agp/backend.c +22 -2
@@ -150,8 +150,17 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge) } bridge->scratch_page_real =phys_to_gart (page_to_phys (page)); - bridge->scratch_page = - bridge->driver->mask_memory (bridge, page,0 ); + bridge->scratch_page = bridge->driver->mask_memory (bridge, +phys_to_gart (page_to_phys (page)),0 ); + +if (bridge->driver->agp_map_page && + bridge->driver->agp_map_page (phys_to_virt (page_to_phys (page)), + &bridge->scratch_page_dma)) { +dev_err (&bridge->dev->dev, +"unable to dma-map scratch page\n" ); + rc = -ENOMEM ; + goto err_out_nounmap; + } } size_value = bridge->driver->fetch_size ();
@@ -191,6 +200,13 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge)return 0 ; err_out: +if (bridge->driver->needs_scratch_page && + bridge->driver->agp_unmap_page) { +void *va =gart_to_virt (bridge->scratch_page_real); + + bridge->driver->agp_unmap_page (va, bridge->scratch_page_dma); + } +err_out_nounmap:if (bridge->driver->needs_scratch_page) {void *va =gart_to_virt (bridge->scratch_page_real);
@@ -221,6 +237,10 @@ static void agp_backend_cleanup(struct agp_bridge_data *bridge) bridge->driver->needs_scratch_page) {void *va =gart_to_virt (bridge->scratch_page_real); +if (bridge->driver->agp_unmap_page) + bridge->driver->agp_unmap_page (va, + bridge->scratch_page_dma); + bridge->driver->agp_destroy_page (va,AGP_PAGE_DESTROY_UNMAP ); bridge->driver->agp_destroy_page (va,AGP_PAGE_DESTROY_FREE ); }
drivers/char/agp/generic.c +13 -3
@@ -437,6 +437,12 @@ int agp_bind_memory(struct agp_memory *curr, off_t pg_start) curr->bridge->driver->cache_flush (); curr->is_flushed = true; } + +if (curr->bridge->driver->agp_map_memory) { + ret_val = curr->bridge->driver->agp_map_memory (curr); +if (ret_val) +return ret_val; + } ret_val = curr->bridge->driver->insert_memory (curr, pg_start, curr->type);if (ret_val !=0 )
@@ -478,6 +484,9 @@ int agp_unbind_memory(struct agp_memory *curr)if (ret_val !=0 )return ret_val; +if (curr->bridge->driver->agp_unmap_memory) + curr->bridge->driver->agp_unmap_memory (curr); + curr->is_bound = false; curr->pg_start =0 ;spin_lock (&curr->bridge->mapped_lock);
@@ -1132,7 +1141,9 @@ int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type) }for (i =0 , j = pg_start; i < mem->page_count; i++, j++) { -writel (bridge->driver->mask_memory (bridge, mem->pages[i], mask_type), +writel (bridge->driver->mask_memory (bridge, +phys_to_gart (page_to_phys (mem->pages[i])), + mask_type), bridge->gatt_table+j); }readl (bridge->gatt_table+j-1 );/* PCI Posting. */
@@ -1347,9 +1358,8 @@ void global_cache_flush(void)EXPORT_SYMBOL (global_cache_flush);unsigned long agp_generic_mask_memory (struct agp_bridge_data *bridge, -struct page *page,int type) +dma_addr_t addr,int type) { -unsigned long addr =phys_to_gart (page_to_phys (page));/* memory type is ignored in the generic routine */ if (bridge->driver->masks)return addr | bridge->driver->masks[0 ].mask;
drivers/char/agp/hp-agp.c +1 -3
@@ -394,10 +394,8 @@ hp_zx1_remove_memory (struct agp_memory *mem, off_t pg_start, int type) }static unsigned long -hp_zx1_mask_memory (struct agp_bridge_data *bridge, -struct page *page,int type) +hp_zx1_mask_memory (struct agp_bridge_data *bridge,dma_addr_t addr,int type) { -unsigned long addr =phys_to_gart (page_to_phys (page));return HP_ZX1_PDIR_VALID_BIT | addr; }
drivers/char/agp/i460-agp.c +3 -10
@@ -61,7 +61,7 @@#define WR_FLUSH_GATT (index) RD_GATT(index)static unsigned long i460_mask_memory (struct agp_bridge_data *bridge, -unsigned long addr,int type); +dma_addr_t addr,int type); static struct { void *gatt; /* ioremap'd GATT area */
@@ -546,20 +546,13 @@ static void i460_destroy_page (struct page *page, int flags)#endif /* I460_LARGE_IO_PAGES */ static unsigned long i460_mask_memory (struct agp_bridge_data *bridge, -unsigned long addr,int type) +dma_addr_t addr,int type) {/* Make sure the returned address is a valid GATT entry */ return bridge->driver->masks[0 ].mask | (((addr & ~((1 <<I460_IO_PAGE_SHIFT ) -1 )) &0xfffff000 ) >>12 ); } -static unsigned long i460_page_mask_memory (struct agp_bridge_data *bridge, -struct page *page,int type) -{ -unsigned long addr =phys_to_gart (page_to_phys (page)); -return i460_mask_memory (bridge, addr, type); -} -const struct agp_bridge_driver intel_i460_driver = { .owner =THIS_MODULE , .aperture_sizes = i460_sizes,
@@ -569,7 +562,7 @@ const struct agp_bridge_driver intel_i460_driver = { .fetch_size = i460_fetch_size, .cleanup = i460_cleanup, .tlb_flush = i460_tlb_flush, - .mask_memory = i460_page_mask_memory, + .mask_memory = i460_mask_memory, .masks = i460_masks, .agp_enable = agp_generic_enable, .cache_flush = global_cache_flush,
drivers/char/agp/intel-agp.c +166 -16
270 lines not on this page · raw message
drivers/char/agp/nvidia-agp.c +1 -1
@@ -225,7 +225,7 @@ static int nvidia_insert_memory(struct agp_memory *mem, off_t pg_start, int type }for (i =0 , j = pg_start; i < mem->page_count; i++, j++) {writel (agp_bridge->driver->mask_memory (agp_bridge, - mem->pages[i], mask_type), +phys_to_gart (page_to_phys (mem->pages[i])), mask_type), agp_bridge->gatt_table+nvidia_private.pg_offset+j); }
drivers/char/agp/parisc-agp.c +2 -10
@@ -32,7 +32,7 @@#define AGP8X_MODE (1 << AGP8X_MODE_BIT)static unsigned long -parisc_agp_mask_memory (struct agp_bridge_data *bridge,unsigned long addr, +parisc_agp_mask_memory (struct agp_bridge_data *bridge,dma_addr_t addr,int type); static struct _parisc_agp_info {
@@ -189,20 +189,12 @@ parisc_agp_remove_memory(struct agp_memory *mem, off_t pg_start, int type) }static unsigned long -parisc_agp_mask_memory (struct agp_bridge_data *bridge,unsigned long addr, +parisc_agp_mask_memory (struct agp_bridge_data *bridge,dma_addr_t addr,int type) {return SBA_PDIR_VALID_BIT | addr; } -static unsigned long -parisc_agp_page_mask_memory (struct agp_bridge_data *bridge,struct page *page, -int type) -{ -unsigned long addr =phys_to_gart (page_to_phys (page)); -return SBA_PDIR_VALID_BIT | addr; -} -static void parisc_agp_enable (struct agp_bridge_data *bridge,u32 mode) {
drivers/char/agp/sgi-agp.c +4 -4
@@ -70,10 +70,9 @@ static void sgi_tioca_tlbflush(struct agp_memory *mem)* entry. */ static unsigned long -sgi_tioca_mask_memory (struct agp_bridge_data *bridge, -struct page *page,int type) +sgi_tioca_mask_memory (struct agp_bridge_data *bridge,dma_addr_t addr, +int type) { -unsigned long addr =phys_to_gart (page_to_phys (page));return tioca_physpage_to_gart (addr); }
@@ -190,7 +189,8 @@ static int sgi_tioca_insert_memory(struct agp_memory *mem, off_t pg_start,for (i =0 , j = pg_start; i < mem->page_count; i++, j++) { table[j] = - bridge->driver->mask_memory (bridge, mem->pages[i], + bridge->driver->mask_memory (bridge, +phys_to_gart (page_to_phys (mem->pages[i])), mem->type); }
drivers/char/agp/sworks-agp.c +3 -1
@@ -349,7 +349,9 @@ static int serverworks_insert_memory(struct agp_memory *mem,for (i =0 , j = pg_start; i < mem->page_count; i++, j++) { addr = (j *PAGE_SIZE ) + agp_bridge->gart_bus_addr; cur_gatt =SVRWRKS_GET_GATT (addr); -writel (agp_bridge->driver->mask_memory (agp_bridge, mem->pages[i], mem->type), cur_gatt+GET_GATT_OFF (addr)); +writel (agp_bridge->driver->mask_memory (agp_bridge, +phys_to_gart (page_to_phys (mem->pages[i])), mem->type), + cur_gatt+GET_GATT_OFF (addr)); }serverworks_tlbflush (mem);return 0 ;
include/linux/agp_backend.h +5 -1
@@ -79,9 +79,13 @@ struct agp_memory {u32 physical;bool is_bound;bool is_flushed; -bool vmalloc_flag; +bool vmalloc_flag; +bool sg_vmalloc_flag;/* list of agp_memory mapped to the aperture */ struct list_head mapped_list; +/* DMA-mapped addresses */ +struct scatterlist *sg_list; +int num_sg; };#define AGP_NORMAL_MEMORY 0
Signature · 2 lines
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Quoted 7 lines
Based on some patches from Zhenyu that I found at
http://people.freedesktop.org/~zhen/agp-mm-*, this set of patches stops the Intel graphics drivers from being utterly broken when the IOMMU is enabled...
Quoted 7 lines
I'm still seeing write faults to seemingly random addresses at startup when I first enable the IOMMU (and before the gfx driver actually starts). Those happen even when I don't use any graphics drivers at all, though.
And I'm slightly confused about the way we use the scratch page without
mask_memory().
Looks good, some comments below:
Quoted 35 lines
commit
d8450c7be46afd0b51d2dc171c9d2eb60366ce05
Author: Zhenyu Wang <[email protected]>
Date: Mon Jul 27 12:59:57 2009 +0100intel_agp: Use PCI DMA API correctly on chipsets new enough to have IOMMU
When graphics dma remapping engine is active, we must fill gart table with dma address from dmar engine, as now graphics device access to graphics memory must go through dma remapping table to get real physical address.
Add this support to all drivers which use
intel_i915_insert_entries()Signed-off-by Zhenyu Wang <[email protected]>Signed-off-by David Woodhouse <[email protected]>commit
ca8cdb1f983c508f586b90870703e85aab87659a
Author: Zhenyu Wang <[email protected]>
Date: Thu Jul 23 17:25:49 2009 +0100agp: Add generic support for graphics dma remapping
New driver hooks for support graphics memory dma remapping are introduced in this patch. It makes generic code can tell if current device needs dma remapping, then call driver provided interfaces for mapping and unmapping. Change has also been made to handle
scratch_pagein remapping case.Signed-off-by Zhenyu Wang <[email protected]>Signed-off-by David Woodhouse <[email protected]>commit
42a509b9cd513ddd536c38cc302c7457a3c811d7
Author: David Woodhouse <[email protected]>
Date: Mon Jul 27 10:27:29 2009 +0100
Quoted 8 lines
agp: Switch
mask_memory()method to take address argument again, not pageIn commit
07613ba2("agp: switch AGP to use page array instead of unsigned long array") we switched themask_memory()method to take a 'struct page *' instead of an address. This is painful, because in some cases it has to be an IOMMU-mapped virtual bus address (in fact, shouldn't it _always_ be adma_addr_treturned frompci_map_xxx(), and we just happen to get lucky most of the time?)
Yup pretty much we always got lucky, its not like AGP and IOMMU systems are a huge item, its really only Intel IGPs which use the AGP subsystem these days.
Quoted 9 lines
struct
agp_bridge_data*agp_generic_find_bridge(structpci_dev*pdev);drivers/char/agp/amd-k7-agp.c +0
-0
drivers/char/agp/amd-k7-agp.c static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type) · line 328
Quoted 5 lines
drivers/char/agp/amd-k7-agp.c +1
-1@@ -325,7 +325,9 @@ static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type) addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr; cur_gatt = GET_GATT(addr); writel(agp_generic_mask_memory(agp_bridge, - mem->pages[i], mem->type), cur_gatt+GET_GATT_OFF(addr)); + phys_to_gart(page_to_phys(mem->pages[i])),
don't suppose we want page_to_gart or is the double function nicer?
Quoted 5 lines
drivers/char/agp/backend.c +0
-0
drivers/char/agp/backend.c static int agp_backend_initialize(struct agp_bridge_data *bridge) · line 157
Quoted 10 lines
drivers/char/agp/backend.c +5
-2@@ -150,8 +150,17 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge) } bridge->scratch_page_real = phys_to_gart(page_to_phys(page)); - bridge->scratch_page = - bridge->driver->mask_memory(bridge, page, 0); + bridge->scratch_page = bridge->driver->mask_memory(bridge, + phys_to_gart(page_to_phys(page)), 0); + + if (bridge->driver->agp_map_page && + bridge->driver->agp_map_page(phys_to_virt(page_to_phys(page)),
and maybe page_to_virt.
Quoted 21 lines
drivers/char/agp/backend.c +20
-0+#ifdef USE_PCI_DMA_API +static int intel_agp_map_page (void *addr,dma_addr_t *ret) +{ + *ret = pci_map_single(intel_private.pcidev, addr, + PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); + if (pci_dma_mapping_error(intel_private.pcidev, *ret)) + return -EINVAL; + return 0; +} + +static void intel_agp_unmap_page (void *addr,dma_addr_t dma) +{ + pci_unmap_single(intel_private.pcidev, dma, + PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); +} + +static int intel_agp_map_memory (struct agp_memory *mem) +{ + struct scatterlist *sg; + int i;
drivers/char/agp/backend.c static int agp_backend_initialize(struct agp_bridge_data *bridge)
Quoted 10 lines
drivers/char/agp/backend.c +10
-0+ + DBG("try mapping %lu pages\n", (unsigned long)mem->page_count); + + if ((mem->page_count * sizeof(*mem->sg_list)) < 2*PAGE_SIZE) + mem->sg_list = kcalloc(mem->page_count, sizeof(*mem->sg_list), + GFP_KERNEL); + + if (mem->sg_list == NULL) { + mem->sg_list = vmalloc(mem->page_count * sizeof(*mem->sg_list)); + mem->sg_vmalloc_flag = 1;
Can we drop vmalloc_flag and use is_vmalloc_addr on the free function?
(aside: yet another place that wants a kmalloc/vmalloc allocator. I suspect vmalloc here to be slow but I suppose there isn't much we can do.)
Quoted 11 lines
+ }
drivers/char/agp/backend.c +10
-0+ + if (!mem->sg_list) { + mem->sg_vmalloc_flag = 0; + return -ENOMEM; + } + sg_init_table(mem->sg_list, mem->page_count); + + sg = mem->sg_list; + for (i = 0 ; i < mem->page_count; i++, sg = sg_next(sg)) + sg_set_page(sg, mem->pages[i], PAGE_SIZE, 0);
drivers/char/agp/backend.c
Quoted 10 lines
drivers/char/agp/backend.c +10
-0+ + mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list, + mem->page_count, PCI_DMA_BIDIRECTIONAL); + if (!mem->num_sg) { + if (mem->sg_vmalloc_flag) + vfree(mem->sg_list); + else + kfree(mem->sg_list); + mem->sg_list = NULL; + mem->sg_vmalloc_flag = 0;
some common cleanup function?
Quoted 7 lines
+ return
-ENOMEM;
+ }
+ return 0;drivers/char/agp/backend.c +4
-0+} + +static void intel_agp_unmap_memory (struct agp_memory *mem) +{
drivers/char/agp/backend.c
Quoted 10 lines
drivers/char/agp/backend.c +10
-0+ DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count); + + pci_unmap_sg(intel_private.pcidev, mem->sg_list, + mem->page_count, PCI_DMA_BIDIRECTIONAL); + if (mem->sg_vmalloc_flag) + vfree(mem->sg_list); + else + kfree(mem->sg_list); + mem->sg_vmalloc_flag = 0; + mem->sg_list = NULL;
since we reproduce it here.
Dave.
Quoted 3 lines · Dave Airlie
Yup pretty much we always got lucky, its not like AGP and IOMMU systems are a huge item, its really only Intel IGPs which use the AGP subsystem these days.
Ah, really? One thing which was bothering me was what happens when I use non-onboard graphics in one of these beasts -- are individual gfx drivers going to need to be fixed too?
Quoted 6 lines
drivers/char/agp/amd-k7-agp.c static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type) · line 328
Quoted 4 lines
cur_gatt = GET_GATT(addr); writel(agp_generic_mask_memory(agp_bridge,drivers/char/agp/amd-k7-agp.c +1
-1- mem->pages[i], mem->type), cur_gatt+GET_GATT_OFF(addr)); + phys_to_gart(page_to_phys(mem->pages[i])),don't suppose we want
page_to_gartor is the double function nicer?
I pondered that briefly. But then observed that phys_to_gart() and gart_to_phys() _always_ describe an identity mapping, so perhaps they could just be ditched completely?
Quoted 13 lines
drivers/char/agp/backend.c static int agp_backend_initialize(struct agp_bridge_data *bridge) · line 157
Quoted 11 lines
drivers/char/agp/backend.c +5
-2@@ -150,8 +150,17 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge) } bridge->scratch_page_real =phys_to_gart (page_to_phys (page)); - bridge->scratch_page = - bridge->driver->mask_memory (bridge, page,0 ); + bridge->scratch_page = bridge->driver->mask_memory (bridge, +phys_to_gart (page_to_phys (page)),0 ); + +if (bridge->driver->agp_map_page && + bridge->driver->agp_map_page(phys_to_virt(page_to_phys(page)),and maybe
page_to_virt.
That's called page_address(), and it (as well as the above construct) is broken with highmem pages. It's actually OK here, since this page is allocated with GFP_DMA32 -- but for cleanliness' sake I should probably switch agp_map_page() to take a 'struct page *' rather than a virtual address.
Quoted 9 lines
drivers/char/agp/intel-agp.c static struct _intel_private { · line 212
Quoted 7 lines
drivers/char/agp/intel-agp.c +7
-0+if ((mem->page_count *sizeof (*mem->sg_list)) <2 *PAGE_SIZE ) + mem->sg_list =kcalloc (mem->page_count,sizeof (*mem->sg_list), +GFP_KERNEL ); + +if (mem->sg_list ==NULL ) { + mem->sg_list =vmalloc (mem->page_count *sizeof (*mem->sg_list)); + mem->sg_vmalloc_flag =1 ;Can we drop
vmalloc_flagand useis_vmalloc_addron the free function?
I suppose so -- we could eliminate the other vmalloc_flag field in 'struct agp_memory' that way too? Doesn't shrink the structure any -- we'd just end up with padding where the flags were.
Quoted 2 lines
(aside: yet another place that wants a kmalloc/vmalloc allocator. I suspect vmalloc here to be slow but I suppose there isn't much we can do.)
http://lwn.net/Articles/342915/ ?
In fact, can't scatterlists do something like that already?
Quoted 11 lines
drivers/char/agp/intel-agp.c static struct _intel_private { · line 233
Quoted 9 lines
drivers/char/agp/intel-agp.c +9
-0+ mem->num_sg =pci_map_sg (intel_private.pcidev, mem->sg_list, + mem->page_count,PCI_DMA_BIDIRECTIONAL ); +if (!mem->num_sg) { +if (mem->sg_vmalloc_flag) +vfree (mem->sg_list); +else +kfree (mem->sg_list); + mem->sg_list =NULL ; + mem->sg_vmalloc_flag =0 ;some common cleanup function?
...
Quoted 1 line
since we reproduce it here.
Yeah, probably a good idea.
Signature · 2 lines
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Quoted 8 lines · David Woodhouse
Quoted 3 lines · Dave Airlie
Yup pretty much we always got lucky, its not like AGP and IOMMU systems are a huge item, its really only Intel IGPs which use the AGP subsystem these days.
Ah, really? One thing which was bothering me was what happens when I use non-onboard graphics in one of these beasts -- are individual gfx drivers going to need to be fixed too?
Yes, more than likely. I think radeon and radeon kms drivers dtrt, at least they
call pci_map_single for the pages they use in their on-chip translation units, and the cards have been used in sparc64 and ppc64 with iommu stuff happening.
nouveau might work when it gets upstream but it may not.
Other PCI and/or PCIE1x cards still around would be MGA and SiS based, and I suspect
they would need work, but they might just work.
nvidia/fglrx binary drivers, who knows.
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
cur_gatt =
GET_GATT(addr); writel(agp_generic_mask_memory(agp_bridge,
- mem->pages[i],mem->type), cur_gatt+GET_GATT_OFF(addr));
+ phys_to_gart(page_to_phys(mem->pages[i])),don't suppose we want
page_to_gartor is the double function nicer?I pondered that briefly. But then observed that
phys_to_gart()andgart_to_phys()_always_ describe an identity mapping, so perhaps they could just be ditched completely?
Yeah that could work too, no idea why they were introduced, well before my time.
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -150,8 +150,17 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge) }Quoted 10 lines
drivers/char/agp/backend.c static int agp_backend_initialize(struct agp_bridge_data *bridge) · line 157
Quoted 8 lines
drivers/char/agp/backend.c +5
-2bridge->scratch_page_real = phys_to_gart(page_to_phys(page)); - bridge->scratch_page = - bridge->driver->mask_memory(bridge, page, 0); + bridge->scratch_page = bridge->driver->mask_memory(bridge, + phys_to_gart(page_to_phys(page)), 0); + + if (bridge->driver->agp_map_page && + bridge->driver->agp_map_page(phys_to_virt(page_to_phys(page)),and maybe
page_to_virt.
Quoted 5 lines
That's called
page_address(), and it (as well as the above construct) is broken with highmem pages. It's actually OK here, since this page is allocated withGFP_DMA32-- but for cleanliness' sake I should probably switchagp_map_page()to take a 'struct page *' rather than a virtual address.
Yes that might be best, having a function called map_page taking an address seems wrong.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/char/agp/intel-agp.c +7
-0+ if ((mem->page_count * sizeof(*mem->sg_list)) < 2*PAGE_SIZE) + mem->sg_list = kcalloc(mem->page_count, sizeof(*mem->sg_list), + GFP_KERNEL); + + if (mem->sg_list == NULL) { + mem->sg_list = vmalloc(mem->page_count * sizeof(*mem->sg_list)); + mem->sg_vmalloc_flag = 1;
Quoted 5 lines
Quoted 1 line
Can we drop
vmalloc_flagand useis_vmalloc_addron the free function?I suppose so -- we could eliminate the other
vmalloc_flagfield in 'structagp_memory' that way too? Doesn't shrink the structure any -- we'd just end up with padding where the flags were.
Yeah we should drop both of them maybe I can do that as a cleanup after this patch.
Quoted 7 lines
Quoted 3 lines
(aside: yet another place that wants a kmalloc/vmalloc allocator. I suspect vmalloc here to be slow but I suppose there isn't much we can do.)
http://lwn.net/Articles/342915/ ?
In fact, can't scatterlists do something like that already?
no idea, I think I've seen vmalloc used in other places before.
Quoted 12 lines
Quoted 11 lines
drivers/char/agp/intel-agp.c static struct _intel_private { · line 233
Quoted 10 lines
drivers/char/agp/intel-agp.c +9
-0+ mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list, + mem->page_count, PCI_DMA_BIDIRECTIONAL); + if (!mem->num_sg) { + if (mem->sg_vmalloc_flag) + vfree(mem->sg_list); + else + kfree(mem->sg_list); + mem->sg_list = NULL; + mem->sg_vmalloc_flag = 0;Quoted 1 line
some common cleanup function?
Quoted 10 lines
...
Quoted 1 line
since we reproduce it here.
Yeah, probably a good idea.
--
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Dave.
Quoted 6 lines · Dave Airlie
Quoted 5 lines · David Woodhouse
Quoted 3 lines · Dave Airlie
Yup pretty much we always got lucky, its not like AGP and IOMMU systems are a huge item, its really only Intel IGPs which use the AGP subsystem these days.
Quoted 8 lines
Quoted 3 lines
Ah, really? One thing which was bothering me was what happens when I use non-onboard graphics in one of these beasts -- are individual gfx drivers going to need to be fixed too?
Yes, more than likely. I think radeon and radeon kms drivers dtrt, at least they call
pci_map_singlefor the pages they use in their on-chip translation units, and the cards have been used in sparc64 and ppc64 with iommu stuff happening.
It's going to perform a lot better if we use pci_map_sg() to map as many pages as possible at once.
Quoted 6 lines
nouveau might work when it gets upstream but it may not.
Other PCI and/or PCIE1x cards still around would be MGA and SiS based, and I suspect they would need work, but they might just work.
nvidia/fglrx binary drivers, who knows.
... or cares.
Quoted 29 lines
Quoted 11 lines
Quoted 7 lines
drivers/char/agp/amd-k7-agp.c static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type) · line 328
Quoted 5 lines
cur_gatt = GET_GATT(addr); writel(agp_generic_mask_memory(agp_bridge,drivers/char/agp/amd-k7-agp.c +1
-1- mem->pages[i], mem->type), cur_gatt+GET_GATT_OFF(addr)); + phys_to_gart(page_to_phys(mem->pages[i])),don't suppose we want
page_to_gartor is the double function nicer?I pondered that briefly. But then observed that
phys_to_gart()andgart_to_phys()_always_ describe an identity mapping, so perhaps they could just be ditched completely?Yeah that could work too, no idea why they were introduced, well before my time.
Quoted 15 lines
Quoted 14 lines
drivers/char/agp/backend.c static int agp_backend_initialize(struct agp_bridge_data *bridge) · line 157
Quoted 12 lines
drivers/char/agp/backend.c +5
-2@@ -150,8 +150,17 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge) } bridge->scratch_page_real =phys_to_gart (page_to_phys (page)); - bridge->scratch_page = - bridge->driver->mask_memory (bridge, page,0 ); + bridge->scratch_page = bridge->driver->mask_memory (bridge, +phys_to_gart (page_to_phys (page)),0 ); + +if (bridge->driver->agp_map_page && + bridge->driver->agp_map_page(phys_to_virt(page_to_phys(page)),and maybe
page_to_virt.
Quoted 8 lines
Quoted 5 lines
That's called
page_address(), and it (as well as the above construct) is broken with highmem pages. It's actually OK here, since this page is allocated withGFP_DMA32-- but for cleanliness' sake I should probably switchagp_map_page()to take a 'struct page *' rather than a virtual address.Yes that might be best, having a function called
map_pagetaking an address seems wrong.
http://git.infradead.org/users/dwmw2/iommu-agp.git?a=commitdiff;h=71fc2ae5
Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/char/agp/intel-agp.c +7
-0+if ((mem->page_count *sizeof (*mem->sg_list)) <2 *PAGE_SIZE ) + mem->sg_list =kcalloc (mem->page_count,sizeof (*mem->sg_list), +GFP_KERNEL ); + +if (mem->sg_list ==NULL ) { + mem->sg_list =vmalloc (mem->page_count *sizeof (*mem->sg_list)); + mem->sg_vmalloc_flag =1 ;Quoted 5 lines
Quoted 1 line
Can we drop
vmalloc_flagand useis_vmalloc_addron the free function?I suppose so -- we could eliminate the other
vmalloc_flagfield in 'structagp_memory' that way too? Doesn't shrink the structure any -- we'd just end up with padding where the flags were.Yeah we should drop both of them maybe I can do that as a cleanup after this patch.
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
(aside: yet another place that wants a kmalloc/vmalloc allocator. I suspect vmalloc here to be slow but I suppose there isn't much we can do.)
http://lwn.net/Articles/342915/ ?
In fact, can't scatterlists do something like that already?
no idea, I think I've seen vmalloc used in other places before.
This fixes it for scatterlists:
http://git.infradead.org/users/dwmw2/iommu-agp.git?a=commitdiff;h=89187864
Signature · 2 lines
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Quoted 7 lines · Dave Airlie
Quoted 5 lines
Quoted 1 line
don't suppose we want
page_to_gartor is the double function nicer?I pondered that briefly. But then observed that
phys_to_gart()andgart_to_phys()_always_ describe an identity mapping, so perhaps they could just be ditched completely?Yeah that could work too, no idea why they were introduced, well before my time.
http://git.infradead.org/users/dwmw2/iommu-agp.git?a=commitdiff;h=8bf2f3a9
It was introduced by Keir in 2005 for Xen (commit 07eee78e). I suspect it can die though, and that the right answer there is also to use the DMA API correctly.
Signature · 2 lines
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Quoted 7 lines · David Woodhouse
Quoted 6 lines · Dave Airlie
Quoted 5 lines
Quoted 1 line
don't suppose we want
page_to_gartor is the double function nicer?I pondered that briefly. But then observed that
phys_to_gart()andgart_to_phys()_always_ describe an identity mapping, so perhaps they could just be ditched completely?
Quoted 8 lines
Quoted 1 line
Yeah that could work too, no idea why they were introduced, well before my time.
http://git.infradead.org/users/dwmw2/iommu-agp.git?a=commitdiff;h=8bf2f3a9
It was introduced by Keir in 2005 for Xen (commit
07eee78e). I suspect it can die though, and that the right answer there is also to use the DMA API correctly.
The commits on that tree looks fine to me. Thanks David! I've also done some testing on G45 here, I've also seen the write faults before graphics device init or after dma unmap pages. I tried to look it up in my memo, which turns out one message like this should be a BIOS bug that doesn't initialize the GTT table correctly. But I'm quite sure about it now, better to ask our chipset people to verify if this problem has been fixed or not.
Signature · 3 lines
Open Source Technology Center, Intel ltd. $gpg --keyserver wwwkeys.pgp.net --recv-keys 4D781827
Quoted 8 lines · Zhenyu Wang
The commits on that tree looks fine to me. Thanks David! I've also done some testing on G45 here, I've also seen the write faults before graphics device init or after dma unmap pages. I tried to look it up in my memo, which turns out one message like this should be a BIOS bug that doesn't initialize the GTT table correctly. But I'm quite sure about it now, better to ask our chipset people to verify if this problem has been fixed or not.
Thanks. I've rebased the tree so that it isn't based on my other iommu changes, which are largely irrelevant. Since the drm-2.6.git tree as advertised in MAINTAINERS seems to be a pristine 2.6.29-rc2, I've done it based on 2.6.31-rc5 instead.
Dave, please pull from
git://git.infradead.org/~dwmw2/iommu-agp.git
David Woodhouse (6):
agp: Switch mask_memory() method to take address argument again, not page
agp: tidy up handling of scratch pages w.r.t. DMA API
agp: Switch agp_{un,}map_page() to take struct page * argument
intel-agp: Move repeated sglist free into separate function
intel-agp: fix sglist allocation to avoid vmalloc()
agp: kill phys_to_gart() and gart_to_phys()
Zhenyu Wang (2):
agp: Add generic support for graphics dma remapping
intel_agp: Use PCI DMA API correctly on chipsets new enough to have IOMMU
arch/alpha/include/asm/agp.h | 4 - arch/ia64/include/asm/agp.h | 4 - arch/parisc/include/asm/agp.h | 4 - arch/powerpc/include/asm/agp.h | 4 - arch/sparc/include/asm/agp.h | 4 - arch/x86/include/asm/agp.h | 4 - drivers/char/agp/agp.h | 15 ++-- drivers/char/agp/ali-agp.c | 4 +- drivers/char/agp/amd-k7-agp.c | 10 ++- drivers/char/agp/amd64-agp.c | 7 +- drivers/char/agp/ati-agp.c | 7 +- drivers/char/agp/backend.c | 32 ++++++- drivers/char/agp/efficeon-agp.c | 4 +- drivers/char/agp/generic.c | 20 ++++- drivers/char/agp/hp-agp.c | 8 +- drivers/char/agp/i460-agp.c | 17 +--- drivers/char/agp/intel-agp.c | 167 +++++++++++++++++++++++++++++++++++---- drivers/char/agp/nvidia-agp.c | 2 +- drivers/char/agp/parisc-agp.c | 12 +--- drivers/char/agp/sgi-agp.c | 8 +- drivers/char/agp/sworks-agp.c | 10 ++- drivers/char/agp/uninorth-agp.c | 2 +- include/linux/agp_backend.h | 5 +- 23 files changed, 247 insertions(+), 107 deletions(-)
Signature · 2 lines
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Quoted 8 lines · David Woodhouse
Quoted 6 lines · Dave Airlie
Quoted 6 lines
Quoted 2 lines
don't suppose we want
page_to_gartor is the double function nicer?I pondered that briefly. But then observed that
phys_to_gart()andgart_to_phys()_always_ describe an identity mapping, so perhaps they could just be ditched completely?
Quoted 9 lines
Quoted 2 lines
Yeah that could work too, no idea why they were introduced, well before my time.
http://git.infradead.org/users/dwmw2/iommu-agp.git?a=commitdiff;h=8bf2f3a9
It was introduced by Keir in 2005 for Xen (commit
07eee78e). I suspect it can die though, and that the right answer there is also to use the DMA API correctly.
And just when I have patches to use them for their original purpose...
Looking back over the thread, are you saying that most users are already using the DMA API correctly for AGP accesses? If that's true then we should be just fine.
J
Quoted 5 lines · Jeremy Fitzhardinge
And just when I have patches to use them for their original purpose...
Looking back over the thread, are you saying that most users are already using the DMA API correctly for AGP accesses? If that's true then we should be just fine.
No, not 'most users'. But perhaps we should.
All we've done so far is make intel-agp use the DMA API correctly. And that's conditional on CONFIG_DMAR. But we could make it unconditional, and make the other drivers do it too. The code is all fairly generic.
Without an IOMMU, the overhead would be fairly minimal.
Signature · 2 lines
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Quoted 7 lines · David Woodhouse
Quoted 5 lines · Jeremy Fitzhardinge
And just when I have patches to use them for their original purpose...
Looking back over the thread, are you saying that most users are already using the DMA API correctly for AGP accesses? If that's true then we should be just fine.
Quoted 8 lines
No, not 'most users'. But perhaps we should.
All we've done so far is make intel-agp use the DMA API correctly. And that's conditional on
CONFIG_DMAR. But we could make it unconditional, and make the other drivers do it too. The code is all fairly generic.Without an IOMMU, the overhead would be fairly minimal.
I'm not sure how prevalent Xen is on AGP systems, I'm going to guess not enough that anyone cares. Intel integrated chips are the only AGP codebase user that are made any more, and they don't really use AGP its just a legacy of the previous designs.
Dave.
Quoted 14 lines · Dave Airlie
Quoted 13 lines · David Woodhouse
Quoted 5 lines · Jeremy Fitzhardinge
And just when I have patches to use them for their original purpose...
Looking back over the thread, are you saying that most users are already using the DMA API correctly for AGP accesses? If that's true then we should be just fine.
No, not 'most users'. But perhaps we should.
All we've done so far is make intel-agp use the DMA API correctly. And that's conditional on
CONFIG_DMAR. But we could make it unconditional, and make the other drivers do it too. The code is all fairly generic.
Quoted 7 lines
Quoted 2 lines
Without an IOMMU, the overhead would be fairly minimal.
I'm not sure how prevalent Xen is on AGP systems, I'm going to guess not enough that anyone cares. Intel integrated chips are the only AGP codebase user that are made any more, and they don't really use AGP its just a legacy of the previous designs.
On the other hand, extending the existing code so that it uses the DMA API correctly _unconditionally_ rather than only for the Intel driver would be fairly simple and should be harmless...
Signature · 1 line
dwmw2
Quoted 10 lines · Dave Airlie
Quoted 7 lines
All we've done so far is make intel-agp use the DMA API correctly. And that's conditional on
CONFIG_DMAR. But we could make it unconditional, and make the other drivers do it too. The code is all fairly generic.Without an IOMMU, the overhead would be fairly minimal.
I'm not sure how prevalent Xen is on AGP systems, I'm going to guess not enough that anyone cares.
What do you mean by AGP here? Do you mean "machine with physical AGP port", or "machine with graphics using the AGP aperature"? There's an increasing interest in putting Xen on desktop machines, so the former is debatable, but the latter (I think?) definitely isn't true.
Quoted 5 lines
Intel integrated chips are the only AGP
codebase user that are made any more, and they don't really use AGP its just a legacy of the previous designs.
Plenty of people are using older machines for testing, etc, so I wouldn't want to exclude them too lightly. I've got bug reports from people using older Radeons, etc.
J
Quoted 9 lines · Jeremy Fitzhardinge
Quoted 8 lines · Dave Airlie
Quoted 7 lines
All we've done so far is make intel-agp use the DMA API correctly. And that's conditional on
CONFIG_DMAR. But we could make it unconditional, and make the other drivers do it too. The code is all fairly generic.Without an IOMMU, the overhead would be fairly minimal.
Quoted 8 lines
Quoted 2 lines
I'm not sure how prevalent Xen is on AGP systems, I'm going to guess not enough that anyone cares.
What do you mean by AGP here? Do you mean "machine with physical AGP port", or "machine with graphics using the AGP aperature"? There's an increasing interest in putting Xen on desktop machines, so the former is debatable, but the latter (I think?) definitely isn't true.
I split my mail along those lines already, an AGP system is one with a physical AGP port, I think you'll probably find a few people still running some Athlons with VIA chipset and radeon 9200s I think,
The graphics using AGP-like apertures are only Intel and we've fixed those.
Quoted 9 lines
Quoted 5 lines
Intel integrated chips are the only AGP
codebase user that are made any more, and they don't really use AGP its just a legacy of the previous designs.Plenty of people are using older machines for testing, etc, so I wouldn't want to exclude them too lightly. I've got bug reports from people using older Radeons, etc.
My main problem with doing this is getting testing coverage on older systems, if we change all the AGP drivers, the time to find we busted something is probably not until the next major distro release with this code in it. So people can run stuff on 5 year old hardware in 6 months? I'm not sure the gains justify the costs, but if you guys really want to do it I'll take the patch, just be prepared to have it ripped out the minute we get a regression we can't figure out because we haven't got the hw anymore.
Dave.
Quoted 8 lines · Dave Airlie
My main problem with doing this is getting testing coverage on older systems, if we change all the AGP drivers, the time to find we busted something is probably not until the next major distro release with this code in it. So people can run stuff on 5 year old hardware in 6 months? I'm not sure the gains justify the costs, but if you guys really want to do it I'll take the patch, just be prepared to have it ripped out the minute we get a regression we can't figure out because we haven't got the hw anymore.
Can you point to some changesets/patches to act as an example/template for the changes needed?
If we come up with someone reporting crashes when using old hardware, we can then make change then get them to test both native and Xen.
J
Quoted 10 lines · Jeremy Fitzhardinge
Quoted 8 lines · Dave Airlie
My main problem with doing this is getting testing coverage on older systems, if we change all the AGP drivers, the time to find we busted something is probably not until the next major distro release with this code in it. So people can run stuff on 5 year old hardware in 6 months? I'm not sure the gains justify the costs, but if you guys really want to do it I'll take the patch, just be prepared to have it ripped out the minute we get a regression we can't figure out because we haven't got the hw anymore.
Quoted 2 lines
Can you point to some changesets/patches to act as an example/template for the changes needed?
http://git.infradead.org/users/dwmw2/iommu-agp.git/shortlog/ba3139f2577eee24479db73b8dfc7d78eaf4c486
Some of the code added to intel-agp should probably be generic.
Signature · 2 lines
David Woodhouse Open Source Technology Centre [email protected] Intel Corporation