1 /*
2  * Copyright 2018 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 #include "core.h"
23 #include "head.h"
24 
25 #include <nvif/cl507d.h>
26 #include <nvif/push507c.h>
27 #include <nvif/timer.h>
28 
29 #include "nouveau_bo.h"
30 
31 void
32 core507d_update(struct nv50_core *core, u32 *interlock, bool ntfy)
33 {
34 	u32 *push;
35 	if ((push = evo_wait(&core->chan, 5))) {
36 		if (ntfy) {
37 			evo_mthd(push, 0x0084, 1);
38 			evo_data(push, 0x80000000 | NV50_DISP_CORE_NTFY);
39 		}
40 		evo_mthd(push, 0x0080, 2);
41 		evo_data(push, interlock[NV50_DISP_INTERLOCK_BASE] |
42 			       interlock[NV50_DISP_INTERLOCK_OVLY]);
43 		evo_data(push, 0x00000000);
44 		evo_kick(push, &core->chan);
45 	}
46 }
47 
48 int
49 core507d_ntfy_wait_done(struct nouveau_bo *bo, u32 offset,
50 			struct nvif_device *device)
51 {
52 	s64 time = nvif_msec(device, 2000ULL,
53 		if (nouveau_bo_rd32(bo, offset / 4))
54 			break;
55 		usleep_range(1, 2);
56 	);
57 	return time < 0 ? time : 0;
58 }
59 
60 void
61 core507d_ntfy_init(struct nouveau_bo *bo, u32 offset)
62 {
63 	nouveau_bo_wr32(bo, offset / 4, 0x00000000);
64 }
65 
66 int
67 core507d_caps_init(struct nouveau_drm *drm, struct nv50_disp *disp)
68 {
69 	struct nvif_push *push = disp->core->chan.push;
70 	int ret;
71 
72 	if ((ret = PUSH_WAIT(push, 2)))
73 		return ret;
74 
75 	PUSH_NVSQ(push, NV507D, 0x008c, 0x00000000);
76 	return PUSH_KICK(push);
77 }
78 
79 int
80 core507d_init(struct nv50_core *core)
81 {
82 	struct nvif_push *push = core->chan.push;
83 	int ret;
84 
85 	if ((ret = PUSH_WAIT(push, 2)))
86 		return ret;
87 
88 	PUSH_NVSQ(push, NV507D, 0x0088, core->chan.sync.handle);
89 	return PUSH_KICK(push);
90 }
91 
92 static const struct nv50_core_func
93 core507d = {
94 	.init = core507d_init,
95 	.ntfy_init = core507d_ntfy_init,
96 	.caps_init = core507d_caps_init,
97 	.ntfy_wait_done = core507d_ntfy_wait_done,
98 	.update = core507d_update,
99 	.head = &head507d,
100 	.dac = &dac507d,
101 	.sor = &sor507d,
102 	.pior = &pior507d,
103 };
104 
105 int
106 core507d_new_(const struct nv50_core_func *func, struct nouveau_drm *drm,
107 	      s32 oclass, struct nv50_core **pcore)
108 {
109 	struct nv50_disp_core_channel_dma_v0 args = {};
110 	struct nv50_disp *disp = nv50_disp(drm->dev);
111 	struct nv50_core *core;
112 	int ret;
113 
114 	if (!(core = *pcore = kzalloc(sizeof(*core), GFP_KERNEL)))
115 		return -ENOMEM;
116 	core->func = func;
117 
118 	ret = nv50_dmac_create(&drm->client.device, &disp->disp->object,
119 			       &oclass, 0, &args, sizeof(args),
120 			       disp->sync->offset, &core->chan);
121 	if (ret) {
122 		NV_ERROR(drm, "core%04x allocation failed: %d\n", oclass, ret);
123 		return ret;
124 	}
125 
126 	return 0;
127 }
128 
129 int
130 core507d_new(struct nouveau_drm *drm, s32 oclass, struct nv50_core **pcore)
131 {
132 	return core507d_new_(&core507d, drm, oclass, pcore);
133 }
134