re-write scatter gather memory allocation yet again...

This time, abandon the use of busdma and start interacting with the VM
system directly.  Make use of the new kmem_alloc_attr() which allows us
to easily allocate non-contiguous pages to back the GART table.  This
should help a lot when starting or restarting X after the system has
been running for a while and memory has become fragmented.

MFC after:	2 weeks
This commit is contained in:
rnoland 2010-04-22 18:44:23 +00:00
parent b733ebaa1c
commit 50a4503e46
6 changed files with 61 additions and 112 deletions

View File

@ -49,6 +49,7 @@ struct drm_file;
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/sglist.h>
#include <sys/stat.h>
#if __FreeBSD_version >= 700000
#include <sys/priv.h>
@ -68,10 +69,13 @@ struct drm_file;
#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_extern.h>
#include <vm/vm_kern.h>
#include <vm/vm_map.h>
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <vm/vm_pager.h>
#include <vm/vm_param.h>
#include <vm/vm_phys.h>
#include <machine/param.h>
#include <machine/pmap.h>
#include <machine/bus.h>
@ -474,11 +478,10 @@ typedef struct drm_agp_head {
} drm_agp_head_t;
typedef struct drm_sg_mem {
unsigned long handle;
void *virtual;
int pages;
dma_addr_t *busaddr;
struct drm_dma_handle *dmah; /* Handle to PCI memory */
vm_offset_t vaddr;
vm_paddr_t *busaddr;
vm_pindex_t pages;
vm_object_t obj;
} drm_sg_mem_t;
#define DRM_MAP_HANDLE_BITS (sizeof(void *) == 4 ? 4 : 24)

View File

