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 <engine/mpeg.h>
25
26#include <core/client.h>
27#include <core/handle.h>
28#include <engine/fifo.h>
29
30struct nv44_mpeg_priv {
31	struct nvkm_mpeg base;
32};
33
34struct nv44_mpeg_chan {
35	struct nvkm_mpeg_chan base;
36};
37
38/*******************************************************************************
39 * PMPEG context
40 ******************************************************************************/
41
42static int
43nv44_mpeg_context_ctor(struct nvkm_object *parent,
44		       struct nvkm_object *engine,
45		       struct nvkm_oclass *oclass, void *data, u32 size,
46		       struct nvkm_object **pobject)
47{
48	struct nv44_mpeg_chan *chan;
49	int ret;
50
51	ret = nvkm_mpeg_context_create(parent, engine, oclass, NULL, 264 * 4,
52				       16, NVOBJ_FLAG_ZERO_ALLOC, &chan);
53	*pobject = nv_object(chan);
54	if (ret)
55		return ret;
56
57	nv_wo32(&chan->base.base, 0x78, 0x02001ec1);
58	return 0;
59}
60
61static int
62nv44_mpeg_context_fini(struct nvkm_object *object, bool suspend)
63{
64
65	struct nv44_mpeg_priv *priv = (void *)object->engine;
66	struct nv44_mpeg_chan *chan = (void *)object;
67	u32 inst = 0x80000000 | nv_gpuobj(chan)->addr >> 4;
68
69	nv_mask(priv, 0x00b32c, 0x00000001, 0x00000000);
70	if (nv_rd32(priv, 0x00b318) == inst)
71		nv_mask(priv, 0x00b318, 0x80000000, 0x00000000);
72	nv_mask(priv, 0x00b32c, 0x00000001, 0x00000001);
73	return 0;
74}
75
76static struct nvkm_oclass
77nv44_mpeg_cclass = {
78	.handle = NV_ENGCTX(MPEG, 0x44),
79	.ofuncs = &(struct nvkm_ofuncs) {
80		.ctor = nv44_mpeg_context_ctor,
81		.dtor = _nvkm_mpeg_context_dtor,
82		.init = _nvkm_mpeg_context_init,
83		.fini = nv44_mpeg_context_fini,
84		.rd32 = _nvkm_mpeg_context_rd32,
85		.wr32 = _nvkm_mpeg_context_wr32,
86	},
87};
88
89/*******************************************************************************
90 * PMPEG engine/subdev functions
91 ******************************************************************************/
92
93static void
94nv44_mpeg_intr(struct nvkm_subdev *subdev)
95{
96	struct nvkm_fifo *pfifo = nvkm_fifo(subdev);
97	struct nvkm_engine *engine = nv_engine(subdev);
98	struct nvkm_object *engctx;
99	struct nvkm_handle *handle;
100	struct nv44_mpeg_priv *priv = (void *)subdev;
101	u32 inst = nv_rd32(priv, 0x00b318) & 0x000fffff;
102	u32 stat = nv_rd32(priv, 0x00b100);
103	u32 type = nv_rd32(priv, 0x00b230);
104	u32 mthd = nv_rd32(priv, 0x00b234);
105	u32 data = nv_rd32(priv, 0x00b238);
106	u32 show = stat;
107	int chid;
108
109	engctx = nvkm_engctx_get(engine, inst);
110	chid   = pfifo->chid(pfifo, engctx);
111
112	if (stat & 0x01000000) {
113		/* happens on initial binding of the object */
114		if (type == 0x00000020 && mthd == 0x0000) {
115			nv_mask(priv, 0x00b308, 0x00000000, 0x00000000);
116			show &= ~0x01000000;
117		}
118
119		if (type == 0x00000010) {
120			handle = nvkm_handle_get_class(engctx, 0x3174);
121			if (handle && !nv_call(handle->object, mthd, data))
122				show &= ~0x01000000;
123			nvkm_handle_put(handle);
124		}
125	}
126
127	nv_wr32(priv, 0x00b100, stat);
128	nv_wr32(priv, 0x00b230, 0x00000001);
129
130	if (show) {
131		nv_error(priv,
132			 "ch %d [0x%08x %s] 0x%08x 0x%08x 0x%08x 0x%08x\n",
133			 chid, inst << 4, nvkm_client_name(engctx), stat,
134			 type, mthd, data);
135	}
136
137	nvkm_engctx_put(engctx);
138}
139
140static void
141nv44_mpeg_me_intr(struct nvkm_subdev *subdev)
142{
143	struct nv44_mpeg_priv *priv = (void *)subdev;
144	u32 stat;
145
146	if ((stat = nv_rd32(priv, 0x00b100)))
147		nv44_mpeg_intr(subdev);
148
149	if ((stat = nv_rd32(priv, 0x00b800))) {
150		nv_error(priv, "PMSRCH 0x%08x\n", stat);
151		nv_wr32(priv, 0x00b800, stat);
152	}
153}
154
155static int
156nv44_mpeg_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
157	       struct nvkm_oclass *oclass, void *data, u32 size,
158	       struct nvkm_object **pobject)
159{
160	struct nv44_mpeg_priv *priv;
161	int ret;
162
163	ret = nvkm_mpeg_create(parent, engine, oclass, &priv);
164	*pobject = nv_object(priv);
165	if (ret)
166		return ret;
167
168	nv_subdev(priv)->unit = 0x00000002;
169	nv_subdev(priv)->intr = nv44_mpeg_me_intr;
170	nv_engine(priv)->cclass = &nv44_mpeg_cclass;
171	nv_engine(priv)->sclass = nv40_mpeg_sclass;
172	nv_engine(priv)->tile_prog = nv31_mpeg_tile_prog;
173	return 0;
174}
175
176struct nvkm_oclass
177nv44_mpeg_oclass = {
178	.handle = NV_ENGINE(MPEG, 0x44),
179	.ofuncs = &(struct nvkm_ofuncs) {
180		.ctor = nv44_mpeg_ctor,
181		.dtor = _nvkm_mpeg_dtor,
182		.init = nv31_mpeg_init,
183		.fini = _nvkm_mpeg_fini,
184	},
185};
186