nv04.c 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2012 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
 */
24
#include "priv.h"
25 26 27

#include <core/gpuobj.h>
#include <subdev/fb.h>
28
#include <subdev/mmu/nv04.h>
29

30
#include <nvif/class.h>
31

B
Ben Skeggs 已提交
32
struct nv04_dmaobj {
33
	struct nvkm_dmaobj base;
34 35 36 37 38
	bool clone;
	u32 flags0;
	u32 flags2;
};

39
static int
B
Ben Skeggs 已提交
40
nv04_dmaobj_bind(struct nvkm_dmaobj *obj, struct nvkm_object *parent,
41
		 struct nvkm_gpuobj **pgpuobj)
42
{
B
Ben Skeggs 已提交
43
	struct nv04_dmaobj *dmaobj = container_of(obj, typeof(*dmaobj), base);
44
	struct nvkm_gpuobj *gpuobj;
B
Ben Skeggs 已提交
45 46 47
	u64 offset = dmaobj->base.start & 0xfffff000;
	u64 adjust = dmaobj->base.start & 0x00000fff;
	u32 length = dmaobj->base.limit - dmaobj->base.start;
48 49
	int ret;

50 51
	if (!nv_iclass(parent, NV_ENGCTX_CLASS)) {
		switch (nv_mclass(parent->parent)) {
52 53 54 55
		case NV03_CHANNEL_DMA:
		case NV10_CHANNEL_DMA:
		case NV17_CHANNEL_DMA:
		case NV40_CHANNEL_DMA:
56 57 58 59 60 61
			break;
		default:
			return -EINVAL;
		}
	}

B
Ben Skeggs 已提交
62
	if (dmaobj->clone) {
B
Ben Skeggs 已提交
63
		struct nv04_mmu *mmu = nv04_mmu(dmaobj);
64
		struct nvkm_memory *pgt = mmu->vm->pgt[0].mem[0];
B
Ben Skeggs 已提交
65
		if (!dmaobj->base.start)
66
			return nvkm_gpuobj_dup(parent, pgt, pgpuobj);
67 68
		nvkm_kmap(pgt);
		offset  = nvkm_ro32(pgt, 8 + (offset >> 10));
69
		offset &= 0xfffff000;
70
		nvkm_done(pgt);
71 72
	}

73
	ret = nvkm_gpuobj_new(parent, parent, 16, 16, 0, &gpuobj);
74 75
	*pgpuobj = gpuobj;
	if (ret == 0) {
76 77 78 79 80 81
		nvkm_kmap(*pgpuobj);
		nvkm_wo32(*pgpuobj, 0x00, dmaobj->flags0 | (adjust << 20));
		nvkm_wo32(*pgpuobj, 0x04, length);
		nvkm_wo32(*pgpuobj, 0x08, dmaobj->flags2 | offset);
		nvkm_wo32(*pgpuobj, 0x0c, dmaobj->flags2 | offset);
		nvkm_done(*pgpuobj);
82 83 84 85 86 87
	}

	return ret;
}

static int
88 89 90
nv04_dmaobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		 struct nvkm_oclass *oclass, void *data, u32 size,
		 struct nvkm_object **pobject)
91
{
92
	struct nvkm_dmaeng *dmaeng = (void *)engine;
B
Ben Skeggs 已提交
93
	struct nv04_mmu *mmu = nv04_mmu(engine);
B
Ben Skeggs 已提交
94
	struct nv04_dmaobj *dmaobj;
95 96
	int ret;

B
Ben Skeggs 已提交
97 98
	ret = nvkm_dmaobj_create(parent, engine, oclass, &data, &size, &dmaobj);
	*pobject = nv_object(dmaobj);
99
	if (ret || (ret = -ENOSYS, size))
100
		return ret;
101

B
Ben Skeggs 已提交
102
	if (dmaobj->base.target == NV_MEM_TARGET_VM) {
103
		if (nv_object(mmu)->oclass == &nv04_mmu_oclass)
B
Ben Skeggs 已提交
104 105 106
			dmaobj->clone = true;
		dmaobj->base.target = NV_MEM_TARGET_PCI;
		dmaobj->base.access = NV_MEM_ACCESS_RW;
107 108
	}

B
Ben Skeggs 已提交
109 110
	dmaobj->flags0 = nv_mclass(dmaobj);
	switch (dmaobj->base.target) {
111
	case NV_MEM_TARGET_VRAM:
B
Ben Skeggs 已提交
112
		dmaobj->flags0 |= 0x00003000;
113 114
		break;
	case NV_MEM_TARGET_PCI:
B
Ben Skeggs 已提交
115
		dmaobj->flags0 |= 0x00023000;
116 117
		break;
	case NV_MEM_TARGET_PCI_NOSNOOP:
B
Ben Skeggs 已提交
118
		dmaobj->flags0 |= 0x00033000;
119 120 121 122 123
		break;
	default:
		return -EINVAL;
	}

B
Ben Skeggs 已提交
124
	switch (dmaobj->base.access) {
125
	case NV_MEM_ACCESS_RO:
B
Ben Skeggs 已提交
126
		dmaobj->flags0 |= 0x00004000;
127 128
		break;
	case NV_MEM_ACCESS_WO:
B
Ben Skeggs 已提交
129
		dmaobj->flags0 |= 0x00008000;
130
	case NV_MEM_ACCESS_RW:
B
Ben Skeggs 已提交
131
		dmaobj->flags2 |= 0x00000002;
132 133 134 135 136
		break;
	default:
		return -EINVAL;
	}

B
Ben Skeggs 已提交
137
	return dmaeng->bind(&dmaobj->base, nv_object(dmaobj), (void *)pobject);
138 139
}

140
static struct nvkm_ofuncs
141 142 143 144 145 146 147
nv04_dmaobj_ofuncs = {
	.ctor =  nv04_dmaobj_ctor,
	.dtor = _nvkm_dmaobj_dtor,
	.init = _nvkm_dmaobj_init,
	.fini = _nvkm_dmaobj_fini,
};

148
static struct nvkm_oclass
149
nv04_dmaeng_sclass[] = {
150 151 152
	{ NV_DMA_FROM_MEMORY, &nv04_dmaobj_ofuncs },
	{ NV_DMA_TO_MEMORY, &nv04_dmaobj_ofuncs },
	{ NV_DMA_IN_MEMORY, &nv04_dmaobj_ofuncs },
153 154 155
	{}
};

156
struct nvkm_oclass *
157 158
nv04_dmaeng_oclass = &(struct nvkm_dmaeng_impl) {
	.base.handle = NV_ENGINE(DMAOBJ, 0x04),
159
	.base.ofuncs = &(struct nvkm_ofuncs) {
160 161 162 163
		.ctor = _nvkm_dmaeng_ctor,
		.dtor = _nvkm_dmaeng_dtor,
		.init = _nvkm_dmaeng_init,
		.fini = _nvkm_dmaeng_fini,
164
	},
165
	.sclass = nv04_dmaeng_sclass,
166 167
	.bind = nv04_dmaobj_bind,
}.base;