Path: blob/master/drivers/gpu/drm/nouveau/nouveau_bo.c
15112 views
/*1* Copyright 2007 Dave Airlied2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*/23/*24* Authors: Dave Airlied <[email protected]>25* Ben Skeggs <[email protected]>26* Jeremy Kolb <[email protected]>27*/2829#include "drmP.h"3031#include "nouveau_drm.h"32#include "nouveau_drv.h"33#include "nouveau_dma.h"34#include "nouveau_mm.h"35#include "nouveau_vm.h"3637#include <linux/log2.h>38#include <linux/slab.h>3940static void41nouveau_bo_del_ttm(struct ttm_buffer_object *bo)42{43struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);44struct drm_device *dev = dev_priv->dev;45struct nouveau_bo *nvbo = nouveau_bo(bo);4647if (unlikely(nvbo->gem))48DRM_ERROR("bo %p still attached to GEM object\n", bo);4950nv10_mem_put_tile_region(dev, nvbo->tile, NULL);51if (nvbo->vma.node) {52nouveau_vm_unmap(&nvbo->vma);53nouveau_vm_put(&nvbo->vma);54}55kfree(nvbo);56}5758static void59nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,60int *align, int *size, int *page_shift)61{62struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);6364if (dev_priv->card_type < NV_50) {65if (nvbo->tile_mode) {66if (dev_priv->chipset >= 0x40) {67*align = 65536;68*size = roundup(*size, 64 * nvbo->tile_mode);6970} else if (dev_priv->chipset >= 0x30) {71*align = 32768;72*size = roundup(*size, 64 * nvbo->tile_mode);7374} else if (dev_priv->chipset >= 0x20) {75*align = 16384;76*size = roundup(*size, 64 * nvbo->tile_mode);7778} else if (dev_priv->chipset >= 0x10) {79*align = 16384;80*size = roundup(*size, 32 * nvbo->tile_mode);81}82}83} else {84if (likely(dev_priv->chan_vm)) {85if (!(flags & TTM_PL_FLAG_TT) && *size > 256 * 1024)86*page_shift = dev_priv->chan_vm->lpg_shift;87else88*page_shift = dev_priv->chan_vm->spg_shift;89} else {90*page_shift = 12;91}9293*size = roundup(*size, (1 << *page_shift));94*align = max((1 << *page_shift), *align);95}9697*size = roundup(*size, PAGE_SIZE);98}99100int101nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,102int size, int align, uint32_t flags, uint32_t tile_mode,103uint32_t tile_flags, struct nouveau_bo **pnvbo)104{105struct drm_nouveau_private *dev_priv = dev->dev_private;106struct nouveau_bo *nvbo;107int ret = 0, page_shift = 0;108109nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);110if (!nvbo)111return -ENOMEM;112INIT_LIST_HEAD(&nvbo->head);113INIT_LIST_HEAD(&nvbo->entry);114nvbo->tile_mode = tile_mode;115nvbo->tile_flags = tile_flags;116nvbo->bo.bdev = &dev_priv->ttm.bdev;117118nouveau_bo_fixup_align(nvbo, flags, &align, &size, &page_shift);119align >>= PAGE_SHIFT;120121if (dev_priv->chan_vm) {122ret = nouveau_vm_get(dev_priv->chan_vm, size, page_shift,123NV_MEM_ACCESS_RW, &nvbo->vma);124if (ret) {125kfree(nvbo);126return ret;127}128}129130nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;131nouveau_bo_placement_set(nvbo, flags, 0);132133nvbo->channel = chan;134ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,135ttm_bo_type_device, &nvbo->placement, align, 0,136false, NULL, size, nouveau_bo_del_ttm);137if (ret) {138/* ttm will call nouveau_bo_del_ttm if it fails.. */139return ret;140}141nvbo->channel = NULL;142143if (nvbo->vma.node)144nvbo->bo.offset = nvbo->vma.offset;145*pnvbo = nvbo;146return 0;147}148149static void150set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)151{152*n = 0;153154if (type & TTM_PL_FLAG_VRAM)155pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;156if (type & TTM_PL_FLAG_TT)157pl[(*n)++] = TTM_PL_FLAG_TT | flags;158if (type & TTM_PL_FLAG_SYSTEM)159pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;160}161162static void163set_placement_range(struct nouveau_bo *nvbo, uint32_t type)164{165struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);166int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;167168if (dev_priv->card_type == NV_10 &&169nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&170nvbo->bo.mem.num_pages < vram_pages / 2) {171/*172* Make sure that the color and depth buffers are handled173* by independent memory controller units. Up to a 9x174* speed up when alpha-blending and depth-test are enabled175* at the same time.176*/177if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {178nvbo->placement.fpfn = vram_pages / 2;179nvbo->placement.lpfn = ~0;180} else {181nvbo->placement.fpfn = 0;182nvbo->placement.lpfn = vram_pages / 2;183}184}185}186187void188nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)189{190struct ttm_placement *pl = &nvbo->placement;191uint32_t flags = TTM_PL_MASK_CACHING |192(nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);193194pl->placement = nvbo->placements;195set_placement_list(nvbo->placements, &pl->num_placement,196type, flags);197198pl->busy_placement = nvbo->busy_placements;199set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,200type | busy, flags);201202set_placement_range(nvbo, type);203}204205int206nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)207{208struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);209struct ttm_buffer_object *bo = &nvbo->bo;210int ret;211212if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {213NV_ERROR(nouveau_bdev(bo->bdev)->dev,214"bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,2151 << bo->mem.mem_type, memtype);216return -EINVAL;217}218219if (nvbo->pin_refcnt++)220return 0;221222ret = ttm_bo_reserve(bo, false, false, false, 0);223if (ret)224goto out;225226nouveau_bo_placement_set(nvbo, memtype, 0);227228ret = nouveau_bo_validate(nvbo, false, false, false);229if (ret == 0) {230switch (bo->mem.mem_type) {231case TTM_PL_VRAM:232dev_priv->fb_aper_free -= bo->mem.size;233break;234case TTM_PL_TT:235dev_priv->gart_info.aper_free -= bo->mem.size;236break;237default:238break;239}240}241ttm_bo_unreserve(bo);242out:243if (unlikely(ret))244nvbo->pin_refcnt--;245return ret;246}247248int249nouveau_bo_unpin(struct nouveau_bo *nvbo)250{251struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);252struct ttm_buffer_object *bo = &nvbo->bo;253int ret;254255if (--nvbo->pin_refcnt)256return 0;257258ret = ttm_bo_reserve(bo, false, false, false, 0);259if (ret)260return ret;261262nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);263264ret = nouveau_bo_validate(nvbo, false, false, false);265if (ret == 0) {266switch (bo->mem.mem_type) {267case TTM_PL_VRAM:268dev_priv->fb_aper_free += bo->mem.size;269break;270case TTM_PL_TT:271dev_priv->gart_info.aper_free += bo->mem.size;272break;273default:274break;275}276}277278ttm_bo_unreserve(bo);279return ret;280}281282int283nouveau_bo_map(struct nouveau_bo *nvbo)284{285int ret;286287ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);288if (ret)289return ret;290291ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);292ttm_bo_unreserve(&nvbo->bo);293return ret;294}295296void297nouveau_bo_unmap(struct nouveau_bo *nvbo)298{299if (nvbo)300ttm_bo_kunmap(&nvbo->kmap);301}302303int304nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,305bool no_wait_reserve, bool no_wait_gpu)306{307int ret;308309ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,310no_wait_reserve, no_wait_gpu);311if (ret)312return ret;313314if (nvbo->vma.node)315nvbo->bo.offset = nvbo->vma.offset;316return 0;317}318319u16320nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)321{322bool is_iomem;323u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);324mem = &mem[index];325if (is_iomem)326return ioread16_native((void __force __iomem *)mem);327else328return *mem;329}330331void332nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)333{334bool is_iomem;335u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);336mem = &mem[index];337if (is_iomem)338iowrite16_native(val, (void __force __iomem *)mem);339else340*mem = val;341}342343u32344nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)345{346bool is_iomem;347u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);348mem = &mem[index];349if (is_iomem)350return ioread32_native((void __force __iomem *)mem);351else352return *mem;353}354355void356nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)357{358bool is_iomem;359u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);360mem = &mem[index];361if (is_iomem)362iowrite32_native(val, (void __force __iomem *)mem);363else364*mem = val;365}366367static struct ttm_backend *368nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)369{370struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);371struct drm_device *dev = dev_priv->dev;372373switch (dev_priv->gart_info.type) {374#if __OS_HAS_AGP375case NOUVEAU_GART_AGP:376return ttm_agp_backend_init(bdev, dev->agp->bridge);377#endif378case NOUVEAU_GART_PDMA:379case NOUVEAU_GART_HW:380return nouveau_sgdma_init_ttm(dev);381default:382NV_ERROR(dev, "Unknown GART type %d\n",383dev_priv->gart_info.type);384break;385}386387return NULL;388}389390static int391nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)392{393/* We'll do this from user space. */394return 0;395}396397static int398nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,399struct ttm_mem_type_manager *man)400{401struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);402struct drm_device *dev = dev_priv->dev;403404switch (type) {405case TTM_PL_SYSTEM:406man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;407man->available_caching = TTM_PL_MASK_CACHING;408man->default_caching = TTM_PL_FLAG_CACHED;409break;410case TTM_PL_VRAM:411if (dev_priv->card_type >= NV_50) {412man->func = &nouveau_vram_manager;413man->io_reserve_fastpath = false;414man->use_io_reserve_lru = true;415} else {416man->func = &ttm_bo_manager_func;417}418man->flags = TTM_MEMTYPE_FLAG_FIXED |419TTM_MEMTYPE_FLAG_MAPPABLE;420man->available_caching = TTM_PL_FLAG_UNCACHED |421TTM_PL_FLAG_WC;422man->default_caching = TTM_PL_FLAG_WC;423break;424case TTM_PL_TT:425if (dev_priv->card_type >= NV_50)426man->func = &nouveau_gart_manager;427else428man->func = &ttm_bo_manager_func;429switch (dev_priv->gart_info.type) {430case NOUVEAU_GART_AGP:431man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;432man->available_caching = TTM_PL_FLAG_UNCACHED |433TTM_PL_FLAG_WC;434man->default_caching = TTM_PL_FLAG_WC;435break;436case NOUVEAU_GART_PDMA:437case NOUVEAU_GART_HW:438man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |439TTM_MEMTYPE_FLAG_CMA;440man->available_caching = TTM_PL_MASK_CACHING;441man->default_caching = TTM_PL_FLAG_CACHED;442man->gpu_offset = dev_priv->gart_info.aper_base;443break;444default:445NV_ERROR(dev, "Unknown GART type: %d\n",446dev_priv->gart_info.type);447return -EINVAL;448}449break;450default:451NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);452return -EINVAL;453}454return 0;455}456457static void458nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)459{460struct nouveau_bo *nvbo = nouveau_bo(bo);461462switch (bo->mem.mem_type) {463case TTM_PL_VRAM:464nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,465TTM_PL_FLAG_SYSTEM);466break;467default:468nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);469break;470}471472*pl = nvbo->placement;473}474475476/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access477* TTM_PL_{VRAM,TT} directly.478*/479480static int481nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,482struct nouveau_bo *nvbo, bool evict,483bool no_wait_reserve, bool no_wait_gpu,484struct ttm_mem_reg *new_mem)485{486struct nouveau_fence *fence = NULL;487int ret;488489ret = nouveau_fence_new(chan, &fence, true);490if (ret)491return ret;492493ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,494no_wait_reserve, no_wait_gpu, new_mem);495nouveau_fence_unref(&fence);496return ret;497}498499static int500nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,501struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)502{503struct nouveau_mem *old_node = old_mem->mm_node;504struct nouveau_mem *new_node = new_mem->mm_node;505struct nouveau_bo *nvbo = nouveau_bo(bo);506u32 page_count = new_mem->num_pages;507u64 src_offset, dst_offset;508int ret;509510src_offset = old_node->tmp_vma.offset;511if (new_node->tmp_vma.node)512dst_offset = new_node->tmp_vma.offset;513else514dst_offset = nvbo->vma.offset;515516page_count = new_mem->num_pages;517while (page_count) {518int line_count = (page_count > 2047) ? 2047 : page_count;519520ret = RING_SPACE(chan, 12);521if (ret)522return ret;523524BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0238, 2);525OUT_RING (chan, upper_32_bits(dst_offset));526OUT_RING (chan, lower_32_bits(dst_offset));527BEGIN_NVC0(chan, 2, NvSubM2MF, 0x030c, 6);528OUT_RING (chan, upper_32_bits(src_offset));529OUT_RING (chan, lower_32_bits(src_offset));530OUT_RING (chan, PAGE_SIZE); /* src_pitch */531OUT_RING (chan, PAGE_SIZE); /* dst_pitch */532OUT_RING (chan, PAGE_SIZE); /* line_length */533OUT_RING (chan, line_count);534BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0300, 1);535OUT_RING (chan, 0x00100110);536537page_count -= line_count;538src_offset += (PAGE_SIZE * line_count);539dst_offset += (PAGE_SIZE * line_count);540}541542return 0;543}544545static int546nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,547struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)548{549struct nouveau_mem *old_node = old_mem->mm_node;550struct nouveau_mem *new_node = new_mem->mm_node;551struct nouveau_bo *nvbo = nouveau_bo(bo);552u64 length = (new_mem->num_pages << PAGE_SHIFT);553u64 src_offset, dst_offset;554int ret;555556src_offset = old_node->tmp_vma.offset;557if (new_node->tmp_vma.node)558dst_offset = new_node->tmp_vma.offset;559else560dst_offset = nvbo->vma.offset;561562while (length) {563u32 amount, stride, height;564565amount = min(length, (u64)(4 * 1024 * 1024));566stride = 16 * 4;567height = amount / stride;568569if (new_mem->mem_type == TTM_PL_VRAM &&570nouveau_bo_tile_layout(nvbo)) {571ret = RING_SPACE(chan, 8);572if (ret)573return ret;574575BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);576OUT_RING (chan, 0);577OUT_RING (chan, 0);578OUT_RING (chan, stride);579OUT_RING (chan, height);580OUT_RING (chan, 1);581OUT_RING (chan, 0);582OUT_RING (chan, 0);583} else {584ret = RING_SPACE(chan, 2);585if (ret)586return ret;587588BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);589OUT_RING (chan, 1);590}591if (old_mem->mem_type == TTM_PL_VRAM &&592nouveau_bo_tile_layout(nvbo)) {593ret = RING_SPACE(chan, 8);594if (ret)595return ret;596597BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);598OUT_RING (chan, 0);599OUT_RING (chan, 0);600OUT_RING (chan, stride);601OUT_RING (chan, height);602OUT_RING (chan, 1);603OUT_RING (chan, 0);604OUT_RING (chan, 0);605} else {606ret = RING_SPACE(chan, 2);607if (ret)608return ret;609610BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);611OUT_RING (chan, 1);612}613614ret = RING_SPACE(chan, 14);615if (ret)616return ret;617618BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);619OUT_RING (chan, upper_32_bits(src_offset));620OUT_RING (chan, upper_32_bits(dst_offset));621BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);622OUT_RING (chan, lower_32_bits(src_offset));623OUT_RING (chan, lower_32_bits(dst_offset));624OUT_RING (chan, stride);625OUT_RING (chan, stride);626OUT_RING (chan, stride);627OUT_RING (chan, height);628OUT_RING (chan, 0x00000101);629OUT_RING (chan, 0x00000000);630BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);631OUT_RING (chan, 0);632633length -= amount;634src_offset += amount;635dst_offset += amount;636}637638return 0;639}640641static inline uint32_t642nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,643struct nouveau_channel *chan, struct ttm_mem_reg *mem)644{645if (mem->mem_type == TTM_PL_TT)646return chan->gart_handle;647return chan->vram_handle;648}649650static int651nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,652struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)653{654u32 src_offset = old_mem->start << PAGE_SHIFT;655u32 dst_offset = new_mem->start << PAGE_SHIFT;656u32 page_count = new_mem->num_pages;657int ret;658659ret = RING_SPACE(chan, 3);660if (ret)661return ret;662663BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);664OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));665OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));666667page_count = new_mem->num_pages;668while (page_count) {669int line_count = (page_count > 2047) ? 2047 : page_count;670671ret = RING_SPACE(chan, 11);672if (ret)673return ret;674675BEGIN_RING(chan, NvSubM2MF,676NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);677OUT_RING (chan, src_offset);678OUT_RING (chan, dst_offset);679OUT_RING (chan, PAGE_SIZE); /* src_pitch */680OUT_RING (chan, PAGE_SIZE); /* dst_pitch */681OUT_RING (chan, PAGE_SIZE); /* line_length */682OUT_RING (chan, line_count);683OUT_RING (chan, 0x00000101);684OUT_RING (chan, 0x00000000);685BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);686OUT_RING (chan, 0);687688page_count -= line_count;689src_offset += (PAGE_SIZE * line_count);690dst_offset += (PAGE_SIZE * line_count);691}692693return 0;694}695696static int697nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,698bool no_wait_reserve, bool no_wait_gpu,699struct ttm_mem_reg *new_mem)700{701struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);702struct nouveau_bo *nvbo = nouveau_bo(bo);703struct ttm_mem_reg *old_mem = &bo->mem;704struct nouveau_channel *chan;705int ret;706707chan = nvbo->channel;708if (!chan) {709chan = dev_priv->channel;710mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);711}712713/* create temporary vma for old memory, this will get cleaned714* up after ttm destroys the ttm_mem_reg715*/716if (dev_priv->card_type >= NV_50) {717struct nouveau_mem *node = old_mem->mm_node;718if (!node->tmp_vma.node) {719u32 page_shift = nvbo->vma.node->type;720if (old_mem->mem_type == TTM_PL_TT)721page_shift = nvbo->vma.vm->spg_shift;722723ret = nouveau_vm_get(chan->vm,724old_mem->num_pages << PAGE_SHIFT,725page_shift, NV_MEM_ACCESS_RO,726&node->tmp_vma);727if (ret)728goto out;729}730731if (old_mem->mem_type == TTM_PL_VRAM)732nouveau_vm_map(&node->tmp_vma, node);733else {734nouveau_vm_map_sg(&node->tmp_vma, 0,735old_mem->num_pages << PAGE_SHIFT,736node, node->pages);737}738}739740if (dev_priv->card_type < NV_50)741ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);742else743if (dev_priv->card_type < NV_C0)744ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);745else746ret = nvc0_bo_move_m2mf(chan, bo, &bo->mem, new_mem);747if (ret == 0) {748ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,749no_wait_reserve,750no_wait_gpu, new_mem);751}752753out:754if (chan == dev_priv->channel)755mutex_unlock(&chan->mutex);756return ret;757}758759static int760nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,761bool no_wait_reserve, bool no_wait_gpu,762struct ttm_mem_reg *new_mem)763{764struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);765u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;766struct ttm_placement placement;767struct ttm_mem_reg tmp_mem;768int ret;769770placement.fpfn = placement.lpfn = 0;771placement.num_placement = placement.num_busy_placement = 1;772placement.placement = placement.busy_placement = &placement_memtype;773774tmp_mem = *new_mem;775tmp_mem.mm_node = NULL;776ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);777if (ret)778return ret;779780ret = ttm_tt_bind(bo->ttm, &tmp_mem);781if (ret)782goto out;783784if (dev_priv->card_type >= NV_50) {785struct nouveau_bo *nvbo = nouveau_bo(bo);786struct nouveau_mem *node = tmp_mem.mm_node;787struct nouveau_vma *vma = &nvbo->vma;788if (vma->node->type != vma->vm->spg_shift)789vma = &node->tmp_vma;790nouveau_vm_map_sg(vma, 0, tmp_mem.num_pages << PAGE_SHIFT,791node, node->pages);792}793794ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);795796if (dev_priv->card_type >= NV_50) {797struct nouveau_bo *nvbo = nouveau_bo(bo);798nouveau_vm_unmap(&nvbo->vma);799}800801if (ret)802goto out;803804ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);805out:806ttm_bo_mem_put(bo, &tmp_mem);807return ret;808}809810static int811nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,812bool no_wait_reserve, bool no_wait_gpu,813struct ttm_mem_reg *new_mem)814{815u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;816struct ttm_placement placement;817struct ttm_mem_reg tmp_mem;818int ret;819820placement.fpfn = placement.lpfn = 0;821placement.num_placement = placement.num_busy_placement = 1;822placement.placement = placement.busy_placement = &placement_memtype;823824tmp_mem = *new_mem;825tmp_mem.mm_node = NULL;826ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);827if (ret)828return ret;829830ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);831if (ret)832goto out;833834ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);835if (ret)836goto out;837838out:839ttm_bo_mem_put(bo, &tmp_mem);840return ret;841}842843static void844nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)845{846struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);847struct nouveau_mem *node = new_mem->mm_node;848struct nouveau_bo *nvbo = nouveau_bo(bo);849struct nouveau_vma *vma = &nvbo->vma;850struct nouveau_vm *vm = vma->vm;851852if (dev_priv->card_type < NV_50)853return;854855switch (new_mem->mem_type) {856case TTM_PL_VRAM:857nouveau_vm_map(vma, node);858break;859case TTM_PL_TT:860if (vma->node->type != vm->spg_shift) {861nouveau_vm_unmap(vma);862vma = &node->tmp_vma;863}864nouveau_vm_map_sg(vma, 0, new_mem->num_pages << PAGE_SHIFT,865node, node->pages);866break;867default:868nouveau_vm_unmap(&nvbo->vma);869break;870}871}872873static int874nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,875struct nouveau_tile_reg **new_tile)876{877struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);878struct drm_device *dev = dev_priv->dev;879struct nouveau_bo *nvbo = nouveau_bo(bo);880u64 offset = new_mem->start << PAGE_SHIFT;881882*new_tile = NULL;883if (new_mem->mem_type != TTM_PL_VRAM)884return 0;885886if (dev_priv->card_type >= NV_10) {887*new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,888nvbo->tile_mode,889nvbo->tile_flags);890}891892return 0;893}894895static void896nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,897struct nouveau_tile_reg *new_tile,898struct nouveau_tile_reg **old_tile)899{900struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);901struct drm_device *dev = dev_priv->dev;902903nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);904*old_tile = new_tile;905}906907static int908nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,909bool no_wait_reserve, bool no_wait_gpu,910struct ttm_mem_reg *new_mem)911{912struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);913struct nouveau_bo *nvbo = nouveau_bo(bo);914struct ttm_mem_reg *old_mem = &bo->mem;915struct nouveau_tile_reg *new_tile = NULL;916int ret = 0;917918if (dev_priv->card_type < NV_50) {919ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);920if (ret)921return ret;922}923924/* Fake bo copy. */925if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {926BUG_ON(bo->mem.mm_node != NULL);927bo->mem = *new_mem;928new_mem->mm_node = NULL;929goto out;930}931932/* Software copy if the card isn't up and running yet. */933if (!dev_priv->channel) {934ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);935goto out;936}937938/* Hardware assisted copy. */939if (new_mem->mem_type == TTM_PL_SYSTEM)940ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);941else if (old_mem->mem_type == TTM_PL_SYSTEM)942ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);943else944ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);945946if (!ret)947goto out;948949/* Fallback to software copy. */950ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);951952out:953if (dev_priv->card_type < NV_50) {954if (ret)955nouveau_bo_vm_cleanup(bo, NULL, &new_tile);956else957nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);958}959960return ret;961}962963static int964nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)965{966return 0;967}968969static int970nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)971{972struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];973struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);974struct drm_device *dev = dev_priv->dev;975int ret;976977mem->bus.addr = NULL;978mem->bus.offset = 0;979mem->bus.size = mem->num_pages << PAGE_SHIFT;980mem->bus.base = 0;981mem->bus.is_iomem = false;982if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))983return -EINVAL;984switch (mem->mem_type) {985case TTM_PL_SYSTEM:986/* System memory */987return 0;988case TTM_PL_TT:989#if __OS_HAS_AGP990if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {991mem->bus.offset = mem->start << PAGE_SHIFT;992mem->bus.base = dev_priv->gart_info.aper_base;993mem->bus.is_iomem = true;994}995#endif996break;997case TTM_PL_VRAM:998{999struct nouveau_mem *node = mem->mm_node;1000u8 page_shift;10011002if (!dev_priv->bar1_vm) {1003mem->bus.offset = mem->start << PAGE_SHIFT;1004mem->bus.base = pci_resource_start(dev->pdev, 1);1005mem->bus.is_iomem = true;1006break;1007}10081009if (dev_priv->card_type == NV_C0)1010page_shift = node->page_shift;1011else1012page_shift = 12;10131014ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size,1015page_shift, NV_MEM_ACCESS_RW,1016&node->bar_vma);1017if (ret)1018return ret;10191020nouveau_vm_map(&node->bar_vma, node);1021if (ret) {1022nouveau_vm_put(&node->bar_vma);1023return ret;1024}10251026mem->bus.offset = node->bar_vma.offset;1027if (dev_priv->card_type == NV_50) /*XXX*/1028mem->bus.offset -= 0x0020000000ULL;1029mem->bus.base = pci_resource_start(dev->pdev, 1);1030mem->bus.is_iomem = true;1031}1032break;1033default:1034return -EINVAL;1035}1036return 0;1037}10381039static void1040nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)1041{1042struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);1043struct nouveau_mem *node = mem->mm_node;10441045if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)1046return;10471048if (!node->bar_vma.node)1049return;10501051nouveau_vm_unmap(&node->bar_vma);1052nouveau_vm_put(&node->bar_vma);1053}10541055static int1056nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)1057{1058struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);1059struct nouveau_bo *nvbo = nouveau_bo(bo);10601061/* as long as the bo isn't in vram, and isn't tiled, we've got1062* nothing to do here.1063*/1064if (bo->mem.mem_type != TTM_PL_VRAM) {1065if (dev_priv->card_type < NV_50 ||1066!nouveau_bo_tile_layout(nvbo))1067return 0;1068}10691070/* make sure bo is in mappable vram */1071if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)1072return 0;107310741075nvbo->placement.fpfn = 0;1076nvbo->placement.lpfn = dev_priv->fb_mappable_pages;1077nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);1078return nouveau_bo_validate(nvbo, false, true, false);1079}10801081void1082nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)1083{1084struct nouveau_fence *old_fence;10851086if (likely(fence))1087nouveau_fence_ref(fence);10881089spin_lock(&nvbo->bo.bdev->fence_lock);1090old_fence = nvbo->bo.sync_obj;1091nvbo->bo.sync_obj = fence;1092spin_unlock(&nvbo->bo.bdev->fence_lock);10931094nouveau_fence_unref(&old_fence);1095}10961097struct ttm_bo_driver nouveau_bo_driver = {1098.create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,1099.invalidate_caches = nouveau_bo_invalidate_caches,1100.init_mem_type = nouveau_bo_init_mem_type,1101.evict_flags = nouveau_bo_evict_flags,1102.move_notify = nouveau_bo_move_ntfy,1103.move = nouveau_bo_move,1104.verify_access = nouveau_bo_verify_access,1105.sync_obj_signaled = __nouveau_fence_signalled,1106.sync_obj_wait = __nouveau_fence_wait,1107.sync_obj_flush = __nouveau_fence_flush,1108.sync_obj_unref = __nouveau_fence_unref,1109.sync_obj_ref = __nouveau_fence_ref,1110.fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,1111.io_mem_reserve = &nouveau_ttm_io_mem_reserve,1112.io_mem_free = &nouveau_ttm_io_mem_free,1113};1114111511161117