1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 #include "priv.h"
25 
26 #include <subdev/fb.h>
27 
28 struct nv50_instmem {
29 	struct nvkm_instmem base;
30 	spinlock_t lock;
31 	u64 addr;
32 };
33 
34 struct nv50_instobj {
35 	struct nvkm_instobj base;
36 	struct nvkm_mem *mem;
37 };
38 
39 /******************************************************************************
40  * instmem object implementation
41  *****************************************************************************/
42 
43 static u32
44 nv50_instobj_rd32(struct nvkm_object *object, u64 offset)
45 {
46 	struct nv50_instmem *imem = (void *)nvkm_instmem(object);
47 	struct nv50_instobj *node = (void *)object;
48 	struct nvkm_device *device = imem->base.subdev.device;
49 	unsigned long flags;
50 	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
51 	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
52 	u32 data;
53 
54 	spin_lock_irqsave(&imem->lock, flags);
55 	if (unlikely(imem->addr != base)) {
56 		nvkm_wr32(device, 0x001700, base >> 16);
57 		imem->addr = base;
58 	}
59 	data = nvkm_rd32(device, 0x700000 + addr);
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 {
67 	struct nv50_instmem *imem = (void *)nvkm_instmem(object);
68 	struct nv50_instobj *node = (void *)object;
69 	struct nvkm_device *device = imem->base.subdev.device;
70 	unsigned long flags;
71 	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
72 	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
73 
74 	spin_lock_irqsave(&imem->lock, flags);
75 	if (unlikely(imem->addr != base)) {
76 		nvkm_wr32(device, 0x001700, base >> 16);
77 		imem->addr = base;
78 	}
79 	nvkm_wr32(device, 0x700000 + addr, data);
80 	spin_unlock_irqrestore(&imem->lock, flags);
81 }
82 
83 static void
84 nv50_instobj_dtor(struct nvkm_object *object)
85 {
86 	struct nv50_instobj *node = (void *)object;
87 	struct nvkm_fb *fb = nvkm_fb(object);
88 	fb->ram->put(fb, &node->mem);
89 	nvkm_instobj_destroy(&node->base);
90 }
91 
92 static int
93 nv50_instobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
94 		  struct nvkm_oclass *oclass, void *data, u32 size,
95 		  struct nvkm_object **pobject)
96 {
97 	struct nvkm_fb *fb = nvkm_fb(parent);
98 	struct nvkm_instobj_args *args = data;
99 	struct nv50_instobj *node;
100 	int ret;
101 
102 	args->size  = max((args->size  + 4095) & ~4095, (u32)4096);
103 	args->align = max((args->align + 4095) & ~4095, (u32)4096);
104 
105 	ret = nvkm_instobj_create(parent, engine, oclass, &node);
106 	*pobject = nv_object(node);
107 	if (ret)
108 		return ret;
109 
110 	ret = fb->ram->get(fb, args->size, args->align, 0, 0x800, &node->mem);
111 	if (ret)
112 		return ret;
113 
114 	node->base.addr = node->mem->offset;
115 	node->base.size = node->mem->size << 12;
116 	node->mem->page_shift = 12;
117 	return 0;
118 }
119 
120 static struct nvkm_instobj_impl
121 nv50_instobj_oclass = {
122 	.base.ofuncs = &(struct nvkm_ofuncs) {
123 		.ctor = nv50_instobj_ctor,
124 		.dtor = nv50_instobj_dtor,
125 		.init = _nvkm_instobj_init,
126 		.fini = _nvkm_instobj_fini,
127 		.rd32 = nv50_instobj_rd32,
128 		.wr32 = nv50_instobj_wr32,
129 	},
130 };
131 
132 /******************************************************************************
133  * instmem subdev implementation
134  *****************************************************************************/
135 
136 static int
137 nv50_instmem_fini(struct nvkm_object *object, bool suspend)
138 {
139 	struct nv50_instmem *imem = (void *)object;
140 	imem->addr = ~0ULL;
141 	return nvkm_instmem_fini(&imem->base, suspend);
142 }
143 
144 static int
145 nv50_instmem_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
146 		  struct nvkm_oclass *oclass, void *data, u32 size,
147 		  struct nvkm_object **pobject)
148 {
149 	struct nv50_instmem *imem;
150 	int ret;
151 
152 	ret = nvkm_instmem_create(parent, engine, oclass, &imem);
153 	*pobject = nv_object(imem);
154 	if (ret)
155 		return ret;
156 
157 	spin_lock_init(&imem->lock);
158 	return 0;
159 }
160 
161 struct nvkm_oclass *
162 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 		.dtor = _nvkm_instmem_dtor,
167 		.init = _nvkm_instmem_init,
168 		.fini = nv50_instmem_fini,
169 	},
170 	.instobj = &nv50_instobj_oclass.base,
171 }.base;
172