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_ram *ram = nvkm_fb(object)->ram;
88 	ram->func->put(ram, &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_ram *ram = nvkm_fb(parent)->ram;
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 = ram->func->get(ram, args->size, args->align, 0, 0x800,
111 			     &node->mem);
112 	if (ret)
113 		return ret;
114 
115 	node->base.addr = node->mem->offset;
116 	node->base.size = node->mem->size << 12;
117 	node->mem->page_shift = 12;
118 	return 0;
119 }
120 
121 static struct nvkm_instobj_impl
122 nv50_instobj_oclass = {
123 	.base.ofuncs = &(struct nvkm_ofuncs) {
124 		.ctor = nv50_instobj_ctor,
125 		.dtor = nv50_instobj_dtor,
126 		.init = _nvkm_instobj_init,
127 		.fini = _nvkm_instobj_fini,
128 		.rd32 = nv50_instobj_rd32,
129 		.wr32 = nv50_instobj_wr32,
130 	},
131 };
132 
133 /******************************************************************************
134  * instmem subdev implementation
135  *****************************************************************************/
136 
137 static int
138 nv50_instmem_fini(struct nvkm_object *object, bool suspend)
139 {
140 	struct nv50_instmem *imem = (void *)object;
141 	imem->addr = ~0ULL;
142 	return nvkm_instmem_fini(&imem->base, suspend);
143 }
144 
145 static int
146 nv50_instmem_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
147 		  struct nvkm_oclass *oclass, void *data, u32 size,
148 		  struct nvkm_object **pobject)
149 {
150 	struct nv50_instmem *imem;
151 	int ret;
152 
153 	ret = nvkm_instmem_create(parent, engine, oclass, &imem);
154 	*pobject = nv_object(imem);
155 	if (ret)
156 		return ret;
157 
158 	spin_lock_init(&imem->lock);
159 	return 0;
160 }
161 
162 struct nvkm_oclass *
163 nv50_instmem_oclass = &(struct nvkm_instmem_impl) {
164 	.base.handle = NV_SUBDEV(INSTMEM, 0x50),
165 	.base.ofuncs = &(struct nvkm_ofuncs) {
166 		.ctor = nv50_instmem_ctor,
167 		.dtor = _nvkm_instmem_dtor,
168 		.init = _nvkm_instmem_init,
169 		.fini = nv50_instmem_fini,
170 	},
171 	.instobj = &nv50_instobj_oclass.base,
172 }.base;
173