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
26void
27nvkm_bar_flush(struct nvkm_bar *bar)
28{
29	if (bar && bar->func->flush)
30		bar->func->flush(bar);
31}
32
33struct nvkm_vmm *
34nvkm_bar_bar1_vmm(struct nvkm_device *device)
35{
36	return device->bar->func->bar1.vmm(device->bar);
37}
38
39void
40nvkm_bar_bar1_reset(struct nvkm_device *device)
41{
42	struct nvkm_bar *bar = device->bar;
43	if (bar) {
44		bar->func->bar1.init(bar);
45		bar->func->bar1.wait(bar);
46	}
47}
48
49struct nvkm_vmm *
50nvkm_bar_bar2_vmm(struct nvkm_device *device)
51{
52	/* Denies access to BAR2 when it's not initialised, used by INSTMEM
53	 * to know when object access needs to go through the BAR0 window.
54	 */
55	struct nvkm_bar *bar = device->bar;
56	if (bar && bar->bar2)
57		return bar->func->bar2.vmm(bar);
58	return NULL;
59}
60
61void
62nvkm_bar_bar2_reset(struct nvkm_device *device)
63{
64	struct nvkm_bar *bar = device->bar;
65	if (bar && bar->bar2) {
66		bar->func->bar2.init(bar);
67		bar->func->bar2.wait(bar);
68	}
69}
70
71void
72nvkm_bar_bar2_fini(struct nvkm_device *device)
73{
74	struct nvkm_bar *bar = device->bar;
75	if (bar && bar->bar2) {
76		bar->func->bar2.fini(bar);
77		bar->bar2 = false;
78	}
79}
80
81void
82nvkm_bar_bar2_init(struct nvkm_device *device)
83{
84	struct nvkm_bar *bar = device->bar;
85	if (bar && bar->subdev.oneinit && !bar->bar2 && bar->func->bar2.init) {
86		bar->func->bar2.init(bar);
87		bar->func->bar2.wait(bar);
88		bar->bar2 = true;
89	}
90}
91
92static int
93nvkm_bar_fini(struct nvkm_subdev *subdev, bool suspend)
94{
95	struct nvkm_bar *bar = nvkm_bar(subdev);
96
97	if (!subdev->use.enabled)
98		return 0;
99
100	if (bar->func->bar1.fini)
101		bar->func->bar1.fini(bar);
102
103	if (!suspend) /* Handled by instmem. */
104		nvkm_bar_bar2_fini(subdev->device);
105
106	return 0;
107}
108
109static int
110nvkm_bar_init(struct nvkm_subdev *subdev)
111{
112	struct nvkm_bar *bar = nvkm_bar(subdev);
113	bar->func->bar1.init(bar);
114	bar->func->bar1.wait(bar);
115	if (bar->func->init)
116		bar->func->init(bar);
117	return 0;
118}
119
120static int
121nvkm_bar_oneinit(struct nvkm_subdev *subdev)
122{
123	struct nvkm_bar *bar = nvkm_bar(subdev);
124	return bar->func->oneinit(bar);
125}
126
127static void *
128nvkm_bar_dtor(struct nvkm_subdev *subdev)
129{
130	struct nvkm_bar *bar = nvkm_bar(subdev);
131
132	return bar->func->dtor(bar);
133}
134
135static const struct nvkm_subdev_func
136nvkm_bar = {
137	.dtor = nvkm_bar_dtor,
138	.oneinit = nvkm_bar_oneinit,
139	.init = nvkm_bar_init,
140	.fini = nvkm_bar_fini,
141};
142
143void
144nvkm_bar_ctor(const struct nvkm_bar_func *func, struct nvkm_device *device,
145	      enum nvkm_subdev_type type, int inst, struct nvkm_bar *bar)
146{
147	nvkm_subdev_ctor(&nvkm_bar, device, type, inst, &bar->subdev);
148	bar->func = func;
149	spin_lock_init(&bar->lock);
150}
151