Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[pandora-kernel.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 drm_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         bool enable_fb;
281
282         /**
283          * Master management.
284          */
285
286         struct vmw_master *active_master;
287         struct vmw_master fbdev_master;
288         struct notifier_block pm_nb;
289
290         struct mutex release_mutex;
291         uint32_t num_3d_resources;
292 };
293
294 static inline struct vmw_private *vmw_priv(struct drm_device *dev)
295 {
296         return (struct vmw_private *)dev->dev_private;
297 }
298
299 static inline struct vmw_fpriv *vmw_fpriv(struct drm_file *file_priv)
300 {
301         return (struct vmw_fpriv *)file_priv->driver_priv;
302 }
303
304 static inline struct vmw_master *vmw_master(struct drm_master *master)
305 {
306         return (struct vmw_master *) master->driver_priv;
307 }
308
309 static inline void vmw_write(struct vmw_private *dev_priv,
310                              unsigned int offset, uint32_t value)
311 {
312         outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
313         outl(value, dev_priv->io_start + VMWGFX_VALUE_PORT);
314 }
315
316 static inline uint32_t vmw_read(struct vmw_private *dev_priv,
317                                 unsigned int offset)
318 {
319         uint32_t val;
320
321         outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
322         val = inl(dev_priv->io_start + VMWGFX_VALUE_PORT);
323         return val;
324 }
325
326 int vmw_3d_resource_inc(struct vmw_private *dev_priv);
327 void vmw_3d_resource_dec(struct vmw_private *dev_priv);
328
329 /**
330  * GMR utilities - vmwgfx_gmr.c
331  */
332
333 extern int vmw_gmr_bind(struct vmw_private *dev_priv,
334                         struct ttm_buffer_object *bo);
335 extern void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id);
336
337 /**
338  * Resource utilities - vmwgfx_resource.c
339  */
340
341 extern struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv);
342 extern void vmw_resource_unreference(struct vmw_resource **p_res);
343 extern struct vmw_resource *vmw_resource_reference(struct vmw_resource *res);
344 extern int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
345                                      struct drm_file *file_priv);
346 extern int vmw_context_define_ioctl(struct drm_device *dev, void *data,
347                                     struct drm_file *file_priv);
348 extern int vmw_context_check(struct vmw_private *dev_priv,
349                              struct ttm_object_file *tfile,
350                              int id);
351 extern void vmw_surface_res_free(struct vmw_resource *res);
352 extern int vmw_surface_init(struct vmw_private *dev_priv,
353                             struct vmw_surface *srf,
354                             void (*res_free) (struct vmw_resource *res));
355 extern int vmw_user_surface_lookup_handle(struct vmw_private *dev_priv,
356                                           struct ttm_object_file *tfile,
357                                           uint32_t handle,
358                                           struct vmw_surface **out);
359 extern int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
360                                      struct drm_file *file_priv);
361 extern int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
362                                     struct drm_file *file_priv);
363 extern int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
364                                        struct drm_file *file_priv);
365 extern int vmw_surface_check(struct vmw_private *dev_priv,
366                              struct ttm_object_file *tfile,
367                              uint32_t handle, int *id);
368 extern void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo);
369 extern int vmw_dmabuf_init(struct vmw_private *dev_priv,
370                            struct vmw_dma_buffer *vmw_bo,
371                            size_t size, struct ttm_placement *placement,
372                            bool interuptable,
373                            void (*bo_free) (struct ttm_buffer_object *bo));
374 extern int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
375                                   struct drm_file *file_priv);
376 extern int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
377                                   struct drm_file *file_priv);
378 extern uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
379                                          uint32_t cur_validate_node);
380 extern void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo);
381 extern int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
382                                   uint32_t id, struct vmw_dma_buffer **out);
383 extern uint32_t vmw_dmabuf_gmr(struct ttm_buffer_object *bo);
384 extern void vmw_dmabuf_set_gmr(struct ttm_buffer_object *bo, uint32_t id);
385 extern int vmw_gmr_id_alloc(struct vmw_private *dev_priv, uint32_t *p_id);
386 extern int vmw_dmabuf_to_start_of_vram(struct vmw_private *vmw_priv,
387                                        struct vmw_dma_buffer *bo);
388 extern int vmw_dmabuf_from_vram(struct vmw_private *vmw_priv,
389                                 struct vmw_dma_buffer *bo);
390 extern void vmw_dmabuf_gmr_unbind(struct ttm_buffer_object *bo);
391 extern int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
392                                   struct drm_file *file_priv);
393 extern int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
394                                   struct drm_file *file_priv);
395 extern int vmw_user_stream_lookup(struct vmw_private *dev_priv,
396                                   struct ttm_object_file *tfile,
397                                   uint32_t *inout_id,
398                                   struct vmw_resource **out);
399
400
401 /**
402  * Misc Ioctl functionality - vmwgfx_ioctl.c
403  */
404
405 extern int vmw_getparam_ioctl(struct drm_device *dev, void *data,
406                               struct drm_file *file_priv);
407 extern int vmw_fifo_debug_ioctl(struct drm_device *dev, void *data,
408                                 struct drm_file *file_priv);
409
410 /**
411  * Fifo utilities - vmwgfx_fifo.c
412  */
413
414 extern int vmw_fifo_init(struct vmw_private *dev_priv,
415                          struct vmw_fifo_state *fifo);
416 extern void vmw_fifo_release(struct vmw_private *dev_priv,
417                              struct vmw_fifo_state *fifo);
418 extern void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes);
419 extern void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes);
420 extern int vmw_fifo_send_fence(struct vmw_private *dev_priv,
421                                uint32_t *sequence);
422 extern void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason);
423 extern int vmw_fifo_mmap(struct file *filp, struct vm_area_struct *vma);
424 extern bool vmw_fifo_have_3d(struct vmw_private *dev_priv);
425 extern bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv);
426
427 /**
428  * TTM glue - vmwgfx_ttm_glue.c
429  */
430
431 extern int vmw_ttm_global_init(struct vmw_private *dev_priv);
432 extern void vmw_ttm_global_release(struct vmw_private *dev_priv);
433 extern int vmw_mmap(struct file *filp, struct vm_area_struct *vma);
434
435 /**
436  * TTM buffer object driver - vmwgfx_buffer.c
437  */
438
439 extern struct ttm_placement vmw_vram_placement;
440 extern struct ttm_placement vmw_vram_ne_placement;
441 extern struct ttm_placement vmw_vram_sys_placement;
442 extern struct ttm_placement vmw_sys_placement;
443 extern struct ttm_bo_driver vmw_bo_driver;
444 extern int vmw_dma_quiescent(struct drm_device *dev);
445
446 /**
447  * Command submission - vmwgfx_execbuf.c
448  */
449
450 extern int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
451                              struct drm_file *file_priv);
452
453 /**
454  * IRQs and wating - vmwgfx_irq.c
455  */
456
457 extern irqreturn_t vmw_irq_handler(DRM_IRQ_ARGS);
458 extern int vmw_wait_fence(struct vmw_private *dev_priv, bool lazy,
459                           uint32_t sequence, bool interruptible,
460                           unsigned long timeout);
461 extern void vmw_irq_preinstall(struct drm_device *dev);
462 extern int vmw_irq_postinstall(struct drm_device *dev);
463 extern void vmw_irq_uninstall(struct drm_device *dev);
464 extern bool vmw_fence_signaled(struct vmw_private *dev_priv,
465                                uint32_t sequence);
466 extern int vmw_fence_wait_ioctl(struct drm_device *dev, void *data,
467                                 struct drm_file *file_priv);
468 extern int vmw_fallback_wait(struct vmw_private *dev_priv,
469                              bool lazy,
470                              bool fifo_idle,
471                              uint32_t sequence,
472                              bool interruptible,
473                              unsigned long timeout);
474 extern void vmw_update_sequence(struct vmw_private *dev_priv,
475                                 struct vmw_fifo_state *fifo_state);
476
477
478 /**
479  * Rudimentary fence objects currently used only for throttling -
480  * vmwgfx_fence.c
481  */
482
483 extern void vmw_fence_queue_init(struct vmw_fence_queue *queue);
484 extern void vmw_fence_queue_takedown(struct vmw_fence_queue *queue);
485 extern int vmw_fence_push(struct vmw_fence_queue *queue,
486                           uint32_t sequence);
487 extern int vmw_fence_pull(struct vmw_fence_queue *queue,
488                           uint32_t signaled_sequence);
489 extern int vmw_wait_lag(struct vmw_private *dev_priv,
490                         struct vmw_fence_queue *queue, uint32_t us);
491
492 /**
493  * Kernel framebuffer - vmwgfx_fb.c
494  */
495
496 int vmw_fb_init(struct vmw_private *vmw_priv);
497 int vmw_fb_close(struct vmw_private *dev_priv);
498 int vmw_fb_off(struct vmw_private *vmw_priv);
499 int vmw_fb_on(struct vmw_private *vmw_priv);
500
501 /**
502  * Kernel modesetting - vmwgfx_kms.c
503  */
504
505 int vmw_kms_init(struct vmw_private *dev_priv);
506 int vmw_kms_close(struct vmw_private *dev_priv);
507 int vmw_kms_save_vga(struct vmw_private *vmw_priv);
508 int vmw_kms_restore_vga(struct vmw_private *vmw_priv);
509 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
510                                 struct drm_file *file_priv);
511 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv);
512 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
513                           struct ttm_object_file *tfile,
514                           struct ttm_buffer_object *bo,
515                           SVGA3dCmdHeader *header);
516 void vmw_kms_write_svga(struct vmw_private *vmw_priv,
517                         unsigned width, unsigned height, unsigned pitch,
518                         unsigned bbp, unsigned depth);
519 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
520                                 struct drm_file *file_priv);
521 u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc);
522
523 /**
524  * Overlay control - vmwgfx_overlay.c
525  */
526
527 int vmw_overlay_init(struct vmw_private *dev_priv);
528 int vmw_overlay_close(struct vmw_private *dev_priv);
529 int vmw_overlay_ioctl(struct drm_device *dev, void *data,
530                       struct drm_file *file_priv);
531 int vmw_overlay_stop_all(struct vmw_private *dev_priv);
532 int vmw_overlay_resume_all(struct vmw_private *dev_priv);
533 int vmw_overlay_pause_all(struct vmw_private *dev_priv);
534 int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out);
535 int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id);
536 int vmw_overlay_num_overlays(struct vmw_private *dev_priv);
537 int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv);
538
539 /**
540  * Inline helper functions
541  */
542
543 static inline void vmw_surface_unreference(struct vmw_surface **srf)
544 {
545         struct vmw_surface *tmp_srf = *srf;
546         struct vmw_resource *res = &tmp_srf->res;
547         *srf = NULL;
548
549         vmw_resource_unreference(&res);
550 }
551
552 static inline struct vmw_surface *vmw_surface_reference(struct vmw_surface *srf)
553 {
554         (void) vmw_resource_reference(&srf->res);
555         return srf;
556 }
557
558 static inline void vmw_dmabuf_unreference(struct vmw_dma_buffer **buf)
559 {
560         struct vmw_dma_buffer *tmp_buf = *buf;
561         struct ttm_buffer_object *bo = &tmp_buf->base;
562         *buf = NULL;
563
564         ttm_bo_unref(&bo);
565 }
566
567 static inline struct vmw_dma_buffer *vmw_dmabuf_reference(struct vmw_dma_buffer *buf)
568 {
569         if (ttm_bo_reference(&buf->base))
570                 return buf;
571         return NULL;
572 }
573
574 #endif