11590700dSBen Skeggs /*
21590700dSBen Skeggs  * Copyright 2018 Red Hat Inc.
31590700dSBen Skeggs  *
41590700dSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
51590700dSBen Skeggs  * copy of this software and associated documentation files (the "Software"),
61590700dSBen Skeggs  * to deal in the Software without restriction, including without limitation
71590700dSBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81590700dSBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
91590700dSBen Skeggs  * Software is furnished to do so, subject to the following conditions:
101590700dSBen Skeggs  *
111590700dSBen Skeggs  * The above copyright notice and this permission notice shall be included in
121590700dSBen Skeggs  * all copies or substantial portions of the Software.
131590700dSBen Skeggs  *
141590700dSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151590700dSBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161590700dSBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
171590700dSBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
181590700dSBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
191590700dSBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
201590700dSBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
211590700dSBen Skeggs  */
221590700dSBen Skeggs #include "core.h"
231590700dSBen Skeggs #include "head.h"
241590700dSBen Skeggs 
25*889fcbe9SBen Skeggs #include <nvif/if0014.h>
265e691222SBen Skeggs #include <nvif/push507c.h>
27ed3d1489SBen Skeggs #include <nvif/timer.h>
281590700dSBen Skeggs 
29b11d8ca1SBen Skeggs #include <nvhw/class/cl507d.h>
30b11d8ca1SBen Skeggs 
311590700dSBen Skeggs #include "nouveau_bo.h"
321590700dSBen Skeggs 
33203f6eafSBen Skeggs int
core507d_update(struct nv50_core * core,u32 * interlock,bool ntfy)3453e0a3e7SBen Skeggs core507d_update(struct nv50_core *core, u32 *interlock, bool ntfy)
3509e1b78aSBen Skeggs {
36203f6eafSBen Skeggs 	struct nvif_push *push = core->chan.push;
37203f6eafSBen Skeggs 	int ret;
38203f6eafSBen Skeggs 
39ef674b68SBen Skeggs 	if ((ret = PUSH_WAIT(push, (ntfy ? 2 : 0) + 3)))
40203f6eafSBen Skeggs 		return ret;
41203f6eafSBen Skeggs 
422806280aSBen Skeggs 	if (ntfy) {
432806280aSBen Skeggs 		PUSH_MTHD(push, NV507D, SET_NOTIFIER_CONTROL,
442806280aSBen Skeggs 			  NVDEF(NV507D, SET_NOTIFIER_CONTROL, MODE, WRITE) |
452806280aSBen Skeggs 			  NVVAL(NV507D, SET_NOTIFIER_CONTROL, OFFSET, NV50_DISP_CORE_NTFY >> 2) |
462806280aSBen Skeggs 			  NVDEF(NV507D, SET_NOTIFIER_CONTROL, NOTIFY, ENABLE));
472806280aSBen Skeggs 	}
48203f6eafSBen Skeggs 
492806280aSBen Skeggs 	PUSH_MTHD(push, NV507D, UPDATE, interlock[NV50_DISP_INTERLOCK_BASE] |
502806280aSBen Skeggs 					interlock[NV50_DISP_INTERLOCK_OVLY] |
512806280aSBen Skeggs 		  NVDEF(NV507D, UPDATE, NOT_DRIVER_FRIENDLY, FALSE) |
522806280aSBen Skeggs 		  NVDEF(NV507D, UPDATE, NOT_DRIVER_UNFRIENDLY, FALSE) |
53a9cfcfcaSBen Skeggs 		  NVDEF(NV507D, UPDATE, INHIBIT_INTERRUPTS, FALSE),
54a9cfcfcaSBen Skeggs 
55a9cfcfcaSBen Skeggs 				SET_NOTIFIER_CONTROL,
56a9cfcfcaSBen Skeggs 		  NVDEF(NV507D, SET_NOTIFIER_CONTROL, NOTIFY, DISABLE));
572806280aSBen Skeggs 
58203f6eafSBen Skeggs 	return PUSH_KICK(push);
5909e1b78aSBen Skeggs }
6009e1b78aSBen Skeggs 
6109e1b78aSBen Skeggs int
core507d_ntfy_wait_done(struct nouveau_bo * bo,u32 offset,struct nvif_device * device)6209e1b78aSBen Skeggs core507d_ntfy_wait_done(struct nouveau_bo *bo, u32 offset,
6309e1b78aSBen Skeggs 			struct nvif_device *device)
6409e1b78aSBen Skeggs {
6509e1b78aSBen Skeggs 	s64 time = nvif_msec(device, 2000ULL,
664daef27cSBen Skeggs 		if (NVBO_TD32(bo, offset, NV_DISP_CORE_NOTIFIER_1, COMPLETION_0, DONE, ==, TRUE))
6709e1b78aSBen Skeggs 			break;
6809e1b78aSBen Skeggs 		usleep_range(1, 2);
6909e1b78aSBen Skeggs 	);
7009e1b78aSBen Skeggs 	return time < 0 ? time : 0;
7109e1b78aSBen Skeggs }
7209e1b78aSBen Skeggs 
7309e1b78aSBen Skeggs void
core507d_ntfy_init(struct nouveau_bo * bo,u32 offset)7409e1b78aSBen Skeggs core507d_ntfy_init(struct nouveau_bo *bo, u32 offset)
7509e1b78aSBen Skeggs {
7638a323b0SBen Skeggs 	NVBO_WR32(bo, offset, NV_DISP_CORE_NOTIFIER_1, COMPLETION_0,
7738a323b0SBen Skeggs 			NVDEF(NV_DISP_CORE_NOTIFIER_1, COMPLETION_0, DONE, FALSE));
7809e1b78aSBen Skeggs }
7909e1b78aSBen Skeggs 
804a2cb418SLyude Paul int
core507d_read_caps(struct nv50_disp * disp)8124d9422eSLyude Paul core507d_read_caps(struct nv50_disp *disp)
824a2cb418SLyude Paul {
833c43c362SBen Skeggs 	struct nvif_push *push = disp->core->chan.push;
843c43c362SBen Skeggs 	int ret;
854a2cb418SLyude Paul 
8624d9422eSLyude Paul 	ret = PUSH_WAIT(push, 6);
8724d9422eSLyude Paul 	if (ret)
883c43c362SBen Skeggs 		return ret;
894a2cb418SLyude Paul 
9024d9422eSLyude Paul 	PUSH_MTHD(push, NV507D, SET_NOTIFIER_CONTROL,
9124d9422eSLyude Paul 		  NVDEF(NV507D, SET_NOTIFIER_CONTROL, MODE, WRITE) |
9224d9422eSLyude Paul 		  NVVAL(NV507D, SET_NOTIFIER_CONTROL, OFFSET, NV50_DISP_CORE_NTFY >> 2) |
9324d9422eSLyude Paul 		  NVDEF(NV507D, SET_NOTIFIER_CONTROL, NOTIFY, ENABLE));
9424d9422eSLyude Paul 
95d8b24526SBen Skeggs 	PUSH_MTHD(push, NV507D, GET_CAPABILITIES, 0x00000000);
9624d9422eSLyude Paul 
9724d9422eSLyude Paul 	PUSH_MTHD(push, NV507D, SET_NOTIFIER_CONTROL,
9824d9422eSLyude Paul 		  NVDEF(NV507D, SET_NOTIFIER_CONTROL, NOTIFY, DISABLE));
9924d9422eSLyude Paul 
1003c43c362SBen Skeggs 	return PUSH_KICK(push);
1014a2cb418SLyude Paul }
1024a2cb418SLyude Paul 
1035e691222SBen Skeggs int
core507d_caps_init(struct nouveau_drm * drm,struct nv50_disp * disp)10424d9422eSLyude Paul core507d_caps_init(struct nouveau_drm *drm, struct nv50_disp *disp)
10524d9422eSLyude Paul {
10624d9422eSLyude Paul 	struct nv50_core *core = disp->core;
10724d9422eSLyude Paul 	struct nouveau_bo *bo = disp->sync;
10824d9422eSLyude Paul 	s64 time;
10924d9422eSLyude Paul 	int ret;
11024d9422eSLyude Paul 
11124d9422eSLyude Paul 	NVBO_WR32(bo, NV50_DISP_CORE_NTFY, NV_DISP_CORE_NOTIFIER_1, CAPABILITIES_1,
11224d9422eSLyude Paul 				     NVDEF(NV_DISP_CORE_NOTIFIER_1, CAPABILITIES_1, DONE, FALSE));
11324d9422eSLyude Paul 
11424d9422eSLyude Paul 	ret = core507d_read_caps(disp);
11524d9422eSLyude Paul 	if (ret < 0)
11624d9422eSLyude Paul 		return ret;
11724d9422eSLyude Paul 
11824d9422eSLyude Paul 	time = nvif_msec(core->chan.base.device, 2000ULL,
11924d9422eSLyude Paul 			 if (NVBO_TD32(bo, NV50_DISP_CORE_NTFY,
12024d9422eSLyude Paul 				       NV_DISP_CORE_NOTIFIER_1, CAPABILITIES_1, DONE, ==, TRUE))
12124d9422eSLyude Paul 				 break;
12224d9422eSLyude Paul 			 usleep_range(1, 2);
12324d9422eSLyude Paul 			 );
12424d9422eSLyude Paul 	if (time < 0)
12524d9422eSLyude Paul 		NV_ERROR(drm, "core caps notifier timeout\n");
12624d9422eSLyude Paul 
12724d9422eSLyude Paul 	return 0;
12824d9422eSLyude Paul }
12924d9422eSLyude Paul 
13024d9422eSLyude Paul int
core507d_init(struct nv50_core * core)13109e1b78aSBen Skeggs core507d_init(struct nv50_core *core)
13209e1b78aSBen Skeggs {
1335e691222SBen Skeggs 	struct nvif_push *push = core->chan.push;
1345e691222SBen Skeggs 	int ret;
1355e691222SBen Skeggs 
1365e691222SBen Skeggs 	if ((ret = PUSH_WAIT(push, 2)))
1375e691222SBen Skeggs 		return ret;
1385e691222SBen Skeggs 
139b11d8ca1SBen Skeggs 	PUSH_MTHD(push, NV507D, SET_CONTEXT_DMA_NOTIFIER, core->chan.sync.handle);
1405e691222SBen Skeggs 	return PUSH_KICK(push);
14109e1b78aSBen Skeggs }
14209e1b78aSBen Skeggs 
1431590700dSBen Skeggs static const struct nv50_core_func
1441590700dSBen Skeggs core507d = {
14509e1b78aSBen Skeggs 	.init = core507d_init,
14609e1b78aSBen Skeggs 	.ntfy_init = core507d_ntfy_init,
1474a2cb418SLyude Paul 	.caps_init = core507d_caps_init,
14809e1b78aSBen Skeggs 	.ntfy_wait_done = core507d_ntfy_wait_done,
14909e1b78aSBen Skeggs 	.update = core507d_update,
1501590700dSBen Skeggs 	.head = &head507d,
1511590700dSBen Skeggs 	.dac = &dac507d,
1521590700dSBen Skeggs 	.sor = &sor507d,
1531590700dSBen Skeggs 	.pior = &pior507d,
1541590700dSBen Skeggs };
1551590700dSBen Skeggs 
15609e1b78aSBen Skeggs int
core507d_new_(const struct nv50_core_func * func,struct nouveau_drm * drm,s32 oclass,struct nv50_core ** pcore)1571590700dSBen Skeggs core507d_new_(const struct nv50_core_func *func, struct nouveau_drm *drm,
1581590700dSBen Skeggs 	      s32 oclass, struct nv50_core **pcore)
1591590700dSBen Skeggs {
160*889fcbe9SBen Skeggs 	struct nvif_disp_chan_v0 args = {};
1611590700dSBen Skeggs 	struct nv50_disp *disp = nv50_disp(drm->dev);
1621590700dSBen Skeggs 	struct nv50_core *core;
1631590700dSBen Skeggs 	int ret;
1641590700dSBen Skeggs 
1651590700dSBen Skeggs 	if (!(core = *pcore = kzalloc(sizeof(*core), GFP_KERNEL)))
1661590700dSBen Skeggs 		return -ENOMEM;
1671590700dSBen Skeggs 	core->func = func;
1681590700dSBen Skeggs 
1691590700dSBen Skeggs 	ret = nv50_dmac_create(&drm->client.device, &disp->disp->object,
1701590700dSBen Skeggs 			       &oclass, 0, &args, sizeof(args),
1710dc9b286SNirmoy Das 			       disp->sync->offset, &core->chan);
1721590700dSBen Skeggs 	if (ret) {
1731590700dSBen Skeggs 		NV_ERROR(drm, "core%04x allocation failed: %d\n", oclass, ret);
1741590700dSBen Skeggs 		return ret;
1751590700dSBen Skeggs 	}
1761590700dSBen Skeggs 
1771590700dSBen Skeggs 	return 0;
1781590700dSBen Skeggs }
1791590700dSBen Skeggs 
1801590700dSBen Skeggs int
core507d_new(struct nouveau_drm * drm,s32 oclass,struct nv50_core ** pcore)1811590700dSBen Skeggs core507d_new(struct nouveau_drm *drm, s32 oclass, struct nv50_core **pcore)
1821590700dSBen Skeggs {
1831590700dSBen Skeggs 	return core507d_new_(&core507d, drm, oclass, pcore);
1841590700dSBen Skeggs }
185