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