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