nv50_vm.c 4.63 KB
Newer Older
Ben Skeggs's avatar
Ben Skeggs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright 2010 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.
 *
 * Authors: Ben Skeggs
 */

#include "drmP.h"

#include "nouveau_drv.h"
#include "nouveau_vm.h"

void
31 32
nv50_vm_map_pgt(struct nouveau_gpuobj *pgd, u32 pde,
		struct nouveau_gpuobj *pgt[2])
Ben Skeggs's avatar
Ben Skeggs committed
33
{
34 35
	u64 phys = 0xdeadcafe00000000ULL;
	u32 coverage = 0;
Ben Skeggs's avatar
Ben Skeggs committed
36

37 38 39 40 41 42 43
	if (pgt[0]) {
		phys = 0x00000003 | pgt[0]->vinst; /* present, 4KiB pages */
		coverage = (pgt[0]->size >> 3) << 12;
	} else
	if (pgt[1]) {
		phys = 0x00000001 | pgt[1]->vinst; /* present */
		coverage = (pgt[1]->size >> 3) << 16;
Ben Skeggs's avatar
Ben Skeggs committed
44 45
	}

46 47 48 49 50 51 52 53
	if (phys & 1) {
		if (coverage <= 32 * 1024 * 1024)
			phys |= 0x60;
		else if (coverage <= 64 * 1024 * 1024)
			phys |= 0x40;
		else if (coverage < 128 * 1024 * 1024)
			phys |= 0x20;
	}
Ben Skeggs's avatar
Ben Skeggs committed
54 55 56 57 58 59

	nv_wo32(pgd, (pde * 8) + 0, lower_32_bits(phys));
	nv_wo32(pgd, (pde * 8) + 4, upper_32_bits(phys));
}

static inline u64
60
nv50_vm_addr(struct nouveau_vma *vma, u64 phys, u32 memtype, u32 target)
Ben Skeggs's avatar
Ben Skeggs committed
61
{
62
	struct drm_nouveau_private *dev_priv = vma->vm->dev->dev_private;
Ben Skeggs's avatar
Ben Skeggs committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

	phys |= 1; /* present */
	phys |= (u64)memtype << 40;

	/* IGPs don't have real VRAM, re-target to stolen system memory */
	if (target == 0 && dev_priv->vram_sys_base) {
		phys  += dev_priv->vram_sys_base;
		target = 3;
	}

	phys |= target << 4;

	if (vma->access & NV_MEM_ACCESS_SYS)
		phys |= (1 << 6);

	if (!(vma->access & NV_MEM_ACCESS_WO))
		phys |= (1 << 3);

	return phys;
}

void
nv50_vm_map(struct nouveau_vma *vma, struct nouveau_gpuobj *pgt,
86
	    struct nouveau_mem *mem, u32 pte, u32 cnt, u64 phys, u64 delta)
Ben Skeggs's avatar
Ben Skeggs committed
87
{
88
	u32 comp = (mem->memtype & 0x180) >> 7;
89 90
	u32 block;
	int i;
Ben Skeggs's avatar
Ben Skeggs committed
91

92
	phys  = nv50_vm_addr(vma, phys, mem->memtype, 0);
Ben Skeggs's avatar
Ben Skeggs committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	pte <<= 3;
	cnt <<= 3;

	while (cnt) {
		u32 offset_h = upper_32_bits(phys);
		u32 offset_l = lower_32_bits(phys);

		for (i = 7; i >= 0; i--) {
			block = 1 << (i + 3);
			if (cnt >= block && !(pte & (block - 1)))
				break;
		}
		offset_l |= (i << 7);

		phys += block << (vma->node->type - 3);
		cnt  -= block;
109 110 111 112 113
		if (comp) {
			u32 tag = mem->tag->start + ((delta >> 16) * comp);
			offset_h |= (tag << 17);
			delta    += block << (vma->node->type - 3);
		}
Ben Skeggs's avatar
Ben Skeggs committed
114 115 116 117 118 119 120 121 122 123 124 125

		while (block) {
			nv_wo32(pgt, pte + 0, offset_l);
			nv_wo32(pgt, pte + 4, offset_h);
			pte += 8;
			block -= 8;
		}
	}
}

void
nv50_vm_map_sg(struct nouveau_vma *vma, struct nouveau_gpuobj *pgt,
126
	       struct nouveau_mem *mem, u32 pte, u32 cnt, dma_addr_t *list)
Ben Skeggs's avatar
Ben Skeggs committed
127 128 129
{
	pte <<= 3;
	while (cnt--) {
130
		u64 phys = nv50_vm_addr(vma, (u64)*list++, mem->memtype, 2);
Ben Skeggs's avatar
Ben Skeggs committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		nv_wo32(pgt, pte + 0, lower_32_bits(phys));
		nv_wo32(pgt, pte + 4, upper_32_bits(phys));
		pte += 8;
	}
}

void
nv50_vm_unmap(struct nouveau_gpuobj *pgt, u32 pte, u32 cnt)
{
	pte <<= 3;
	while (cnt--) {
		nv_wo32(pgt, pte + 0, 0x00000000);
		nv_wo32(pgt, pte + 4, 0x00000000);
		pte += 8;
	}
}

void
nv50_vm_flush(struct nouveau_vm *vm)
{
	struct drm_nouveau_private *dev_priv = vm->dev->dev_private;
	struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
153
	struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
154
	int i;
Ben Skeggs's avatar
Ben Skeggs committed
155 156

	pinstmem->flush(vm->dev);
157 158 159 160 161 162 163 164

	/* BAR */
	if (vm != dev_priv->chan_vm) {
		nv50_vm_flush_engine(vm->dev, 6);
		return;
	}

	pfifo->tlb_flush(vm->dev);
165 166 167 168
	for (i = 0; i < NVOBJ_ENGINE_NR; i++) {
		if (atomic_read(&vm->engref[i]))
			dev_priv->eng[i]->tlb_flush(vm->dev, i);
	}
Ben Skeggs's avatar
Ben Skeggs committed
169 170 171 172 173
}

void
nv50_vm_flush_engine(struct drm_device *dev, int engine)
{
174
	struct drm_nouveau_private *dev_priv = dev->dev_private;
175
	unsigned long flags;
176

177
	spin_lock_irqsave(&dev_priv->vm_lock, flags);
Ben Skeggs's avatar
Ben Skeggs committed
178 179 180
	nv_wr32(dev, 0x100c80, (engine << 16) | 1);
	if (!nv_wait(dev, 0x100c80, 0x00000001, 0x00000000))
		NV_ERROR(dev, "vm flush timeout: engine %d\n", engine);
181
	spin_unlock_irqrestore(&dev_priv->vm_lock, flags);
Ben Skeggs's avatar
Ben Skeggs committed
182
}