2018-07-26 21:59:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* 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, sublicense,
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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 NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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 USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "nouveau_dmem.h"
|
|
|
|
#include "nouveau_drv.h"
|
|
|
|
#include "nouveau_chan.h"
|
|
|
|
#include "nouveau_dma.h"
|
|
|
|
#include "nouveau_mem.h"
|
|
|
|
#include "nouveau_bo.h"
|
2020-03-04 00:13:39 +00:00
|
|
|
#include "nouveau_svm.h"
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
#include <nvif/class.h>
|
|
|
|
#include <nvif/object.h>
|
2020-03-16 19:32:15 +00:00
|
|
|
#include <nvif/if000c.h>
|
2018-07-26 21:59:13 +00:00
|
|
|
#include <nvif/if500b.h>
|
|
|
|
#include <nvif/if900b.h>
|
2020-03-04 00:13:39 +00:00
|
|
|
#include <nvif/if000c.h>
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
#include <linux/sched/mm.h>
|
|
|
|
#include <linux/hmm.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: this is ugly right now we are using TTM to allocate vram and we pin
|
|
|
|
* it in vram while in use. We likely want to overhaul memory management for
|
|
|
|
* nouveau to be more page like (not necessarily with system page size but a
|
|
|
|
* bigger page size) at lowest level and have some shim layer on top that would
|
|
|
|
* provide the same functionality as TTM.
|
|
|
|
*/
|
|
|
|
#define DMEM_CHUNK_SIZE (2UL << 20)
|
|
|
|
#define DMEM_CHUNK_NPAGES (DMEM_CHUNK_SIZE >> PAGE_SHIFT)
|
|
|
|
|
2019-02-15 00:35:05 +00:00
|
|
|
enum nouveau_aper {
|
|
|
|
NOUVEAU_APER_VIRT,
|
|
|
|
NOUVEAU_APER_VRAM,
|
|
|
|
NOUVEAU_APER_HOST,
|
|
|
|
};
|
|
|
|
|
2018-07-26 21:59:13 +00:00
|
|
|
typedef int (*nouveau_migrate_copy_t)(struct nouveau_drm *drm, u64 npages,
|
2019-02-15 00:35:05 +00:00
|
|
|
enum nouveau_aper, u64 dst_addr,
|
|
|
|
enum nouveau_aper, u64 src_addr);
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
struct nouveau_dmem_chunk {
|
|
|
|
struct list_head list;
|
|
|
|
struct nouveau_bo *bo;
|
|
|
|
struct nouveau_drm *drm;
|
|
|
|
unsigned long callocated;
|
2020-04-21 23:11:07 +00:00
|
|
|
struct dev_pagemap pagemap;
|
2018-07-26 21:59:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nouveau_dmem_migrate {
|
|
|
|
nouveau_migrate_copy_t copy_func;
|
|
|
|
struct nouveau_channel *chan;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nouveau_dmem {
|
2019-06-26 12:27:18 +00:00
|
|
|
struct nouveau_drm *drm;
|
2018-07-26 21:59:13 +00:00
|
|
|
struct nouveau_dmem_migrate migrate;
|
2020-04-21 23:11:07 +00:00
|
|
|
struct list_head chunks;
|
2018-07-26 21:59:13 +00:00
|
|
|
struct mutex mutex;
|
2020-04-21 23:11:07 +00:00
|
|
|
struct page *free_pages;
|
|
|
|
spinlock_t lock;
|
2018-07-26 21:59:13 +00:00
|
|
|
};
|
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
static struct nouveau_dmem_chunk *nouveau_page_to_chunk(struct page *page)
|
|
|
|
{
|
|
|
|
return container_of(page->pgmap, struct nouveau_dmem_chunk, pagemap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nouveau_drm *page_to_drm(struct page *page)
|
2019-06-26 12:27:18 +00:00
|
|
|
{
|
2020-04-21 23:11:07 +00:00
|
|
|
struct nouveau_dmem_chunk *chunk = nouveau_page_to_chunk(page);
|
|
|
|
|
|
|
|
return chunk->drm;
|
2019-06-26 12:27:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 07:59:21 +00:00
|
|
|
static unsigned long nouveau_dmem_page_addr(struct page *page)
|
|
|
|
{
|
2020-04-21 23:11:07 +00:00
|
|
|
struct nouveau_dmem_chunk *chunk = nouveau_page_to_chunk(page);
|
|
|
|
unsigned long off = (page_to_pfn(page) << PAGE_SHIFT) -
|
|
|
|
chunk->pagemap.res.start;
|
2019-08-14 07:59:21 +00:00
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
return chunk->bo->bo.offset + off;
|
2019-08-14 07:59:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:27:18 +00:00
|
|
|
static void nouveau_dmem_page_free(struct page *page)
|
2018-07-26 21:59:13 +00:00
|
|
|
{
|
2020-04-21 23:11:07 +00:00
|
|
|
struct nouveau_dmem_chunk *chunk = nouveau_page_to_chunk(page);
|
|
|
|
struct nouveau_dmem *dmem = chunk->drm->dmem;
|
|
|
|
|
|
|
|
spin_lock(&dmem->lock);
|
|
|
|
page->zone_device_data = dmem->free_pages;
|
|
|
|
dmem->free_pages = page;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
WARN_ON(!chunk->callocated);
|
|
|
|
chunk->callocated--;
|
|
|
|
/*
|
|
|
|
* FIXME when chunk->callocated reach 0 we should add the chunk to
|
|
|
|
* a reclaim list so that it can be freed in case of memory pressure.
|
|
|
|
*/
|
2020-04-21 23:11:07 +00:00
|
|
|
spin_unlock(&dmem->lock);
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 07:59:22 +00:00
|
|
|
static void nouveau_dmem_fence_done(struct nouveau_fence **fence)
|
|
|
|
{
|
|
|
|
if (fence) {
|
|
|
|
nouveau_fence_wait(*fence, true, false);
|
|
|
|
nouveau_fence_unref(fence);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* FIXME wait for channel to be IDLE before calling finalizing
|
|
|
|
* the hmem object.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
static vm_fault_t nouveau_dmem_fault_copy_one(struct nouveau_drm *drm,
|
|
|
|
struct vm_fault *vmf, struct migrate_vma *args,
|
|
|
|
dma_addr_t *dma_addr)
|
2018-07-26 21:59:13 +00:00
|
|
|
{
|
2019-02-15 05:50:16 +00:00
|
|
|
struct device *dev = drm->dev->dev;
|
2019-08-14 07:59:24 +00:00
|
|
|
struct page *dpage, *spage;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
spage = migrate_pfn_to_page(args->src[0]);
|
|
|
|
if (!spage || !(args->src[0] & MIGRATE_PFN_MIGRATE))
|
|
|
|
return 0;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
dpage = alloc_page_vma(GFP_HIGHUSER, vmf->vma, vmf->address);
|
|
|
|
if (!dpage)
|
|
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
lock_page(dpage);
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
*dma_addr = dma_map_page(dev, dpage, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
|
|
if (dma_mapping_error(dev, *dma_addr))
|
|
|
|
goto error_free_page;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
if (drm->dmem->migrate.copy_func(drm, 1, NOUVEAU_APER_HOST, *dma_addr,
|
|
|
|
NOUVEAU_APER_VRAM, nouveau_dmem_page_addr(spage)))
|
|
|
|
goto error_dma_unmap;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
args->dst[0] = migrate_pfn(page_to_pfn(dpage)) | MIGRATE_PFN_LOCKED;
|
|
|
|
return 0;
|
2019-02-15 05:50:16 +00:00
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
error_dma_unmap:
|
|
|
|
dma_unmap_page(dev, *dma_addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
|
|
error_free_page:
|
|
|
|
__free_page(dpage);
|
|
|
|
return VM_FAULT_SIGBUS;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:27:18 +00:00
|
|
|
static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf)
|
2018-07-26 21:59:13 +00:00
|
|
|
{
|
2020-04-21 23:11:07 +00:00
|
|
|
struct nouveau_drm *drm = page_to_drm(vmf->page);
|
|
|
|
struct nouveau_dmem *dmem = drm->dmem;
|
2019-08-14 07:59:24 +00:00
|
|
|
struct nouveau_fence *fence;
|
|
|
|
unsigned long src = 0, dst = 0;
|
|
|
|
dma_addr_t dma_addr = 0;
|
|
|
|
vm_fault_t ret;
|
2019-08-14 07:59:19 +00:00
|
|
|
struct migrate_vma args = {
|
|
|
|
.vma = vmf->vma,
|
|
|
|
.start = vmf->address,
|
|
|
|
.end = vmf->address + PAGE_SIZE,
|
2019-08-14 07:59:24 +00:00
|
|
|
.src = &src,
|
|
|
|
.dst = &dst,
|
2020-03-16 19:32:14 +00:00
|
|
|
.src_owner = drm->dev,
|
2019-08-14 07:59:19 +00:00
|
|
|
};
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME what we really want is to find some heuristic to migrate more
|
|
|
|
* than just one page on CPU fault. When such fault happens it is very
|
|
|
|
* likely that more surrounding page will CPU fault too.
|
|
|
|
*/
|
2019-08-14 07:59:19 +00:00
|
|
|
if (migrate_vma_setup(&args) < 0)
|
2018-07-26 21:59:13 +00:00
|
|
|
return VM_FAULT_SIGBUS;
|
2019-08-14 07:59:19 +00:00
|
|
|
if (!args.cpages)
|
|
|
|
return 0;
|
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
ret = nouveau_dmem_fault_copy_one(drm, vmf, &args, &dma_addr);
|
|
|
|
if (ret || dst == 0)
|
|
|
|
goto done;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:24 +00:00
|
|
|
nouveau_fence_new(dmem->migrate.chan, false, &fence);
|
|
|
|
migrate_vma_pages(&args);
|
|
|
|
nouveau_dmem_fence_done(&fence);
|
|
|
|
dma_unmap_page(drm->dev->dev, dma_addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
|
|
done:
|
2019-08-14 07:59:19 +00:00
|
|
|
migrate_vma_finalize(&args);
|
2019-08-14 07:59:24 +00:00
|
|
|
return ret;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:27:18 +00:00
|
|
|
static const struct dev_pagemap_ops nouveau_dmem_pagemap_ops = {
|
|
|
|
.page_free = nouveau_dmem_page_free,
|
|
|
|
.migrate_to_ram = nouveau_dmem_migrate_to_ram,
|
2018-07-26 21:59:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2020-04-21 23:11:07 +00:00
|
|
|
nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage)
|
2018-07-26 21:59:13 +00:00
|
|
|
{
|
|
|
|
struct nouveau_dmem_chunk *chunk;
|
2020-04-21 23:11:07 +00:00
|
|
|
struct resource *res;
|
|
|
|
struct page *page;
|
|
|
|
void *ptr;
|
|
|
|
unsigned long i, pfn_first;
|
2018-07-26 21:59:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
chunk = kzalloc(sizeof(*chunk), GFP_KERNEL);
|
2018-07-26 21:59:13 +00:00
|
|
|
if (chunk == NULL) {
|
2020-04-21 23:11:07 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
/* Allocate unused physical address space for device private pages. */
|
|
|
|
res = request_free_mem_region(&iomem_resource, DMEM_CHUNK_SIZE,
|
|
|
|
"nouveau_dmem");
|
|
|
|
if (IS_ERR(res)) {
|
|
|
|
ret = PTR_ERR(res);
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk->drm = drm;
|
|
|
|
chunk->pagemap.type = MEMORY_DEVICE_PRIVATE;
|
|
|
|
chunk->pagemap.res = *res;
|
|
|
|
chunk->pagemap.ops = &nouveau_dmem_pagemap_ops;
|
|
|
|
chunk->pagemap.owner = drm->dev;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
ret = nouveau_bo_new(&drm->client, DMEM_CHUNK_SIZE, 0,
|
|
|
|
TTM_PL_FLAG_VRAM, 0, 0, NULL, NULL,
|
|
|
|
&chunk->bo);
|
|
|
|
if (ret)
|
2020-04-21 23:11:07 +00:00
|
|
|
goto out_release;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
|
2020-04-21 23:11:07 +00:00
|
|
|
if (ret)
|
|
|
|
goto out_bo_free;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
ptr = memremap_pages(&chunk->pagemap, numa_node_id());
|
|
|
|
if (IS_ERR(ptr)) {
|
|
|
|
ret = PTR_ERR(ptr);
|
|
|
|
goto out_bo_unpin;
|
|
|
|
}
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
mutex_lock(&drm->dmem->mutex);
|
2020-04-21 23:11:07 +00:00
|
|
|
list_add(&chunk->list, &drm->dmem->chunks);
|
2018-07-26 21:59:13 +00:00
|
|
|
mutex_unlock(&drm->dmem->mutex);
|
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
pfn_first = chunk->pagemap.res.start >> PAGE_SHIFT;
|
|
|
|
page = pfn_to_page(pfn_first);
|
|
|
|
spin_lock(&drm->dmem->lock);
|
|
|
|
for (i = 0; i < DMEM_CHUNK_NPAGES - 1; ++i, ++page) {
|
|
|
|
page->zone_device_data = drm->dmem->free_pages;
|
|
|
|
drm->dmem->free_pages = page;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
2020-04-21 23:11:07 +00:00
|
|
|
*ppage = page;
|
|
|
|
chunk->callocated++;
|
|
|
|
spin_unlock(&drm->dmem->lock);
|
|
|
|
|
|
|
|
NV_INFO(drm, "DMEM: registered %ldMB of device memory\n",
|
|
|
|
DMEM_CHUNK_SIZE >> 20);
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-04-21 23:11:07 +00:00
|
|
|
|
|
|
|
out_bo_unpin:
|
|
|
|
nouveau_bo_unpin(chunk->bo);
|
|
|
|
out_bo_free:
|
|
|
|
nouveau_bo_ref(NULL, &chunk->bo);
|
|
|
|
out_release:
|
|
|
|
release_mem_region(chunk->pagemap.res.start,
|
|
|
|
resource_size(&chunk->pagemap.res));
|
|
|
|
out_free:
|
|
|
|
kfree(chunk);
|
|
|
|
out:
|
|
|
|
return ret;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct page *
|
|
|
|
nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm)
|
|
|
|
{
|
2020-04-21 23:11:07 +00:00
|
|
|
struct nouveau_dmem_chunk *chunk;
|
|
|
|
struct page *page = NULL;
|
2018-07-26 21:59:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
spin_lock(&drm->dmem->lock);
|
|
|
|
if (drm->dmem->free_pages) {
|
|
|
|
page = drm->dmem->free_pages;
|
|
|
|
drm->dmem->free_pages = page->zone_device_data;
|
|
|
|
chunk = nouveau_page_to_chunk(page);
|
|
|
|
chunk->callocated++;
|
|
|
|
spin_unlock(&drm->dmem->lock);
|
|
|
|
} else {
|
|
|
|
spin_unlock(&drm->dmem->lock);
|
|
|
|
ret = nouveau_dmem_chunk_alloc(drm, &page);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
get_page(page);
|
|
|
|
lock_page(page);
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nouveau_dmem_page_free_locked(struct nouveau_drm *drm, struct page *page)
|
|
|
|
{
|
|
|
|
unlock_page(page);
|
|
|
|
put_page(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nouveau_dmem_resume(struct nouveau_drm *drm)
|
|
|
|
{
|
|
|
|
struct nouveau_dmem_chunk *chunk;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (drm->dmem == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&drm->dmem->mutex);
|
2020-04-21 23:11:07 +00:00
|
|
|
list_for_each_entry(chunk, &drm->dmem->chunks, list) {
|
2018-07-26 21:59:13 +00:00
|
|
|
ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
|
|
|
|
/* FIXME handle pin failure */
|
|
|
|
WARN_ON(ret);
|
|
|
|
}
|
|
|
|
mutex_unlock(&drm->dmem->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nouveau_dmem_suspend(struct nouveau_drm *drm)
|
|
|
|
{
|
|
|
|
struct nouveau_dmem_chunk *chunk;
|
|
|
|
|
|
|
|
if (drm->dmem == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&drm->dmem->mutex);
|
2020-04-21 23:11:07 +00:00
|
|
|
list_for_each_entry(chunk, &drm->dmem->chunks, list)
|
2018-07-26 21:59:13 +00:00
|
|
|
nouveau_bo_unpin(chunk->bo);
|
|
|
|
mutex_unlock(&drm->dmem->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nouveau_dmem_fini(struct nouveau_drm *drm)
|
|
|
|
{
|
|
|
|
struct nouveau_dmem_chunk *chunk, *tmp;
|
|
|
|
|
|
|
|
if (drm->dmem == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&drm->dmem->mutex);
|
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
list_for_each_entry_safe(chunk, tmp, &drm->dmem->chunks, list) {
|
|
|
|
nouveau_bo_unpin(chunk->bo);
|
|
|
|
nouveau_bo_ref(NULL, &chunk->bo);
|
2018-07-26 21:59:13 +00:00
|
|
|
list_del(&chunk->list);
|
2020-04-21 23:11:07 +00:00
|
|
|
memunmap_pages(&chunk->pagemap);
|
|
|
|
release_mem_region(chunk->pagemap.res.start,
|
|
|
|
resource_size(&chunk->pagemap.res));
|
2018-07-26 21:59:13 +00:00
|
|
|
kfree(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&drm->dmem->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nvc0b5_migrate_copy(struct nouveau_drm *drm, u64 npages,
|
2019-02-15 00:35:05 +00:00
|
|
|
enum nouveau_aper dst_aper, u64 dst_addr,
|
|
|
|
enum nouveau_aper src_aper, u64 src_addr)
|
2018-07-26 21:59:13 +00:00
|
|
|
{
|
|
|
|
struct nouveau_channel *chan = drm->dmem->migrate.chan;
|
2019-02-15 00:35:05 +00:00
|
|
|
u32 launch_dma = (1 << 9) /* MULTI_LINE_ENABLE. */ |
|
|
|
|
(1 << 8) /* DST_MEMORY_LAYOUT_PITCH. */ |
|
|
|
|
(1 << 7) /* SRC_MEMORY_LAYOUT_PITCH. */ |
|
|
|
|
(1 << 2) /* FLUSH_ENABLE_TRUE. */ |
|
|
|
|
(2 << 0) /* DATA_TRANSFER_TYPE_NON_PIPELINED. */;
|
2018-07-26 21:59:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-02-15 00:35:05 +00:00
|
|
|
ret = RING_SPACE(chan, 13);
|
2018-07-26 21:59:13 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-02-15 00:35:05 +00:00
|
|
|
if (src_aper != NOUVEAU_APER_VIRT) {
|
|
|
|
switch (src_aper) {
|
|
|
|
case NOUVEAU_APER_VRAM:
|
|
|
|
BEGIN_IMC0(chan, NvSubCopy, 0x0260, 0);
|
|
|
|
break;
|
|
|
|
case NOUVEAU_APER_HOST:
|
|
|
|
BEGIN_IMC0(chan, NvSubCopy, 0x0260, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
launch_dma |= 0x00001000; /* SRC_TYPE_PHYSICAL. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst_aper != NOUVEAU_APER_VIRT) {
|
|
|
|
switch (dst_aper) {
|
|
|
|
case NOUVEAU_APER_VRAM:
|
|
|
|
BEGIN_IMC0(chan, NvSubCopy, 0x0264, 0);
|
|
|
|
break;
|
|
|
|
case NOUVEAU_APER_HOST:
|
|
|
|
BEGIN_IMC0(chan, NvSubCopy, 0x0264, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
launch_dma |= 0x00002000; /* DST_TYPE_PHYSICAL. */
|
|
|
|
}
|
|
|
|
|
2018-07-26 21:59:13 +00:00
|
|
|
BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
|
|
|
|
OUT_RING (chan, upper_32_bits(src_addr));
|
|
|
|
OUT_RING (chan, lower_32_bits(src_addr));
|
|
|
|
OUT_RING (chan, upper_32_bits(dst_addr));
|
|
|
|
OUT_RING (chan, lower_32_bits(dst_addr));
|
|
|
|
OUT_RING (chan, PAGE_SIZE);
|
|
|
|
OUT_RING (chan, PAGE_SIZE);
|
|
|
|
OUT_RING (chan, PAGE_SIZE);
|
|
|
|
OUT_RING (chan, npages);
|
2019-02-15 00:35:05 +00:00
|
|
|
BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
|
|
|
|
OUT_RING (chan, launch_dma);
|
2018-07-26 21:59:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nouveau_dmem_migrate_init(struct nouveau_drm *drm)
|
|
|
|
{
|
|
|
|
switch (drm->ttm.copy.oclass) {
|
|
|
|
case PASCAL_DMA_COPY_A:
|
|
|
|
case PASCAL_DMA_COPY_B:
|
|
|
|
case VOLTA_DMA_COPY_A:
|
|
|
|
case TURING_DMA_COPY_A:
|
|
|
|
drm->dmem->migrate.copy_func = nvc0b5_migrate_copy;
|
|
|
|
drm->dmem->migrate.chan = drm->ttm.chan;
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nouveau_dmem_init(struct nouveau_drm *drm)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* This only make sense on PASCAL or newer */
|
|
|
|
if (drm->client.device.info.family < NV_DEVICE_INFO_V0_PASCAL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(drm->dmem = kzalloc(sizeof(*drm->dmem), GFP_KERNEL)))
|
|
|
|
return;
|
|
|
|
|
2019-06-26 12:27:18 +00:00
|
|
|
drm->dmem->drm = drm;
|
2018-07-26 21:59:13 +00:00
|
|
|
mutex_init(&drm->dmem->mutex);
|
2020-04-21 23:11:07 +00:00
|
|
|
INIT_LIST_HEAD(&drm->dmem->chunks);
|
|
|
|
mutex_init(&drm->dmem->mutex);
|
|
|
|
spin_lock_init(&drm->dmem->lock);
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
/* Initialize migration dma helpers before registering memory */
|
|
|
|
ret = nouveau_dmem_migrate_init(drm);
|
2020-04-21 23:11:07 +00:00
|
|
|
if (ret) {
|
|
|
|
kfree(drm->dmem);
|
|
|
|
drm->dmem = NULL;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm,
|
2020-03-04 00:13:39 +00:00
|
|
|
unsigned long src, dma_addr_t *dma_addr, u64 *pfn)
|
2018-07-26 21:59:13 +00:00
|
|
|
{
|
2019-02-15 05:50:16 +00:00
|
|
|
struct device *dev = drm->dev->dev;
|
2019-08-14 07:59:25 +00:00
|
|
|
struct page *dpage, *spage;
|
2020-03-04 00:13:39 +00:00
|
|
|
unsigned long paddr;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
spage = migrate_pfn_to_page(src);
|
|
|
|
if (!spage || !(src & MIGRATE_PFN_MIGRATE))
|
|
|
|
goto out;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
dpage = nouveau_dmem_page_alloc_locked(drm);
|
|
|
|
if (!dpage)
|
2020-03-04 00:13:39 +00:00
|
|
|
goto out;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
*dma_addr = dma_map_page(dev, spage, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
|
|
if (dma_mapping_error(dev, *dma_addr))
|
|
|
|
goto out_free_page;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2020-03-04 00:13:39 +00:00
|
|
|
paddr = nouveau_dmem_page_addr(dpage);
|
2019-08-14 07:59:25 +00:00
|
|
|
if (drm->dmem->migrate.copy_func(drm, 1, NOUVEAU_APER_VRAM,
|
2020-03-04 00:13:39 +00:00
|
|
|
paddr, NOUVEAU_APER_HOST, *dma_addr))
|
2019-08-14 07:59:25 +00:00
|
|
|
goto out_dma_unmap;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2020-03-04 00:13:39 +00:00
|
|
|
*pfn = NVIF_VMM_PFNMAP_V0_V | NVIF_VMM_PFNMAP_V0_VRAM |
|
|
|
|
((paddr >> PAGE_SHIFT) << NVIF_VMM_PFNMAP_V0_ADDR_SHIFT);
|
|
|
|
if (src & MIGRATE_PFN_WRITE)
|
|
|
|
*pfn |= NVIF_VMM_PFNMAP_V0_W;
|
2019-08-14 07:59:27 +00:00
|
|
|
return migrate_pfn(page_to_pfn(dpage)) | MIGRATE_PFN_LOCKED;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
out_dma_unmap:
|
|
|
|
dma_unmap_page(dev, *dma_addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
|
|
out_free_page:
|
|
|
|
nouveau_dmem_page_free_locked(drm, dpage);
|
|
|
|
out:
|
2020-03-04 00:13:39 +00:00
|
|
|
*pfn = NVIF_VMM_PFNMAP_V0_NONE;
|
2019-08-14 07:59:25 +00:00
|
|
|
return 0;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm,
|
2020-03-04 00:13:39 +00:00
|
|
|
struct nouveau_svmm *svmm, struct migrate_vma *args,
|
|
|
|
dma_addr_t *dma_addrs, u64 *pfns)
|
2018-07-26 21:59:13 +00:00
|
|
|
{
|
2019-08-14 07:59:25 +00:00
|
|
|
struct nouveau_fence *fence;
|
|
|
|
unsigned long addr = args->start, nr_dma = 0, i;
|
|
|
|
|
|
|
|
for (i = 0; addr < args->end; i++) {
|
|
|
|
args->dst[i] = nouveau_dmem_migrate_copy_one(drm, args->src[i],
|
2020-03-04 00:13:39 +00:00
|
|
|
dma_addrs + nr_dma, pfns + i);
|
2019-08-14 07:59:25 +00:00
|
|
|
if (args->dst[i])
|
|
|
|
nr_dma++;
|
|
|
|
addr += PAGE_SIZE;
|
|
|
|
}
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
nouveau_fence_new(drm->dmem->migrate.chan, false, &fence);
|
|
|
|
migrate_vma_pages(args);
|
|
|
|
nouveau_dmem_fence_done(&fence);
|
2020-03-04 00:13:39 +00:00
|
|
|
nouveau_pfns_map(svmm, args->vma->vm_mm, args->start, pfns, i);
|
2019-02-15 05:50:16 +00:00
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
while (nr_dma--) {
|
|
|
|
dma_unmap_page(drm->dev->dev, dma_addrs[nr_dma], PAGE_SIZE,
|
|
|
|
DMA_BIDIRECTIONAL);
|
2019-02-15 05:50:16 +00:00
|
|
|
}
|
2019-08-14 07:59:19 +00:00
|
|
|
migrate_vma_finalize(args);
|
|
|
|
}
|
2018-07-26 21:59:13 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
nouveau_dmem_migrate_vma(struct nouveau_drm *drm,
|
2020-03-04 00:13:39 +00:00
|
|
|
struct nouveau_svmm *svmm,
|
2018-07-26 21:59:13 +00:00
|
|
|
struct vm_area_struct *vma,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
2019-08-14 07:59:19 +00:00
|
|
|
unsigned long npages = (end - start) >> PAGE_SHIFT;
|
|
|
|
unsigned long max = min(SG_MAX_SINGLE_ALLOC, npages);
|
2019-08-14 07:59:25 +00:00
|
|
|
dma_addr_t *dma_addrs;
|
2019-08-14 07:59:19 +00:00
|
|
|
struct migrate_vma args = {
|
|
|
|
.vma = vma,
|
|
|
|
.start = start,
|
|
|
|
};
|
2020-03-04 00:13:39 +00:00
|
|
|
unsigned long i;
|
|
|
|
u64 *pfns;
|
2019-08-14 07:59:19 +00:00
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
2020-04-21 23:11:07 +00:00
|
|
|
if (drm->dmem == NULL)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2019-11-29 16:28:28 +00:00
|
|
|
args.src = kcalloc(max, sizeof(*args.src), GFP_KERNEL);
|
2019-08-14 07:59:19 +00:00
|
|
|
if (!args.src)
|
|
|
|
goto out;
|
2019-11-29 16:28:28 +00:00
|
|
|
args.dst = kcalloc(max, sizeof(*args.dst), GFP_KERNEL);
|
2019-08-14 07:59:19 +00:00
|
|
|
if (!args.dst)
|
|
|
|
goto out_free_src;
|
2018-07-26 21:59:13 +00:00
|
|
|
|
2019-08-14 07:59:25 +00:00
|
|
|
dma_addrs = kmalloc_array(max, sizeof(*dma_addrs), GFP_KERNEL);
|
|
|
|
if (!dma_addrs)
|
|
|
|
goto out_free_dst;
|
|
|
|
|
2020-03-04 00:13:39 +00:00
|
|
|
pfns = nouveau_pfns_alloc(max);
|
|
|
|
if (!pfns)
|
|
|
|
goto out_free_dma;
|
|
|
|
|
|
|
|
for (i = 0; i < npages; i += max) {
|
|
|
|
args.end = start + (max << PAGE_SHIFT);
|
2019-08-14 07:59:19 +00:00
|
|
|
ret = migrate_vma_setup(&args);
|
2018-07-26 21:59:13 +00:00
|
|
|
if (ret)
|
2020-03-04 00:13:39 +00:00
|
|
|
goto out_free_pfns;
|
2019-08-14 07:59:19 +00:00
|
|
|
|
|
|
|
if (args.cpages)
|
2020-03-04 00:13:39 +00:00
|
|
|
nouveau_dmem_migrate_chunk(drm, svmm, &args, dma_addrs,
|
|
|
|
pfns);
|
2019-08-14 07:59:19 +00:00
|
|
|
args.start = args.end;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 07:59:19 +00:00
|
|
|
ret = 0;
|
2020-03-04 00:13:39 +00:00
|
|
|
out_free_pfns:
|
|
|
|
nouveau_pfns_free(pfns);
|
2019-08-14 07:59:25 +00:00
|
|
|
out_free_dma:
|
|
|
|
kfree(dma_addrs);
|
2019-08-14 07:59:19 +00:00
|
|
|
out_free_dst:
|
|
|
|
kfree(args.dst);
|
|
|
|
out_free_src:
|
|
|
|
kfree(args.src);
|
2018-07-26 21:59:13 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nouveau_dmem_convert_pfn(struct nouveau_drm *drm,
|
|
|
|
struct hmm_range *range)
|
|
|
|
{
|
|
|
|
unsigned long i, npages;
|
|
|
|
|
|
|
|
npages = (range->end - range->start) >> PAGE_SHIFT;
|
|
|
|
for (i = 0; i < npages; ++i) {
|
|
|
|
struct page *page;
|
|
|
|
uint64_t addr;
|
|
|
|
|
2019-07-24 06:52:57 +00:00
|
|
|
page = hmm_device_entry_to_page(range, range->pfns[i]);
|
2018-07-26 21:59:13 +00:00
|
|
|
if (page == NULL)
|
|
|
|
continue;
|
|
|
|
|
2020-03-16 19:32:15 +00:00
|
|
|
if (!is_device_private_page(page))
|
2018-07-26 21:59:13 +00:00
|
|
|
continue;
|
|
|
|
|
2019-08-14 07:59:21 +00:00
|
|
|
addr = nouveau_dmem_page_addr(page);
|
2018-07-26 21:59:13 +00:00
|
|
|
range->pfns[i] &= ((1UL << range->pfn_shift) - 1);
|
|
|
|
range->pfns[i] |= (addr >> PAGE_SHIFT) << range->pfn_shift;
|
2020-03-16 19:32:15 +00:00
|
|
|
range->pfns[i] |= NVIF_VMM_PFNMAP_V0_VRAM;
|
2018-07-26 21:59:13 +00:00
|
|
|
}
|
|
|
|
}
|