Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[sfrench/cifs-2.6.git] / drivers / gpu / drm / vmwgfx / vmwgfx_drv.h
1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * 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  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28 #ifndef _VMWGFX_DRV_H_
29 #define _VMWGFX_DRV_H_
30
31 #include "vmwgfx_reg.h"
32 #include "drmP.h"
33 #include "vmwgfx_drm.h"
34 #include "drm_hashtab.h"
35 #include "linux/suspend.h"
36 #include "ttm/ttm_bo_driver.h"
37 #include "ttm/ttm_object.h"
38 #include "ttm/ttm_lock.h"
39 #include "ttm/ttm_execbuf_util.h"
40 #include "ttm/ttm_module.h"
41
42 #define VMWGFX_DRIVER_DATE "20100209"
43 #define VMWGFX_DRIVER_MAJOR 1
44 #define VMWGFX_DRIVER_MINOR 2
45 #define VMWGFX_DRIVER_PATCHLEVEL 0
46 #define VMWGFX_FILE_PAGE_OFFSET 0x00100000
47 #define VMWGFX_FIFO_STATIC_SIZE (1024*1024)
48 #define VMWGFX_MAX_RELOCATIONS 2048
49 #define VMWGFX_MAX_GMRS 2048
50 #define VMWGFX_MAX_DISPLAYS 16
51
52 struct vmw_fpriv {
53         struct drm_master *locked_master;
54         struct ttm_object_file *tfile;
55 };
56
57 struct vmw_dma_buffer {
58         struct ttm_buffer_object base;
59         struct list_head validate_list;
60         struct list_head gmr_lru;
61         uint32_t gmr_id;
62         bool gmr_bound;
63         uint32_t cur_validate_node;
64         bool on_validate_list;
65 };
66
67 struct vmw_resource {
68         struct kref kref;
69         struct vmw_private *dev_priv;
70         struct idr *idr;
71         int id;
72         enum ttm_object_type res_type;
73         bool avail;
74         void (*hw_destroy) (struct vmw_resource *res);
75         void (*res_free) (struct vmw_resource *res);
76
77         /* TODO is a generic snooper needed? */
78 #if 0
79         void (*snoop)(struct vmw_resource *res,
80                       struct ttm_object_file *tfile,
81                       SVGA3dCmdHeader *header);
82         void *snoop_priv;
83 #endif
84 };
85
86 struct vmw_cursor_snooper {
87         struct drm_crtc *crtc;
88         size_t age;
89         uint32_t *image;
90 };
91
92 struct vmw_surface {
93         struct vmw_resource res;
94         uint32_t flags;
95         uint32_t format;
96         uint32_t mip_levels[DRM_VMW_MAX_SURFACE_FACES];
97         struct drm_vmw_size *sizes;
98         uint32_t num_sizes;
99
100         bool scanout;
101
102         /* TODO so far just a extra pointer */
103         struct vmw_cursor_snooper snooper;
104 };
105
106 struct vmw_fence_queue {
107         struct list_head head;
108         struct timespec lag;
109         struct timespec lag_time;
110         spinlock_t lock;
111 };
112
113 struct vmw_fifo_state {
114         unsigned long reserved_size;
115         __le32 *dynamic_buffer;
116         __le32 *static_buffer;
117         __le32 *last_buffer;
118         uint32_t last_data_size;
119         uint32_t last_buffer_size;
120         bool last_buffer_add;
121         unsigned long static_buffer_size;
122         bool using_bounce_buffer;
123         uint32_t capabilities;
124         struct mutex fifo_mutex;
125         struct rw_semaphore rwsem;
126         struct vmw_fence_queue fence_queue;
127 };
128
129 struct vmw_relocation {
130         SVGAGuestPtr *location;
131         uint32_t index;
132 };
133
134 struct vmw_sw_context{
135         struct ida bo_list;
136         uint32_t last_cid;
137         bool cid_valid;
138         uint32_t last_sid;
139         uint32_t sid_translation;
140         bool sid_valid;
141         struct ttm_object_file *tfile;
142         struct list_head validate_nodes;
143         struct vmw_relocation relocs[VMWGFX_MAX_RELOCATIONS];
144         uint32_t cur_reloc;
145         struct ttm_validate_buffer val_bufs[VMWGFX_MAX_GMRS];
146         uint32_t cur_val_buf;
147 };
148
149 struct vmw_legacy_display;
150 struct vmw_overlay;
151
152 struct vmw_master {
153         struct ttm_lock lock;
154 };
155
156 struct vmw_vga_topology_state {
157         uint32_t width;
158         uint32_t height;
159         uint32_t primary;
160         uint32_t pos_x;
161         uint32_t pos_y;
162 };
163
164 struct vmw_private {
165         struct ttm_bo_device bdev;
166         struct ttm_bo_global_ref bo_global_ref;
167         struct ttm_global_reference mem_global_ref;
168
169         struct vmw_fifo_state fifo;
170
171         struct drm_device *dev;
172         unsigned long vmw_chipset;
173         unsigned int io_start;
174         uint32_t vram_start;
175         uint32_t vram_size;
176         uint32_t mmio_start;
177         uint32_t mmio_size;
178         uint32_t fb_max_width;
179         uint32_t fb_max_height;
180         __le32 __iomem *mmio_virt;
181         int mmio_mtrr;
182         uint32_t capabilities;
183         uint32_t max_gmr_descriptors;
184         uint32_t max_gmr_ids;
185         struct mutex hw_mutex;
186
187         /*
188          * VGA registers.
189          */
190
191         struct vmw_vga_topology_state vga_save[VMWGFX_MAX_DISPLAYS];
192         uint32_t vga_width;
193         uint32_t vga_height;
194         uint32_t vga_depth;
195         uint32_t vga_bpp;
196         uint32_t vga_pseudo;
197         uint32_t vga_red_mask;
198         uint32_t vga_green_mask;
199         uint32_t vga_blue_mask;
200         uint32_t vga_bpl;
201         uint32_t vga_pitchlock;
202
203         uint32_t num_displays;
204
205         /*
206          * Framebuffer info.
207          */
208
209         void *fb_info;
210         struct vmw_legacy_display *ldu_priv;
211         struct vmw_overlay *overlay_priv;
212
213         /*
214          * Context and surface management.
215          */
216
217         rwlock_t resource_lock;
218         struct idr context_idr;
219         struct idr surface_idr;
220         struct idr stream_idr;
221
222         /*
223          * Block lastclose from racing with firstopen.
224          */
225
226         struct mutex init_mutex;
227
228         /*
229          * A resource manager for kernel-only surfaces and
230          * contexts.
231          */
232
233         struct ttm_object_device *tdev;
234
235         /*
236          * Fencing and IRQs.
237          */
238
239         atomic_t fence_seq;
240         wait_queue_head_t fence_queue;
241         wait_queue_head_t fifo_queue;
242         atomic_t fence_queue_waiters;
243         atomic_t fifo_queue_waiters;
244         uint32_t last_read_sequence;
245         spinlock_t irq_lock;
246
247         /*
248          * Device state
249          */
250
251         uint32_t traces_state;
252         uint32_t enable_state;
253         uint32_t config_done_state;
254
255         /**
256          * Execbuf
257          */
258         /**
259          * Protected by the cmdbuf mutex.
260          */
261
262         struct vmw_sw_context ctx;
263         uint32_t val_seq;
264         struct mutex cmdbuf_mutex;
265
266         /**
267          * GMR management. Protected by the lru spinlock.
268          */
269
270         struct ida gmr_ida;
271         struct list_head gmr_lru;
272
273
274         /**
275          * Operating mode.
276          */
277
278         bool stealth;
279         bool is_opened;
280
281         /**
282          * Master management.
283          */
284
285         struct vmw_master *active_master;
286         struct vmw_master fbdev_master;
287         struct notifier_block pm_nb;
288 };
289
290 static inline struct vmw_private *vmw_priv(struct drm_device *dev)
291 {
292         return (struct vmw_private *)dev->dev_private;
293 }
294
295 static inline struct vmw_fpriv *vmw_fpriv(struct drm_file *file_priv)
296 {
297         return (struct vmw_fpriv *)file_priv->driver_priv;
298 }
299
300 static inline struct vmw_master *vmw_master(struct drm_master *master)
301 {
302         return (struct vmw_master *) master->driver_priv;
303 }
304
305 static inline void vmw_write(struct vmw_private *dev_priv,
306                              unsigned int offset, uint32_t value)
307 {
308         outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
309         outl(value, dev_priv->io_start + VMWGFX_VALUE_PORT);
310 }
311
312 static inline uint32_t vmw_read(struct vmw_private *dev_priv,
313                                 unsigned int offset)
314 {
315         uint32_t val;
316
317         outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
318         val = inl(dev_priv->io_start + VMWGFX_VALUE_PORT);
319         return val;
320 }
321
322 /**
323  * GMR utilities - vmwgfx_gmr.c
324  */
325
326 extern int vmw_gmr_bind(struct vmw_private *dev_priv,
327                         struct ttm_buffer_object *bo);
328 extern void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id);
329
330 /**
331  * Resource utilities - vmwgfx_resource.c
332  */
333
334 extern struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv);
335 extern void vmw_resource_unreference(struct vmw_resource **p_res);
336 extern struct vmw_resource *vmw_resource_reference(struct vmw_resource *res);
337 extern int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
338                                      struct drm_file *file_priv);
339 extern int vmw_context_define_ioctl(struct drm_device *dev, void *data,
340                                     struct drm_file *file_priv);
341 extern int vmw_context_check(struct vmw_private *dev_priv,
342                              struct ttm_object_file *tfile,
343                              int id);
344 extern void vmw_surface_res_free(struct vmw_resource *res);
345 extern int vmw_surface_init(struct vmw_private *dev_priv,
346                             struct vmw_surface *srf,
347                             void (*res_free) (struct vmw_resource *res));
348 extern int vmw_user_surface_lookup_handle(struct vmw_private *dev_priv,
349                                           struct ttm_object_file *tfile,
350                                           uint32_t handle,
351                                           struct vmw_surface **out);
352 extern int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
353                                      struct drm_file *file_priv);
354 extern int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
355                                     struct drm_file *file_priv);
356 extern int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
357                                        struct drm_file *file_priv);
358 extern int vmw_surface_check(struct vmw_private *dev_priv,
359                              struct ttm_object_file *tfile,
360                              uint32_t handle, int *id);
361 extern void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo);
362 extern int vmw_dmabuf_init(struct vmw_private *dev_priv,
363                            struct vmw_dma_buffer *vmw_bo,
364                            size_t size, struct ttm_placement *placement,
365                            bool interuptable,
366                            void (*bo_free) (struct ttm_buffer_object *bo));
367 extern int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
368                                   struct drm_file *file_priv);
369 extern int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
370                                   struct drm_file *file_priv);
371 extern uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
372                                          uint32_t cur_validate_node);
373 extern void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo);
374 extern int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
375                                   uint32_t id, struct vmw_dma_buffer **out);
376 extern uint32_t vmw_dmabuf_gmr(struct ttm_buffer_object *bo);
377 extern void vmw_dmabuf_set_gmr(struct ttm_buffer_object *bo, uint32_t id);
378 extern int vmw_gmr_id_alloc(struct vmw_private *dev_priv, uint32_t *p_id);
379 extern int vmw_dmabuf_to_start_of_vram(struct vmw_private *vmw_priv,
380                                        struct vmw_dma_buffer *bo);
381 extern int vmw_dmabuf_from_vram(struct vmw_private *vmw_priv,
382                                 struct vmw_dma_buffer *bo);
383 extern void vmw_dmabuf_gmr_unbind(struct ttm_buffer_object *bo);
384 extern int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
385                                   struct drm_file *file_priv);
386 extern int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
387                                   struct drm_file *file_priv);
388 extern int vmw_user_stream_lookup(struct vmw_private *dev_priv,
389                                   struct ttm_object_file *tfile,
390                                   uint32_t *inout_id,
391                                   struct vmw_resource **out);
392
393
394 /**
395  * Misc Ioctl functionality - vmwgfx_ioctl.c
396  */
397
398 extern int vmw_getparam_ioctl(struct drm_device *dev, void *data,
399                               struct drm_file *file_priv);
400 extern int vmw_fifo_debug_ioctl(struct drm_device *dev, void *data,
401                                 struct drm_file *file_priv);
402
403 /**
404  * Fifo utilities - vmwgfx_fifo.c
405  */
406
407 extern int vmw_fifo_init(struct vmw_private *dev_priv,
408                          struct vmw_fifo_state *fifo);
409 extern void vmw_fifo_release(struct vmw_private *dev_priv,
410                              struct vmw_fifo_state *fifo);
411 extern void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes);
412 extern void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes);
413 extern int vmw_fifo_send_fence(struct vmw_private *dev_priv,
414                                uint32_t *sequence);
415 extern void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason);
416 extern int vmw_fifo_mmap(struct file *filp, struct vm_area_struct *vma);
417 extern bool vmw_fifo_have_3d(struct vmw_private *dev_priv);
418 extern bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv);
419
420 /**
421  * TTM glue - vmwgfx_ttm_glue.c
422  */
423
424 extern int vmw_ttm_global_init(struct vmw_private *dev_priv);
425 extern void vmw_ttm_global_release(struct vmw_private *dev_priv);
426 extern int vmw_mmap(struct file *filp, struct vm_area_struct *vma);
427
428 /**
429  * TTM buffer object driver - vmwgfx_buffer.c
430  */
431
432 extern struct ttm_placement vmw_vram_placement;
433 extern struct ttm_placement vmw_vram_ne_placement;
434 extern struct ttm_placement vmw_vram_sys_placement;
435 extern struct ttm_placement vmw_sys_placement;
436 extern struct ttm_bo_driver vmw_bo_driver;
437 extern int vmw_dma_quiescent(struct drm_device *dev);
438
439 /**
440  * Command submission - vmwgfx_execbuf.c
441  */
442
443 extern int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
444                              struct drm_file *file_priv);
445
446 /**
447  * IRQs and wating - vmwgfx_irq.c
448  */
449
450 extern irqreturn_t vmw_irq_handler(DRM_IRQ_ARGS);
451 extern int vmw_wait_fence(struct vmw_private *dev_priv, bool lazy,
452                           uint32_t sequence, bool interruptible,
453                           unsigned long timeout);
454 extern void vmw_irq_preinstall(struct drm_device *dev);
455 extern int vmw_irq_postinstall(struct drm_device *dev);
456 extern void vmw_irq_uninstall(struct drm_device *dev);
457 extern bool vmw_fence_signaled(struct vmw_private *dev_priv,
458                                uint32_t sequence);
459 extern int vmw_fence_wait_ioctl(struct drm_device *dev, void *data,
460                                 struct drm_file *file_priv);
461 extern int vmw_fallback_wait(struct vmw_private *dev_priv,
462                              bool lazy,
463                              bool fifo_idle,
464                              uint32_t sequence,
465                              bool interruptible,
466                              unsigned long timeout);
467 extern void vmw_update_sequence(struct vmw_private *dev_priv,
468                                 struct vmw_fifo_state *fifo_state);
469
470
471 /**
472  * Rudimentary fence objects currently used only for throttling -
473  * vmwgfx_fence.c
474  */
475
476 extern void vmw_fence_queue_init(struct vmw_fence_queue *queue);
477 extern void vmw_fence_queue_takedown(struct vmw_fence_queue *queue);
478 extern int vmw_fence_push(struct vmw_fence_queue *queue,
479                           uint32_t sequence);
480 extern int vmw_fence_pull(struct vmw_fence_queue *queue,
481                           uint32_t signaled_sequence);
482 extern int vmw_wait_lag(struct vmw_private *dev_priv,
483                         struct vmw_fence_queue *queue, uint32_t us);
484
485 /**
486  * Kernel framebuffer - vmwgfx_fb.c
487  */
488
489 int vmw_fb_init(struct vmw_private *vmw_priv);
490 int vmw_fb_close(struct vmw_private *dev_priv);
491 int vmw_fb_off(struct vmw_private *vmw_priv);
492 int vmw_fb_on(struct vmw_private *vmw_priv);
493
494 /**
495  * Kernel modesetting - vmwgfx_kms.c
496  */
497
498 int vmw_kms_init(struct vmw_private *dev_priv);
499 int vmw_kms_close(struct vmw_private *dev_priv);
500 int vmw_kms_save_vga(struct vmw_private *vmw_priv);
501 int vmw_kms_restore_vga(struct vmw_private *vmw_priv);
502 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
503                                 struct drm_file *file_priv);
504 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv);
505 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
506                           struct ttm_object_file *tfile,
507                           struct ttm_buffer_object *bo,
508                           SVGA3dCmdHeader *header);
509 void vmw_kms_write_svga(struct vmw_private *vmw_priv,
510                         unsigned width, unsigned height, unsigned pitch,
511                         unsigned bbp, unsigned depth);
512 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
513                                 struct drm_file *file_priv);
514
515 /**
516  * Overlay control - vmwgfx_overlay.c
517  */
518
519 int vmw_overlay_init(struct vmw_private *dev_priv);
520 int vmw_overlay_close(struct vmw_private *dev_priv);
521 int vmw_overlay_ioctl(struct drm_device *dev, void *data,
522                       struct drm_file *file_priv);
523 int vmw_overlay_stop_all(struct vmw_private *dev_priv);
524 int vmw_overlay_resume_all(struct vmw_private *dev_priv);
525 int vmw_overlay_pause_all(struct vmw_private *dev_priv);
526 int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out);
527 int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id);
528 int vmw_overlay_num_overlays(struct vmw_private *dev_priv);
529 int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv);
530
531 /**
532  * Inline helper functions
533  */
534
535 static inline void vmw_surface_unreference(struct vmw_surface **srf)
536 {
537         struct vmw_surface *tmp_srf = *srf;
538         struct vmw_resource *res = &tmp_srf->res;
539         *srf = NULL;
540
541         vmw_resource_unreference(&res);
542 }
543
544 static inline struct vmw_surface *vmw_surface_reference(struct vmw_surface *srf)
545 {
546         (void) vmw_resource_reference(&srf->res);
547         return srf;
548 }
549
550 static inline void vmw_dmabuf_unreference(struct vmw_dma_buffer **buf)
551 {
552         struct vmw_dma_buffer *tmp_buf = *buf;
553         struct ttm_buffer_object *bo = &tmp_buf->base;
554         *buf = NULL;
555
556         ttm_bo_unref(&bo);
557 }
558
559 static inline struct vmw_dma_buffer *vmw_dmabuf_reference(struct vmw_dma_buffer *buf)
560 {
561         if (ttm_bo_reference(&buf->base))
562                 return buf;
563         return NULL;
564 }
565
566 #endif