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 "nv50.h"
25 
26 #include <subdev/bar.h>
27 #include <engine/disp.h>
28 #include <engine/fifo.h>
29 
30 #include <nvif/event.h>
31 #include <nvif/ioctl.h>
32 
33 /*******************************************************************************
34  * software context
35  ******************************************************************************/
36 
37 static int
38 gf100_sw_chan_vblsem_release(struct nvkm_notify *notify)
39 {
40 	struct nv50_sw_chan *chan =
41 		container_of(notify, typeof(*chan), vblank.notify[notify->index]);
42 	struct nvkm_sw *sw = chan->base.sw;
43 	struct nvkm_device *device = sw->engine.subdev.device;
44 	u32 inst = chan->base.fifo->inst->addr >> 12;
45 
46 	nvkm_wr32(device, 0x001718, 0x80000000 | inst);
47 	nvkm_bar_flush(device->bar);
48 	nvkm_wr32(device, 0x06000c, upper_32_bits(chan->vblank.offset));
49 	nvkm_wr32(device, 0x060010, lower_32_bits(chan->vblank.offset));
50 	nvkm_wr32(device, 0x060014, chan->vblank.value);
51 
52 	return NVKM_NOTIFY_DROP;
53 }
54 
55 static bool
56 gf100_sw_chan_mthd(struct nvkm_sw_chan *base, int subc, u32 mthd, u32 data)
57 {
58 	struct nv50_sw_chan *chan = nv50_sw_chan(base);
59 	struct nvkm_engine *engine = chan->base.object.engine;
60 	struct nvkm_device *device = engine->subdev.device;
61 	switch (mthd) {
62 	case 0x0400:
63 		chan->vblank.offset &= 0x00ffffffffULL;
64 		chan->vblank.offset |= (u64)data << 32;
65 		return true;
66 	case 0x0404:
67 		chan->vblank.offset &= 0xff00000000ULL;
68 		chan->vblank.offset |= data;
69 		return true;
70 	case 0x0408:
71 		chan->vblank.value = data;
72 		return true;
73 	case 0x040c:
74 		if (data < device->disp->vblank.index_nr) {
75 			nvkm_notify_get(&chan->vblank.notify[data]);
76 			return true;
77 		}
78 		break;
79 	case 0x600: /* MP.PM_UNK000 */
80 		nvkm_wr32(device, 0x419e00, data);
81 		return true;
82 	case 0x644: /* MP.TRAP_WARP_ERROR_EN */
83 		if (!(data & ~0x001ffffe)) {
84 			nvkm_wr32(device, 0x419e44, data);
85 			return true;
86 		}
87 		break;
88 	case 0x6ac: /* MP.PM_UNK0AC */
89 		nvkm_wr32(device, 0x419eac, data);
90 		return true;
91 	default:
92 		break;
93 	}
94 	return false;
95 }
96 
97 static const struct nvkm_sw_chan_func
98 gf100_sw_chan = {
99 	.dtor = nv50_sw_chan_dtor,
100 	.mthd = gf100_sw_chan_mthd,
101 };
102 
103 static int
104 gf100_sw_chan_new(struct nvkm_sw *sw, struct nvkm_fifo_chan *fifoch,
105 		  const struct nvkm_oclass *oclass,
106 		  struct nvkm_object **pobject)
107 {
108 	struct nvkm_disp *disp = sw->engine.subdev.device->disp;
109 	struct nv50_sw_chan *chan;
110 	int ret, i;
111 
112 	if (!(chan = kzalloc(sizeof(*chan), GFP_KERNEL)))
113 		return -ENOMEM;
114 	*pobject = &chan->base.object;
115 
116 	ret = nvkm_sw_chan_ctor(&gf100_sw_chan, sw, fifoch, oclass,
117 				&chan->base);
118 	if (ret)
119 		return ret;
120 
121 	for (i = 0; disp && i < disp->vblank.index_nr; i++) {
122 		ret = nvkm_notify_init(NULL, &disp->vblank,
123 				       gf100_sw_chan_vblsem_release, false,
124 				       &(struct nvif_notify_head_req_v0) {
125 					.head = i,
126 				       },
127 				       sizeof(struct nvif_notify_head_req_v0),
128 				       sizeof(struct nvif_notify_head_rep_v0),
129 				       &chan->vblank.notify[i]);
130 		if (ret)
131 			return ret;
132 	}
133 
134 	return 0;
135 }
136 
137 /*******************************************************************************
138  * software engine/subdev functions
139  ******************************************************************************/
140 
141 static const struct nvkm_sw_func
142 gf100_sw_func = {
143 	.chan_new = gf100_sw_chan_new,
144 	.sclass = {
145 		{ nvkm_nvsw_new, { -1, -1, NVIF_IOCTL_NEW_V0_SW_GF100 } },
146 		{}
147 	}
148 };
149 
150 struct nvkm_oclass *
151 gf100_sw_oclass = &(struct nv50_sw_oclass) {
152 	.base.handle = NV_ENGINE(SW, 0xc0),
153 	.base.ofuncs = &(struct nvkm_ofuncs) {
154 		.ctor = nv50_sw_ctor,
155 		.dtor = _nvkm_sw_dtor,
156 		.init = _nvkm_sw_init,
157 		.fini = _nvkm_sw_fini,
158 	},
159 	.func = &gf100_sw_func,
160 }.base;
161