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 "gf100.h"
25 
26 #include <core/memory.h>
27 #include <core/option.h>
28 #include <subdev/fb.h>
29 #include <subdev/mmu.h>
30 
31 struct nvkm_vmm *
32 gf100_bar_bar1_vmm(struct nvkm_bar *base)
33 {
34 	return gf100_bar(base)->bar[1].vm;
35 }
36 
37 void
38 gf100_bar_bar1_wait(struct nvkm_bar *base)
39 {
40 	/* NFI why it's twice. */
41 	nvkm_bar_flush(base);
42 	nvkm_bar_flush(base);
43 }
44 
45 void
46 gf100_bar_bar1_fini(struct nvkm_bar *bar)
47 {
48 	nvkm_mask(bar->subdev.device, 0x001704, 0x80000000, 0x00000000);
49 }
50 
51 void
52 gf100_bar_bar1_init(struct nvkm_bar *base)
53 {
54 	struct nvkm_device *device = base->subdev.device;
55 	struct gf100_bar *bar = gf100_bar(base);
56 	const u32 addr = nvkm_memory_addr(bar->bar[1].inst) >> 12;
57 	nvkm_wr32(device, 0x001704, 0x80000000 | addr);
58 }
59 
60 struct nvkm_vmm *
61 gf100_bar_bar2_vmm(struct nvkm_bar *base)
62 {
63 	return gf100_bar(base)->bar[0].vm;
64 }
65 
66 void
67 gf100_bar_bar2_fini(struct nvkm_bar *bar)
68 {
69 	nvkm_mask(bar->subdev.device, 0x001714, 0x80000000, 0x00000000);
70 }
71 
72 void
73 gf100_bar_bar2_init(struct nvkm_bar *base)
74 {
75 	struct nvkm_device *device = base->subdev.device;
76 	struct gf100_bar *bar = gf100_bar(base);
77 	u32 addr = nvkm_memory_addr(bar->bar[0].inst) >> 12;
78 	if (bar->bar2_halve)
79 		addr |= 0x40000000;
80 	nvkm_wr32(device, 0x001714, 0x80000000 | addr);
81 }
82 
83 static int
84 gf100_bar_oneinit_bar(struct gf100_bar *bar, struct gf100_barN *bar_vm,
85 		      struct lock_class_key *key, int bar_nr)
86 {
87 	struct nvkm_device *device = bar->base.subdev.device;
88 	struct nvkm_vm *vm;
89 	resource_size_t bar_len;
90 	int ret;
91 
92 	ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x1000, 0, false,
93 			      &bar_vm->inst);
94 	if (ret)
95 		return ret;
96 
97 	bar_len = device->func->resource_size(device, bar_nr);
98 	if (bar_nr == 3 && bar->bar2_halve)
99 		bar_len >>= 1;
100 
101 	ret = nvkm_vm_new(device, 0, bar_len, 0, key, &vm);
102 	if (ret)
103 		return ret;
104 
105 	atomic_inc(&vm->engref[NVKM_SUBDEV_BAR]);
106 
107 	/*
108 	 * Bootstrap page table lookup.
109 	 */
110 	if (bar_nr == 3) {
111 		ret = nvkm_vm_boot(vm, bar_len);
112 		if (ret) {
113 			nvkm_vm_ref(NULL, &vm, NULL);
114 			return ret;
115 		}
116 	}
117 
118 	ret = nvkm_vm_ref(vm, &bar_vm->vm, bar_vm->inst);
119 	nvkm_vm_ref(NULL, &vm, NULL);
120 	if (ret)
121 		return ret;
122 
123 	return 0;
124 }
125 
126 int
127 gf100_bar_oneinit(struct nvkm_bar *base)
128 {
129 	static struct lock_class_key bar1_lock;
130 	static struct lock_class_key bar2_lock;
131 	struct gf100_bar *bar = gf100_bar(base);
132 	int ret;
133 
134 	/* BAR2 */
135 	if (bar->base.func->bar2.init) {
136 		ret = gf100_bar_oneinit_bar(bar, &bar->bar[0], &bar2_lock, 3);
137 		if (ret)
138 			return ret;
139 
140 		bar->base.subdev.oneinit = true;
141 		nvkm_bar_bar2_init(bar->base.subdev.device);
142 	}
143 
144 	/* BAR1 */
145 	ret = gf100_bar_oneinit_bar(bar, &bar->bar[1], &bar1_lock, 1);
146 	if (ret)
147 		return ret;
148 
149 	return 0;
150 }
151 
152 void *
153 gf100_bar_dtor(struct nvkm_bar *base)
154 {
155 	struct gf100_bar *bar = gf100_bar(base);
156 
157 	nvkm_vm_ref(NULL, &bar->bar[1].vm, bar->bar[1].inst);
158 	nvkm_memory_unref(&bar->bar[1].inst);
159 
160 	nvkm_vm_ref(NULL, &bar->bar[0].vm, bar->bar[0].inst);
161 	nvkm_memory_unref(&bar->bar[0].inst);
162 	return bar;
163 }
164 
165 int
166 gf100_bar_new_(const struct nvkm_bar_func *func, struct nvkm_device *device,
167 	       int index, struct nvkm_bar **pbar)
168 {
169 	struct gf100_bar *bar;
170 	if (!(bar = kzalloc(sizeof(*bar), GFP_KERNEL)))
171 		return -ENOMEM;
172 	nvkm_bar_ctor(func, device, index, &bar->base);
173 	bar->bar2_halve = nvkm_boolopt(device->cfgopt, "NvBar2Halve", false);
174 	*pbar = &bar->base;
175 	return 0;
176 }
177 
178 static const struct nvkm_bar_func
179 gf100_bar_func = {
180 	.dtor = gf100_bar_dtor,
181 	.oneinit = gf100_bar_oneinit,
182 	.bar1.init = gf100_bar_bar1_init,
183 	.bar1.fini = gf100_bar_bar1_fini,
184 	.bar1.wait = gf100_bar_bar1_wait,
185 	.bar1.vmm = gf100_bar_bar1_vmm,
186 	.bar2.init = gf100_bar_bar2_init,
187 	.bar2.fini = gf100_bar_bar2_fini,
188 	.bar2.wait = gf100_bar_bar1_wait,
189 	.bar2.vmm = gf100_bar_bar2_vmm,
190 	.flush = g84_bar_flush,
191 };
192 
193 int
194 gf100_bar_new(struct nvkm_device *device, int index, struct nvkm_bar **pbar)
195 {
196 	return gf100_bar_new_(&gf100_bar_func, device, index, pbar);
197 }
198