nv50.c 5.0 KB
Newer Older
1
/*
2
 * Copyright 2012 Red Hat Inc.
3
 *
4 5 6 7 8 9
 * 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:
10
 *
11 12
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
13
 *
14 15 16 17 18 19 20
 * 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.
21
 *
22
 * Authors: Ben Skeggs
23
 */
24
#include "priv.h"
25

26
#include <subdev/fb.h>
27

B
Ben Skeggs 已提交
28
struct nv50_instmem {
29
	struct nvkm_instmem base;
30 31
	spinlock_t lock;
	u64 addr;
32 33
};

B
Ben Skeggs 已提交
34
struct nv50_instobj {
35 36
	struct nvkm_instobj base;
	struct nvkm_mem *mem;
37
};
38

39 40 41
/******************************************************************************
 * instmem object implementation
 *****************************************************************************/
42

43
static u32
44
nv50_instobj_rd32(struct nvkm_object *object, u64 offset)
45
{
B
Ben Skeggs 已提交
46 47
	struct nv50_instmem *imem = (void *)nvkm_instmem(object);
	struct nv50_instobj *node = (void *)object;
48
	struct nvkm_device *device = imem->base.subdev.device;
49 50 51 52 53
	unsigned long flags;
	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
	u32 data;

B
Ben Skeggs 已提交
54 55
	spin_lock_irqsave(&imem->lock, flags);
	if (unlikely(imem->addr != base)) {
56
		nvkm_wr32(device, 0x001700, base >> 16);
B
Ben Skeggs 已提交
57
		imem->addr = base;
58
	}
59
	data = nvkm_rd32(device, 0x700000 + addr);
B
Ben Skeggs 已提交
60
	spin_unlock_irqrestore(&imem->lock, flags);
61
	return data;
62 63
}

64
static void
65
nv50_instobj_wr32(struct nvkm_object *object, u64 offset, u32 data)
66
{
B
Ben Skeggs 已提交
67 68
	struct nv50_instmem *imem = (void *)nvkm_instmem(object);
	struct nv50_instobj *node = (void *)object;
69
	struct nvkm_device *device = imem->base.subdev.device;
70 71 72
	unsigned long flags;
	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
73

B
Ben Skeggs 已提交
74 75
	spin_lock_irqsave(&imem->lock, flags);
	if (unlikely(imem->addr != base)) {
76
		nvkm_wr32(device, 0x001700, base >> 16);
B
Ben Skeggs 已提交
77
		imem->addr = base;
78
	}
79
	nvkm_wr32(device, 0x700000 + addr, data);
B
Ben Skeggs 已提交
80
	spin_unlock_irqrestore(&imem->lock, flags);
81 82
}

83
static void
84
nv50_instobj_dtor(struct nvkm_object *object)
85
{
B
Ben Skeggs 已提交
86
	struct nv50_instobj *node = (void *)object;
B
Ben Skeggs 已提交
87 88
	struct nvkm_fb *fb = nvkm_fb(object);
	fb->ram->put(fb, &node->mem);
89
	nvkm_instobj_destroy(&node->base);
90 91 92
}

static int
93 94 95
nv50_instobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, void *data, u32 size,
		  struct nvkm_object **pobject)
96
{
B
Ben Skeggs 已提交
97
	struct nvkm_fb *fb = nvkm_fb(parent);
98
	struct nvkm_instobj_args *args = data;
B
Ben Skeggs 已提交
99
	struct nv50_instobj *node;
100 101 102 103 104
	int ret;

	args->size  = max((args->size  + 4095) & ~4095, (u32)4096);
	args->align = max((args->align + 4095) & ~4095, (u32)4096);

105
	ret = nvkm_instobj_create(parent, engine, oclass, &node);
106 107 108 109
	*pobject = nv_object(node);
	if (ret)
		return ret;

B
Ben Skeggs 已提交
110
	ret = fb->ram->get(fb, args->size, args->align, 0, 0x800, &node->mem);
111 112 113 114 115 116 117 118 119
	if (ret)
		return ret;

	node->base.addr = node->mem->offset;
	node->base.size = node->mem->size << 12;
	node->mem->page_shift = 12;
	return 0;
}

120
static struct nvkm_instobj_impl
121
nv50_instobj_oclass = {
122
	.base.ofuncs = &(struct nvkm_ofuncs) {
123 124
		.ctor = nv50_instobj_ctor,
		.dtor = nv50_instobj_dtor,
125 126
		.init = _nvkm_instobj_init,
		.fini = _nvkm_instobj_fini,
127 128 129
		.rd32 = nv50_instobj_rd32,
		.wr32 = nv50_instobj_wr32,
	},
130 131
};

132 133 134 135
/******************************************************************************
 * instmem subdev implementation
 *****************************************************************************/

136
static int
137
nv50_instmem_fini(struct nvkm_object *object, bool suspend)
138
{
B
Ben Skeggs 已提交
139 140 141
	struct nv50_instmem *imem = (void *)object;
	imem->addr = ~0ULL;
	return nvkm_instmem_fini(&imem->base, suspend);
142 143
}

144
static int
145 146 147
nv50_instmem_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, void *data, u32 size,
		  struct nvkm_object **pobject)
148
{
B
Ben Skeggs 已提交
149
	struct nv50_instmem *imem;
150
	int ret;
151

B
Ben Skeggs 已提交
152 153
	ret = nvkm_instmem_create(parent, engine, oclass, &imem);
	*pobject = nv_object(imem);
154 155
	if (ret)
		return ret;
156

B
Ben Skeggs 已提交
157
	spin_lock_init(&imem->lock);
158 159 160
	return 0;
}

161 162
struct nvkm_oclass *
nv50_instmem_oclass = &(struct nvkm_instmem_impl) {
163
	.base.handle = NV_SUBDEV(INSTMEM, 0x50),
164
	.base.ofuncs = &(struct nvkm_ofuncs) {
165
		.ctor = nv50_instmem_ctor,
166 167
		.dtor = _nvkm_instmem_dtor,
		.init = _nvkm_instmem_init,
168 169
		.fini = nv50_instmem_fini,
	},
170
	.instobj = &nv50_instobj_oclass.base,
171
}.base;