1*677dec6eSriastradh /*	$NetBSD: hwsq.h,v 1.3 2021/12/18 23:45:38 riastradh Exp $	*/
2d350ecf5Sriastradh 
3*677dec6eSriastradh /* SPDX-License-Identifier: MIT */
4d350ecf5Sriastradh #ifndef __NVKM_BUS_HWSQ_H__
5d350ecf5Sriastradh #define __NVKM_BUS_HWSQ_H__
6d350ecf5Sriastradh #include <subdev/bus.h>
7d350ecf5Sriastradh 
8d350ecf5Sriastradh struct hwsq {
9d350ecf5Sriastradh 	struct nvkm_subdev *subdev;
10d350ecf5Sriastradh 	struct nvkm_hwsq *hwsq;
11d350ecf5Sriastradh 	int sequence;
12d350ecf5Sriastradh };
13d350ecf5Sriastradh 
14d350ecf5Sriastradh struct hwsq_reg {
15d350ecf5Sriastradh 	int sequence;
16d350ecf5Sriastradh 	bool force;
17d350ecf5Sriastradh 	u32 addr;
18d350ecf5Sriastradh 	u32 stride; /* in bytes */
19d350ecf5Sriastradh 	u32 mask;
20d350ecf5Sriastradh 	u32 data;
21d350ecf5Sriastradh };
22d350ecf5Sriastradh 
23d350ecf5Sriastradh static inline struct hwsq_reg
hwsq_stride(u32 addr,u32 stride,u32 mask)24d350ecf5Sriastradh hwsq_stride(u32 addr, u32 stride, u32 mask)
25d350ecf5Sriastradh {
26d350ecf5Sriastradh 	return (struct hwsq_reg) {
27d350ecf5Sriastradh 		.sequence = 0,
28d350ecf5Sriastradh 		.force = 0,
29d350ecf5Sriastradh 		.addr = addr,
30d350ecf5Sriastradh 		.stride = stride,
31d350ecf5Sriastradh 		.mask = mask,
32d350ecf5Sriastradh 		.data = 0xdeadbeef,
33d350ecf5Sriastradh 	};
34d350ecf5Sriastradh }
35d350ecf5Sriastradh 
36d350ecf5Sriastradh static inline struct hwsq_reg
hwsq_reg2(u32 addr1,u32 addr2)37d350ecf5Sriastradh hwsq_reg2(u32 addr1, u32 addr2)
38d350ecf5Sriastradh {
39d350ecf5Sriastradh 	return (struct hwsq_reg) {
40d350ecf5Sriastradh 		.sequence = 0,
41d350ecf5Sriastradh 		.force = 0,
42d350ecf5Sriastradh 		.addr = addr1,
43d350ecf5Sriastradh 		.stride = addr2 - addr1,
44d350ecf5Sriastradh 		.mask = 0x3,
45d350ecf5Sriastradh 		.data = 0xdeadbeef,
46d350ecf5Sriastradh 	};
47d350ecf5Sriastradh }
48d350ecf5Sriastradh 
49d350ecf5Sriastradh static inline struct hwsq_reg
hwsq_reg(u32 addr)50d350ecf5Sriastradh hwsq_reg(u32 addr)
51d350ecf5Sriastradh {
52d350ecf5Sriastradh 	return (struct hwsq_reg) {
53d350ecf5Sriastradh 		.sequence = 0,
54d350ecf5Sriastradh 		.force = 0,
55d350ecf5Sriastradh 		.addr = addr,
56d350ecf5Sriastradh 		.stride = 0,
57d350ecf5Sriastradh 		.mask = 0x1,
58d350ecf5Sriastradh 		.data = 0xdeadbeef,
59d350ecf5Sriastradh 	};
60d350ecf5Sriastradh }
61d350ecf5Sriastradh 
62d350ecf5Sriastradh static inline int
hwsq_init(struct hwsq * ram,struct nvkm_subdev * subdev)63d350ecf5Sriastradh hwsq_init(struct hwsq *ram, struct nvkm_subdev *subdev)
64d350ecf5Sriastradh {
65d350ecf5Sriastradh 	int ret;
66d350ecf5Sriastradh 
67d350ecf5Sriastradh 	ret = nvkm_hwsq_init(subdev, &ram->hwsq);
68d350ecf5Sriastradh 	if (ret)
69d350ecf5Sriastradh 		return ret;
70d350ecf5Sriastradh 
71d350ecf5Sriastradh 	ram->sequence++;
72d350ecf5Sriastradh 	ram->subdev = subdev;
73d350ecf5Sriastradh 	return 0;
74d350ecf5Sriastradh }
75d350ecf5Sriastradh 
76d350ecf5Sriastradh static inline int
hwsq_exec(struct hwsq * ram,bool exec)77d350ecf5Sriastradh hwsq_exec(struct hwsq *ram, bool exec)
78d350ecf5Sriastradh {
79d350ecf5Sriastradh 	int ret = 0;
80d350ecf5Sriastradh 	if (ram->subdev) {
81d350ecf5Sriastradh 		ret = nvkm_hwsq_fini(&ram->hwsq, exec);
82d350ecf5Sriastradh 		ram->subdev = NULL;
83d350ecf5Sriastradh 	}
84d350ecf5Sriastradh 	return ret;
85d350ecf5Sriastradh }
86d350ecf5Sriastradh 
87d350ecf5Sriastradh static inline u32
hwsq_rd32(struct hwsq * ram,struct hwsq_reg * reg)88d350ecf5Sriastradh hwsq_rd32(struct hwsq *ram, struct hwsq_reg *reg)
89d350ecf5Sriastradh {
90d350ecf5Sriastradh 	struct nvkm_device *device = ram->subdev->device;
91d350ecf5Sriastradh 	if (reg->sequence != ram->sequence)
92d350ecf5Sriastradh 		reg->data = nvkm_rd32(device, reg->addr);
93d350ecf5Sriastradh 	return reg->data;
94d350ecf5Sriastradh }
95d350ecf5Sriastradh 
96d350ecf5Sriastradh static inline void
hwsq_wr32(struct hwsq * ram,struct hwsq_reg * reg,u32 data)97d350ecf5Sriastradh hwsq_wr32(struct hwsq *ram, struct hwsq_reg *reg, u32 data)
98d350ecf5Sriastradh {
99d350ecf5Sriastradh 	u32 mask, off = 0;
100d350ecf5Sriastradh 
101d350ecf5Sriastradh 	reg->sequence = ram->sequence;
102d350ecf5Sriastradh 	reg->data = data;
103d350ecf5Sriastradh 
104d350ecf5Sriastradh 	for (mask = reg->mask; mask > 0; mask = (mask & ~1) >> 1) {
105d350ecf5Sriastradh 		if (mask & 1)
106d350ecf5Sriastradh 			nvkm_hwsq_wr32(ram->hwsq, reg->addr+off, reg->data);
107d350ecf5Sriastradh 
108d350ecf5Sriastradh 		off += reg->stride;
109d350ecf5Sriastradh 	}
110d350ecf5Sriastradh }
111d350ecf5Sriastradh 
112d350ecf5Sriastradh static inline void
hwsq_nuke(struct hwsq * ram,struct hwsq_reg * reg)113d350ecf5Sriastradh hwsq_nuke(struct hwsq *ram, struct hwsq_reg *reg)
114d350ecf5Sriastradh {
115d350ecf5Sriastradh 	reg->force = true;
116d350ecf5Sriastradh }
117d350ecf5Sriastradh 
118d350ecf5Sriastradh static inline u32
hwsq_mask(struct hwsq * ram,struct hwsq_reg * reg,u32 mask,u32 data)119d350ecf5Sriastradh hwsq_mask(struct hwsq *ram, struct hwsq_reg *reg, u32 mask, u32 data)
120d350ecf5Sriastradh {
121d350ecf5Sriastradh 	u32 temp = hwsq_rd32(ram, reg);
122d350ecf5Sriastradh 	if (temp != ((temp & ~mask) | data) || reg->force)
123d350ecf5Sriastradh 		hwsq_wr32(ram, reg, (temp & ~mask) | data);
124d350ecf5Sriastradh 	return temp;
125d350ecf5Sriastradh }
126d350ecf5Sriastradh 
127d350ecf5Sriastradh static inline void
hwsq_setf(struct hwsq * ram,u8 flag,int data)128d350ecf5Sriastradh hwsq_setf(struct hwsq *ram, u8 flag, int data)
129d350ecf5Sriastradh {
130d350ecf5Sriastradh 	nvkm_hwsq_setf(ram->hwsq, flag, data);
131d350ecf5Sriastradh }
132d350ecf5Sriastradh 
133d350ecf5Sriastradh static inline void
hwsq_wait(struct hwsq * ram,u8 flag,u8 data)134d350ecf5Sriastradh hwsq_wait(struct hwsq *ram, u8 flag, u8 data)
135d350ecf5Sriastradh {
136d350ecf5Sriastradh 	nvkm_hwsq_wait(ram->hwsq, flag, data);
137d350ecf5Sriastradh }
138d350ecf5Sriastradh 
139d350ecf5Sriastradh static inline void
hwsq_wait_vblank(struct hwsq * ram)140d350ecf5Sriastradh hwsq_wait_vblank(struct hwsq *ram)
141d350ecf5Sriastradh {
142d350ecf5Sriastradh 	nvkm_hwsq_wait_vblank(ram->hwsq);
143d350ecf5Sriastradh }
144d350ecf5Sriastradh 
145d350ecf5Sriastradh static inline void
hwsq_nsec(struct hwsq * ram,u32 nsec)146d350ecf5Sriastradh hwsq_nsec(struct hwsq *ram, u32 nsec)
147d350ecf5Sriastradh {
148d350ecf5Sriastradh 	nvkm_hwsq_nsec(ram->hwsq, nsec);
149d350ecf5Sriastradh }
150d350ecf5Sriastradh #endif
151