Path: blob/master/drivers/gpu/drm/i915/i915_gem_tiling.c
15113 views
/*1* Copyright © 2008 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22* Authors:23* Eric Anholt <[email protected]>24*25*/2627#include "linux/string.h"28#include "linux/bitops.h"29#include "drmP.h"30#include "drm.h"31#include "i915_drm.h"32#include "i915_drv.h"3334/** @file i915_gem_tiling.c35*36* Support for managing tiling state of buffer objects.37*38* The idea behind tiling is to increase cache hit rates by rearranging39* pixel data so that a group of pixel accesses are in the same cacheline.40* Performance improvement from doing this on the back/depth buffer are on41* the order of 30%.42*43* Intel architectures make this somewhat more complicated, though, by44* adjustments made to addressing of data when the memory is in interleaved45* mode (matched pairs of DIMMS) to improve memory bandwidth.46* For interleaved memory, the CPU sends every sequential 64 bytes47* to an alternate memory channel so it can get the bandwidth from both.48*49* The GPU also rearranges its accesses for increased bandwidth to interleaved50* memory, and it matches what the CPU does for non-tiled. However, when tiled51* it does it a little differently, since one walks addresses not just in the52* X direction but also Y. So, along with alternating channels when bit53* 6 of the address flips, it also alternates when other bits flip -- Bits 954* (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)55* are common to both the 915 and 965-class hardware.56*57* The CPU also sometimes XORs in higher bits as well, to improve58* bandwidth doing strided access like we do so frequently in graphics. This59* is called "Channel XOR Randomization" in the MCH documentation. The result60* is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address61* decode.62*63* All of this bit 6 XORing has an effect on our memory management,64* as we need to make sure that the 3d driver can correctly address object65* contents.66*67* If we don't have interleaved memory, all tiling is safe and no swizzling is68* required.69*70* When bit 17 is XORed in, we simply refuse to tile at all. Bit71* 17 is not just a page offset, so as we page an objet out and back in,72* individual pages in it will have different bit 17 addresses, resulting in73* each 64 bytes being swapped with its neighbor!74*75* Otherwise, if interleaved, we have to tell the 3d driver what the address76* swizzling it needs to do is, since it's writing with the CPU to the pages77* (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the78* pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling79* required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order80* to match what the GPU expects.81*/8283/**84* Detects bit 6 swizzling of address lookup between IGD access and CPU85* access through main memory.86*/87void88i915_gem_detect_bit_6_swizzle(struct drm_device *dev)89{90drm_i915_private_t *dev_priv = dev->dev_private;91uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;92uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;9394if (INTEL_INFO(dev)->gen >= 5) {95/* On Ironlake whatever DRAM config, GPU always do96* same swizzling setup.97*/98swizzle_x = I915_BIT_6_SWIZZLE_9_10;99swizzle_y = I915_BIT_6_SWIZZLE_9;100} else if (IS_GEN2(dev)) {101/* As far as we know, the 865 doesn't have these bit 6102* swizzling issues.103*/104swizzle_x = I915_BIT_6_SWIZZLE_NONE;105swizzle_y = I915_BIT_6_SWIZZLE_NONE;106} else if (IS_MOBILE(dev)) {107uint32_t dcc;108109/* On mobile 9xx chipsets, channel interleave by the CPU is110* determined by DCC. For single-channel, neither the CPU111* nor the GPU do swizzling. For dual channel interleaved,112* the GPU's interleave is bit 9 and 10 for X tiled, and bit113* 9 for Y tiled. The CPU's interleave is independent, and114* can be based on either bit 11 (haven't seen this yet) or115* bit 17 (common).116*/117dcc = I915_READ(DCC);118switch (dcc & DCC_ADDRESSING_MODE_MASK) {119case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:120case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:121swizzle_x = I915_BIT_6_SWIZZLE_NONE;122swizzle_y = I915_BIT_6_SWIZZLE_NONE;123break;124case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:125if (dcc & DCC_CHANNEL_XOR_DISABLE) {126/* This is the base swizzling by the GPU for127* tiled buffers.128*/129swizzle_x = I915_BIT_6_SWIZZLE_9_10;130swizzle_y = I915_BIT_6_SWIZZLE_9;131} else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {132/* Bit 11 swizzling by the CPU in addition. */133swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;134swizzle_y = I915_BIT_6_SWIZZLE_9_11;135} else {136/* Bit 17 swizzling by the CPU in addition. */137swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;138swizzle_y = I915_BIT_6_SWIZZLE_9_17;139}140break;141}142if (dcc == 0xffffffff) {143DRM_ERROR("Couldn't read from MCHBAR. "144"Disabling tiling.\n");145swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;146swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;147}148} else {149/* The 965, G33, and newer, have a very flexible memory150* configuration. It will enable dual-channel mode151* (interleaving) on as much memory as it can, and the GPU152* will additionally sometimes enable different bit 6153* swizzling for tiled objects from the CPU.154*155* Here's what I found on the G965:156* slot fill memory size swizzling157* 0A 0B 1A 1B 1-ch 2-ch158* 512 0 0 0 512 0 O159* 512 0 512 0 16 1008 X160* 512 0 0 512 16 1008 X161* 0 512 0 512 16 1008 X162* 1024 1024 1024 0 2048 1024 O163*164* We could probably detect this based on either the DRB165* matching, which was the case for the swizzling required in166* the table above, or from the 1-ch value being less than167* the minimum size of a rank.168*/169if (I915_READ16(C0DRB3) != I915_READ16(C1DRB3)) {170swizzle_x = I915_BIT_6_SWIZZLE_NONE;171swizzle_y = I915_BIT_6_SWIZZLE_NONE;172} else {173swizzle_x = I915_BIT_6_SWIZZLE_9_10;174swizzle_y = I915_BIT_6_SWIZZLE_9;175}176}177178dev_priv->mm.bit_6_swizzle_x = swizzle_x;179dev_priv->mm.bit_6_swizzle_y = swizzle_y;180}181182/* Check pitch constriants for all chips & tiling formats */183static bool184i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)185{186int tile_width;187188/* Linear is always fine */189if (tiling_mode == I915_TILING_NONE)190return true;191192if (IS_GEN2(dev) ||193(tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))194tile_width = 128;195else196tile_width = 512;197198/* check maximum stride & object size */199if (INTEL_INFO(dev)->gen >= 4) {200/* i965 stores the end address of the gtt mapping in the fence201* reg, so dont bother to check the size */202if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)203return false;204} else {205if (stride > 8192)206return false;207208if (IS_GEN3(dev)) {209if (size > I830_FENCE_MAX_SIZE_VAL << 20)210return false;211} else {212if (size > I830_FENCE_MAX_SIZE_VAL << 19)213return false;214}215}216217/* 965+ just needs multiples of tile width */218if (INTEL_INFO(dev)->gen >= 4) {219if (stride & (tile_width - 1))220return false;221return true;222}223224/* Pre-965 needs power of two tile widths */225if (stride < tile_width)226return false;227228if (stride & (stride - 1))229return false;230231return true;232}233234/* Is the current GTT allocation valid for the change in tiling? */235static bool236i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)237{238u32 size;239240if (tiling_mode == I915_TILING_NONE)241return true;242243if (INTEL_INFO(obj->base.dev)->gen >= 4)244return true;245246if (INTEL_INFO(obj->base.dev)->gen == 3) {247if (obj->gtt_offset & ~I915_FENCE_START_MASK)248return false;249} else {250if (obj->gtt_offset & ~I830_FENCE_START_MASK)251return false;252}253254/*255* Previous chips need to be aligned to the size of the smallest256* fence register that can contain the object.257*/258if (INTEL_INFO(obj->base.dev)->gen == 3)259size = 1024*1024;260else261size = 512*1024;262263while (size < obj->base.size)264size <<= 1;265266if (obj->gtt_space->size != size)267return false;268269if (obj->gtt_offset & (size - 1))270return false;271272return true;273}274275/**276* Sets the tiling mode of an object, returning the required swizzling of277* bit 6 of addresses in the object.278*/279int280i915_gem_set_tiling(struct drm_device *dev, void *data,281struct drm_file *file)282{283struct drm_i915_gem_set_tiling *args = data;284drm_i915_private_t *dev_priv = dev->dev_private;285struct drm_i915_gem_object *obj;286int ret = 0;287288obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));289if (&obj->base == NULL)290return -ENOENT;291292if (!i915_tiling_ok(dev,293args->stride, obj->base.size, args->tiling_mode)) {294drm_gem_object_unreference_unlocked(&obj->base);295return -EINVAL;296}297298if (obj->pin_count) {299drm_gem_object_unreference_unlocked(&obj->base);300return -EBUSY;301}302303if (args->tiling_mode == I915_TILING_NONE) {304args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;305args->stride = 0;306} else {307if (args->tiling_mode == I915_TILING_X)308args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;309else310args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;311312/* Hide bit 17 swizzling from the user. This prevents old Mesa313* from aborting the application on sw fallbacks to bit 17,314* and we use the pread/pwrite bit17 paths to swizzle for it.315* If there was a user that was relying on the swizzle316* information for drm_intel_bo_map()ed reads/writes this would317* break it, but we don't have any of those.318*/319if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)320args->swizzle_mode = I915_BIT_6_SWIZZLE_9;321if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)322args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;323324/* If we can't handle the swizzling, make it untiled. */325if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {326args->tiling_mode = I915_TILING_NONE;327args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;328args->stride = 0;329}330}331332mutex_lock(&dev->struct_mutex);333if (args->tiling_mode != obj->tiling_mode ||334args->stride != obj->stride) {335/* We need to rebind the object if its current allocation336* no longer meets the alignment restrictions for its new337* tiling mode. Otherwise we can just leave it alone, but338* need to ensure that any fence register is cleared.339*/340i915_gem_release_mmap(obj);341342obj->map_and_fenceable =343obj->gtt_space == NULL ||344(obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end &&345i915_gem_object_fence_ok(obj, args->tiling_mode));346347/* Rebind if we need a change of alignment */348if (!obj->map_and_fenceable) {349u32 unfenced_alignment =350i915_gem_get_unfenced_gtt_alignment(dev,351obj->base.size,352args->tiling_mode);353if (obj->gtt_offset & (unfenced_alignment - 1))354ret = i915_gem_object_unbind(obj);355}356357if (ret == 0) {358obj->tiling_changed = true;359obj->tiling_mode = args->tiling_mode;360obj->stride = args->stride;361}362}363/* we have to maintain this existing ABI... */364args->stride = obj->stride;365args->tiling_mode = obj->tiling_mode;366drm_gem_object_unreference(&obj->base);367mutex_unlock(&dev->struct_mutex);368369return ret;370}371372/**373* Returns the current tiling mode and required bit 6 swizzling for the object.374*/375int376i915_gem_get_tiling(struct drm_device *dev, void *data,377struct drm_file *file)378{379struct drm_i915_gem_get_tiling *args = data;380drm_i915_private_t *dev_priv = dev->dev_private;381struct drm_i915_gem_object *obj;382383obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));384if (&obj->base == NULL)385return -ENOENT;386387mutex_lock(&dev->struct_mutex);388389args->tiling_mode = obj->tiling_mode;390switch (obj->tiling_mode) {391case I915_TILING_X:392args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;393break;394case I915_TILING_Y:395args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;396break;397case I915_TILING_NONE:398args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;399break;400default:401DRM_ERROR("unknown tiling mode\n");402}403404/* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */405if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)406args->swizzle_mode = I915_BIT_6_SWIZZLE_9;407if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)408args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;409410drm_gem_object_unreference(&obj->base);411mutex_unlock(&dev->struct_mutex);412413return 0;414}415416/**417* Swap every 64 bytes of this page around, to account for it having a new418* bit 17 of its physical address and therefore being interpreted differently419* by the GPU.420*/421static void422i915_gem_swizzle_page(struct page *page)423{424char temp[64];425char *vaddr;426int i;427428vaddr = kmap(page);429430for (i = 0; i < PAGE_SIZE; i += 128) {431memcpy(temp, &vaddr[i], 64);432memcpy(&vaddr[i], &vaddr[i + 64], 64);433memcpy(&vaddr[i + 64], temp, 64);434}435436kunmap(page);437}438439void440i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object *obj)441{442struct drm_device *dev = obj->base.dev;443drm_i915_private_t *dev_priv = dev->dev_private;444int page_count = obj->base.size >> PAGE_SHIFT;445int i;446447if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)448return;449450if (obj->bit_17 == NULL)451return;452453for (i = 0; i < page_count; i++) {454char new_bit_17 = page_to_phys(obj->pages[i]) >> 17;455if ((new_bit_17 & 0x1) !=456(test_bit(i, obj->bit_17) != 0)) {457i915_gem_swizzle_page(obj->pages[i]);458set_page_dirty(obj->pages[i]);459}460}461}462463void464i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object *obj)465{466struct drm_device *dev = obj->base.dev;467drm_i915_private_t *dev_priv = dev->dev_private;468int page_count = obj->base.size >> PAGE_SHIFT;469int i;470471if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)472return;473474if (obj->bit_17 == NULL) {475obj->bit_17 = kmalloc(BITS_TO_LONGS(page_count) *476sizeof(long), GFP_KERNEL);477if (obj->bit_17 == NULL) {478DRM_ERROR("Failed to allocate memory for bit 17 "479"record\n");480return;481}482}483484for (i = 0; i < page_count; i++) {485if (page_to_phys(obj->pages[i]) & (1 << 17))486__set_bit(i, obj->bit_17);487else488__clear_bit(i, obj->bit_17);489}490}491492493