xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/subdev/bar/gf100.c (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
1245dcfe9SBen Skeggs /*
2245dcfe9SBen Skeggs  * Copyright 2012 Red Hat Inc.
3245dcfe9SBen Skeggs  *
4245dcfe9SBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5245dcfe9SBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6245dcfe9SBen Skeggs  * to deal in the Software without restriction, including without limitation
7245dcfe9SBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8245dcfe9SBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9245dcfe9SBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10245dcfe9SBen Skeggs  *
11245dcfe9SBen Skeggs  * The above copyright notice and this permission notice shall be included in
12245dcfe9SBen Skeggs  * all copies or substantial portions of the Software.
13245dcfe9SBen Skeggs  *
14245dcfe9SBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15245dcfe9SBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16245dcfe9SBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17245dcfe9SBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18245dcfe9SBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19245dcfe9SBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20245dcfe9SBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21245dcfe9SBen Skeggs  *
22245dcfe9SBen Skeggs  * Authors: Ben Skeggs
23245dcfe9SBen Skeggs  */
2432932281SBen Skeggs #include "gf100.h"
25245dcfe9SBen Skeggs 
26d30af7ceSBen Skeggs #include <core/memory.h>
27485c2c91SBen Skeggs #include <core/option.h>
28245dcfe9SBen Skeggs #include <subdev/fb.h>
29245dcfe9SBen Skeggs #include <subdev/mmu.h>
30245dcfe9SBen Skeggs 
31570889dcSBen Skeggs struct nvkm_vmm *
gf100_bar_bar1_vmm(struct nvkm_bar * base)32570889dcSBen Skeggs gf100_bar_bar1_vmm(struct nvkm_bar *base)
33245dcfe9SBen Skeggs {
347eac5f4eSBen Skeggs 	return gf100_bar(base)->bar[1].vmm;
35245dcfe9SBen Skeggs }
36245dcfe9SBen Skeggs 
37269fe32dSBen Skeggs void
gf100_bar_bar1_wait(struct nvkm_bar * base)387313cfa4SBen Skeggs gf100_bar_bar1_wait(struct nvkm_bar *base)
397313cfa4SBen Skeggs {
407313cfa4SBen Skeggs 	/* NFI why it's twice. */
417313cfa4SBen Skeggs 	nvkm_bar_flush(base);
427313cfa4SBen Skeggs 	nvkm_bar_flush(base);
437313cfa4SBen Skeggs }
447313cfa4SBen Skeggs 
457313cfa4SBen Skeggs void
gf100_bar_bar1_fini(struct nvkm_bar * bar)46bbb163e1SBen Skeggs gf100_bar_bar1_fini(struct nvkm_bar *bar)
47bbb163e1SBen Skeggs {
48bbb163e1SBen Skeggs 	nvkm_mask(bar->subdev.device, 0x001704, 0x80000000, 0x00000000);
49bbb163e1SBen Skeggs }
50bbb163e1SBen Skeggs 
51bbb163e1SBen Skeggs void
gf100_bar_bar1_init(struct nvkm_bar * base)527313cfa4SBen Skeggs gf100_bar_bar1_init(struct nvkm_bar *base)
537313cfa4SBen Skeggs {
547313cfa4SBen Skeggs 	struct nvkm_device *device = base->subdev.device;
557313cfa4SBen Skeggs 	struct gf100_bar *bar = gf100_bar(base);
56d30af7ceSBen Skeggs 	const u32 addr = nvkm_memory_addr(bar->bar[1].inst) >> 12;
577313cfa4SBen Skeggs 	nvkm_wr32(device, 0x001704, 0x80000000 | addr);
587313cfa4SBen Skeggs }
597313cfa4SBen Skeggs 
60a78dbce9SBen Skeggs struct nvkm_vmm *
gf100_bar_bar2_vmm(struct nvkm_bar * base)61a78dbce9SBen Skeggs gf100_bar_bar2_vmm(struct nvkm_bar *base)
62a78dbce9SBen Skeggs {
637eac5f4eSBen Skeggs 	return gf100_bar(base)->bar[0].vmm;
64a78dbce9SBen Skeggs }
65a78dbce9SBen Skeggs 
667313cfa4SBen Skeggs void
gf100_bar_bar2_fini(struct nvkm_bar * bar)67e988952eSBen Skeggs gf100_bar_bar2_fini(struct nvkm_bar *bar)
68e988952eSBen Skeggs {
69e988952eSBen Skeggs 	nvkm_mask(bar->subdev.device, 0x001714, 0x80000000, 0x00000000);
70e988952eSBen Skeggs }
71e988952eSBen Skeggs 
72e988952eSBen Skeggs void
gf100_bar_bar2_init(struct nvkm_bar * base)7348fe0247SBen Skeggs gf100_bar_bar2_init(struct nvkm_bar *base)
74269fe32dSBen Skeggs {
7548fe0247SBen Skeggs 	struct nvkm_device *device = base->subdev.device;
76269fe32dSBen Skeggs 	struct gf100_bar *bar = gf100_bar(base);
77d30af7ceSBen Skeggs 	u32 addr = nvkm_memory_addr(bar->bar[0].inst) >> 12;
78269fe32dSBen Skeggs 	if (bar->bar2_halve)
79269fe32dSBen Skeggs 		addr |= 0x40000000;
80269fe32dSBen Skeggs 	nvkm_wr32(device, 0x001714, 0x80000000 | addr);
81269fe32dSBen Skeggs }
82269fe32dSBen Skeggs 
83245dcfe9SBen Skeggs static int
gf100_bar_oneinit_bar(struct gf100_bar * bar,struct gf100_barN * bar_vm,struct lock_class_key * key,int bar_nr)84269fe32dSBen Skeggs gf100_bar_oneinit_bar(struct gf100_bar *bar, struct gf100_barN *bar_vm,
851de68568SBen Skeggs 		      struct lock_class_key *key, int bar_nr)
86245dcfe9SBen Skeggs {
8732932281SBen Skeggs 	struct nvkm_device *device = bar->base.subdev.device;
88245dcfe9SBen Skeggs 	resource_size_t bar_len;
89245dcfe9SBen Skeggs 	int ret;
90245dcfe9SBen Skeggs 
91adb53d27SBen Skeggs 	ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x1000, 0, false,
92d30af7ceSBen Skeggs 			      &bar_vm->inst);
93245dcfe9SBen Skeggs 	if (ret)
94245dcfe9SBen Skeggs 		return ret;
95245dcfe9SBen Skeggs 
967e8820feSBen Skeggs 	bar_len = device->func->resource_size(device, bar_nr);
9712e08bebSJon Derrick 	if (!bar_len)
9812e08bebSJon Derrick 		return -ENOMEM;
99485c2c91SBen Skeggs 	if (bar_nr == 3 && bar->bar2_halve)
100485c2c91SBen Skeggs 		bar_len >>= 1;
101245dcfe9SBen Skeggs 
1027eac5f4eSBen Skeggs 	ret = nvkm_vmm_new(device, 0, bar_len, NULL, 0, key,
1037eac5f4eSBen Skeggs 			   (bar_nr == 3) ? "bar2" : "bar1", &bar_vm->vmm);
104245dcfe9SBen Skeggs 	if (ret)
105245dcfe9SBen Skeggs 		return ret;
106245dcfe9SBen Skeggs 
1077eac5f4eSBen Skeggs 	atomic_inc(&bar_vm->vmm->engref[NVKM_SUBDEV_BAR]);
1087eac5f4eSBen Skeggs 	bar_vm->vmm->debug = bar->base.subdev.debug;
109245dcfe9SBen Skeggs 
110245dcfe9SBen Skeggs 	/*
111245dcfe9SBen Skeggs 	 * Bootstrap page table lookup.
112245dcfe9SBen Skeggs 	 */
113245dcfe9SBen Skeggs 	if (bar_nr == 3) {
1147eac5f4eSBen Skeggs 		ret = nvkm_vmm_boot(bar_vm->vmm);
115245dcfe9SBen Skeggs 		if (ret)
116245dcfe9SBen Skeggs 			return ret;
1177eac5f4eSBen Skeggs 	}
118245dcfe9SBen Skeggs 
1197eac5f4eSBen Skeggs 	return nvkm_vmm_join(bar_vm->vmm, bar_vm->inst);
120245dcfe9SBen Skeggs }
121245dcfe9SBen Skeggs 
122245dcfe9SBen Skeggs int
gf100_bar_oneinit(struct nvkm_bar * base)12332932281SBen Skeggs gf100_bar_oneinit(struct nvkm_bar *base)
124245dcfe9SBen Skeggs {
1251de68568SBen Skeggs 	static struct lock_class_key bar1_lock;
126269fe32dSBen Skeggs 	static struct lock_class_key bar2_lock;
12732932281SBen Skeggs 	struct gf100_bar *bar = gf100_bar(base);
128245dcfe9SBen Skeggs 	int ret;
129245dcfe9SBen Skeggs 
130269fe32dSBen Skeggs 	/* BAR2 */
131a78dbce9SBen Skeggs 	if (bar->base.func->bar2.init) {
132269fe32dSBen Skeggs 		ret = gf100_bar_oneinit_bar(bar, &bar->bar[0], &bar2_lock, 3);
133245dcfe9SBen Skeggs 		if (ret)
134245dcfe9SBen Skeggs 			return ret;
1358e644cb2SBen Skeggs 
1368e644cb2SBen Skeggs 		bar->base.subdev.oneinit = true;
1378e644cb2SBen Skeggs 		nvkm_bar_bar2_init(bar->base.subdev.device);
138245dcfe9SBen Skeggs 	}
139245dcfe9SBen Skeggs 
140245dcfe9SBen Skeggs 	/* BAR1 */
141269fe32dSBen Skeggs 	ret = gf100_bar_oneinit_bar(bar, &bar->bar[1], &bar1_lock, 1);
142245dcfe9SBen Skeggs 	if (ret)
143245dcfe9SBen Skeggs 		return ret;
144245dcfe9SBen Skeggs 
145245dcfe9SBen Skeggs 	return 0;
146245dcfe9SBen Skeggs }
147245dcfe9SBen Skeggs 
14832932281SBen Skeggs void *
gf100_bar_dtor(struct nvkm_bar * base)14932932281SBen Skeggs gf100_bar_dtor(struct nvkm_bar *base)
15032932281SBen Skeggs {
15132932281SBen Skeggs 	struct gf100_bar *bar = gf100_bar(base);
15232932281SBen Skeggs 
1537eac5f4eSBen Skeggs 	nvkm_vmm_part(bar->bar[1].vmm, bar->bar[1].inst);
1547eac5f4eSBen Skeggs 	nvkm_vmm_unref(&bar->bar[1].vmm);
155d30af7ceSBen Skeggs 	nvkm_memory_unref(&bar->bar[1].inst);
15632932281SBen Skeggs 
1577eac5f4eSBen Skeggs 	nvkm_vmm_part(bar->bar[0].vmm, bar->bar[0].inst);
1587eac5f4eSBen Skeggs 	nvkm_vmm_unref(&bar->bar[0].vmm);
159d30af7ceSBen Skeggs 	nvkm_memory_unref(&bar->bar[0].inst);
16032932281SBen Skeggs 	return bar;
16132932281SBen Skeggs }
16232932281SBen Skeggs 
16332932281SBen Skeggs int
gf100_bar_new_(const struct nvkm_bar_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_bar ** pbar)16432932281SBen Skeggs gf100_bar_new_(const struct nvkm_bar_func *func, struct nvkm_device *device,
165*917b24a3SBen Skeggs 	       enum nvkm_subdev_type type, int inst, struct nvkm_bar **pbar)
16632932281SBen Skeggs {
16732932281SBen Skeggs 	struct gf100_bar *bar;
16832932281SBen Skeggs 	if (!(bar = kzalloc(sizeof(*bar), GFP_KERNEL)))
16932932281SBen Skeggs 		return -ENOMEM;
170*917b24a3SBen Skeggs 	nvkm_bar_ctor(func, device, type, inst, &bar->base);
171485c2c91SBen Skeggs 	bar->bar2_halve = nvkm_boolopt(device->cfgopt, "NvBar2Halve", false);
17232932281SBen Skeggs 	*pbar = &bar->base;
17332932281SBen Skeggs 	return 0;
17432932281SBen Skeggs }
17532932281SBen Skeggs 
17632932281SBen Skeggs static const struct nvkm_bar_func
17732932281SBen Skeggs gf100_bar_func = {
178245dcfe9SBen Skeggs 	.dtor = gf100_bar_dtor,
17932932281SBen Skeggs 	.oneinit = gf100_bar_oneinit,
1807313cfa4SBen Skeggs 	.bar1.init = gf100_bar_bar1_init,
181bbb163e1SBen Skeggs 	.bar1.fini = gf100_bar_bar1_fini,
1827313cfa4SBen Skeggs 	.bar1.wait = gf100_bar_bar1_wait,
183570889dcSBen Skeggs 	.bar1.vmm = gf100_bar_bar1_vmm,
18448fe0247SBen Skeggs 	.bar2.init = gf100_bar_bar2_init,
185e988952eSBen Skeggs 	.bar2.fini = gf100_bar_bar2_fini,
18648fe0247SBen Skeggs 	.bar2.wait = gf100_bar_bar1_wait,
187a78dbce9SBen Skeggs 	.bar2.vmm = gf100_bar_bar2_vmm,
18832932281SBen Skeggs 	.flush = g84_bar_flush,
189245dcfe9SBen Skeggs };
19032932281SBen Skeggs 
19132932281SBen Skeggs int
gf100_bar_new(struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_bar ** pbar)192*917b24a3SBen Skeggs gf100_bar_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
193*917b24a3SBen Skeggs 	      struct nvkm_bar **pbar)
19432932281SBen Skeggs {
195*917b24a3SBen Skeggs 	return gf100_bar_new_(&gf100_bar_func, device, type, inst, pbar);
19632932281SBen Skeggs }
197