1450N/A/*
1450N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1450N/A */
1450N/A
1450N/A/* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*- */
1450N/A/*
1450N/A * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
1450N/A *
1450N/A * The Weather Channel (TM) funded Tungsten Graphics to develop the
1450N/A * initial release of the Radeon 8500 driver under the XFree86 license.
1450N/A * This notice must be preserved.
1450N/A *
1450N/A * Permission is hereby granted, free of charge, to any person obtaining a
1450N/A * copy of this software and associated documentation files (the "Software"),
1450N/A * to deal in the Software without restriction, including without limitation
1450N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1450N/A * and/or sell copies of the Software, and to permit persons to whom the
1450N/A * Software is furnished to do so, subject to the following conditions:
1450N/A *
1450N/A * The above copyright notice and this permission notice (including the next
1450N/A * paragraph) shall be included in all copies or substantial portions of the
1450N/A * Software.
1450N/A *
1450N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1450N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1450N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1450N/A * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1450N/A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1450N/A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1450N/A * DEALINGS IN THE SOFTWARE.
1450N/A *
1450N/A * Authors:
1450N/A * Keith Whitwell <keith@tungstengraphics.com>
1450N/A */
1450N/A
1450N/A#include "drmP.h"
1450N/A#include "drm.h"
1450N/A#include "radeon_drm.h"
1450N/A#include "radeon_drv.h"
1450N/A#include "radeon_io32.h"
1450N/A
1450N/A/*
1450N/A * Very simple allocator for GART memory, working on a static range
1450N/A * already mapped into each client's address space.
1450N/A */
1450N/A
1450N/Astatic struct mem_block *
1450N/Asplit_block(struct mem_block *p, int start, int size, drm_file_t *filp)
1450N/A{
1450N/A /* Maybe cut off the start of an existing block */
1450N/A if (start > p->start) {
1450N/A struct mem_block *newblock =
1450N/A drm_alloc(sizeof (*newblock), DRM_MEM_BUFS);
1450N/A if (!newblock)
1450N/A goto out;
1450N/A newblock->start = start;
1450N/A newblock->size = p->size - (start - p->start);
1450N/A newblock->filp = NULL;
1450N/A newblock->next = p->next;
1450N/A newblock->prev = p;
1450N/A p->next->prev = newblock;
1450N/A p->next = newblock;
1450N/A p->size -= newblock->size;
1450N/A p = newblock;
1450N/A }
1450N/A
1450N/A /* Maybe cut off the end of an existing block */
1450N/A if (size < p->size) {
1450N/A struct mem_block *newblock =
1450N/A drm_alloc(sizeof (*newblock), DRM_MEM_BUFS);
1450N/A if (!newblock)
1450N/A goto out;
1450N/A newblock->start = start + size;
1450N/A newblock->size = p->size - size;
1450N/A newblock->filp = NULL;
1450N/A newblock->next = p->next;
1450N/A newblock->prev = p;
1450N/A p->next->prev = newblock;
1450N/A p->next = newblock;
1450N/A p->size = size;
1450N/A }
1450N/A
1450N/Aout:
1450N/A /* Our block is in the middle */
1450N/A p->filp = filp;
1450N/A return (p);
1450N/A}
1450N/A
1450N/Astatic struct mem_block *
1450N/Aalloc_block(struct mem_block *heap, int size, int align2, drm_file_t *filp)
1450N/A{
1450N/A struct mem_block *p;
1450N/A int mask = (1 << align2) - 1;
1450N/A
1450N/A for (p = heap->next; p != heap; p = p->next) {
1450N/A int start = (p->start + mask) & ~mask;
1450N/A if (p->filp == 0 && start + size <= p->start + p->size)
1450N/A return (split_block(p, start, size, filp));
1450N/A }
1450N/A
1450N/A return (NULL);
1450N/A}
1450N/A
1450N/Astatic struct mem_block *
1450N/Afind_block(struct mem_block *heap, int start)
1450N/A{
1450N/A struct mem_block *p;
1450N/A
1450N/A for (p = heap->next; p != heap; p = p->next)
1450N/A if (p->start == start)
1450N/A return (p);
1450N/A
1450N/A return (NULL);
1450N/A}
1450N/A
1450N/Astatic void
1450N/Afree_block(struct mem_block *p)
1450N/A{
1450N/A p->filp = NULL;
1450N/A
1450N/A /*
1450N/A * Assumes a single contiguous range. Needs a special filp in
1450N/A * 'heap' to stop it being subsumed.
1450N/A */
1450N/A if (p->next->filp == 0) {
1450N/A struct mem_block *q = p->next;
1450N/A p->size += q->size;
1450N/A p->next = q->next;
1450N/A p->next->prev = p;
1450N/A drm_free(q, sizeof (*q), DRM_MEM_BUFS);
1450N/A }
1450N/A
1450N/A if (p->prev->filp == 0) {
1450N/A struct mem_block *q = p->prev;
1450N/A q->size += p->size;
1450N/A q->next = p->next;
1450N/A q->next->prev = q;
1450N/A drm_free(p, sizeof (*q), DRM_MEM_BUFS);
1450N/A }
1450N/A}
1450N/A
1450N/A/*
1450N/A * Initialize. How to check for an uninitialized heap?
1450N/A */
1450N/Astatic int
1450N/Ainit_heap(struct mem_block **heap, int start, int size)
1450N/A{
1450N/A struct mem_block *blocks = drm_alloc(sizeof (*blocks), DRM_MEM_BUFS);
1450N/A
1450N/A if (!blocks)
1450N/A return (ENOMEM);
1450N/A
1450N/A *heap = drm_alloc(sizeof (**heap), DRM_MEM_BUFS);
1450N/A if (!*heap) {
1450N/A drm_free(blocks, sizeof (*blocks), DRM_MEM_BUFS);
1450N/A return (ENOMEM);
1450N/A }
1450N/A
1450N/A blocks->start = start;
1450N/A blocks->size = size;
1450N/A blocks->filp = NULL;
1450N/A blocks->next = blocks->prev = *heap;
1450N/A
1450N/A (void) memset(*heap, 0, sizeof (**heap));
1450N/A (*heap)->filp = (drm_file_t *)-1;
1450N/A (*heap)->next = (*heap)->prev = blocks;
1450N/A return (0);
1450N/A}
1450N/A
1450N/A/*
1450N/A * Free all blocks associated with the releasing file.
1450N/A */
1450N/Avoid
1450N/Aradeon_mem_release(drm_file_t *filp, struct mem_block *heap)
1450N/A{
1450N/A struct mem_block *p;
1450N/A
1450N/A if (!heap || !heap->next)
1450N/A return;
1450N/A
1450N/A for (p = heap->next; p != heap; p = p->next) {
1450N/A if (p->filp == filp)
1450N/A p->filp = NULL;
1450N/A }
1450N/A
1450N/A /*
1450N/A * Assumes a single contiguous range. Needs a special filp in
1450N/A * 'heap' to stop it being subsumed.
1450N/A */
1450N/A for (p = heap->next; p != heap; p = p->next) {
1450N/A while (p->filp == 0 && p->next->filp == 0) {
1450N/A struct mem_block *q = p->next;
1450N/A p->size += q->size;
1450N/A p->next = q->next;
1450N/A p->next->prev = p;
1450N/A drm_free(q, sizeof (*q), DRM_MEM_DRIVER);
1450N/A }
1450N/A }
1450N/A}
1450N/A
1450N/A/*
1450N/A * Shutdown.
1450N/A */
1450N/Avoid
1450N/Aradeon_mem_takedown(struct mem_block **heap)
1450N/A{
1450N/A struct mem_block *p;
1450N/A
1450N/A if (!*heap)
1450N/A return;
1450N/A
1450N/A for (p = (*heap)->next; p != *heap; ) {
1450N/A struct mem_block *q = p;
1450N/A p = p->next;
1450N/A drm_free(q, sizeof (*q), DRM_MEM_DRIVER);
1450N/A }
1450N/A
1450N/A drm_free(*heap, sizeof (**heap), DRM_MEM_DRIVER);
1450N/A *heap = NULL;
1450N/A}
1450N/A
1450N/A/* IOCTL HANDLERS */
1450N/A
1450N/Astatic struct mem_block **
1450N/Aget_heap(drm_radeon_private_t *dev_priv, int region)
1450N/A{
1450N/A switch (region) {
1450N/A case RADEON_MEM_REGION_GART:
1450N/A return (&dev_priv->gart_heap);
1450N/A case RADEON_MEM_REGION_FB:
1450N/A return (&dev_priv->fb_heap);
1450N/A default:
1450N/A return (NULL);
1450N/A }
1450N/A}
1450N/A
1450N/A/*ARGSUSED*/
1450N/Aint
1450N/Aradeon_mem_alloc(DRM_IOCTL_ARGS)
1450N/A{
1450N/A DRM_DEVICE;
1450N/A drm_radeon_private_t *dev_priv = dev->dev_private;
1450N/A drm_radeon_mem_alloc_t alloc;
1450N/A struct mem_block *block, **heap;
1450N/A
1450N/A if (!dev_priv) {
1450N/A DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
1450N/A return (EINVAL);
1450N/A }
1450N/A
1450N/A#ifdef _MULTI_DATAMODEL
1450N/A if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
1450N/A drm_radeon_mem_alloc_32_t alloc32;
1450N/A
1450N/A DRM_COPYFROM_WITH_RETURN(&alloc32, (void *) data,
1450N/A sizeof (alloc32));
1450N/A alloc.region = alloc32.region;
1450N/A alloc.alignment = alloc32.alignment;
1450N/A alloc.size = alloc32.size;
1450N/A alloc.region_offset = (void *)(uintptr_t)alloc32.region_offset;
1450N/A } else {
1450N/A#endif
1450N/A DRM_COPYFROM_WITH_RETURN(&alloc, (void *) data, sizeof (alloc));
1450N/A#ifdef _MULTI_DATAMODEL
1450N/A }
1450N/A#endif
1450N/A
1450N/A heap = get_heap(dev_priv, alloc.region);
1450N/A if (!heap || !*heap)
1450N/A return (EFAULT);
1450N/A
1450N/A /*
1450N/A * Make things easier on ourselves: all allocations at least
1450N/A * 4k aligned.
1450N/A */
1450N/A if (alloc.alignment < 12)
1450N/A alloc.alignment = 12;
1450N/A
1450N/A block = alloc_block(*heap, alloc.size, alloc.alignment, fpriv);
1450N/A
1450N/A if (!block)
1450N/A return (ENOMEM);
1450N/A
1450N/A if (DRM_COPY_TO_USER(alloc.region_offset, &block->start,
1450N/A sizeof (int))) {
1450N/A DRM_ERROR("copy_to_user\n");
1450N/A return (EFAULT);
1450N/A }
1450N/A
1450N/A return (0);
1450N/A}
1450N/A
1450N/A/*ARGSUSED*/
1450N/Aint
1450N/Aradeon_mem_free(DRM_IOCTL_ARGS)
1450N/A{
1450N/A DRM_DEVICE;
1450N/A drm_radeon_private_t *dev_priv = dev->dev_private;
1450N/A drm_radeon_mem_free_t memfree;
1450N/A struct mem_block *block, **heap;
1450N/A
1450N/A if (!dev_priv) {
1450N/A DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
1450N/A return (EINVAL);
1450N/A }
1450N/A
1450N/A DRM_COPYFROM_WITH_RETURN(&memfree, (void *) data, sizeof (memfree));
1450N/A
1450N/A heap = get_heap(dev_priv, memfree.region);
1450N/A if (!heap || !*heap)
1450N/A return (EFAULT);
1450N/A
1450N/A block = find_block(*heap, memfree.region_offset);
1450N/A if (!block)
1450N/A return (EFAULT);
1450N/A
1450N/A if (block->filp != fpriv)
1450N/A return (EPERM);
1450N/A
1450N/A free_block(block);
1450N/A return (0);
1450N/A}
1450N/A
1450N/A/*ARGSUSED*/
1450N/Aint
1450N/Aradeon_mem_init_heap(DRM_IOCTL_ARGS)
1450N/A{
1450N/A DRM_DEVICE;
1450N/A drm_radeon_private_t *dev_priv = dev->dev_private;
1450N/A drm_radeon_mem_init_heap_t initheap;
1450N/A struct mem_block **heap;
1450N/A
1450N/A if (!dev_priv) {
1450N/A DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
1450N/A return (EINVAL);
1450N/A }
1450N/A
1450N/A DRM_COPYFROM_WITH_RETURN(&initheap, (void *) data, sizeof (initheap));
1450N/A
1450N/A heap = get_heap(dev_priv, initheap.region);
1450N/A if (!heap)
1450N/A return (EFAULT);
1450N/A
1450N/A if (*heap) {
1450N/A DRM_ERROR("heap already initialized?");
1450N/A return (EFAULT);
1450N/A }
1450N/A
1450N/A return (init_heap(heap, initheap.start, initheap.size));
1450N/A}