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