@ -226,8 +226,8 @@ int drm_addmap(struct drm_device * dev, unsigned long offset,
DRM_LOCK();
return EINVAL;
}
map->virtual = (void *)(dev->sg->handle + offset);
map->offset = dev->sg->handle + offset;
map->virtual = (void *)(dev->sg->vaddr + offset);
map->offset = dev->sg->vaddr + offset;
break;
case _DRM_CONSISTENT:
/* Unfortunately, we don't get any alignment specification from
@ -744,7 +744,7 @@ static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *reques
buf->offset = (dma->byte_count + offset);
buf->bus_address = agp_offset + offset;
buf->address = (void *)(agp_offset + offset + dev->sg->handle);
buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
buf->next = NULL;
buf->pending = 0;
buf->file_priv = NULL;

View File

@ -1,5 +1,5 @@
/*-
* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
* Copyright (c) 2009 Robert C. Noland III <rnoland@FreeBSD.org>
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
@ -20,11 +20,6 @@
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Authors:
* Gareth Hughes <gareth@valinux.com>
* Eric Anholt <anholt@FreeBSD.org>
*
*/
#include <sys/cdefs.h>
@ -32,99 +27,58 @@ __FBSDID("$FreeBSD$");
/** @file drm_scatter.c
* Allocation of memory for scatter-gather mappings by the graphics chip.
*
* The memory allocated here is then made into an aperture in the card
* by drm_ati_pcigart_init().
* by mapping the pages into the GART.
*/
#include "dev/drm/drmP.h"
static void drm_sg_alloc_cb(void *arg, bus_dma_segment_t *segs,
int nsegs, int error);
int
drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather *request)
{
struct drm_sg_mem *entry;
struct drm_dma_handle *dmah;
int ret;
vm_size_t size;
vm_pindex_t pindex;
if (dev->sg)
return EINVAL;
entry = malloc(sizeof(*entry), DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
entry->pages = round_page(request->size) / PAGE_SIZE;
DRM_DEBUG("sg size=%ld pages=%d\n", request->size, entry->pages);
DRM_DEBUG("request size=%ld\n", request->size);
entry = malloc(sizeof(*entry), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
size = round_page(request->size);
entry->pages = OFF_TO_IDX(size);
entry->busaddr = malloc(entry->pages * sizeof(*entry->busaddr),
DRM_MEM_PAGES, M_WAITOK | M_ZERO);
dmah = malloc(sizeof(struct drm_dma_handle), DRM_MEM_DMA,
M_WAITOK | M_ZERO);
entry->dmah = dmah;
DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
ret = bus_dma_tag_create(NULL, PAGE_SIZE, 0, /* tag, align, boundary */
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
NULL, NULL, /* filtfunc, filtfuncargs */
request->size, entry->pages, /* maxsize, nsegs */
PAGE_SIZE, 0, /* maxsegsize, flags */
NULL, NULL, /* lockfunc, lockfuncargs */
&dmah->tag);
if (ret != 0) {
entry->vaddr = kmem_alloc_attr(kernel_map, size, M_WAITOK | M_ZERO,
0, BUS_SPACE_MAXADDR_32BIT, VM_MEMATTR_WRITE_COMBINING);
if (entry->vaddr == 0) {
drm_sg_cleanup(entry);
return ENOMEM;
return (ENOMEM);
}
ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
if (ret != 0) {
drm_sg_cleanup(entry);
return ENOMEM;
}
entry->handle = (unsigned long)dmah->vaddr;
entry->virtual = dmah->vaddr;
ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr,
request->size, drm_sg_alloc_cb, entry, BUS_DMA_NOWAIT);
if (ret != 0) {
drm_sg_cleanup(entry);
return ENOMEM;
for(pindex = 0; pindex < entry->pages; pindex++) {
entry->busaddr[pindex] =
vtophys(entry->vaddr + IDX_TO_OFF(pindex));
}
DRM_LOCK();
if (dev->sg) {
DRM_UNLOCK();
drm_sg_cleanup(entry);
return EINVAL;
return (EINVAL);
}
dev->sg = entry;
DRM_UNLOCK();
pmap_change_attr((vm_offset_t)dmah->vaddr, request->size,
PAT_WRITE_COMBINING);
request->handle = entry->vaddr;
request->handle = entry->handle;
DRM_DEBUG("allocated %ju pages @ 0x%08zx, contents=%08lx\n",
entry->pages, entry->vaddr, *(unsigned long *)entry->vaddr);
DRM_DEBUG("handle=%08lx, kva=%p, contents=%08lx\n", entry->handle,
entry->virtual, *(unsigned long *)entry->virtual);
return 0;
}
static void
drm_sg_alloc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
{
struct drm_sg_mem *entry = arg;
int i;
if (error != 0)
return;
for(i = 0 ; i < nsegs ; i++) {
entry->busaddr[i] = segs[i].ds_addr;
DRM_DEBUG("segment %d @ 0x%016lx\n", i,
(unsigned long)segs[i].ds_addr);
}
return (0);
}
int
@ -135,23 +89,22 @@ drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
DRM_DEBUG("\n");
return drm_sg_alloc(dev, request);
return (drm_sg_alloc(dev, request));
}
void
drm_sg_cleanup(struct drm_sg_mem *entry)
{
struct drm_dma_handle *dmah = entry->dmah;
if (entry == NULL)
return;
if (dmah->map != NULL)
bus_dmamap_unload(dmah->tag, dmah->map);
if (dmah->vaddr != NULL)
bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
if (dmah->tag != NULL)
bus_dma_tag_destroy(dmah->tag);
free(dmah, DRM_MEM_DMA);
free(entry->busaddr, DRM_MEM_PAGES);
free(entry, DRM_MEM_SGLISTS);
if (entry->vaddr != 0)
kmem_free(kernel_map, entry->vaddr, IDX_TO_OFF(entry->pages));
free(entry->busaddr, DRM_MEM_SGLISTS);
free(entry, DRM_MEM_DRIVER);
return;
}
int
@ -165,12 +118,12 @@ drm_sg_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
dev->sg = NULL;
DRM_UNLOCK();
if (!entry || entry->handle != request->handle)
return EINVAL;
if (!entry || entry->vaddr != request->handle)
return (EINVAL);
DRM_DEBUG("sg free virtual = 0x%lx\n", entry->handle);
DRM_DEBUG("free 0x%zx\n", entry->vaddr);
drm_sg_cleanup(entry);
return 0;
return (0);
}

View File

@ -327,8 +327,7 @@ static void r128_cce_init_ring_buffer(struct drm_device * dev,
ring_start = dev_priv->cce_ring->offset - dev->agp->base;
else
#endif
ring_start = dev_priv->cce_ring->offset -
(unsigned long)dev->sg->virtual;
ring_start = dev_priv->cce_ring->offset - dev->sg->vaddr;
R128_WRITE(R128_PM4_BUFFER_OFFSET, ring_start | R128_AGP_OFFSET);
@ -541,7 +540,7 @@ static int r128_do_init_cce(struct drm_device * dev, drm_r128_init_t * init)
dev_priv->cce_buffers_offset = dev->agp->base;
else
#endif
dev_priv->cce_buffers_offset = (unsigned long)dev->sg->virtual;
dev_priv->cce_buffers_offset = dev->sg->vaddr;
dev_priv->ring.start = (u32 *) dev_priv->cce_ring->virtual;
dev_priv->ring.end = ((u32 *) dev_priv->cce_ring->virtual

View File

@ -1670,9 +1670,8 @@ static void r600_cp_init_ring_buffer(struct drm_device *dev,
} else
#endif
{
rptr_addr = dev_priv->ring_rptr->offset
- ((unsigned long) dev->sg->virtual)
+ dev_priv->gart_vm_start;
rptr_addr = dev_priv->ring_rptr->offset - dev->sg->vaddr +
dev_priv->gart_vm_start;
}
RADEON_WRITE(R600_CP_RB_RPTR_ADDR,
rptr_addr & 0xffffffff);
@ -1706,9 +1705,8 @@ static void r600_cp_init_ring_buffer(struct drm_device *dev,
+ dev_priv->gart_vm_start);
} else
#endif
ring_start = (dev_priv->cp_ring->offset
- (unsigned long)dev->sg->virtual
+ dev_priv->gart_vm_start);
ring_start = dev_priv->cp_ring->offset - dev->sg->vaddr +
dev_priv->gart_vm_start;
RADEON_WRITE(R600_CP_RB_BASE, ring_start >> 8);
@ -2012,9 +2010,8 @@ int r600_do_init_cp(struct drm_device *dev, drm_radeon_init_t *init,
+ dev_priv->gart_vm_start);
else
#endif
dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
- (unsigned long)dev->sg->virtual
+ dev_priv->gart_vm_start);
dev_priv->gart_buffers_offset = dev->agp_buffer_map->offset -
dev->sg->vaddr + dev_priv->gart_vm_start;
DRM_DEBUG("fb 0x%08x size %d\n",
(unsigned int) dev_priv->fb_location,

View File

@ -720,9 +720,8 @@ static void radeon_cp_init_ring_buffer(struct drm_device * dev,
+ dev_priv->gart_vm_start);
} else
#endif
ring_start = (dev_priv->cp_ring->offset
- (unsigned long)dev->sg->virtual
+ dev_priv->gart_vm_start);
ring_start = (dev_priv->cp_ring->offset - dev->sg->vaddr +
dev_priv->gart_vm_start);
RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
@ -744,9 +743,8 @@ static void radeon_cp_init_ring_buffer(struct drm_device * dev,
#endif
{
RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
dev_priv->ring_rptr->offset
- ((unsigned long) dev->sg->virtual)
+ dev_priv->gart_vm_start);
dev_priv->ring_rptr->offset - dev->sg->vaddr +
dev_priv->gart_vm_start);
}
/* Set ring buffer size */
@ -1377,9 +1375,8 @@ static int radeon_do_init_cp(struct drm_device *dev, drm_radeon_init_t *init,
+ dev_priv->gart_vm_start);
else
#endif
dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
- (unsigned long)dev->sg->virtual
+ dev_priv->gart_vm_start);
dev_priv->gart_buffers_offset = dev->agp_buffer_map->offset -
dev->sg->vaddr + dev_priv->gart_vm_start;
DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);