nouveau_drv.h revision fce2bad0ee2666d6a10bfeb634b1021469cc3d79
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		"20090420"
34
35#define DRIVER_MAJOR		0
36#define DRIVER_MINOR		0
37#define DRIVER_PATCHLEVEL	16
38
39#define NOUVEAU_FAMILY   0x0000FFFF
40#define NOUVEAU_FLAGS    0xFFFF0000
41
42#include "ttm/ttm_bo_api.h"
43#include "ttm/ttm_bo_driver.h"
44#include "ttm/ttm_placement.h"
45#include "ttm/ttm_memory.h"
46#include "ttm/ttm_module.h"
47
48struct nouveau_fpriv {
49	struct ttm_object_file *tfile;
50};
51
52#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
53
54#include "nouveau_drm.h"
55#include "nouveau_reg.h"
56#include "nouveau_bios.h"
57#include "nouveau_util.h"
58struct nouveau_grctx;
59
60#define MAX_NUM_DCB_ENTRIES 16
61
62#define NOUVEAU_MAX_CHANNEL_NR 128
63#define NOUVEAU_MAX_TILE_NR 15
64
65#define NV50_VM_MAX_VRAM (2*1024*1024*1024ULL)
66#define NV50_VM_BLOCK    (512*1024*1024ULL)
67#define NV50_VM_VRAM_NR  (NV50_VM_MAX_VRAM / NV50_VM_BLOCK)
68
69struct nouveau_tile_reg {
70	bool used;
71	uint32_t addr;
72	uint32_t limit;
73	uint32_t pitch;
74	uint32_t zcomp;
75	struct drm_mm_node *tag_mem;
76	struct nouveau_fence *fence;
77};
78
79struct nouveau_bo {
80	struct ttm_buffer_object bo;
81	struct ttm_placement placement;
82	u32 placements[3];
83	u32 busy_placements[3];
84	struct ttm_bo_kmap_obj kmap;
85	struct list_head head;
86
87	/* protected by ttm_bo_reserve() */
88	struct drm_file *reserved_by;
89	struct list_head entry;
90	int pbbo_index;
91	bool validate_mapped;
92
93	struct nouveau_channel *channel;
94
95	bool mappable;
96	bool no_vm;
97
98	uint32_t tile_mode;
99	uint32_t tile_flags;
100	struct nouveau_tile_reg *tile;
101
102	struct drm_gem_object *gem;
103	int pin_refcnt;
104};
105
106#define nouveau_bo_tile_layout(nvbo)				\
107	((nvbo)->tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK)
108
109static inline struct nouveau_bo *
110nouveau_bo(struct ttm_buffer_object *bo)
111{
112	return container_of(bo, struct nouveau_bo, bo);
113}
114
115static inline struct nouveau_bo *
116nouveau_gem_object(struct drm_gem_object *gem)
117{
118	return gem ? gem->driver_private : NULL;
119}
120
121/* TODO: submit equivalent to TTM generic API upstream? */
122static inline void __iomem *
123nvbo_kmap_obj_iovirtual(struct nouveau_bo *nvbo)
124{
125	bool is_iomem;
126	void __iomem *ioptr = (void __force __iomem *)ttm_kmap_obj_virtual(
127						&nvbo->kmap, &is_iomem);
128	WARN_ON_ONCE(ioptr && !is_iomem);
129	return ioptr;
130}
131
132enum nouveau_flags {
133	NV_NFORCE   = 0x10000000,
134	NV_NFORCE2  = 0x20000000
135};
136
137#define NVOBJ_ENGINE_SW		0
138#define NVOBJ_ENGINE_GR		1
139#define NVOBJ_ENGINE_PPP	2
140#define NVOBJ_ENGINE_COPY	3
141#define NVOBJ_ENGINE_VP		4
142#define NVOBJ_ENGINE_CRYPT      5
143#define NVOBJ_ENGINE_BSP	6
144#define NVOBJ_ENGINE_DISPLAY	0xcafe0001
145#define NVOBJ_ENGINE_INT	0xdeadbeef
146
147#define NVOBJ_FLAG_ZERO_ALLOC		(1 << 1)
148#define NVOBJ_FLAG_ZERO_FREE		(1 << 2)
149struct nouveau_gpuobj {
150	struct drm_device *dev;
151	struct kref refcount;
152	struct list_head list;
153
154	struct drm_mm_node *im_pramin;
155	struct nouveau_bo *im_backing;
156	uint32_t *im_backing_suspend;
157	int im_bound;
158
159	uint32_t flags;
160
161	u32 size;
162	u32 pinst;
163	u32 cinst;
164	u64 vinst;
165
166	uint32_t engine;
167	uint32_t class;
168
169	void (*dtor)(struct drm_device *, struct nouveau_gpuobj *);
170	void *priv;
171};
172
173struct nouveau_page_flip_state {
174	struct list_head head;
175	struct drm_pending_vblank_event *event;
176	int crtc, bpp, pitch, x, y;
177	uint64_t offset;
178};
179
180enum nouveau_channel_mutex_class {
181	NOUVEAU_UCHANNEL_MUTEX,
182	NOUVEAU_KCHANNEL_MUTEX
183};
184
185struct nouveau_channel {
186	struct drm_device *dev;
187	int id;
188
189	/* references to the channel data structure */
190	struct kref ref;
191	/* users of the hardware channel resources, the hardware
192	 * context will be kicked off when it reaches zero. */
193	atomic_t users;
194	struct mutex mutex;
195
196	/* owner of this fifo */
197	struct drm_file *file_priv;
198	/* mapping of the fifo itself */
199	struct drm_local_map *map;
200
201	/* mapping of the regs controling the fifo */
202	void __iomem *user;
203	uint32_t user_get;
204	uint32_t user_put;
205
206	/* Fencing */
207	struct {
208		/* lock protects the pending list only */
209		spinlock_t lock;
210		struct list_head pending;
211		uint32_t sequence;
212		uint32_t sequence_ack;
213		atomic_t last_sequence_irq;
214	} fence;
215
216	/* DMA push buffer */
217	struct nouveau_gpuobj *pushbuf;
218	struct nouveau_bo     *pushbuf_bo;
219	uint32_t               pushbuf_base;
220
221	/* Notifier memory */
222	struct nouveau_bo *notifier_bo;
223	struct drm_mm notifier_heap;
224
225	/* PFIFO context */
226	struct nouveau_gpuobj *ramfc;
227	struct nouveau_gpuobj *cache;
228
229	/* PGRAPH context */
230	/* XXX may be merge 2 pointers as private data ??? */
231	struct nouveau_gpuobj *ramin_grctx;
232	struct nouveau_gpuobj *crypt_ctx;
233	void *pgraph_ctx;
234
235	/* NV50 VM */
236	struct nouveau_gpuobj *vm_pd;
237	struct nouveau_gpuobj *vm_gart_pt;
238	struct nouveau_gpuobj *vm_vram_pt[NV50_VM_VRAM_NR];
239
240	/* Objects */
241	struct nouveau_gpuobj *ramin; /* Private instmem */
242	struct drm_mm          ramin_heap; /* Private PRAMIN heap */
243	struct nouveau_ramht  *ramht; /* Hash table */
244
245	/* GPU object info for stuff used in-kernel (mm_enabled) */
246	uint32_t m2mf_ntfy;
247	uint32_t vram_handle;
248	uint32_t gart_handle;
249	bool accel_done;
250
251	/* Push buffer state (only for drm's channel on !mm_enabled) */
252	struct {
253		int max;
254		int free;
255		int cur;
256		int put;
257		/* access via pushbuf_bo */
258
259		int ib_base;
260		int ib_max;
261		int ib_free;
262		int ib_put;
263	} dma;
264
265	uint32_t sw_subchannel[8];
266
267	struct {
268		struct nouveau_gpuobj *vblsem;
269		uint32_t vblsem_head;
270		uint32_t vblsem_offset;
271		uint32_t vblsem_rval;
272		struct list_head vbl_wait;
273		struct list_head flip;
274	} nvsw;
275
276	struct {
277		bool active;
278		char name[32];
279		struct drm_info_list info;
280	} debugfs;
281};
282
283struct nouveau_instmem_engine {
284	void	*priv;
285
286	int	(*init)(struct drm_device *dev);
287	void	(*takedown)(struct drm_device *dev);
288	int	(*suspend)(struct drm_device *dev);
289	void	(*resume)(struct drm_device *dev);
290
291	int	(*populate)(struct drm_device *, struct nouveau_gpuobj *,
292			    u32 *size, u32 align);
293	void	(*clear)(struct drm_device *, struct nouveau_gpuobj *);
294	int	(*bind)(struct drm_device *, struct nouveau_gpuobj *);
295	int	(*unbind)(struct drm_device *, struct nouveau_gpuobj *);
296	void	(*flush)(struct drm_device *);
297};
298
299struct nouveau_mc_engine {
300	int  (*init)(struct drm_device *dev);
301	void (*takedown)(struct drm_device *dev);
302};
303
304struct nouveau_timer_engine {
305	int      (*init)(struct drm_device *dev);
306	void     (*takedown)(struct drm_device *dev);
307	uint64_t (*read)(struct drm_device *dev);
308};
309
310struct nouveau_fb_engine {
311	int num_tiles;
312	struct drm_mm tag_heap;
313
314	int  (*init)(struct drm_device *dev);
315	void (*takedown)(struct drm_device *dev);
316
317	void (*init_tile_region)(struct drm_device *dev, int i,
318				 uint32_t addr, uint32_t size,
319				 uint32_t pitch, uint32_t flags);
320	void (*set_tile_region)(struct drm_device *dev, int i);
321	void (*free_tile_region)(struct drm_device *dev, int i);
322};
323
324struct nouveau_fifo_engine {
325	int  channels;
326
327	struct nouveau_gpuobj *playlist[2];
328	int cur_playlist;
329
330	int  (*init)(struct drm_device *);
331	void (*takedown)(struct drm_device *);
332
333	void (*disable)(struct drm_device *);
334	void (*enable)(struct drm_device *);
335	bool (*reassign)(struct drm_device *, bool enable);
336	bool (*cache_pull)(struct drm_device *dev, bool enable);
337
338	int  (*channel_id)(struct drm_device *);
339
340	int  (*create_context)(struct nouveau_channel *);
341	void (*destroy_context)(struct nouveau_channel *);
342	int  (*load_context)(struct nouveau_channel *);
343	int  (*unload_context)(struct drm_device *);
344	void (*tlb_flush)(struct drm_device *dev);
345};
346
347struct nouveau_pgraph_engine {
348	bool accel_blocked;
349	bool registered;
350	int grctx_size;
351
352	/* NV2x/NV3x context table (0x400780) */
353	struct nouveau_gpuobj *ctx_table;
354
355	int  (*init)(struct drm_device *);
356	void (*takedown)(struct drm_device *);
357
358	void (*fifo_access)(struct drm_device *, bool);
359
360	struct nouveau_channel *(*channel)(struct drm_device *);
361	int  (*create_context)(struct nouveau_channel *);
362	void (*destroy_context)(struct nouveau_channel *);
363	int  (*load_context)(struct nouveau_channel *);
364	int  (*unload_context)(struct drm_device *);
365	void (*tlb_flush)(struct drm_device *dev);
366
367	void (*set_tile_region)(struct drm_device *dev, int i);
368};
369
370struct nouveau_display_engine {
371	int (*early_init)(struct drm_device *);
372	void (*late_takedown)(struct drm_device *);
373	int (*create)(struct drm_device *);
374	int (*init)(struct drm_device *);
375	void (*destroy)(struct drm_device *);
376};
377
378struct nouveau_gpio_engine {
379	void *priv;
380
381	int  (*init)(struct drm_device *);
382	void (*takedown)(struct drm_device *);
383
384	int  (*get)(struct drm_device *, enum dcb_gpio_tag);
385	int  (*set)(struct drm_device *, enum dcb_gpio_tag, int state);
386
387	int  (*irq_register)(struct drm_device *, enum dcb_gpio_tag,
388			     void (*)(void *, int), void *);
389	void (*irq_unregister)(struct drm_device *, enum dcb_gpio_tag,
390			       void (*)(void *, int), void *);
391	bool (*irq_enable)(struct drm_device *, enum dcb_gpio_tag, bool on);
392};
393
394struct nouveau_pm_voltage_level {
395	u8 voltage;
396	u8 vid;
397};
398
399struct nouveau_pm_voltage {
400	bool supported;
401	u8 vid_mask;
402
403	struct nouveau_pm_voltage_level *level;
404	int nr_level;
405};
406
407#define NOUVEAU_PM_MAX_LEVEL 8
408struct nouveau_pm_level {
409	struct device_attribute dev_attr;
410	char name[32];
411	int id;
412
413	u32 core;
414	u32 memory;
415	u32 shader;
416	u32 unk05;
417
418	u8 voltage;
419	u8 fanspeed;
420
421	u16 memscript;
422};
423
424struct nouveau_pm_temp_sensor_constants {
425	u16 offset_constant;
426	s16 offset_mult;
427	u16 offset_div;
428	u16 slope_mult;
429	u16 slope_div;
430};
431
432struct nouveau_pm_threshold_temp {
433	s16 critical;
434	s16 down_clock;
435	s16 fan_boost;
436};
437
438struct nouveau_pm_memtiming {
439	u32 reg_100220;
440	u32 reg_100224;
441	u32 reg_100228;
442	u32 reg_10022c;
443	u32 reg_100230;
444	u32 reg_100234;
445	u32 reg_100238;
446	u32 reg_10023c;
447};
448
449struct nouveau_pm_memtimings {
450	bool supported;
451	struct nouveau_pm_memtiming *timing;
452	int nr_timing;
453};
454
455struct nouveau_pm_engine {
456	struct nouveau_pm_voltage voltage;
457	struct nouveau_pm_level perflvl[NOUVEAU_PM_MAX_LEVEL];
458	int nr_perflvl;
459	struct nouveau_pm_memtimings memtimings;
460	struct nouveau_pm_temp_sensor_constants sensor_constants;
461	struct nouveau_pm_threshold_temp threshold_temp;
462
463	struct nouveau_pm_level boot;
464	struct nouveau_pm_level *cur;
465
466	struct device *hwmon;
467	struct notifier_block acpi_nb;
468
469	int (*clock_get)(struct drm_device *, u32 id);
470	void *(*clock_pre)(struct drm_device *, struct nouveau_pm_level *,
471			   u32 id, int khz);
472	void (*clock_set)(struct drm_device *, void *);
473	int (*voltage_get)(struct drm_device *);
474	int (*voltage_set)(struct drm_device *, int voltage);
475	int (*fanspeed_get)(struct drm_device *);
476	int (*fanspeed_set)(struct drm_device *, int fanspeed);
477	int (*temp_get)(struct drm_device *);
478};
479
480struct nouveau_crypt_engine {
481	bool registered;
482
483	int  (*init)(struct drm_device *);
484	void (*takedown)(struct drm_device *);
485	int  (*create_context)(struct nouveau_channel *);
486	void (*destroy_context)(struct nouveau_channel *);
487	void (*tlb_flush)(struct drm_device *dev);
488};
489
490struct nouveau_engine {
491	struct nouveau_instmem_engine instmem;
492	struct nouveau_mc_engine      mc;
493	struct nouveau_timer_engine   timer;
494	struct nouveau_fb_engine      fb;
495	struct nouveau_pgraph_engine  graph;
496	struct nouveau_fifo_engine    fifo;
497	struct nouveau_display_engine display;
498	struct nouveau_gpio_engine    gpio;
499	struct nouveau_pm_engine      pm;
500	struct nouveau_crypt_engine   crypt;
501};
502
503struct nouveau_pll_vals {
504	union {
505		struct {
506#ifdef __BIG_ENDIAN
507			uint8_t N1, M1, N2, M2;
508#else
509			uint8_t M1, N1, M2, N2;
510#endif
511		};
512		struct {
513			uint16_t NM1, NM2;
514		} __attribute__((packed));
515	};
516	int log2P;
517
518	int refclk;
519};
520
521enum nv04_fp_display_regs {
522	FP_DISPLAY_END,
523	FP_TOTAL,
524	FP_CRTC,
525	FP_SYNC_START,
526	FP_SYNC_END,
527	FP_VALID_START,
528	FP_VALID_END
529};
530
531struct nv04_crtc_reg {
532	unsigned char MiscOutReg;
533	uint8_t CRTC[0xa0];
534	uint8_t CR58[0x10];
535	uint8_t Sequencer[5];
536	uint8_t Graphics[9];
537	uint8_t Attribute[21];
538	unsigned char DAC[768];
539
540	/* PCRTC regs */
541	uint32_t fb_start;
542	uint32_t crtc_cfg;
543	uint32_t cursor_cfg;
544	uint32_t gpio_ext;
545	uint32_t crtc_830;
546	uint32_t crtc_834;
547	uint32_t crtc_850;
548	uint32_t crtc_eng_ctrl;
549
550	/* PRAMDAC regs */
551	uint32_t nv10_cursync;
552	struct nouveau_pll_vals pllvals;
553	uint32_t ramdac_gen_ctrl;
554	uint32_t ramdac_630;
555	uint32_t ramdac_634;
556	uint32_t tv_setup;
557	uint32_t tv_vtotal;
558	uint32_t tv_vskew;
559	uint32_t tv_vsync_delay;
560	uint32_t tv_htotal;
561	uint32_t tv_hskew;
562	uint32_t tv_hsync_delay;
563	uint32_t tv_hsync_delay2;
564	uint32_t fp_horiz_regs[7];
565	uint32_t fp_vert_regs[7];
566	uint32_t dither;
567	uint32_t fp_control;
568	uint32_t dither_regs[6];
569	uint32_t fp_debug_0;
570	uint32_t fp_debug_1;
571	uint32_t fp_debug_2;
572	uint32_t fp_margin_color;
573	uint32_t ramdac_8c0;
574	uint32_t ramdac_a20;
575	uint32_t ramdac_a24;
576	uint32_t ramdac_a34;
577	uint32_t ctv_regs[38];
578};
579
580struct nv04_output_reg {
581	uint32_t output;
582	int head;
583};
584
585struct nv04_mode_state {
586	struct nv04_crtc_reg crtc_reg[2];
587	uint32_t pllsel;
588	uint32_t sel_clk;
589};
590
591enum nouveau_card_type {
592	NV_04      = 0x00,
593	NV_10      = 0x10,
594	NV_20      = 0x20,
595	NV_30      = 0x30,
596	NV_40      = 0x40,
597	NV_50      = 0x50,
598	NV_C0      = 0xc0,
599};
600
601struct drm_nouveau_private {
602	struct drm_device *dev;
603
604	/* the card type, takes NV_* as values */
605	enum nouveau_card_type card_type;
606	/* exact chipset, derived from NV_PMC_BOOT_0 */
607	int chipset;
608	int flags;
609
610	void __iomem *mmio;
611
612	spinlock_t ramin_lock;
613	void __iomem *ramin;
614	u32 ramin_size;
615	u32 ramin_base;
616	bool ramin_available;
617	struct drm_mm ramin_heap;
618	struct list_head gpuobj_list;
619	struct list_head classes;
620
621	struct nouveau_bo *vga_ram;
622
623	/* interrupt handling */
624	void (*irq_handler[32])(struct drm_device *);
625	bool msi_enabled;
626	struct workqueue_struct *wq;
627	struct work_struct irq_work;
628
629	struct list_head vbl_waiting;
630
631	struct {
632		struct drm_global_reference mem_global_ref;
633		struct ttm_bo_global_ref bo_global_ref;
634		struct ttm_bo_device bdev;
635		atomic_t validate_sequence;
636	} ttm;
637
638	struct {
639		spinlock_t lock;
640		struct drm_mm heap;
641		struct nouveau_bo *bo;
642	} fence;
643
644	struct {
645		spinlock_t lock;
646		struct nouveau_channel *ptr[NOUVEAU_MAX_CHANNEL_NR];
647	} channels;
648
649	struct nouveau_engine engine;
650	struct nouveau_channel *channel;
651
652	/* For PFIFO and PGRAPH. */
653	spinlock_t context_switch_lock;
654
655	/* RAMIN configuration, RAMFC, RAMHT and RAMRO offsets */
656	struct nouveau_ramht  *ramht;
657	struct nouveau_gpuobj *ramfc;
658	struct nouveau_gpuobj *ramro;
659
660	uint32_t ramin_rsvd_vram;
661
662	struct {
663		enum {
664			NOUVEAU_GART_NONE = 0,
665			NOUVEAU_GART_AGP,
666			NOUVEAU_GART_SGDMA
667		} type;
668		uint64_t aper_base;
669		uint64_t aper_size;
670		uint64_t aper_free;
671
672		struct nouveau_gpuobj *sg_ctxdma;
673		struct page *sg_dummy_page;
674		dma_addr_t sg_dummy_bus;
675	} gart_info;
676
677	/* nv10-nv40 tiling regions */
678	struct {
679		struct nouveau_tile_reg reg[NOUVEAU_MAX_TILE_NR];
680		spinlock_t lock;
681	} tile;
682
683	/* VRAM/fb configuration */
684	uint64_t vram_size;
685	uint64_t vram_sys_base;
686	u32 vram_rblock_size;
687
688	uint64_t fb_phys;
689	uint64_t fb_available_size;
690	uint64_t fb_mappable_pages;
691	uint64_t fb_aper_free;
692	int fb_mtrr;
693
694	/* G8x/G9x virtual address space */
695	uint64_t vm_gart_base;
696	uint64_t vm_gart_size;
697	uint64_t vm_vram_base;
698	uint64_t vm_vram_size;
699	uint64_t vm_end;
700	struct nouveau_gpuobj *vm_vram_pt[NV50_VM_VRAM_NR];
701	int vm_vram_pt_nr;
702
703	struct nvbios vbios;
704
705	struct nv04_mode_state mode_reg;
706	struct nv04_mode_state saved_reg;
707	uint32_t saved_vga_font[4][16384];
708	uint32_t crtc_owner;
709	uint32_t dac_users[4];
710
711	struct nouveau_suspend_resume {
712		uint32_t *ramin_copy;
713	} susres;
714
715	struct backlight_device *backlight;
716
717	struct nouveau_channel *evo;
718	u32 evo_alloc;
719	struct {
720		struct dcb_entry *dcb;
721		u16 script;
722		u32 pclk;
723	} evo_irq;
724
725	struct {
726		struct dentry *channel_root;
727	} debugfs;
728
729	struct nouveau_fbdev *nfbdev;
730	struct apertures_struct *apertures;
731};
732
733static inline struct drm_nouveau_private *
734nouveau_private(struct drm_device *dev)
735{
736	return dev->dev_private;
737}
738
739static inline struct drm_nouveau_private *
740nouveau_bdev(struct ttm_bo_device *bd)
741{
742	return container_of(bd, struct drm_nouveau_private, ttm.bdev);
743}
744
745static inline int
746nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pnvbo)
747{
748	struct nouveau_bo *prev;
749
750	if (!pnvbo)
751		return -EINVAL;
752	prev = *pnvbo;
753
754	*pnvbo = ref ? nouveau_bo(ttm_bo_reference(&ref->bo)) : NULL;
755	if (prev) {
756		struct ttm_buffer_object *bo = &prev->bo;
757
758		ttm_bo_unref(&bo);
759	}
760
761	return 0;
762}
763
764/* nouveau_drv.c */
765extern int nouveau_agpmode;
766extern int nouveau_duallink;
767extern int nouveau_uscript_lvds;
768extern int nouveau_uscript_tmds;
769extern int nouveau_vram_pushbuf;
770extern int nouveau_vram_notify;
771extern int nouveau_fbpercrtc;
772extern int nouveau_tv_disable;
773extern char *nouveau_tv_norm;
774extern int nouveau_reg_debug;
775extern char *nouveau_vbios;
776extern int nouveau_ignorelid;
777extern int nouveau_nofbaccel;
778extern int nouveau_noaccel;
779extern int nouveau_force_post;
780extern int nouveau_override_conntype;
781extern char *nouveau_perflvl;
782extern int nouveau_perflvl_wr;
783extern int nouveau_msi;
784
785extern int nouveau_pci_suspend(struct pci_dev *pdev, pm_message_t pm_state);
786extern int nouveau_pci_resume(struct pci_dev *pdev);
787
788/* nouveau_state.c */
789extern void nouveau_preclose(struct drm_device *dev, struct drm_file *);
790extern int  nouveau_load(struct drm_device *, unsigned long flags);
791extern int  nouveau_firstopen(struct drm_device *);
792extern void nouveau_lastclose(struct drm_device *);
793extern int  nouveau_unload(struct drm_device *);
794extern int  nouveau_ioctl_getparam(struct drm_device *, void *data,
795				   struct drm_file *);
796extern int  nouveau_ioctl_setparam(struct drm_device *, void *data,
797				   struct drm_file *);
798extern bool nouveau_wait_until(struct drm_device *, uint64_t timeout,
799			       uint32_t reg, uint32_t mask, uint32_t val);
800extern bool nouveau_wait_for_idle(struct drm_device *);
801extern int  nouveau_card_init(struct drm_device *);
802
803/* nouveau_mem.c */
804extern int  nouveau_mem_vram_init(struct drm_device *);
805extern void nouveau_mem_vram_fini(struct drm_device *);
806extern int  nouveau_mem_gart_init(struct drm_device *);
807extern void nouveau_mem_gart_fini(struct drm_device *);
808extern int  nouveau_mem_init_agp(struct drm_device *);
809extern int  nouveau_mem_reset_agp(struct drm_device *);
810extern void nouveau_mem_close(struct drm_device *);
811extern struct nouveau_tile_reg *nv10_mem_set_tiling(
812	struct drm_device *dev, uint32_t addr, uint32_t size,
813	uint32_t pitch, uint32_t flags);
814extern void nv10_mem_put_tile_region(struct drm_device *dev,
815				     struct nouveau_tile_reg *tile,
816				     struct nouveau_fence *fence);
817extern int  nv50_mem_vm_bind_linear(struct drm_device *, uint64_t virt,
818				    uint32_t size, uint32_t flags,
819				    uint64_t phys);
820extern void nv50_mem_vm_unbind(struct drm_device *, uint64_t virt,
821			       uint32_t size);
822
823/* nouveau_notifier.c */
824extern int  nouveau_notifier_init_channel(struct nouveau_channel *);
825extern void nouveau_notifier_takedown_channel(struct nouveau_channel *);
826extern int  nouveau_notifier_alloc(struct nouveau_channel *, uint32_t handle,
827				   int cout, uint32_t *offset);
828extern int  nouveau_notifier_offset(struct nouveau_gpuobj *, uint32_t *);
829extern int  nouveau_ioctl_notifier_alloc(struct drm_device *, void *data,
830					 struct drm_file *);
831extern int  nouveau_ioctl_notifier_free(struct drm_device *, void *data,
832					struct drm_file *);
833
834/* nouveau_channel.c */
835extern struct drm_ioctl_desc nouveau_ioctls[];
836extern int nouveau_max_ioctl;
837extern void nouveau_channel_cleanup(struct drm_device *, struct drm_file *);
838extern int  nouveau_channel_alloc(struct drm_device *dev,
839				  struct nouveau_channel **chan,
840				  struct drm_file *file_priv,
841				  uint32_t fb_ctxdma, uint32_t tt_ctxdma);
842extern struct nouveau_channel *
843nouveau_channel_get_unlocked(struct nouveau_channel *);
844extern struct nouveau_channel *
845nouveau_channel_get(struct drm_device *, struct drm_file *, int id);
846extern void nouveau_channel_put_unlocked(struct nouveau_channel **);
847extern void nouveau_channel_put(struct nouveau_channel **);
848extern void nouveau_channel_ref(struct nouveau_channel *chan,
849				struct nouveau_channel **pchan);
850
851/* nouveau_object.c */
852#define NVOBJ_CLASS(d,c,e) do {                                                \
853	int ret = nouveau_gpuobj_class_new((d), (c), NVOBJ_ENGINE_##e);        \
854	if (ret)                                                               \
855		return ret;                                                    \
856} while(0)
857
858#define NVOBJ_MTHD(d,c,m,e) do {                                               \
859	int ret = nouveau_gpuobj_mthd_new((d), (c), (m), (e));                 \
860	if (ret)                                                               \
861		return ret;                                                    \
862} while(0)
863
864extern int  nouveau_gpuobj_early_init(struct drm_device *);
865extern int  nouveau_gpuobj_init(struct drm_device *);
866extern void nouveau_gpuobj_takedown(struct drm_device *);
867extern int  nouveau_gpuobj_suspend(struct drm_device *dev);
868extern void nouveau_gpuobj_suspend_cleanup(struct drm_device *dev);
869extern void nouveau_gpuobj_resume(struct drm_device *dev);
870extern int  nouveau_gpuobj_class_new(struct drm_device *, u32 class, u32 eng);
871extern int  nouveau_gpuobj_mthd_new(struct drm_device *, u32 class, u32 mthd,
872				    int (*exec)(struct nouveau_channel *,
873					        u32 class, u32 mthd, u32 data));
874extern int  nouveau_gpuobj_mthd_call(struct nouveau_channel *, u32, u32, u32);
875extern int  nouveau_gpuobj_mthd_call2(struct drm_device *, int, u32, u32, u32);
876extern int nouveau_gpuobj_channel_init(struct nouveau_channel *,
877				       uint32_t vram_h, uint32_t tt_h);
878extern void nouveau_gpuobj_channel_takedown(struct nouveau_channel *);
879extern int nouveau_gpuobj_new(struct drm_device *, struct nouveau_channel *,
880			      uint32_t size, int align, uint32_t flags,
881			      struct nouveau_gpuobj **);
882extern void nouveau_gpuobj_ref(struct nouveau_gpuobj *,
883			       struct nouveau_gpuobj **);
884extern int nouveau_gpuobj_new_fake(struct drm_device *, u32 pinst, u64 vinst,
885				   u32 size, u32 flags,
886				   struct nouveau_gpuobj **);
887extern int nouveau_gpuobj_dma_new(struct nouveau_channel *, int class,
888				  uint64_t offset, uint64_t size, int access,
889				  int target, struct nouveau_gpuobj **);
890extern int nouveau_gpuobj_gart_dma_new(struct nouveau_channel *,
891				       uint64_t offset, uint64_t size,
892				       int access, struct nouveau_gpuobj **,
893				       uint32_t *o_ret);
894extern int nouveau_gpuobj_gr_new(struct nouveau_channel *, int class,
895				 struct nouveau_gpuobj **);
896extern int nouveau_ioctl_grobj_alloc(struct drm_device *, void *data,
897				     struct drm_file *);
898extern int nouveau_ioctl_gpuobj_free(struct drm_device *, void *data,
899				     struct drm_file *);
900
901/* nouveau_irq.c */
902extern int         nouveau_irq_init(struct drm_device *);
903extern void        nouveau_irq_fini(struct drm_device *);
904extern irqreturn_t nouveau_irq_handler(DRM_IRQ_ARGS);
905extern void        nouveau_irq_register(struct drm_device *, int status_bit,
906					void (*)(struct drm_device *));
907extern void        nouveau_irq_unregister(struct drm_device *, int status_bit);
908extern void        nouveau_irq_preinstall(struct drm_device *);
909extern int         nouveau_irq_postinstall(struct drm_device *);
910extern void        nouveau_irq_uninstall(struct drm_device *);
911
912/* nouveau_sgdma.c */
913extern int nouveau_sgdma_init(struct drm_device *);
914extern void nouveau_sgdma_takedown(struct drm_device *);
915extern int nouveau_sgdma_get_page(struct drm_device *, uint32_t offset,
916				  uint32_t *page);
917extern struct ttm_backend *nouveau_sgdma_init_ttm(struct drm_device *);
918
919/* nouveau_debugfs.c */
920#if defined(CONFIG_DRM_NOUVEAU_DEBUG)
921extern int  nouveau_debugfs_init(struct drm_minor *);
922extern void nouveau_debugfs_takedown(struct drm_minor *);
923extern int  nouveau_debugfs_channel_init(struct nouveau_channel *);
924extern void nouveau_debugfs_channel_fini(struct nouveau_channel *);
925#else
926static inline int
927nouveau_debugfs_init(struct drm_minor *minor)
928{
929	return 0;
930}
931
932static inline void nouveau_debugfs_takedown(struct drm_minor *minor)
933{
934}
935
936static inline int
937nouveau_debugfs_channel_init(struct nouveau_channel *chan)
938{
939	return 0;
940}
941
942static inline void
943nouveau_debugfs_channel_fini(struct nouveau_channel *chan)
944{
945}
946#endif
947
948/* nouveau_dma.c */
949extern void nouveau_dma_pre_init(struct nouveau_channel *);
950extern int  nouveau_dma_init(struct nouveau_channel *);
951extern int  nouveau_dma_wait(struct nouveau_channel *, int slots, int size);
952
953/* nouveau_acpi.c */
954#define ROM_BIOS_PAGE 4096
955#if defined(CONFIG_ACPI)
956void nouveau_register_dsm_handler(void);
957void nouveau_unregister_dsm_handler(void);
958int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
959bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
960int nouveau_acpi_edid(struct drm_device *, struct drm_connector *);
961#else
962static inline void nouveau_register_dsm_handler(void) {}
963static inline void nouveau_unregister_dsm_handler(void) {}
964static inline bool nouveau_acpi_rom_supported(struct pci_dev *pdev) { return false; }
965static inline int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) { return -EINVAL; }
966static inline int nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector) { return -EINVAL; }
967#endif
968
969/* nouveau_backlight.c */
970#ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
971extern int nouveau_backlight_init(struct drm_device *);
972extern void nouveau_backlight_exit(struct drm_device *);
973#else
974static inline int nouveau_backlight_init(struct drm_device *dev)
975{
976	return 0;
977}
978
979static inline void nouveau_backlight_exit(struct drm_device *dev) { }
980#endif
981
982/* nouveau_bios.c */
983extern int nouveau_bios_init(struct drm_device *);
984extern void nouveau_bios_takedown(struct drm_device *dev);
985extern int nouveau_run_vbios_init(struct drm_device *);
986extern void nouveau_bios_run_init_table(struct drm_device *, uint16_t table,
987					struct dcb_entry *);
988extern struct dcb_gpio_entry *nouveau_bios_gpio_entry(struct drm_device *,
989						      enum dcb_gpio_tag);
990extern struct dcb_connector_table_entry *
991nouveau_bios_connector_entry(struct drm_device *, int index);
992extern u32 get_pll_register(struct drm_device *, enum pll_types);
993extern int get_pll_limits(struct drm_device *, uint32_t limit_match,
994			  struct pll_lims *);
995extern int nouveau_bios_run_display_table(struct drm_device *,
996					  struct dcb_entry *,
997					  uint32_t script, int pxclk);
998extern void *nouveau_bios_dp_table(struct drm_device *, struct dcb_entry *,
999				   int *length);
1000extern bool nouveau_bios_fp_mode(struct drm_device *, struct drm_display_mode *);
1001extern uint8_t *nouveau_bios_embedded_edid(struct drm_device *);
1002extern int nouveau_bios_parse_lvds_table(struct drm_device *, int pxclk,
1003					 bool *dl, bool *if_is_24bit);
1004extern int run_tmds_table(struct drm_device *, struct dcb_entry *,
1005			  int head, int pxclk);
1006extern int call_lvds_script(struct drm_device *, struct dcb_entry *, int head,
1007			    enum LVDS_script, int pxclk);
1008
1009/* nouveau_ttm.c */
1010int nouveau_ttm_global_init(struct drm_nouveau_private *);
1011void nouveau_ttm_global_release(struct drm_nouveau_private *);
1012int nouveau_ttm_mmap(struct file *, struct vm_area_struct *);
1013
1014/* nouveau_dp.c */
1015int nouveau_dp_auxch(struct nouveau_i2c_chan *auxch, int cmd, int addr,
1016		     uint8_t *data, int data_nr);
1017bool nouveau_dp_detect(struct drm_encoder *);
1018bool nouveau_dp_link_train(struct drm_encoder *);
1019
1020/* nv04_fb.c */
1021extern int  nv04_fb_init(struct drm_device *);
1022extern void nv04_fb_takedown(struct drm_device *);
1023
1024/* nv10_fb.c */
1025extern int  nv10_fb_init(struct drm_device *);
1026extern void nv10_fb_takedown(struct drm_device *);
1027extern void nv10_fb_init_tile_region(struct drm_device *dev, int i,
1028				     uint32_t addr, uint32_t size,
1029				     uint32_t pitch, uint32_t flags);
1030extern void nv10_fb_set_tile_region(struct drm_device *dev, int i);
1031extern void nv10_fb_free_tile_region(struct drm_device *dev, int i);
1032
1033/* nv30_fb.c */
1034extern int  nv30_fb_init(struct drm_device *);
1035extern void nv30_fb_takedown(struct drm_device *);
1036extern void nv30_fb_init_tile_region(struct drm_device *dev, int i,
1037				     uint32_t addr, uint32_t size,
1038				     uint32_t pitch, uint32_t flags);
1039extern void nv30_fb_free_tile_region(struct drm_device *dev, int i);
1040
1041/* nv40_fb.c */
1042extern int  nv40_fb_init(struct drm_device *);
1043extern void nv40_fb_takedown(struct drm_device *);
1044extern void nv40_fb_set_tile_region(struct drm_device *dev, int i);
1045
1046/* nv50_fb.c */
1047extern int  nv50_fb_init(struct drm_device *);
1048extern void nv50_fb_takedown(struct drm_device *);
1049extern void nv50_fb_vm_trap(struct drm_device *, int display, const char *);
1050
1051/* nvc0_fb.c */
1052extern int  nvc0_fb_init(struct drm_device *);
1053extern void nvc0_fb_takedown(struct drm_device *);
1054
1055/* nv04_fifo.c */
1056extern int  nv04_fifo_init(struct drm_device *);
1057extern void nv04_fifo_fini(struct drm_device *);
1058extern void nv04_fifo_disable(struct drm_device *);
1059extern void nv04_fifo_enable(struct drm_device *);
1060extern bool nv04_fifo_reassign(struct drm_device *, bool);
1061extern bool nv04_fifo_cache_pull(struct drm_device *, bool);
1062extern int  nv04_fifo_channel_id(struct drm_device *);
1063extern int  nv04_fifo_create_context(struct nouveau_channel *);
1064extern void nv04_fifo_destroy_context(struct nouveau_channel *);
1065extern int  nv04_fifo_load_context(struct nouveau_channel *);
1066extern int  nv04_fifo_unload_context(struct drm_device *);
1067extern void nv04_fifo_isr(struct drm_device *);
1068
1069/* nv10_fifo.c */
1070extern int  nv10_fifo_init(struct drm_device *);
1071extern int  nv10_fifo_channel_id(struct drm_device *);
1072extern int  nv10_fifo_create_context(struct nouveau_channel *);
1073extern int  nv10_fifo_load_context(struct nouveau_channel *);
1074extern int  nv10_fifo_unload_context(struct drm_device *);
1075
1076/* nv40_fifo.c */
1077extern int  nv40_fifo_init(struct drm_device *);
1078extern int  nv40_fifo_create_context(struct nouveau_channel *);
1079extern int  nv40_fifo_load_context(struct nouveau_channel *);
1080extern int  nv40_fifo_unload_context(struct drm_device *);
1081
1082/* nv50_fifo.c */
1083extern int  nv50_fifo_init(struct drm_device *);
1084extern void nv50_fifo_takedown(struct drm_device *);
1085extern int  nv50_fifo_channel_id(struct drm_device *);
1086extern int  nv50_fifo_create_context(struct nouveau_channel *);
1087extern void nv50_fifo_destroy_context(struct nouveau_channel *);
1088extern int  nv50_fifo_load_context(struct nouveau_channel *);
1089extern int  nv50_fifo_unload_context(struct drm_device *);
1090extern void nv50_fifo_tlb_flush(struct drm_device *dev);
1091
1092/* nvc0_fifo.c */
1093extern int  nvc0_fifo_init(struct drm_device *);
1094extern void nvc0_fifo_takedown(struct drm_device *);
1095extern void nvc0_fifo_disable(struct drm_device *);
1096extern void nvc0_fifo_enable(struct drm_device *);
1097extern bool nvc0_fifo_reassign(struct drm_device *, bool);
1098extern bool nvc0_fifo_cache_pull(struct drm_device *, bool);
1099extern int  nvc0_fifo_channel_id(struct drm_device *);
1100extern int  nvc0_fifo_create_context(struct nouveau_channel *);
1101extern void nvc0_fifo_destroy_context(struct nouveau_channel *);
1102extern int  nvc0_fifo_load_context(struct nouveau_channel *);
1103extern int  nvc0_fifo_unload_context(struct drm_device *);
1104
1105/* nv04_graph.c */
1106extern int  nv04_graph_init(struct drm_device *);
1107extern void nv04_graph_takedown(struct drm_device *);
1108extern void nv04_graph_fifo_access(struct drm_device *, bool);
1109extern struct nouveau_channel *nv04_graph_channel(struct drm_device *);
1110extern int  nv04_graph_create_context(struct nouveau_channel *);
1111extern void nv04_graph_destroy_context(struct nouveau_channel *);
1112extern int  nv04_graph_load_context(struct nouveau_channel *);
1113extern int  nv04_graph_unload_context(struct drm_device *);
1114extern int  nv04_graph_mthd_page_flip(struct nouveau_channel *chan,
1115				      u32 class, u32 mthd, u32 data);
1116extern struct nouveau_bitfield nv04_graph_nsource[];
1117
1118/* nv10_graph.c */
1119extern int  nv10_graph_init(struct drm_device *);
1120extern void nv10_graph_takedown(struct drm_device *);
1121extern struct nouveau_channel *nv10_graph_channel(struct drm_device *);
1122extern int  nv10_graph_create_context(struct nouveau_channel *);
1123extern void nv10_graph_destroy_context(struct nouveau_channel *);
1124extern int  nv10_graph_load_context(struct nouveau_channel *);
1125extern int  nv10_graph_unload_context(struct drm_device *);
1126extern void nv10_graph_set_tile_region(struct drm_device *dev, int i);
1127extern struct nouveau_bitfield nv10_graph_intr[];
1128extern struct nouveau_bitfield nv10_graph_nstatus[];
1129
1130/* nv20_graph.c */
1131extern int  nv20_graph_create_context(struct nouveau_channel *);
1132extern void nv20_graph_destroy_context(struct nouveau_channel *);
1133extern int  nv20_graph_load_context(struct nouveau_channel *);
1134extern int  nv20_graph_unload_context(struct drm_device *);
1135extern int  nv20_graph_init(struct drm_device *);
1136extern void nv20_graph_takedown(struct drm_device *);
1137extern int  nv30_graph_init(struct drm_device *);
1138extern void nv20_graph_set_tile_region(struct drm_device *dev, int i);
1139
1140/* nv40_graph.c */
1141extern int  nv40_graph_init(struct drm_device *);
1142extern void nv40_graph_takedown(struct drm_device *);
1143extern struct nouveau_channel *nv40_graph_channel(struct drm_device *);
1144extern int  nv40_graph_create_context(struct nouveau_channel *);
1145extern void nv40_graph_destroy_context(struct nouveau_channel *);
1146extern int  nv40_graph_load_context(struct nouveau_channel *);
1147extern int  nv40_graph_unload_context(struct drm_device *);
1148extern void nv40_grctx_init(struct nouveau_grctx *);
1149extern void nv40_graph_set_tile_region(struct drm_device *dev, int i);
1150
1151/* nv50_graph.c */
1152extern int  nv50_graph_init(struct drm_device *);
1153extern void nv50_graph_takedown(struct drm_device *);
1154extern void nv50_graph_fifo_access(struct drm_device *, bool);
1155extern struct nouveau_channel *nv50_graph_channel(struct drm_device *);
1156extern int  nv50_graph_create_context(struct nouveau_channel *);
1157extern void nv50_graph_destroy_context(struct nouveau_channel *);
1158extern int  nv50_graph_load_context(struct nouveau_channel *);
1159extern int  nv50_graph_unload_context(struct drm_device *);
1160extern int  nv50_grctx_init(struct nouveau_grctx *);
1161extern void nv50_graph_tlb_flush(struct drm_device *dev);
1162extern void nv86_graph_tlb_flush(struct drm_device *dev);
1163
1164/* nvc0_graph.c */
1165extern int  nvc0_graph_init(struct drm_device *);
1166extern void nvc0_graph_takedown(struct drm_device *);
1167extern void nvc0_graph_fifo_access(struct drm_device *, bool);
1168extern struct nouveau_channel *nvc0_graph_channel(struct drm_device *);
1169extern int  nvc0_graph_create_context(struct nouveau_channel *);
1170extern void nvc0_graph_destroy_context(struct nouveau_channel *);
1171extern int  nvc0_graph_load_context(struct nouveau_channel *);
1172extern int  nvc0_graph_unload_context(struct drm_device *);
1173
1174/* nv84_crypt.c */
1175extern int  nv84_crypt_init(struct drm_device *dev);
1176extern void nv84_crypt_fini(struct drm_device *dev);
1177extern int  nv84_crypt_create_context(struct nouveau_channel *);
1178extern void nv84_crypt_destroy_context(struct nouveau_channel *);
1179extern void nv84_crypt_tlb_flush(struct drm_device *dev);
1180
1181/* nv04_instmem.c */
1182extern int  nv04_instmem_init(struct drm_device *);
1183extern void nv04_instmem_takedown(struct drm_device *);
1184extern int  nv04_instmem_suspend(struct drm_device *);
1185extern void nv04_instmem_resume(struct drm_device *);
1186extern int  nv04_instmem_populate(struct drm_device *, struct nouveau_gpuobj *,
1187				  u32 *size, u32 align);
1188extern void nv04_instmem_clear(struct drm_device *, struct nouveau_gpuobj *);
1189extern int  nv04_instmem_bind(struct drm_device *, struct nouveau_gpuobj *);
1190extern int  nv04_instmem_unbind(struct drm_device *, struct nouveau_gpuobj *);
1191extern void nv04_instmem_flush(struct drm_device *);
1192
1193/* nv50_instmem.c */
1194extern int  nv50_instmem_init(struct drm_device *);
1195extern void nv50_instmem_takedown(struct drm_device *);
1196extern int  nv50_instmem_suspend(struct drm_device *);
1197extern void nv50_instmem_resume(struct drm_device *);
1198extern int  nv50_instmem_populate(struct drm_device *, struct nouveau_gpuobj *,
1199				  u32 *size, u32 align);
1200extern void nv50_instmem_clear(struct drm_device *, struct nouveau_gpuobj *);
1201extern int  nv50_instmem_bind(struct drm_device *, struct nouveau_gpuobj *);
1202extern int  nv50_instmem_unbind(struct drm_device *, struct nouveau_gpuobj *);
1203extern void nv50_instmem_flush(struct drm_device *);
1204extern void nv84_instmem_flush(struct drm_device *);
1205extern void nv50_vm_flush(struct drm_device *, int engine);
1206
1207/* nvc0_instmem.c */
1208extern int  nvc0_instmem_init(struct drm_device *);
1209extern void nvc0_instmem_takedown(struct drm_device *);
1210extern int  nvc0_instmem_suspend(struct drm_device *);
1211extern void nvc0_instmem_resume(struct drm_device *);
1212extern int  nvc0_instmem_populate(struct drm_device *, struct nouveau_gpuobj *,
1213				  u32 *size, u32 align);
1214extern void nvc0_instmem_clear(struct drm_device *, struct nouveau_gpuobj *);
1215extern int  nvc0_instmem_bind(struct drm_device *, struct nouveau_gpuobj *);
1216extern int  nvc0_instmem_unbind(struct drm_device *, struct nouveau_gpuobj *);
1217extern void nvc0_instmem_flush(struct drm_device *);
1218
1219/* nv04_mc.c */
1220extern int  nv04_mc_init(struct drm_device *);
1221extern void nv04_mc_takedown(struct drm_device *);
1222
1223/* nv40_mc.c */
1224extern int  nv40_mc_init(struct drm_device *);
1225extern void nv40_mc_takedown(struct drm_device *);
1226
1227/* nv50_mc.c */
1228extern int  nv50_mc_init(struct drm_device *);
1229extern void nv50_mc_takedown(struct drm_device *);
1230
1231/* nv04_timer.c */
1232extern int  nv04_timer_init(struct drm_device *);
1233extern uint64_t nv04_timer_read(struct drm_device *);
1234extern void nv04_timer_takedown(struct drm_device *);
1235
1236extern long nouveau_compat_ioctl(struct file *file, unsigned int cmd,
1237				 unsigned long arg);
1238
1239/* nv04_dac.c */
1240extern int nv04_dac_create(struct drm_connector *, struct dcb_entry *);
1241extern uint32_t nv17_dac_sample_load(struct drm_encoder *encoder);
1242extern int nv04_dac_output_offset(struct drm_encoder *encoder);
1243extern void nv04_dac_update_dacclk(struct drm_encoder *encoder, bool enable);
1244extern bool nv04_dac_in_use(struct drm_encoder *encoder);
1245
1246/* nv04_dfp.c */
1247extern int nv04_dfp_create(struct drm_connector *, struct dcb_entry *);
1248extern int nv04_dfp_get_bound_head(struct drm_device *dev, struct dcb_entry *dcbent);
1249extern void nv04_dfp_bind_head(struct drm_device *dev, struct dcb_entry *dcbent,
1250			       int head, bool dl);
1251extern void nv04_dfp_disable(struct drm_device *dev, int head);
1252extern void nv04_dfp_update_fp_control(struct drm_encoder *encoder, int mode);
1253
1254/* nv04_tv.c */
1255extern int nv04_tv_identify(struct drm_device *dev, int i2c_index);
1256extern int nv04_tv_create(struct drm_connector *, struct dcb_entry *);
1257
1258/* nv17_tv.c */
1259extern int nv17_tv_create(struct drm_connector *, struct dcb_entry *);
1260
1261/* nv04_display.c */
1262extern int nv04_display_early_init(struct drm_device *);
1263extern void nv04_display_late_takedown(struct drm_device *);
1264extern int nv04_display_create(struct drm_device *);
1265extern int nv04_display_init(struct drm_device *);
1266extern void nv04_display_destroy(struct drm_device *);
1267
1268/* nv04_crtc.c */
1269extern int nv04_crtc_create(struct drm_device *, int index);
1270
1271/* nouveau_bo.c */
1272extern struct ttm_bo_driver nouveau_bo_driver;
1273extern int nouveau_bo_new(struct drm_device *, struct nouveau_channel *,
1274			  int size, int align, uint32_t flags,
1275			  uint32_t tile_mode, uint32_t tile_flags,
1276			  bool no_vm, bool mappable, struct nouveau_bo **);
1277extern int nouveau_bo_pin(struct nouveau_bo *, uint32_t flags);
1278extern int nouveau_bo_unpin(struct nouveau_bo *);
1279extern int nouveau_bo_map(struct nouveau_bo *);
1280extern void nouveau_bo_unmap(struct nouveau_bo *);
1281extern void nouveau_bo_placement_set(struct nouveau_bo *, uint32_t type,
1282				     uint32_t busy);
1283extern u16 nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index);
1284extern void nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val);
1285extern u32 nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index);
1286extern void nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val);
1287extern void nouveau_bo_fence(struct nouveau_bo *, struct nouveau_fence *);
1288
1289/* nouveau_fence.c */
1290struct nouveau_fence;
1291extern int nouveau_fence_init(struct drm_device *);
1292extern void nouveau_fence_fini(struct drm_device *);
1293extern int nouveau_fence_channel_init(struct nouveau_channel *);
1294extern void nouveau_fence_channel_fini(struct nouveau_channel *);
1295extern void nouveau_fence_update(struct nouveau_channel *);
1296extern int nouveau_fence_new(struct nouveau_channel *, struct nouveau_fence **,
1297			     bool emit);
1298extern int nouveau_fence_emit(struct nouveau_fence *);
1299extern void nouveau_fence_work(struct nouveau_fence *fence,
1300			       void (*work)(void *priv, bool signalled),
1301			       void *priv);
1302struct nouveau_channel *nouveau_fence_channel(struct nouveau_fence *);
1303
1304extern bool __nouveau_fence_signalled(void *obj, void *arg);
1305extern int __nouveau_fence_wait(void *obj, void *arg, bool lazy, bool intr);
1306extern int __nouveau_fence_flush(void *obj, void *arg);
1307extern void __nouveau_fence_unref(void **obj);
1308extern void *__nouveau_fence_ref(void *obj);
1309
1310static inline bool nouveau_fence_signalled(struct nouveau_fence *obj)
1311{
1312	return __nouveau_fence_signalled(obj, NULL);
1313}
1314static inline int
1315nouveau_fence_wait(struct nouveau_fence *obj, bool lazy, bool intr)
1316{
1317	return __nouveau_fence_wait(obj, NULL, lazy, intr);
1318}
1319extern int nouveau_fence_sync(struct nouveau_fence *, struct nouveau_channel *);
1320static inline int nouveau_fence_flush(struct nouveau_fence *obj)
1321{
1322	return __nouveau_fence_flush(obj, NULL);
1323}
1324static inline void nouveau_fence_unref(struct nouveau_fence **obj)
1325{
1326	__nouveau_fence_unref((void **)obj);
1327}
1328static inline struct nouveau_fence *nouveau_fence_ref(struct nouveau_fence *obj)
1329{
1330	return __nouveau_fence_ref(obj);
1331}
1332
1333/* nouveau_gem.c */
1334extern int nouveau_gem_new(struct drm_device *, struct nouveau_channel *,
1335			   int size, int align, uint32_t flags,
1336			   uint32_t tile_mode, uint32_t tile_flags,
1337			   bool no_vm, bool mappable, struct nouveau_bo **);
1338extern int nouveau_gem_object_new(struct drm_gem_object *);
1339extern void nouveau_gem_object_del(struct drm_gem_object *);
1340extern int nouveau_gem_ioctl_new(struct drm_device *, void *,
1341				 struct drm_file *);
1342extern int nouveau_gem_ioctl_pushbuf(struct drm_device *, void *,
1343				     struct drm_file *);
1344extern int nouveau_gem_ioctl_cpu_prep(struct drm_device *, void *,
1345				      struct drm_file *);
1346extern int nouveau_gem_ioctl_cpu_fini(struct drm_device *, void *,
1347				      struct drm_file *);
1348extern int nouveau_gem_ioctl_info(struct drm_device *, void *,
1349				  struct drm_file *);
1350
1351/* nouveau_display.c */
1352int nouveau_vblank_enable(struct drm_device *dev, int crtc);
1353void nouveau_vblank_disable(struct drm_device *dev, int crtc);
1354int nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1355			   struct drm_pending_vblank_event *event);
1356int nouveau_finish_page_flip(struct nouveau_channel *,
1357			     struct nouveau_page_flip_state *);
1358
1359/* nv10_gpio.c */
1360int nv10_gpio_get(struct drm_device *dev, enum dcb_gpio_tag tag);
1361int nv10_gpio_set(struct drm_device *dev, enum dcb_gpio_tag tag, int state);
1362
1363/* nv50_gpio.c */
1364int nv50_gpio_init(struct drm_device *dev);
1365void nv50_gpio_fini(struct drm_device *dev);
1366int nv50_gpio_get(struct drm_device *dev, enum dcb_gpio_tag tag);
1367int nv50_gpio_set(struct drm_device *dev, enum dcb_gpio_tag tag, int state);
1368int  nv50_gpio_irq_register(struct drm_device *, enum dcb_gpio_tag,
1369			    void (*)(void *, int), void *);
1370void nv50_gpio_irq_unregister(struct drm_device *, enum dcb_gpio_tag,
1371			      void (*)(void *, int), void *);
1372bool nv50_gpio_irq_enable(struct drm_device *, enum dcb_gpio_tag, bool on);
1373
1374/* nv50_calc. */
1375int nv50_calc_pll(struct drm_device *, struct pll_lims *, int clk,
1376		  int *N1, int *M1, int *N2, int *M2, int *P);
1377int nv50_calc_pll2(struct drm_device *, struct pll_lims *,
1378		   int clk, int *N, int *fN, int *M, int *P);
1379
1380#ifndef ioread32_native
1381#ifdef __BIG_ENDIAN
1382#define ioread16_native ioread16be
1383#define iowrite16_native iowrite16be
1384#define ioread32_native  ioread32be
1385#define iowrite32_native iowrite32be
1386#else /* def __BIG_ENDIAN */
1387#define ioread16_native ioread16
1388#define iowrite16_native iowrite16
1389#define ioread32_native  ioread32
1390#define iowrite32_native iowrite32
1391#endif /* def __BIG_ENDIAN else */
1392#endif /* !ioread32_native */
1393
1394/* channel control reg access */
1395static inline u32 nvchan_rd32(struct nouveau_channel *chan, unsigned reg)
1396{
1397	return ioread32_native(chan->user + reg);
1398}
1399
1400static inline void nvchan_wr32(struct nouveau_channel *chan,
1401							unsigned reg, u32 val)
1402{
1403	iowrite32_native(val, chan->user + reg);
1404}
1405
1406/* register access */
1407static inline u32 nv_rd32(struct drm_device *dev, unsigned reg)
1408{
1409	struct drm_nouveau_private *dev_priv = dev->dev_private;
1410	return ioread32_native(dev_priv->mmio + reg);
1411}
1412
1413static inline void nv_wr32(struct drm_device *dev, unsigned reg, u32 val)
1414{
1415	struct drm_nouveau_private *dev_priv = dev->dev_private;
1416	iowrite32_native(val, dev_priv->mmio + reg);
1417}
1418
1419static inline u32 nv_mask(struct drm_device *dev, u32 reg, u32 mask, u32 val)
1420{
1421	u32 tmp = nv_rd32(dev, reg);
1422	nv_wr32(dev, reg, (tmp & ~mask) | val);
1423	return tmp;
1424}
1425
1426static inline u8 nv_rd08(struct drm_device *dev, unsigned reg)
1427{
1428	struct drm_nouveau_private *dev_priv = dev->dev_private;
1429	return ioread8(dev_priv->mmio + reg);
1430}
1431
1432static inline void nv_wr08(struct drm_device *dev, unsigned reg, u8 val)
1433{
1434	struct drm_nouveau_private *dev_priv = dev->dev_private;
1435	iowrite8(val, dev_priv->mmio + reg);
1436}
1437
1438#define nv_wait(dev, reg, mask, val) \
1439	nouveau_wait_until(dev, 2000000000ULL, (reg), (mask), (val))
1440
1441/* PRAMIN access */
1442static inline u32 nv_ri32(struct drm_device *dev, unsigned offset)
1443{
1444	struct drm_nouveau_private *dev_priv = dev->dev_private;
1445	return ioread32_native(dev_priv->ramin + offset);
1446}
1447
1448static inline void nv_wi32(struct drm_device *dev, unsigned offset, u32 val)
1449{
1450	struct drm_nouveau_private *dev_priv = dev->dev_private;
1451	iowrite32_native(val, dev_priv->ramin + offset);
1452}
1453
1454/* object access */
1455extern u32 nv_ro32(struct nouveau_gpuobj *, u32 offset);
1456extern void nv_wo32(struct nouveau_gpuobj *, u32 offset, u32 val);
1457
1458/*
1459 * Logging
1460 * Argument d is (struct drm_device *).
1461 */
1462#define NV_PRINTK(level, d, fmt, arg...) \
1463	printk(level "[" DRM_NAME "] " DRIVER_NAME " %s: " fmt, \
1464					pci_name(d->pdev), ##arg)
1465#ifndef NV_DEBUG_NOTRACE
1466#define NV_DEBUG(d, fmt, arg...) do {                                          \
1467	if (drm_debug & DRM_UT_DRIVER) {                                       \
1468		NV_PRINTK(KERN_DEBUG, d, "%s:%d - " fmt, __func__,             \
1469			  __LINE__, ##arg);                                    \
1470	}                                                                      \
1471} while (0)
1472#define NV_DEBUG_KMS(d, fmt, arg...) do {                                      \
1473	if (drm_debug & DRM_UT_KMS) {                                          \
1474		NV_PRINTK(KERN_DEBUG, d, "%s:%d - " fmt, __func__,             \
1475			  __LINE__, ##arg);                                    \
1476	}                                                                      \
1477} while (0)
1478#else
1479#define NV_DEBUG(d, fmt, arg...) do {                                          \
1480	if (drm_debug & DRM_UT_DRIVER)                                         \
1481		NV_PRINTK(KERN_DEBUG, d, fmt, ##arg);                          \
1482} while (0)
1483#define NV_DEBUG_KMS(d, fmt, arg...) do {                                      \
1484	if (drm_debug & DRM_UT_KMS)                                            \
1485		NV_PRINTK(KERN_DEBUG, d, fmt, ##arg);                          \
1486} while (0)
1487#endif
1488#define NV_ERROR(d, fmt, arg...) NV_PRINTK(KERN_ERR, d, fmt, ##arg)
1489#define NV_INFO(d, fmt, arg...) NV_PRINTK(KERN_INFO, d, fmt, ##arg)
1490#define NV_TRACEWARN(d, fmt, arg...) NV_PRINTK(KERN_NOTICE, d, fmt, ##arg)
1491#define NV_TRACE(d, fmt, arg...) NV_PRINTK(KERN_INFO, d, fmt, ##arg)
1492#define NV_WARN(d, fmt, arg...) NV_PRINTK(KERN_WARNING, d, fmt, ##arg)
1493
1494/* nouveau_reg_debug bitmask */
1495enum {
1496	NOUVEAU_REG_DEBUG_MC             = 0x1,
1497	NOUVEAU_REG_DEBUG_VIDEO          = 0x2,
1498	NOUVEAU_REG_DEBUG_FB             = 0x4,
1499	NOUVEAU_REG_DEBUG_EXTDEV         = 0x8,
1500	NOUVEAU_REG_DEBUG_CRTC           = 0x10,
1501	NOUVEAU_REG_DEBUG_RAMDAC         = 0x20,
1502	NOUVEAU_REG_DEBUG_VGACRTC        = 0x40,
1503	NOUVEAU_REG_DEBUG_RMVIO          = 0x80,
1504	NOUVEAU_REG_DEBUG_VGAATTR        = 0x100,
1505	NOUVEAU_REG_DEBUG_EVO            = 0x200,
1506};
1507
1508#define NV_REG_DEBUG(type, dev, fmt, arg...) do { \
1509	if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_##type) \
1510		NV_PRINTK(KERN_DEBUG, dev, "%s: " fmt, __func__, ##arg); \
1511} while (0)
1512
1513static inline bool
1514nv_two_heads(struct drm_device *dev)
1515{
1516	struct drm_nouveau_private *dev_priv = dev->dev_private;
1517	const int impl = dev->pci_device & 0x0ff0;
1518
1519	if (dev_priv->card_type >= NV_10 && impl != 0x0100 &&
1520	    impl != 0x0150 && impl != 0x01a0 && impl != 0x0200)
1521		return true;
1522
1523	return false;
1524}
1525
1526static inline bool
1527nv_gf4_disp_arch(struct drm_device *dev)
1528{
1529	return nv_two_heads(dev) && (dev->pci_device & 0x0ff0) != 0x0110;
1530}
1531
1532static inline bool
1533nv_two_reg_pll(struct drm_device *dev)
1534{
1535	struct drm_nouveau_private *dev_priv = dev->dev_private;
1536	const int impl = dev->pci_device & 0x0ff0;
1537
1538	if (impl == 0x0310 || impl == 0x0340 || dev_priv->card_type >= NV_40)
1539		return true;
1540	return false;
1541}
1542
1543static inline bool
1544nv_match_device(struct drm_device *dev, unsigned device,
1545		unsigned sub_vendor, unsigned sub_device)
1546{
1547	return dev->pdev->device == device &&
1548		dev->pdev->subsystem_vendor == sub_vendor &&
1549		dev->pdev->subsystem_device == sub_device;
1550}
1551
1552#define NV_SW                                                        0x0000506e
1553#define NV_SW_DMA_SEMAPHORE                                          0x00000060
1554#define NV_SW_SEMAPHORE_OFFSET                                       0x00000064
1555#define NV_SW_SEMAPHORE_ACQUIRE                                      0x00000068
1556#define NV_SW_SEMAPHORE_RELEASE                                      0x0000006c
1557#define NV_SW_YIELD                                                  0x00000080
1558#define NV_SW_DMA_VBLSEM                                             0x0000018c
1559#define NV_SW_VBLSEM_OFFSET                                          0x00000400
1560#define NV_SW_VBLSEM_RELEASE_VALUE                                   0x00000404
1561#define NV_SW_VBLSEM_RELEASE                                         0x00000408
1562#define NV_SW_PAGE_FLIP                                              0x00000500
1563
1564#endif /* __NOUVEAU_DRV_H__ */
1565