drm/linux-core/i915_buffer.c

284 lines
7.6 KiB
C
Raw Normal View History

2006-08-22 02:24:48 -06:00
/**************************************************************************
*
2006-08-22 02:24:48 -06:00
* Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
* All Rights Reserved.
*
2006-08-22 02:24:48 -06:00
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
2006-08-22 02:24:48 -06:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2006-08-22 02:24:48 -06:00
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
*
2006-08-22 02:24:48 -06:00
**************************************************************************/
/*
* Authors: Thomas Hellstr<EFBFBD>m <thomas-at-tungstengraphics-dot-com>
*/
#include "drmP.h"
2006-08-31 13:42:29 -06:00
#include "i915_drm.h"
#include "i915_drv.h"
2006-08-22 02:24:48 -06:00
2007-11-05 18:32:58 -07:00
struct drm_ttm_backend *i915_create_ttm_backend_entry(struct drm_device *dev)
2006-08-22 02:24:48 -06:00
{
return drm_agp_init_ttm(dev);
2006-08-22 02:24:48 -06:00
}
2006-08-31 13:42:29 -06:00
int i915_fence_types(struct drm_buffer_object *bo,
2007-11-05 18:32:58 -07:00
uint32_t *fclass,
uint32_t *type)
2006-08-31 13:42:29 -06:00
{
if (bo->mem.mask & (DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE))
2006-08-31 13:42:29 -06:00
*type = 3;
2006-09-01 03:23:21 -06:00
else
*type = 1;
2006-08-31 13:42:29 -06:00
return 0;
}
2007-11-05 18:32:58 -07:00
int i915_invalidate_caches(struct drm_device *dev, uint64_t flags)
2006-08-31 13:42:29 -06:00
{
/*
* FIXME: Only emit once per batchbuffer submission.
*/
2006-08-31 13:42:29 -06:00
uint32_t flush_cmd = MI_NO_WRITE_FLUSH;
if (flags & DRM_BO_FLAG_READ)
flush_cmd |= MI_READ_FLUSH;
if (flags & DRM_BO_FLAG_EXE)
flush_cmd |= MI_EXE_FLUSH;
return i915_emit_mi_flush(dev, flush_cmd);
}
2007-11-05 18:32:58 -07:00
int i915_init_mem_type(struct drm_device *dev, uint32_t type,
struct drm_mem_type_manager *man)
{
2007-02-12 12:34:50 -07:00
switch (type) {
case DRM_BO_MEM_LOCAL:
man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE |
2007-02-12 12:34:50 -07:00
_DRM_FLAG_MEMTYPE_CACHED;
man->drm_bus_maptype = 0;
man->gpu_offset = 0;
break;
case DRM_BO_MEM_TT:
2007-02-06 08:59:45 -07:00
if (!(drm_core_has_AGP(dev) && dev->agp)) {
2007-02-12 12:34:50 -07:00
DRM_ERROR("AGP is not enabled for memory type %u\n",
(unsigned)type);
2007-02-06 08:59:45 -07:00
return -EINVAL;
}
man->io_offset = dev->agp->agp_info.aper_base;
man->io_size = dev->agp->agp_info.aper_size * 1024 * 1024;
man->io_addr = NULL;
man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE |
2007-02-12 12:34:50 -07:00
_DRM_FLAG_MEMTYPE_CSELECT | _DRM_FLAG_NEEDS_IOREMAP;
man->drm_bus_maptype = _DRM_AGP;
man->gpu_offset = 0;
break;
case DRM_BO_MEM_PRIV0:
if (!(drm_core_has_AGP(dev) && dev->agp)) {
2007-02-12 12:34:50 -07:00
DRM_ERROR("AGP is not enabled for memory type %u\n",
(unsigned)type);
return -EINVAL;
}
man->io_offset = dev->agp->agp_info.aper_base;
man->io_size = dev->agp->agp_info.aper_size * 1024 * 1024;
2007-02-06 08:59:45 -07:00
man->io_addr = NULL;
man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE |
2007-02-12 12:34:50 -07:00
_DRM_FLAG_MEMTYPE_FIXED | _DRM_FLAG_NEEDS_IOREMAP;
man->drm_bus_maptype = _DRM_AGP;
man->gpu_offset = 0;
break;
default:
2007-02-12 12:34:50 -07:00
DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
return -EINVAL;
}
return 0;
}
2007-02-05 08:13:32 -07:00
2007-07-15 21:37:02 -06:00
uint32_t i915_evict_mask(struct drm_buffer_object *bo)
{
switch (bo->mem.mem_type) {
case DRM_BO_MEM_LOCAL:
case DRM_BO_MEM_TT:
return DRM_BO_FLAG_MEM_LOCAL;
default:
return DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_CACHED;
}
}
#if 0 /* See comment below */
2007-07-15 20:32:51 -06:00
static void i915_emit_copy_blit(struct drm_device * dev,
2007-02-12 12:34:50 -07:00
uint32_t src_offset,
uint32_t dst_offset,
uint32_t pages, int direction)
2007-02-05 08:13:32 -07:00
{
uint32_t cur_pages;
uint32_t stride = PAGE_SIZE;
drm_i915_private_t *dev_priv = dev->dev_private;
RING_LOCALS;
if (!dev_priv)
return;
2007-02-12 12:34:50 -07:00
i915_kernel_lost_context(dev);
2007-02-12 12:34:50 -07:00
while (pages > 0) {
2007-02-05 08:13:32 -07:00
cur_pages = pages;
if (cur_pages > 2048)
cur_pages = 2048;
pages -= cur_pages;
BEGIN_LP_RING(6);
OUT_RING(SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
2007-02-05 08:13:32 -07:00
XY_SRC_COPY_BLT_WRITE_RGB);
2007-02-12 12:34:50 -07:00
OUT_RING((stride & 0xffff) | (0xcc << 16) | (1 << 24) |
(1 << 25) | (direction ? (1 << 30) : 0));
OUT_RING((cur_pages << 16) | PAGE_SIZE);
2007-02-05 08:13:32 -07:00
OUT_RING(dst_offset);
OUT_RING(stride & 0xffff);
OUT_RING(src_offset);
ADVANCE_LP_RING();
}
return;
}
2007-07-15 21:37:02 -06:00
static int i915_move_blit(struct drm_buffer_object * bo,
int evict, int no_wait, struct drm_bo_mem_reg * new_mem)
{
2007-07-15 21:37:02 -06:00
struct drm_bo_mem_reg *old_mem = &bo->mem;
int dir = 0;
2007-02-12 12:34:50 -07:00
if ((old_mem->mem_type == new_mem->mem_type) &&
(new_mem->mm_node->start <
old_mem->mm_node->start + old_mem->mm_node->size)) {
dir = 1;
}
i915_emit_copy_blit(bo->dev,
old_mem->mm_node->start << PAGE_SHIFT,
new_mem->mm_node->start << PAGE_SHIFT,
2007-02-12 12:34:50 -07:00
new_mem->num_pages, dir);
i915_emit_mi_flush(bo->dev, MI_READ_FLUSH | MI_EXE_FLUSH);
return drm_bo_move_accel_cleanup(bo, evict, no_wait, 0,
DRM_FENCE_TYPE_EXE |
2007-02-12 12:34:50 -07:00
DRM_I915_FENCE_TYPE_RW,
DRM_I915_FENCE_FLAG_FLUSHED, new_mem);
}
/*
* Flip destination ttm into cached-coherent AGP,
* then blit and subsequently move out again.
*/
2007-07-15 21:37:02 -06:00
static int i915_move_flip(struct drm_buffer_object * bo,
int evict, int no_wait, struct drm_bo_mem_reg * new_mem)
{
2007-07-15 20:32:51 -06:00
struct drm_device *dev = bo->dev;
2007-07-15 21:37:02 -06:00
struct drm_bo_mem_reg tmp_mem;
int ret;
tmp_mem = *new_mem;
tmp_mem.mm_node = NULL;
tmp_mem.mask = DRM_BO_FLAG_MEM_TT |
2007-02-12 12:34:50 -07:00
DRM_BO_FLAG_CACHED | DRM_BO_FLAG_FORCE_CACHING;
ret = drm_bo_mem_space(bo, &tmp_mem, no_wait);
2007-02-12 12:34:50 -07:00
if (ret)
return ret;
2007-02-12 12:34:50 -07:00
ret = drm_bind_ttm(bo->ttm, &tmp_mem);
2007-02-12 12:34:50 -07:00
if (ret)
goto out_cleanup;
ret = i915_move_blit(bo, 1, no_wait, &tmp_mem);
2007-02-12 12:34:50 -07:00
if (ret)
goto out_cleanup;
2007-02-12 12:34:50 -07:00
ret = drm_bo_move_ttm(bo, evict, no_wait, new_mem);
out_cleanup:
if (tmp_mem.mm_node) {
mutex_lock(&dev->struct_mutex);
if (tmp_mem.mm_node != bo->pinned_node)
drm_mm_put_block(tmp_mem.mm_node);
tmp_mem.mm_node = NULL;
mutex_unlock(&dev->struct_mutex);
}
return ret;
}
#endif
/*
2007-11-05 18:32:58 -07:00
* Disable i915_move_flip for now, since we can't guarantee that the hardware
* lock is held here. To re-enable we need to make sure either
* a) The X server is using DRM to submit commands to the ring, or
2007-11-05 18:32:58 -07:00
* b) DRM can use the HP ring for these blits. This means i915 needs to
* implement a new ring submission mechanism and fence class.
*/
2007-11-05 18:32:58 -07:00
int i915_move(struct drm_buffer_object *bo,
int evict, int no_wait, struct drm_bo_mem_reg *new_mem)
{
2007-07-15 21:37:02 -06:00
struct drm_bo_mem_reg *old_mem = &bo->mem;
2007-02-10 04:06:36 -07:00
if (old_mem->mem_type == DRM_BO_MEM_LOCAL) {
return drm_bo_move_memcpy(bo, evict, no_wait, new_mem);
2007-02-10 04:06:36 -07:00
} else if (new_mem->mem_type == DRM_BO_MEM_LOCAL) {
2007-11-05 18:32:58 -07:00
if (0) /*i915_move_flip(bo, evict, no_wait, new_mem)*/
2007-02-12 12:34:50 -07:00
return drm_bo_move_memcpy(bo, evict, no_wait, new_mem);
} else {
2007-11-05 18:32:58 -07:00
if (0) /*i915_move_blit(bo, evict, no_wait, new_mem)*/
2007-02-12 12:34:50 -07:00
return drm_bo_move_memcpy(bo, evict, no_wait, new_mem);
}
return 0;
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
static inline void clflush(volatile void *__p)
{
asm volatile("clflush %0" : "+m" (*(char __force *)__p));
}
#endif
static inline void drm_cache_flush_addr(void *virt)
{
2007-11-05 18:32:58 -07:00
int i;
for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
clflush(virt+i);
}
static inline void drm_cache_flush_page(struct page *p)
{
drm_cache_flush_addr(page_address(p));
}
void i915_flush_ttm(struct drm_ttm *ttm)
{
int i;
if (!ttm)
return;
DRM_MEMORYBARRIER();
for (i = ttm->num_pages-1; i >= 0; i--)
drm_cache_flush_page(drm_ttm_get_page(ttm, i));
DRM_MEMORYBARRIER();
}