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 <nvhw/class/cl507d.h>
30 
31 #include "nouveau_bo.h"
32 
33 int
34 core507d_update(struct nv50_core *core, u32 *interlock, bool ntfy)
35 {
36 	struct nvif_push *push = core->chan.push;
37 	int ret;
38 
39 	if ((ret = PUSH_WAIT(push, 5)))
40 		return ret;
41 
42 	if (ntfy) {
43 		PUSH_MTHD(push, NV507D, SET_NOTIFIER_CONTROL,
44 			  NVDEF(NV507D, SET_NOTIFIER_CONTROL, MODE, WRITE) |
45 			  NVVAL(NV507D, SET_NOTIFIER_CONTROL, OFFSET, NV50_DISP_CORE_NTFY >> 2) |
46 			  NVDEF(NV507D, SET_NOTIFIER_CONTROL, NOTIFY, ENABLE));
47 	}
48 
49 	PUSH_MTHD(push, NV507D, UPDATE, interlock[NV50_DISP_INTERLOCK_BASE] |
50 					interlock[NV50_DISP_INTERLOCK_OVLY] |
51 		  NVDEF(NV507D, UPDATE, NOT_DRIVER_FRIENDLY, FALSE) |
52 		  NVDEF(NV507D, UPDATE, NOT_DRIVER_UNFRIENDLY, FALSE) |
53 		  NVDEF(NV507D, UPDATE, INHIBIT_INTERRUPTS, FALSE));
54 
55 	return PUSH_KICK(push);
56 }
57 
58 int
59 core507d_ntfy_wait_done(struct nouveau_bo *bo, u32 offset,
60 			struct nvif_device *device)
61 {
62 	s64 time = nvif_msec(device, 2000ULL,
63 		if (NVBO_TD32(bo, offset, NV_DISP_CORE_NOTIFIER_1, COMPLETION_0, DONE, ==, TRUE))
64 			break;
65 		usleep_range(1, 2);
66 	);
67 	return time < 0 ? time : 0;
68 }
69 
70 void
71 core507d_ntfy_init(struct nouveau_bo *bo, u32 offset)
72 {
73 	NVBO_WR32(bo, offset, NV_DISP_CORE_NOTIFIER_1, COMPLETION_0,
74 			NVDEF(NV_DISP_CORE_NOTIFIER_1, COMPLETION_0, DONE, FALSE));
75 }
76 
77 int
78 core507d_caps_init(struct nouveau_drm *drm, struct nv50_disp *disp)
79 {
80 	struct nvif_push *push = disp->core->chan.push;
81 	int ret;
82 
83 	if ((ret = PUSH_WAIT(push, 2)))
84 		return ret;
85 
86 	PUSH_MTHD(push, NV507D, GET_CAPABILITIES, 0x00000000);
87 	return PUSH_KICK(push);
88 }
89 
90 int
91 core507d_init(struct nv50_core *core)
92 {
93 	struct nvif_push *push = core->chan.push;
94 	int ret;
95 
96 	if ((ret = PUSH_WAIT(push, 2)))
97 		return ret;
98 
99 	PUSH_MTHD(push, NV507D, SET_CONTEXT_DMA_NOTIFIER, core->chan.sync.handle);
100 	return PUSH_KICK(push);
101 }
102 
103 static const struct nv50_core_func
104 core507d = {
105 	.init = core507d_init,
106 	.ntfy_init = core507d_ntfy_init,
107 	.caps_init = core507d_caps_init,
108 	.ntfy_wait_done = core507d_ntfy_wait_done,
109 	.update = core507d_update,
110 	.head = &head507d,
111 	.dac = &dac507d,
112 	.sor = &sor507d,
113 	.pior = &pior507d,
114 };
115 
116 int
117 core507d_new_(const struct nv50_core_func *func, struct nouveau_drm *drm,
118 	      s32 oclass, struct nv50_core **pcore)
119 {
120 	struct nv50_disp_core_channel_dma_v0 args = {};
121 	struct nv50_disp *disp = nv50_disp(drm->dev);
122 	struct nv50_core *core;
123 	int ret;
124 
125 	if (!(core = *pcore = kzalloc(sizeof(*core), GFP_KERNEL)))
126 		return -ENOMEM;
127 	core->func = func;
128 
129 	ret = nv50_dmac_create(&drm->client.device, &disp->disp->object,
130 			       &oclass, 0, &args, sizeof(args),
131 			       disp->sync->offset, &core->chan);
132 	if (ret) {
133 		NV_ERROR(drm, "core%04x allocation failed: %d\n", oclass, ret);
134 		return ret;
135 	}
136 
137 	return 0;
138 }
139 
140 int
141 core507d_new(struct nouveau_drm *drm, s32 oclass, struct nv50_core **pcore)
142 {
143 	return core507d_new_(&core507d, drm, oclass, pcore);
144 }
145