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