drm/vmwgfx: Optimize memory footprint for DMA buffers.
[safe/jmp/linux-2.6] / 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 "20090724"
43 #define VMWGFX_DRIVER_MAJOR 0
44 #define VMWGFX_DRIVER_MINOR 1
45 #define VMWGFX_DRIVER_PATCHLEVEL 2
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
51 struct vmw_fpriv {
52         struct drm_master *locked_master;
53         struct ttm_object_file *tfile;
54 };
55
56 struct vmw_dma_buffer {
57         struct ttm_buffer_object base;
58         struct list_head validate_list;
59         struct list_head gmr_lru;
60         uint32_t gmr_id;
61         bool gmr_bound;
62         uint32_t cur_validate_node;
63         bool on_validate_list;
64 };
65
66 struct vmw_resource {
67         struct kref kref;
68         struct vmw_private *dev_priv;
69         struct idr *idr;
70         int id;
71         enum ttm_object_type res_type;
72         bool avail;
73         void (*hw_destroy) (struct vmw_resource *res);
74         void (*res_free) (struct vmw_resource *res);
75
76         /* TODO is a generic snooper needed? */
77 #if 0
78         void (*snoop)(struct vmw_resource *res,
79                       struct ttm_object_file *tfile,
80                       SVGA3dCmdHeader *header);
81         void *snoop_priv;
82 #endif
83 };
84
85 struct vmw_cursor_snooper {
86         struct drm_crtc *crtc;
87         size_t age;
88         uint32_t *image;
89 };
90
91 struct vmw_surface {
92         struct vmw_resource res;
93         uint32_t flags;
94         uint32_t format;
95         uint32_t mip_levels[DRM_VMW_MAX_SURFACE_FACES];
96         struct drm_vmw_size *sizes;
97         uint32_t num_sizes;
98
99         /* TODO so far just a extra pointer */
100         struct vmw_cursor_snooper snooper;
101 };
102
103 struct vmw_fifo_state {
104         unsigned long reserved_size;
105         __le32 *dynamic_buffer;
106         __le32 *static_buffer;
107         __le32 *last_buffer;
108         uint32_t last_data_size;
109         uint32_t last_buffer_size;
110         bool last_buffer_add;
111         unsigned long static_buffer_size;
112         bool using_bounce_buffer;
113         uint32_t capabilities;
114         struct rw_semaphore rwsem;
115 };
116
117 struct vmw_relocation {
118         SVGAGuestPtr *location;
119         uint32_t index;
120 };
121
122 struct vmw_sw_context{
123         struct ida bo_list;
124         uint32_t last_cid;
125         bool cid_valid;
126         uint32_t last_sid;
127         uint32_t sid_translation;
128         bool sid_valid;
129         struct ttm_object_file *tfile;
130         struct list_head validate_nodes;
131         struct vmw_relocation relocs[VMWGFX_MAX_RELOCATIONS];
132         uint32_t cur_reloc;
133         struct ttm_validate_buffer val_bufs[VMWGFX_MAX_GMRS];
134         uint32_t cur_val_buf;
135 };
136
137 struct vmw_legacy_display;
138 struct vmw_overlay;
139
140 struct vmw_master {
141         struct ttm_lock lock;
142 };
143
144 struct vmw_private {
145         struct ttm_bo_device bdev;
146         struct ttm_bo_global_ref bo_global_ref;
147         struct ttm_global_reference mem_global_ref;
148
149         struct vmw_fifo_state fifo;
150
151         struct drm_device *dev;
152         unsigned long vmw_chipset;
153         unsigned int io_start;
154         uint32_t vram_start;
155         uint32_t vram_size;
156         uint32_t mmio_start;
157         uint32_t mmio_size;
158         uint32_t fb_max_width;
159         uint32_t fb_max_height;
160         __le32 __iomem *mmio_virt;
161         int mmio_mtrr;
162         uint32_t capabilities;
163         uint32_t max_gmr_descriptors;
164         uint32_t max_gmr_ids;
165         struct mutex hw_mutex;
166
167         /*
168          * VGA registers.
169          */
170
171         uint32_t vga_width;
172         uint32_t vga_height;
173         uint32_t vga_depth;
174         uint32_t vga_bpp;
175         uint32_t vga_pseudo;
176         uint32_t vga_red_mask;
177         uint32_t vga_blue_mask;
178         uint32_t vga_green_mask;
179
180         /*
181          * Framebuffer info.
182          */
183
184         void *fb_info;
185         struct vmw_legacy_display *ldu_priv;
186         struct vmw_overlay *overlay_priv;
187
188         /*
189          * Context and surface management.
190          */
191
192         rwlock_t resource_lock;
193         struct idr context_idr;
194         struct idr surface_idr;
195         struct idr stream_idr;
196
197         /*
198          * Block lastclose from racing with firstopen.
199          */
200
201         struct mutex init_mutex;
202
203         /*
204          * A resource manager for kernel-only surfaces and
205          * contexts.
206          */
207
208         struct ttm_object_device *tdev;
209
210         /*
211          * Fencing and IRQs.
212          */
213
214         uint32_t fence_seq;
215         wait_queue_head_t fence_queue;
216         wait_queue_head_t fifo_queue;
217         atomic_t fence_queue_waiters;
218         atomic_t fifo_queue_waiters;
219         uint32_t last_read_sequence;
220         spinlock_t irq_lock;
221
222         /*
223          * Device state
224          */
225
226         uint32_t traces_state;
227         uint32_t enable_state;
228         uint32_t config_done_state;
229
230         /**
231          * Execbuf
232          */
233         /**
234          * Protected by the cmdbuf mutex.
235          */
236
237         struct vmw_sw_context ctx;
238         uint32_t val_seq;
239         struct mutex cmdbuf_mutex;
240
241         /**
242          * GMR management. Protected by the lru spinlock.
243          */
244
245         struct ida gmr_ida;
246         struct list_head gmr_lru;
247
248
249         /**
250          * Operating mode.
251          */
252
253         bool stealth;
254         bool is_opened;
255
256         /**
257          * Master management.
258          */
259
260         struct vmw_master *active_master;
261         struct vmw_master fbdev_master;
262         struct notifier_block pm_nb;
263 };
264
265 static inline struct vmw_private *vmw_priv(struct drm_device *dev)
266 {
267         return (struct vmw_private *)dev->dev_private;
268 }
269
270 static inline struct vmw_fpriv *vmw_fpriv(struct drm_file *file_priv)
271 {
272         return (struct vmw_fpriv *)file_priv->driver_priv;
273 }
274
275 static inline struct vmw_master *vmw_master(struct drm_master *master)
276 {
277         return (struct vmw_master *) master->driver_priv;
278 }
279
280 static inline void vmw_write(struct vmw_private *dev_priv,
281                              unsigned int offset, uint32_t value)
282 {
283         outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
284         outl(value, dev_priv->io_start + VMWGFX_VALUE_PORT);
285 }
286
287 static inline uint32_t vmw_read(struct vmw_private *dev_priv,
288                                 unsigned int offset)
289 {
290         uint32_t val;
291
292         outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
293         val = inl(dev_priv->io_start + VMWGFX_VALUE_PORT);
294         return val;
295 }
296
297 /**
298  * GMR utilities - vmwgfx_gmr.c
299  */
300
301 extern int vmw_gmr_bind(struct vmw_private *dev_priv,
302                         struct ttm_buffer_object *bo);
303 extern void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id);
304
305 /**
306  * Resource utilities - vmwgfx_resource.c
307  */
308
309 extern struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv);
310 extern void vmw_resource_unreference(struct vmw_resource **p_res);
311 extern struct vmw_resource *vmw_resource_reference(struct vmw_resource *res);
312 extern int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
313                                      struct drm_file *file_priv);
314 extern int vmw_context_define_ioctl(struct drm_device *dev, void *data,
315                                     struct drm_file *file_priv);
316 extern int vmw_context_check(struct vmw_private *dev_priv,
317                              struct ttm_object_file *tfile,
318                              int id);
319 extern void vmw_surface_res_free(struct vmw_resource *res);
320 extern int vmw_surface_init(struct vmw_private *dev_priv,
321                             struct vmw_surface *srf,
322                             void (*res_free) (struct vmw_resource *res));
323 extern int vmw_user_surface_lookup_handle(struct vmw_private *dev_priv,
324                                           struct ttm_object_file *tfile,
325                                           uint32_t handle,
326                                           struct vmw_surface **out);
327 extern int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
328                                      struct drm_file *file_priv);
329 extern int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
330                                     struct drm_file *file_priv);
331 extern int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
332                                        struct drm_file *file_priv);
333 extern int vmw_surface_check(struct vmw_private *dev_priv,
334                              struct ttm_object_file *tfile,
335                              uint32_t handle, int *id);
336 extern void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo);
337 extern int vmw_dmabuf_init(struct vmw_private *dev_priv,
338                            struct vmw_dma_buffer *vmw_bo,
339                            size_t size, struct ttm_placement *placement,
340                            bool interuptable,
341                            void (*bo_free) (struct ttm_buffer_object *bo));
342 extern int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
343                                   struct drm_file *file_priv);
344 extern int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
345                                   struct drm_file *file_priv);
346 extern uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
347                                          uint32_t cur_validate_node);
348 extern void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo);
349 extern int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
350                                   uint32_t id, struct vmw_dma_buffer **out);
351 extern uint32_t vmw_dmabuf_gmr(struct ttm_buffer_object *bo);
352 extern void vmw_dmabuf_set_gmr(struct ttm_buffer_object *bo, uint32_t id);
353 extern int vmw_gmr_id_alloc(struct vmw_private *dev_priv, uint32_t *p_id);
354 extern int vmw_dmabuf_to_start_of_vram(struct vmw_private *vmw_priv,
355                                        struct vmw_dma_buffer *bo);
356 extern int vmw_dmabuf_from_vram(struct vmw_private *vmw_priv,
357                                 struct vmw_dma_buffer *bo);
358 extern void vmw_dmabuf_gmr_unbind(struct ttm_buffer_object *bo);
359 extern int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
360                                   struct drm_file *file_priv);
361 extern int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
362                                   struct drm_file *file_priv);
363 extern int vmw_user_stream_lookup(struct vmw_private *dev_priv,
364                                   struct ttm_object_file *tfile,
365                                   uint32_t *inout_id,
366                                   struct vmw_resource **out);
367
368
369 /**
370  * Misc Ioctl functionality - vmwgfx_ioctl.c
371  */
372
373 extern int vmw_getparam_ioctl(struct drm_device *dev, void *data,
374                               struct drm_file *file_priv);
375 extern int vmw_fifo_debug_ioctl(struct drm_device *dev, void *data,
376                                 struct drm_file *file_priv);
377
378 /**
379  * Fifo utilities - vmwgfx_fifo.c
380  */
381
382 extern int vmw_fifo_init(struct vmw_private *dev_priv,
383                          struct vmw_fifo_state *fifo);
384 extern void vmw_fifo_release(struct vmw_private *dev_priv,
385                              struct vmw_fifo_state *fifo);
386 extern void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes);
387 extern void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes);
388 extern int vmw_fifo_send_fence(struct vmw_private *dev_priv,
389                                uint32_t *sequence);
390 extern void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason);
391 extern int vmw_fifo_mmap(struct file *filp, struct vm_area_struct *vma);
392
393 /**
394  * TTM glue - vmwgfx_ttm_glue.c
395  */
396
397 extern int vmw_ttm_global_init(struct vmw_private *dev_priv);
398 extern void vmw_ttm_global_release(struct vmw_private *dev_priv);
399 extern int vmw_mmap(struct file *filp, struct vm_area_struct *vma);
400
401 /**
402  * TTM buffer object driver - vmwgfx_buffer.c
403  */
404
405 extern struct ttm_placement vmw_vram_placement;
406 extern struct ttm_placement vmw_vram_ne_placement;
407 extern struct ttm_placement vmw_vram_sys_placement;
408 extern struct ttm_placement vmw_sys_placement;
409 extern struct ttm_bo_driver vmw_bo_driver;
410 extern int vmw_dma_quiescent(struct drm_device *dev);
411
412 /**
413  * Command submission - vmwgfx_execbuf.c
414  */
415
416 extern int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
417                              struct drm_file *file_priv);
418
419 /**
420  * IRQs and wating - vmwgfx_irq.c
421  */
422
423 extern irqreturn_t vmw_irq_handler(DRM_IRQ_ARGS);
424 extern int vmw_wait_fence(struct vmw_private *dev_priv, bool lazy,
425                           uint32_t sequence, bool interruptible,
426                           unsigned long timeout);
427 extern void vmw_irq_preinstall(struct drm_device *dev);
428 extern int vmw_irq_postinstall(struct drm_device *dev);
429 extern void vmw_irq_uninstall(struct drm_device *dev);
430 extern bool vmw_fence_signaled(struct vmw_private *dev_priv,
431                                uint32_t sequence);
432 extern int vmw_fence_wait_ioctl(struct drm_device *dev, void *data,
433                                 struct drm_file *file_priv);
434 extern int vmw_fallback_wait(struct vmw_private *dev_priv,
435                              bool lazy,
436                              bool fifo_idle,
437                              uint32_t sequence,
438                              bool interruptible,
439                              unsigned long timeout);
440
441 /**
442  * Kernel framebuffer - vmwgfx_fb.c
443  */
444
445 int vmw_fb_init(struct vmw_private *vmw_priv);
446 int vmw_fb_close(struct vmw_private *dev_priv);
447 int vmw_fb_off(struct vmw_private *vmw_priv);
448 int vmw_fb_on(struct vmw_private *vmw_priv);
449
450 /**
451  * Kernel modesetting - vmwgfx_kms.c
452  */
453
454 int vmw_kms_init(struct vmw_private *dev_priv);
455 int vmw_kms_close(struct vmw_private *dev_priv);
456 int vmw_kms_save_vga(struct vmw_private *vmw_priv);
457 int vmw_kms_restore_vga(struct vmw_private *vmw_priv);
458 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
459                                 struct drm_file *file_priv);
460 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv);
461 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
462                           struct ttm_object_file *tfile,
463                           struct ttm_buffer_object *bo,
464                           SVGA3dCmdHeader *header);
465
466 /**
467  * Overlay control - vmwgfx_overlay.c
468  */
469
470 int vmw_overlay_init(struct vmw_private *dev_priv);
471 int vmw_overlay_close(struct vmw_private *dev_priv);
472 int vmw_overlay_ioctl(struct drm_device *dev, void *data,
473                       struct drm_file *file_priv);
474 int vmw_overlay_stop_all(struct vmw_private *dev_priv);
475 int vmw_overlay_resume_all(struct vmw_private *dev_priv);
476 int vmw_overlay_pause_all(struct vmw_private *dev_priv);
477 int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out);
478 int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id);
479 int vmw_overlay_num_overlays(struct vmw_private *dev_priv);
480 int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv);
481
482 /**
483  * Inline helper functions
484  */
485
486 static inline void vmw_surface_unreference(struct vmw_surface **srf)
487 {
488         struct vmw_surface *tmp_srf = *srf;
489         struct vmw_resource *res = &tmp_srf->res;
490         *srf = NULL;
491
492         vmw_resource_unreference(&res);
493 }
494
495 static inline struct vmw_surface *vmw_surface_reference(struct vmw_surface *srf)
496 {
497         (void) vmw_resource_reference(&srf->res);
498         return srf;
499 }
500
501 static inline void vmw_dmabuf_unreference(struct vmw_dma_buffer **buf)
502 {
503         struct vmw_dma_buffer *tmp_buf = *buf;
504         struct ttm_buffer_object *bo = &tmp_buf->base;
505         *buf = NULL;
506
507         ttm_bo_unref(&bo);
508 }
509
510 static inline struct vmw_dma_buffer *vmw_dmabuf_reference(struct vmw_dma_buffer *buf)
511 {
512         if (ttm_bo_reference(&buf->base))
513                 return buf;
514         return NULL;
515 }
516
517 #endif