1*b7019ac5SIlia Mirkin /* SPDX-License-Identifier: MIT */
2c39f472eSBen Skeggs #ifndef __NVKM_BUS_HWSQ_H__
3c39f472eSBen Skeggs #define __NVKM_BUS_HWSQ_H__
4c39f472eSBen Skeggs #include <subdev/bus.h>
5c39f472eSBen Skeggs
6c39f472eSBen Skeggs struct hwsq {
75f8824deSBen Skeggs struct nvkm_subdev *subdev;
85f8824deSBen Skeggs struct nvkm_hwsq *hwsq;
9c39f472eSBen Skeggs int sequence;
10c39f472eSBen Skeggs };
11c39f472eSBen Skeggs
12c39f472eSBen Skeggs struct hwsq_reg {
13c39f472eSBen Skeggs int sequence;
14c39f472eSBen Skeggs bool force;
153834b632SRoy Spliet u32 addr;
163834b632SRoy Spliet u32 stride; /* in bytes */
173834b632SRoy Spliet u32 mask;
18c39f472eSBen Skeggs u32 data;
19c39f472eSBen Skeggs };
20c39f472eSBen Skeggs
21c39f472eSBen Skeggs static inline struct hwsq_reg
hwsq_stride(u32 addr,u32 stride,u32 mask)223834b632SRoy Spliet hwsq_stride(u32 addr, u32 stride, u32 mask)
233834b632SRoy Spliet {
243834b632SRoy Spliet return (struct hwsq_reg) {
253834b632SRoy Spliet .sequence = 0,
263834b632SRoy Spliet .force = 0,
273834b632SRoy Spliet .addr = addr,
283834b632SRoy Spliet .stride = stride,
293834b632SRoy Spliet .mask = mask,
303834b632SRoy Spliet .data = 0xdeadbeef,
313834b632SRoy Spliet };
323834b632SRoy Spliet }
333834b632SRoy Spliet
343834b632SRoy Spliet static inline struct hwsq_reg
hwsq_reg2(u32 addr1,u32 addr2)35c39f472eSBen Skeggs hwsq_reg2(u32 addr1, u32 addr2)
36c39f472eSBen Skeggs {
37c39f472eSBen Skeggs return (struct hwsq_reg) {
38c39f472eSBen Skeggs .sequence = 0,
39c39f472eSBen Skeggs .force = 0,
403834b632SRoy Spliet .addr = addr1,
413834b632SRoy Spliet .stride = addr2 - addr1,
423834b632SRoy Spliet .mask = 0x3,
43c39f472eSBen Skeggs .data = 0xdeadbeef,
44c39f472eSBen Skeggs };
45c39f472eSBen Skeggs }
46c39f472eSBen Skeggs
47c39f472eSBen Skeggs static inline struct hwsq_reg
hwsq_reg(u32 addr)48c39f472eSBen Skeggs hwsq_reg(u32 addr)
49c39f472eSBen Skeggs {
503834b632SRoy Spliet return (struct hwsq_reg) {
513834b632SRoy Spliet .sequence = 0,
523834b632SRoy Spliet .force = 0,
533834b632SRoy Spliet .addr = addr,
543834b632SRoy Spliet .stride = 0,
553834b632SRoy Spliet .mask = 0x1,
563834b632SRoy Spliet .data = 0xdeadbeef,
573834b632SRoy Spliet };
58c39f472eSBen Skeggs }
59c39f472eSBen Skeggs
60c39f472eSBen Skeggs static inline int
hwsq_init(struct hwsq * ram,struct nvkm_subdev * subdev)615f8824deSBen Skeggs hwsq_init(struct hwsq *ram, struct nvkm_subdev *subdev)
62c39f472eSBen Skeggs {
63c39f472eSBen Skeggs int ret;
64c39f472eSBen Skeggs
65bb23f9d7SBen Skeggs ret = nvkm_hwsq_init(subdev, &ram->hwsq);
66c39f472eSBen Skeggs if (ret)
67c39f472eSBen Skeggs return ret;
68c39f472eSBen Skeggs
69c39f472eSBen Skeggs ram->sequence++;
70c39f472eSBen Skeggs ram->subdev = subdev;
71c39f472eSBen Skeggs return 0;
72c39f472eSBen Skeggs }
73c39f472eSBen Skeggs
74c39f472eSBen Skeggs static inline int
hwsq_exec(struct hwsq * ram,bool exec)75c39f472eSBen Skeggs hwsq_exec(struct hwsq *ram, bool exec)
76c39f472eSBen Skeggs {
77c39f472eSBen Skeggs int ret = 0;
78c39f472eSBen Skeggs if (ram->subdev) {
795f8824deSBen Skeggs ret = nvkm_hwsq_fini(&ram->hwsq, exec);
80c39f472eSBen Skeggs ram->subdev = NULL;
81c39f472eSBen Skeggs }
82c39f472eSBen Skeggs return ret;
83c39f472eSBen Skeggs }
84c39f472eSBen Skeggs
85c39f472eSBen Skeggs static inline u32
hwsq_rd32(struct hwsq * ram,struct hwsq_reg * reg)86c39f472eSBen Skeggs hwsq_rd32(struct hwsq *ram, struct hwsq_reg *reg)
87c39f472eSBen Skeggs {
8814caba44SBen Skeggs struct nvkm_device *device = ram->subdev->device;
89c39f472eSBen Skeggs if (reg->sequence != ram->sequence)
9014caba44SBen Skeggs reg->data = nvkm_rd32(device, reg->addr);
91c39f472eSBen Skeggs return reg->data;
92c39f472eSBen Skeggs }
93c39f472eSBen Skeggs
94c39f472eSBen Skeggs static inline void
hwsq_wr32(struct hwsq * ram,struct hwsq_reg * reg,u32 data)95c39f472eSBen Skeggs hwsq_wr32(struct hwsq *ram, struct hwsq_reg *reg, u32 data)
96c39f472eSBen Skeggs {
973834b632SRoy Spliet u32 mask, off = 0;
983834b632SRoy Spliet
99c39f472eSBen Skeggs reg->sequence = ram->sequence;
100c39f472eSBen Skeggs reg->data = data;
1013834b632SRoy Spliet
1023834b632SRoy Spliet for (mask = reg->mask; mask > 0; mask = (mask & ~1) >> 1) {
1033834b632SRoy Spliet if (mask & 1)
1043834b632SRoy Spliet nvkm_hwsq_wr32(ram->hwsq, reg->addr+off, reg->data);
1053834b632SRoy Spliet
1063834b632SRoy Spliet off += reg->stride;
1073834b632SRoy Spliet }
108c39f472eSBen Skeggs }
109c39f472eSBen Skeggs
110c39f472eSBen Skeggs static inline void
hwsq_nuke(struct hwsq * ram,struct hwsq_reg * reg)111c39f472eSBen Skeggs hwsq_nuke(struct hwsq *ram, struct hwsq_reg *reg)
112c39f472eSBen Skeggs {
113c39f472eSBen Skeggs reg->force = true;
114c39f472eSBen Skeggs }
115c39f472eSBen Skeggs
116c39f472eSBen Skeggs static inline u32
hwsq_mask(struct hwsq * ram,struct hwsq_reg * reg,u32 mask,u32 data)117c39f472eSBen Skeggs hwsq_mask(struct hwsq *ram, struct hwsq_reg *reg, u32 mask, u32 data)
118c39f472eSBen Skeggs {
119c39f472eSBen Skeggs u32 temp = hwsq_rd32(ram, reg);
120c39f472eSBen Skeggs if (temp != ((temp & ~mask) | data) || reg->force)
121c39f472eSBen Skeggs hwsq_wr32(ram, reg, (temp & ~mask) | data);
122c39f472eSBen Skeggs return temp;
123c39f472eSBen Skeggs }
124c39f472eSBen Skeggs
125c39f472eSBen Skeggs static inline void
hwsq_setf(struct hwsq * ram,u8 flag,int data)126c39f472eSBen Skeggs hwsq_setf(struct hwsq *ram, u8 flag, int data)
127c39f472eSBen Skeggs {
1285f8824deSBen Skeggs nvkm_hwsq_setf(ram->hwsq, flag, data);
129c39f472eSBen Skeggs }
130c39f472eSBen Skeggs
131c39f472eSBen Skeggs static inline void
hwsq_wait(struct hwsq * ram,u8 flag,u8 data)132c39f472eSBen Skeggs hwsq_wait(struct hwsq *ram, u8 flag, u8 data)
133c39f472eSBen Skeggs {
1345f8824deSBen Skeggs nvkm_hwsq_wait(ram->hwsq, flag, data);
135c39f472eSBen Skeggs }
136c39f472eSBen Skeggs
137c39f472eSBen Skeggs static inline void
hwsq_wait_vblank(struct hwsq * ram)138271c2766SRoy Spliet hwsq_wait_vblank(struct hwsq *ram)
139271c2766SRoy Spliet {
140271c2766SRoy Spliet nvkm_hwsq_wait_vblank(ram->hwsq);
141271c2766SRoy Spliet }
142271c2766SRoy Spliet
143271c2766SRoy Spliet static inline void
hwsq_nsec(struct hwsq * ram,u32 nsec)144c39f472eSBen Skeggs hwsq_nsec(struct hwsq *ram, u32 nsec)
145c39f472eSBen Skeggs {
1465f8824deSBen Skeggs nvkm_hwsq_nsec(ram->hwsq, nsec);
147c39f472eSBen Skeggs }
148c39f472eSBen Skeggs #endif
149