Merge branch 'master' into cleanup
[platform/upstream/libdrm.git] / shared-core / nouveau_drv.h
1 /*
2  * Copyright 2005 Stephane Marchesin.
3  * 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, sublicense,
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 next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
19  * VA LINUX SYSTEMS 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
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifndef __NOUVEAU_DRV_H__
26 #define __NOUVEAU_DRV_H__
27
28 #define DRIVER_AUTHOR           "Stephane Marchesin"
29 #define DRIVER_EMAIL            "dri-devel@lists.sourceforge.net"
30
31 #define DRIVER_NAME             "nouveau"
32 #define DRIVER_DESC             "nVidia Riva/TNT/GeForce"
33 #define DRIVER_DATE             "20060213"
34
35 #define DRIVER_MAJOR            0
36 #define DRIVER_MINOR            0
37 #define DRIVER_PATCHLEVEL       8
38
39 #define NOUVEAU_FAMILY   0x0000FFFF
40 #define NOUVEAU_FLAGS    0xFFFF0000
41
42 #include "nouveau_drm.h"
43 #include "nouveau_reg.h"
44
45 struct mem_block {
46         struct mem_block *next;
47         struct mem_block *prev;
48         uint64_t start;
49         uint64_t size;
50         DRMFILE filp;           /* 0: free, -1: heap, other: real files */
51         int flags;
52         drm_local_map_t *map;
53 };
54
55 enum nouveau_flags {
56         NV_NFORCE   =0x10000000,
57         NV_NFORCE2  =0x20000000
58 };
59
60 #define NVOBJ_ENGINE_SW         0
61 #define NVOBJ_ENGINE_GR         1
62 #define NVOBJ_ENGINE_INT        0xdeadbeef
63
64 #define NVOBJ_FLAG_ALLOW_NO_REFS        (1 << 0)
65 #define NVOBJ_FLAG_ZERO_ALLOC           (1 << 1)
66 #define NVOBJ_FLAG_ZERO_FREE            (1 << 2)
67 #define NVOBJ_FLAG_FAKE                 (1 << 3)
68 typedef struct nouveau_gpuobj {
69         struct nouveau_gpuobj *next;
70         struct nouveau_gpuobj *prev;
71
72         int im_channel;
73         struct mem_block *im_pramin;
74         struct mem_block *im_backing;
75         int im_bound;
76
77         uint32_t flags;
78         int refcount;
79
80         uint32_t engine;
81         uint32_t class;
82 } nouveau_gpuobj_t;
83
84 typedef struct nouveau_gpuobj_ref {
85         struct nouveau_gpuobj_ref *next;
86
87         nouveau_gpuobj_t *gpuobj;
88         uint32_t instance;
89
90         int channel;
91         int handle;
92 } nouveau_gpuobj_ref_t;
93
94 struct nouveau_fifo
95 {
96         /* owner of this fifo */
97         DRMFILE filp;
98         /* mapping of the fifo itself */
99         drm_local_map_t *map;
100         /* mapping of the regs controling the fifo */
101         drm_local_map_t *regs;
102
103         /* DMA push buffer */
104         nouveau_gpuobj_ref_t *pushbuf;
105         struct mem_block     *pushbuf_mem;
106         uint32_t              pushbuf_base;
107
108         /* Notifier memory */
109         struct mem_block *notifier_block;
110         struct mem_block *notifier_heap;
111         drm_local_map_t  *notifier_map;
112
113         /* PFIFO context */
114         nouveau_gpuobj_ref_t *ramfc;
115
116         /* PGRAPH context */
117         nouveau_gpuobj_ref_t *ramin_grctx;
118         uint32_t pgraph_ctx [340]; /* XXX dynamic alloc ? */
119
120         /* Objects */
121         nouveau_gpuobj_ref_t *ramin; /* Private instmem */
122         struct mem_block     *ramin_heap; /* Private PRAMIN heap */
123         nouveau_gpuobj_ref_t *ramht; /* Hash table */
124         nouveau_gpuobj_ref_t *ramht_refs; /* Objects referenced by RAMHT */
125 };
126
127 struct nouveau_config {
128         struct {
129                 int location;
130                 int size;
131         } cmdbuf;
132 };
133
134 typedef struct nouveau_engine_func {
135         struct {
136                 void    *priv;
137
138                 int     (*init)(drm_device_t *dev);
139                 void    (*takedown)(drm_device_t *dev);
140
141                 int     (*populate)(drm_device_t *, nouveau_gpuobj_t *,
142                                     uint32_t *size);
143                 void    (*clear)(drm_device_t *, nouveau_gpuobj_t *);
144                 int     (*bind)(drm_device_t *, nouveau_gpuobj_t *);
145                 int     (*unbind)(drm_device_t *, nouveau_gpuobj_t *);
146         } instmem;
147
148         struct {
149                 int     (*init)(drm_device_t *dev);
150                 void    (*takedown)(drm_device_t *dev);
151         } mc;
152
153         struct {
154                 int     (*init)(drm_device_t *dev);
155                 void    (*takedown)(drm_device_t *dev);
156         } timer;
157
158         struct {
159                 int     (*init)(drm_device_t *dev);
160                 void    (*takedown)(drm_device_t *dev);
161         } fb;
162
163         struct {
164                 int     (*init)(drm_device_t *);
165                 void    (*takedown)(drm_device_t *);
166
167                 int     (*create_context)(drm_device_t *, int channel);
168                 void    (*destroy_context)(drm_device_t *, int channel);
169                 int     (*load_context)(drm_device_t *, int channel);
170                 int     (*save_context)(drm_device_t *, int channel);
171         } graph;
172
173         struct {
174                 void    *priv;
175
176                 int     (*init)(drm_device_t *);
177                 void    (*takedown)(drm_device_t *);
178
179                 int     (*create_context)(drm_device_t *, int channel);
180                 void    (*destroy_context)(drm_device_t *, int channel);
181                 int     (*load_context)(drm_device_t *, int channel);
182                 int     (*save_context)(drm_device_t *, int channel);
183         } fifo;
184 } nouveau_engine_func_t;
185
186 typedef struct drm_nouveau_private {
187         enum {
188                 NOUVEAU_CARD_INIT_DOWN,
189                 NOUVEAU_CARD_INIT_DONE,
190                 NOUVEAU_CARD_INIT_FAILED
191         } init_state;
192
193         /* the card type, takes NV_* as values */
194         int card_type;
195         /* exact chipset, derived from NV_PMC_BOOT_0 */
196         int chipset;
197         int flags;
198
199         drm_local_map_t *mmio;
200         drm_local_map_t *fb;
201         drm_local_map_t *ramin; /* NV40 onwards */
202
203         int fifo_alloc_count;
204         struct nouveau_fifo *fifos[NV_MAX_FIFO_NUMBER];
205
206         struct nouveau_engine_func Engine;
207
208         /* RAMIN configuration, RAMFC, RAMHT and RAMRO offsets */
209         nouveau_gpuobj_t *ramht;
210         uint32_t ramin_rsvd_vram;
211         uint32_t ramht_offset;
212         uint32_t ramht_size;
213         uint32_t ramht_bits;
214         uint32_t ramfc_offset;
215         uint32_t ramfc_size;
216         uint32_t ramro_offset;
217         uint32_t ramro_size;
218
219         /* base physical adresses */
220         uint64_t fb_phys;
221         uint64_t fb_available_size;
222         uint64_t agp_phys;
223         uint64_t agp_available_size;
224
225         /* the mtrr covering the FB */
226         int fb_mtrr;
227
228         struct mem_block *agp_heap;
229         struct mem_block *fb_heap;
230         struct mem_block *fb_nomap_heap;
231         struct mem_block *ramin_heap;
232         struct mem_block *pci_heap;
233
234         /* context table pointed to be NV_PGRAPH_CHANNEL_CTX_TABLE (0x400780) */
235         uint32_t ctx_table_size;
236         nouveau_gpuobj_ref_t *ctx_table;
237
238         struct nouveau_config config;
239
240         nouveau_gpuobj_t *gpuobj_all;
241 }
242 drm_nouveau_private_t;
243
244 /* nouveau_state.c */
245 extern void nouveau_preclose(drm_device_t * dev, DRMFILE filp);
246 extern int nouveau_load(struct drm_device *dev, unsigned long flags);
247 extern int nouveau_firstopen(struct drm_device *dev);
248 extern void nouveau_lastclose(struct drm_device *dev);
249 extern int nouveau_unload(struct drm_device *dev);
250 extern int nouveau_ioctl_getparam(DRM_IOCTL_ARGS);
251 extern int nouveau_ioctl_setparam(DRM_IOCTL_ARGS);
252 extern void nouveau_wait_for_idle(struct drm_device *dev);
253 extern int nouveau_ioctl_card_init(DRM_IOCTL_ARGS);
254
255 /* nouveau_mem.c */
256 extern int               nouveau_mem_init_heap(struct mem_block **,
257                                                uint64_t start, uint64_t size);
258 extern struct mem_block *nouveau_mem_alloc_block(struct mem_block *,
259                                                  uint64_t size, int align2,
260                                                  DRMFILE);
261 extern void              nouveau_mem_takedown(struct mem_block **heap);
262 extern void              nouveau_mem_free_block(struct mem_block *);
263 extern uint64_t          nouveau_mem_fb_amount(struct drm_device *dev);
264 extern void              nouveau_mem_release(DRMFILE filp, struct mem_block *heap);
265 extern int               nouveau_ioctl_mem_alloc(DRM_IOCTL_ARGS);
266 extern int               nouveau_ioctl_mem_free(DRM_IOCTL_ARGS);
267 extern struct mem_block* nouveau_mem_alloc(struct drm_device *dev, int alignment, uint64_t size, int flags, DRMFILE filp);
268 extern void              nouveau_mem_free(struct drm_device* dev, struct mem_block*);
269 extern int               nouveau_mem_init(struct drm_device *dev);
270 extern void              nouveau_mem_close(struct drm_device *dev);
271
272 /* nouveau_notifier.c */
273 extern int  nouveau_notifier_init_channel(drm_device_t *, int channel, DRMFILE);
274 extern void nouveau_notifier_takedown_channel(drm_device_t *, int channel);
275 extern int  nouveau_notifier_alloc(drm_device_t *, int channel,
276                                    uint32_t handle, int cout, uint32_t *offset);
277 extern int  nouveau_ioctl_notifier_alloc(DRM_IOCTL_ARGS);
278
279 /* nouveau_fifo.c */
280 extern int  nouveau_fifo_init(drm_device_t *dev);
281 extern int  nouveau_fifo_number(drm_device_t *dev);
282 extern int  nouveau_fifo_ctx_size(drm_device_t *dev);
283 extern void nouveau_fifo_cleanup(drm_device_t *dev, DRMFILE filp);
284 extern int  nouveau_fifo_owner(drm_device_t *dev, DRMFILE filp, int channel);
285 extern void nouveau_fifo_free(drm_device_t *dev, int channel);
286
287 /* nouveau_object.c */
288 extern void nouveau_gpuobj_takedown(drm_device_t *dev);
289 extern int nouveau_gpuobj_channel_init(drm_device_t *, int channel,
290                                        uint32_t vram_h, uint32_t tt_h);
291 extern void nouveau_gpuobj_channel_takedown(drm_device_t *, int channel);
292 extern int nouveau_gpuobj_new(drm_device_t *, int channel, int size, int align,
293                               uint32_t flags, nouveau_gpuobj_t **);
294 extern int nouveau_gpuobj_del(drm_device_t *, nouveau_gpuobj_t **);
295 extern int nouveau_gpuobj_ref_add(drm_device_t *, int channel, uint32_t handle,
296                                   nouveau_gpuobj_t *, nouveau_gpuobj_ref_t **);
297 extern int nouveau_gpuobj_ref_del(drm_device_t *, nouveau_gpuobj_ref_t **);
298 extern int nouveau_gpuobj_new_ref(drm_device_t *, int chan_obj, int chan_ref,
299                                   uint32_t handle, int size, int align,
300                                   uint32_t flags, nouveau_gpuobj_ref_t **);
301 extern int nouveau_gpuobj_new_fake(drm_device_t *, uint32_t offset,
302                                    uint32_t size, uint32_t flags,
303                                    nouveau_gpuobj_t**, nouveau_gpuobj_ref_t**);
304 extern int nouveau_gpuobj_dma_new(drm_device_t *, int channel, int class,
305                                   uint64_t offset, uint64_t size,
306                                   int access, int target, nouveau_gpuobj_t **);
307 extern int nouveau_gpuobj_gr_new(drm_device_t *, int channel, int class,
308                                  nouveau_gpuobj_t **);
309 extern int nouveau_ioctl_grobj_alloc(DRM_IOCTL_ARGS);
310
311 /* nouveau_irq.c */
312 extern irqreturn_t nouveau_irq_handler(DRM_IRQ_ARGS);
313 extern void        nouveau_irq_preinstall(drm_device_t*);
314 extern void        nouveau_irq_postinstall(drm_device_t*);
315 extern void        nouveau_irq_uninstall(drm_device_t*);
316
317 /* nv04_fb.c */
318 extern int  nv04_fb_init(drm_device_t *dev);
319 extern void nv04_fb_takedown(drm_device_t *dev);
320
321 /* nv10_fb.c */
322 extern int  nv10_fb_init(drm_device_t *dev);
323 extern void nv10_fb_takedown(drm_device_t *dev);
324
325 /* nv40_fb.c */
326 extern int  nv40_fb_init(drm_device_t *dev);
327 extern void nv40_fb_takedown(drm_device_t *dev);
328
329 /* nv04_fifo.c */
330 extern int  nv04_fifo_create_context(drm_device_t *dev, int channel);
331 extern void nv04_fifo_destroy_context(drm_device_t *dev, int channel);
332 extern int  nv04_fifo_load_context(drm_device_t *dev, int channel);
333 extern int  nv04_fifo_save_context(drm_device_t *dev, int channel);
334
335 /* nv10_fifo.c */
336 extern int  nv10_fifo_create_context(drm_device_t *dev, int channel);
337 extern void nv10_fifo_destroy_context(drm_device_t *dev, int channel);
338 extern int  nv10_fifo_load_context(drm_device_t *dev, int channel);
339 extern int  nv10_fifo_save_context(drm_device_t *dev, int channel);
340
341 /* nv40_fifo.c */
342 extern int  nv40_fifo_create_context(drm_device_t *, int channel);
343 extern void nv40_fifo_destroy_context(drm_device_t *, int channel);
344 extern int  nv40_fifo_load_context(drm_device_t *, int channel);
345 extern int  nv40_fifo_save_context(drm_device_t *, int channel);
346
347 /* nv50_fifo.c */
348 extern int  nv50_fifo_init(drm_device_t *);
349 extern void nv50_fifo_takedown(drm_device_t *);
350 extern int  nv50_fifo_create_context(drm_device_t *, int channel);
351 extern void nv50_fifo_destroy_context(drm_device_t *, int channel);
352 extern int  nv50_fifo_load_context(drm_device_t *, int channel);
353 extern int  nv50_fifo_save_context(drm_device_t *, int channel);
354
355 /* nv04_graph.c */
356 extern void nouveau_nv04_context_switch(drm_device_t *dev);
357 extern int  nv04_graph_init(drm_device_t *dev);
358 extern void nv04_graph_takedown(drm_device_t *dev);
359 extern int  nv04_graph_create_context(drm_device_t *dev, int channel);
360 extern void nv04_graph_destroy_context(drm_device_t *dev, int channel);
361 extern int  nv04_graph_load_context(drm_device_t *dev, int channel);
362 extern int  nv04_graph_save_context(drm_device_t *dev, int channel);
363
364 /* nv10_graph.c */
365 extern void nouveau_nv10_context_switch(drm_device_t *dev);
366 extern int  nv10_graph_init(drm_device_t *dev);
367 extern void nv10_graph_takedown(drm_device_t *dev);
368 extern int  nv10_graph_create_context(drm_device_t *dev, int channel);
369 extern void nv10_graph_destroy_context(drm_device_t *dev, int channel);
370 extern int  nv10_graph_load_context(drm_device_t *dev, int channel);
371 extern int  nv10_graph_save_context(drm_device_t *dev, int channel);
372
373 /* nv20_graph.c */
374 extern void nouveau_nv20_context_switch(drm_device_t *dev);
375 extern int  nv20_graph_init(drm_device_t *dev);
376 extern void nv20_graph_takedown(drm_device_t *dev);
377 extern int  nv20_graph_create_context(drm_device_t *dev, int channel);
378 extern void nv20_graph_destroy_context(drm_device_t *dev, int channel);
379 extern int  nv20_graph_load_context(drm_device_t *dev, int channel);
380 extern int  nv20_graph_save_context(drm_device_t *dev, int channel);
381
382 /* nv30_graph.c */
383 extern int  nv30_graph_init(drm_device_t *dev);
384 extern void nv30_graph_takedown(drm_device_t *dev);
385 extern int  nv30_graph_create_context(drm_device_t *, int channel);
386 extern void nv30_graph_destroy_context(drm_device_t *, int channel);
387 extern int  nv30_graph_load_context(drm_device_t *, int channel);
388 extern int  nv30_graph_save_context(drm_device_t *, int channel);
389
390 /* nv40_graph.c */
391 extern int  nv40_graph_init(drm_device_t *);
392 extern void nv40_graph_takedown(drm_device_t *);
393 extern int  nv40_graph_create_context(drm_device_t *, int channel);
394 extern void nv40_graph_destroy_context(drm_device_t *, int channel);
395 extern int  nv40_graph_load_context(drm_device_t *, int channel);
396 extern int  nv40_graph_save_context(drm_device_t *, int channel);
397
398 /* nv50_graph.c */
399 extern int  nv50_graph_init(drm_device_t *);
400 extern void nv50_graph_takedown(drm_device_t *);
401 extern int  nv50_graph_create_context(drm_device_t *, int channel);
402 extern void nv50_graph_destroy_context(drm_device_t *, int channel);
403 extern int  nv50_graph_load_context(drm_device_t *, int channel);
404 extern int  nv50_graph_save_context(drm_device_t *, int channel);
405
406 /* nv04_instmem.c */
407 extern int  nv04_instmem_init(drm_device_t *dev);
408 extern void nv04_instmem_takedown(drm_device_t *dev);
409 extern int  nv04_instmem_populate(drm_device_t*, nouveau_gpuobj_t*,
410                                   uint32_t *size);
411 extern void nv04_instmem_clear(drm_device_t*, nouveau_gpuobj_t*);
412 extern int  nv04_instmem_bind(drm_device_t*, nouveau_gpuobj_t*);
413 extern int  nv04_instmem_unbind(drm_device_t*, nouveau_gpuobj_t*);
414
415 /* nv50_instmem.c */
416 extern int  nv50_instmem_init(drm_device_t *dev);
417 extern void nv50_instmem_takedown(drm_device_t *dev);
418 extern int  nv50_instmem_populate(drm_device_t*, nouveau_gpuobj_t*,
419                                   uint32_t *size);
420 extern void nv50_instmem_clear(drm_device_t*, nouveau_gpuobj_t*);
421 extern int  nv50_instmem_bind(drm_device_t*, nouveau_gpuobj_t*);
422 extern int  nv50_instmem_unbind(drm_device_t*, nouveau_gpuobj_t*);
423
424 /* nv04_mc.c */
425 extern int  nv04_mc_init(drm_device_t *dev);
426 extern void nv04_mc_takedown(drm_device_t *dev);
427
428 /* nv40_mc.c */
429 extern int  nv40_mc_init(drm_device_t *dev);
430 extern void nv40_mc_takedown(drm_device_t *dev);
431
432 /* nv50_mc.c */
433 extern int  nv50_mc_init(drm_device_t *dev);
434 extern void nv50_mc_takedown(drm_device_t *dev);
435
436 /* nv04_timer.c */
437 extern int  nv04_timer_init(drm_device_t *dev);
438 extern void nv04_timer_takedown(drm_device_t *dev);
439
440 extern long nouveau_compat_ioctl(struct file *filp, unsigned int cmd,
441                                 unsigned long arg);
442
443 #if defined(__powerpc__)
444 #define NV_READ(reg)        in_be32((void __iomem *)(dev_priv->mmio)->handle + (reg) )
445 #define NV_WRITE(reg,val)   out_be32((void __iomem *)(dev_priv->mmio)->handle + (reg) , (val) )
446 #else
447 #define NV_READ(reg)        DRM_READ32(  dev_priv->mmio, (reg) )
448 #define NV_WRITE(reg,val)   DRM_WRITE32( dev_priv->mmio, (reg), (val) )
449 #endif
450
451 /* PRAMIN access */
452 #if defined(__powerpc__)
453 #define NV_RI32(o) in_be32((void __iomem *)(dev_priv->ramin)->handle+(o))
454 #define NV_WI32(o,v) out_be32((void __iomem*)(dev_priv->ramin)->handle+(o), (v))
455 #else
456 #define NV_RI32(o) DRM_READ32(dev_priv->ramin, (o))
457 #define NV_WI32(o,v) DRM_WRITE32(dev_priv->ramin, (o), (v))
458 #endif
459
460 #define INSTANCE_RD(o,i) NV_RI32((o)->im_pramin->start + ((i)<<2))
461 #define INSTANCE_WR(o,i,v) NV_WI32((o)->im_pramin->start + ((i)<<2), (v))
462
463 #endif /* __NOUVEAU_DRV_H__ */
464