1/*	$NetBSD: via_drv.h,v 1.7 2021/12/19 12:30:23 riastradh Exp $	*/
2
3/*
4 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
5 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sub license,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26#ifndef _VIA_DRV_H_
27#define _VIA_DRV_H_
28
29#include <linux/irqreturn.h>
30#include <linux/jiffies.h>
31#include <linux/sched.h>
32#include <linux/sched/signal.h>
33#include <linux/wait.h>
34
35#include <drm/drm_ioctl.h>
36#include <drm/drm_legacy.h>
37#include <drm/drm_mm.h>
38#include <drm/via_drm.h>
39
40#define DRIVER_AUTHOR	"Various"
41
42#define DRIVER_NAME		"via"
43#define DRIVER_DESC		"VIA Unichrome / Pro"
44#define DRIVER_DATE		"20070202"
45
46#define DRIVER_MAJOR		2
47#define DRIVER_MINOR		11
48#define DRIVER_PATCHLEVEL	1
49
50#include "via_verifier.h"
51
52#include "via_dmablit.h"
53
54#define VIA_PCI_BUF_SIZE 60000
55#define VIA_FIRE_BUF_SIZE  1024
56#define VIA_NUM_IRQS 4
57
58typedef struct drm_via_ring_buffer {
59	drm_local_map_t map;
60	char *virtual_start;
61} drm_via_ring_buffer_t;
62
63typedef uint32_t maskarray_t[5];
64
65typedef struct drm_via_irq {
66#ifdef __NetBSD__
67	spinlock_t irq_lock;
68	unsigned irq_received;
69#else
70	atomic_t irq_received;
71#endif
72	uint32_t pending_mask;
73	uint32_t enable_mask;
74#ifdef __NetBSD__
75	drm_waitqueue_t irq_queue;
76#else
77	wait_queue_head_t irq_queue;
78#endif
79} drm_via_irq_t;
80
81typedef struct drm_via_private {
82	drm_via_sarea_t *sarea_priv;
83	drm_local_map_t *sarea;
84	drm_local_map_t *fb;
85	drm_local_map_t *mmio;
86	unsigned long agpAddr;
87#ifdef __NetBSD__
88	spinlock_t decoder_lock[VIA_NR_XVMC_LOCKS];
89	drm_waitqueue_t decoder_queue[VIA_NR_XVMC_LOCKS];
90#else
91	wait_queue_head_t decoder_queue[VIA_NR_XVMC_LOCKS];
92#endif
93	char *dma_ptr;
94	unsigned int dma_low;
95	unsigned int dma_high;
96	unsigned int dma_offset;
97	uint32_t dma_wrap;
98	volatile uint32_t *last_pause_ptr;
99	volatile uint32_t *hw_addr_ptr;
100	drm_via_ring_buffer_t ring;
101	ktime_t last_vblank;
102	int last_vblank_valid;
103	ktime_t nsec_per_vblank;
104	atomic_t vbl_received;
105	drm_via_state_t hc_state;
106	char pci_buf[VIA_PCI_BUF_SIZE];
107	const uint32_t *fire_offsets[VIA_FIRE_BUF_SIZE];
108	uint32_t num_fire_offsets;
109	int chipset;
110	drm_via_irq_t via_irqs[VIA_NUM_IRQS];
111	unsigned num_irqs;
112	maskarray_t *irq_masks;
113	uint32_t irq_enable_mask;
114	uint32_t irq_pending_mask;
115	int *irq_map;
116	unsigned int idle_fault;
117	int vram_initialized;
118	struct drm_mm vram_mm;
119	int agp_initialized;
120	struct drm_mm agp_mm;
121	/** Mapping of userspace keys to mm objects */
122	struct idr object_idr;
123	unsigned long vram_offset;
124	unsigned long agp_offset;
125	drm_via_blitq_t blit_queues[VIA_NUM_BLIT_ENGINES];
126	uint32_t dma_diff;
127} drm_via_private_t;
128
129struct via_file_private {
130	struct list_head obj_list;
131};
132
133enum via_family {
134  VIA_OTHER = 0,     /* Baseline */
135  VIA_PRO_GROUP_A,   /* Another video engine and DMA commands */
136  VIA_DX9_0          /* Same video as pro_group_a, but 3D is unsupported */
137};
138
139/* VIA MMIO register access */
140static inline u32 via_read(struct drm_via_private *dev_priv, u32 reg)
141{
142	return DRM_READ32(dev_priv->mmio, reg);
143}
144
145static inline void via_write(struct drm_via_private *dev_priv, u32 reg,
146			     u32 val)
147{
148	DRM_WRITE32(dev_priv->mmio, reg, val);
149}
150
151static inline void via_write8(struct drm_via_private *dev_priv, u32 reg,
152			      u32 val)
153{
154	DRM_WRITE8(dev_priv->mmio, reg, val);
155}
156
157static inline void via_write8_mask(struct drm_via_private *dev_priv,
158				   u32 reg, u32 mask, u32 val)
159{
160	u32 tmp;
161
162	tmp = DRM_READ8(dev_priv->mmio, reg);
163	tmp = (tmp & ~mask) | (val & mask);
164	DRM_WRITE8(dev_priv->mmio, reg, tmp);
165}
166
167/*
168 * Poll in a loop waiting for 'contidition' to be true.
169 * Note: A direct replacement with wait_event_interruptible_timeout()
170 *       will not work unless driver is updated to emit wake_up()
171 *       in relevant places that can impact the 'condition'
172 *
173 * Returns:
174 *   ret keeps current value if 'condition' becomes true
175 *   ret = -BUSY if timeout happens
176 *   ret = -EINTR if a signal interrupted the waiting period
177 */
178#define VIA_WAIT_ON( ret, queue, timeout, condition )		\
179do {								\
180	DECLARE_WAITQUEUE(entry, current);			\
181	unsigned long end = jiffies + (timeout);		\
182	add_wait_queue(&(queue), &entry);			\
183								\
184	for (;;) {						\
185		__set_current_state(TASK_INTERRUPTIBLE);	\
186		if (condition)					\
187			break;					\
188		if (time_after_eq(jiffies, end)) {		\
189			ret = -EBUSY;				\
190			break;					\
191		}						\
192		schedule_timeout((HZ/100 > 1) ? HZ/100 : 1);	\
193		if (signal_pending(current)) {			\
194			ret = -EINTR;				\
195			break;					\
196		}						\
197	}							\
198	__set_current_state(TASK_RUNNING);			\
199	remove_wait_queue(&(queue), &entry);			\
200} while (0)
201
202extern const struct drm_ioctl_desc via_ioctls[];
203extern int via_max_ioctl;
204
205extern int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv);
206extern int via_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv);
207extern int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv);
208extern int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv);
209extern int via_map_init(struct drm_device *dev, void *data, struct drm_file *file_priv);
210extern int via_decoder_futex(struct drm_device *dev, void *data, struct drm_file *file_priv);
211extern int via_wait_irq(struct drm_device *dev, void *data, struct drm_file *file_priv);
212extern int via_dma_blit_sync(struct drm_device *dev, void *data, struct drm_file *file_priv);
213extern int via_dma_blit(struct drm_device *dev, void *data, struct drm_file *file_priv);
214
215extern int via_driver_load(struct drm_device *dev, unsigned long chipset);
216extern void via_driver_unload(struct drm_device *dev);
217
218extern int via_init_context(struct drm_device *dev, int context);
219extern int via_final_context(struct drm_device *dev, int context);
220
221extern int via_do_cleanup_map(struct drm_device *dev);
222extern u32 via_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
223extern int via_enable_vblank(struct drm_device *dev, unsigned int pipe);
224extern void via_disable_vblank(struct drm_device *dev, unsigned int pipe);
225
226extern irqreturn_t via_driver_irq_handler(DRM_IRQ_ARGS);
227extern void via_driver_irq_preinstall(struct drm_device *dev);
228extern int via_driver_irq_postinstall(struct drm_device *dev);
229extern void via_driver_irq_uninstall(struct drm_device *dev);
230
231extern int via_dma_cleanup(struct drm_device *dev);
232extern void via_init_command_verifier(void);
233extern int via_driver_dma_quiescent(struct drm_device *dev);
234extern void via_init_futex(drm_via_private_t *dev_priv);
235extern void via_cleanup_futex(drm_via_private_t *dev_priv);
236extern void via_release_futex(drm_via_private_t *dev_priv, int context);
237
238extern void via_reclaim_buffers_locked(struct drm_device *dev,
239				       struct drm_file *file_priv);
240extern void via_lastclose(struct drm_device *dev);
241
242extern void via_dmablit_handler(struct drm_device *dev, int engine, int from_irq);
243extern void via_init_dmablit(struct drm_device *dev);
244
245#endif
246