Pekka J Enberg
2008-Mar-09 16:37 UTC
[Nouveau] [PATCH 4/4] nouveau: clean up nouveau_mem_alloc function
From: Pekka Enberg <penberg at cs.helsinki.fi> Convert the macros in nouveau_mem_alloc() to nouveau_mem_alloc_agp(), nouveau_mem_alloc_fb(), and nouveau_mem_alloc_pci() functions that use a new generic __nouveau_mem_alloc() function to allocate and map memory. Signed-off-by: Pekka Enberg <penberg at cs.helsinki.fi> --- shared-core/nouveau_mem.c | 174 ++++++++++++++++++++++++++++++---------------- 1 file changed, 117 insertions(+), 57 deletions(-) Index: drm/shared-core/nouveau_mem.c ==================================================================--- drm.orig/shared-core/nouveau_mem.c +++ drm/shared-core/nouveau_mem.c @@ -567,13 +567,91 @@ static int nouveau_addmap_agp(struct drm return nouveau_addmap(dev, block, block->start, type); } +static struct mem_block * +__nouveau_mem_alloc(struct drm_device *dev, int type, struct mem_block *heap, + uint64_t size, int alignment, int flags, + struct drm_file *file_priv) +{ + struct mem_block *block; + int err; + + block = nouveau_mem_alloc_block(heap, size, alignment, file_priv); + if (!block) + return NULL; + block->flags = type; + + /* + * Does the block need mapping? + */ + if (!(flags & NOUVEAU_MEM_MAPPED)) + return block; + + switch (type) { + case NOUVEAU_MEM_AGP: + err = nouveau_addmap_agp(dev, block); + break; + case NOUVEAU_MEM_FB: + err = nouveau_addmap_fb(dev, block); + break; + case NOUVEAU_MEM_PCI: + err = nouveau_addmap_pci(dev, block); + break; + default: + err = -EINVAL; + break; + }; + if (err) + goto error; + + return block; +error: + nouveau_mem_free_block(block); + return NULL; +} + +static struct mem_block * +nouveau_mem_alloc_pci(struct drm_device *dev, uint64_t size, int alignment, + int flags, struct drm_file *file_priv) +{ + struct drm_nouveau_private *dev_priv = dev->dev_private; + + return __nouveau_mem_alloc(dev, NOUVEAU_MEM_PCI, dev_priv->pci_heap, + size, alignment, flags, file_priv); +} + +static struct mem_block * +nouveau_mem_alloc_agp(struct drm_device *dev, uint64_t size, int alignment, + int flags, struct drm_file *file_priv) +{ + struct drm_nouveau_private *dev_priv = dev->dev_private; + + return __nouveau_mem_alloc(dev, NOUVEAU_MEM_AGP, dev_priv->agp_heap, + size, alignment, flags, file_priv); +} + +static struct mem_block * +nouveau_mem_alloc_fb(struct drm_device *dev, uint64_t size, int alignment, + int flags, struct drm_file *file_priv) +{ + struct drm_nouveau_private *dev_priv = dev->dev_private; + struct mem_block *block; + + if (!(flags & NOUVEAU_MEM_MAPPED)) { + block = __nouveau_mem_alloc(dev, NOUVEAU_MEM_FB, + dev_priv->fb_nomap_heap, size, + alignment, flags, file_priv); + if (block) + return block; + } + return __nouveau_mem_alloc(dev, NOUVEAU_MEM_FB, dev_priv->fb_heap, + size, alignment, flags, file_priv); +} + struct mem_block* nouveau_mem_alloc(struct drm_device *dev, int alignment, uint64_t size, int flags, struct drm_file *file_priv) { struct mem_block *block; - int type; - struct drm_nouveau_private *dev_priv = dev->dev_private; /* * Make things easier on ourselves: all allocations are page-aligned. @@ -594,67 +672,49 @@ struct mem_block* nouveau_mem_alloc(stru if (size & (~PAGE_MASK)) size = ((size/PAGE_SIZE) + 1) * PAGE_SIZE; + if (flags & NOUVEAU_MEM_FB) { + block = nouveau_mem_alloc_fb(dev, size, alignment, flags, + file_priv); + if (block) + goto out; + } -#define NOUVEAU_MEM_ALLOC_AGP {\ - type=NOUVEAU_MEM_AGP;\ - block = nouveau_mem_alloc_block(dev_priv->agp_heap, size,\ - alignment, file_priv); \ - if (block) goto alloc_ok;\ - } - -#define NOUVEAU_MEM_ALLOC_PCI {\ - type = NOUVEAU_MEM_PCI;\ - block = nouveau_mem_alloc_block(dev_priv->pci_heap, size, \ - alignment, file_priv); \ - if ( block ) goto alloc_ok;\ - } - -#define NOUVEAU_MEM_ALLOC_FB {\ - type=NOUVEAU_MEM_FB;\ - if (!(flags&NOUVEAU_MEM_MAPPED)) {\ - block = nouveau_mem_alloc_block(dev_priv->fb_nomap_heap,\ - size, alignment, \ - file_priv); \ - if (block) goto alloc_ok;\ - }\ - block = nouveau_mem_alloc_block(dev_priv->fb_heap, size,\ - alignment, file_priv);\ - if (block) goto alloc_ok;\ - } - - - if (flags&NOUVEAU_MEM_FB) NOUVEAU_MEM_ALLOC_FB - if (flags&NOUVEAU_MEM_AGP) NOUVEAU_MEM_ALLOC_AGP - if (flags&NOUVEAU_MEM_PCI) NOUVEAU_MEM_ALLOC_PCI - if (flags&NOUVEAU_MEM_FB_ACCEPTABLE) NOUVEAU_MEM_ALLOC_FB - if (flags&NOUVEAU_MEM_AGP_ACCEPTABLE) NOUVEAU_MEM_ALLOC_AGP - if (flags&NOUVEAU_MEM_PCI_ACCEPTABLE) NOUVEAU_MEM_ALLOC_PCI - + if (flags & NOUVEAU_MEM_AGP) { + block = nouveau_mem_alloc_agp(dev, size, alignment, flags, + file_priv); + if (block) + goto out; + } - return NULL; + if (flags & NOUVEAU_MEM_PCI) { + block = nouveau_mem_alloc_pci(dev, size, alignment, flags, + file_priv); + if (block) + goto out; + } -alloc_ok: - block->flags=type; + if (flags & NOUVEAU_MEM_FB_ACCEPTABLE) { + block = nouveau_mem_alloc_fb(dev, size, alignment, flags, + file_priv); + if (block) + goto out; + } - if (flags&NOUVEAU_MEM_MAPPED) - { - int ret = 0; - - if (type == NOUVEAU_MEM_AGP) - ret = nouveau_addmap_agp(dev, block); - else if (type == NOUVEAU_MEM_FB) - ret = nouveau_addmap_fb(dev, block); - else if (type == NOUVEAU_MEM_PCI) - ret = nouveau_addmap_pci(dev, block); - - if (ret) { - nouveau_mem_free_block(block); - return NULL; - } + if (flags & NOUVEAU_MEM_AGP_ACCEPTABLE) { + block = nouveau_mem_alloc_agp(dev, size, alignment, flags, + file_priv); + if (block) + goto out; } - DRM_DEBUG("allocated %lld bytes at 0x%llx type=0x%08x\n", block->size, block->start, block->flags); - return block; + if (flags & NOUVEAU_MEM_PCI_ACCEPTABLE) + block = nouveau_mem_alloc_pci(dev, size, alignment, flags, + file_priv); +out: + if (block) + DRM_DEBUG("allocated %lld bytes at 0x%llx type=0x%08x\n", + block->size, block->start, block->flags); + return block; } void nouveau_mem_free(struct drm_device* dev, struct mem_block* block)
Possibly Parallel Threads
- [PATCH 1/4] nouveau: add nouveau_addmap_{agp, fb, pci} functions
- [PATCH 2/4] nouveau: add nouveau_map_block function
- [PATCH 3/4] nouveau: move nouveau_map_block() call to nouveau_addmap()
- Fixes and workarounds for regressions and issues in the randr-1.2 branch
- [PATCH] added comments