/* * Copyright 2005 Stephane Marchesin. * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #ifndef __NOUVEAU_DRV_H__ #define __NOUVEAU_DRV_H__ #define DRIVER_AUTHOR "Stephane Marchesin" #define DRIVER_EMAIL "dri-devel@lists.sourceforge.net" #define DRIVER_NAME "nouveau" #define DRIVER_DESC "nVidia Riva/TNT/GeForce" #define DRIVER_DATE "20060213" #define DRIVER_MAJOR 0 #define DRIVER_MINOR 0 #define DRIVER_PATCHLEVEL 10 #define NOUVEAU_FAMILY 0x0000FFFF #define NOUVEAU_FLAGS 0xFFFF0000 #include "nouveau_drm.h" #include "nouveau_reg.h" struct mem_block { struct mem_block *next; struct mem_block *prev; uint64_t start; uint64_t size; struct drm_file *file_priv; /* NULL: free, -1: heap, other: real files */ int flags; drm_local_map_t *map; drm_handle_t map_handle; }; enum nouveau_flags { NV_NFORCE =0x10000000, NV_NFORCE2 =0x20000000 }; #define NVOBJ_ENGINE_SW 0 #define NVOBJ_ENGINE_GR 1 #define NVOBJ_ENGINE_INT 0xdeadbeef #define NVOBJ_FLAG_ALLOW_NO_REFS (1 << 0) #define NVOBJ_FLAG_ZERO_ALLOC (1 << 1) #define NVOBJ_FLAG_ZERO_FREE (1 << 2) #define NVOBJ_FLAG_FAKE (1 << 3) struct nouveau_gpuobj { struct list_head list; int im_channel; struct mem_block *im_pramin; struct mem_block *im_backing; int im_bound; uint32_t flags; int refcount; uint32_t engine; uint32_t class; void (*dtor)(struct drm_device *, struct nouveau_gpuobj *); void *priv; }; struct nouveau_gpuobj_ref { struct list_head list; struct nouveau_gpuobj *gpuobj; uint32_t instance; int channel; int handle; }; struct nouveau_channel { struct drm_device *dev; int id; /* owner of this fifo */ struct drm_file *file_priv; /* mapping of the fifo itself */ drm_local_map_t *map; /* mapping of the regs controling the fifo */ drm_local_map_t *regs; /* Fencing */ uint32_t next_sequence; /* DMA push buffer */ struct nouveau_gpuobj_ref *pushbuf; struct mem_block *pushbuf_mem; uint32_t pushbuf_base; /* FIFO user control regs */ uint32_t user, user_size; uint32_t put; uint32_t get; uint32_t ref_cnt; /* Notifier memory */ struct mem_block *notifier_block; struct mem_block *notifier_heap; drm_local_map_t *notifier_map; /* PFIFO context */ struct nouveau_gpuobj_ref *ramfc; /* PGRAPH context */ /* XXX may be merge 2 pointers as private data ??? */ struct nouveau_gpuobj_ref *ramin_grctx; void *pgraph_ctx; /* NV50 VM */ struct nouveau_gpuobj *vm_pd; struct nouveau_gpuobj_ref *vm_gart_pt; /* Objects */ struct nouveau_gpuobj_ref *ramin; /* Private instmem */ struct mem_block *ramin_heap; /* Private PRAMIN heap */ struct nouveau_gpuobj_ref *ramht; /* Hash table */ struct list_head ramht_refs; /* Objects referenced by RAMHT */ }; struct nouveau_drm_channel { struct nouveau_channel *chan; /* DMA state */ int max, put, cur, free; int push_free; volatile uint32_t *pushbuf; /* Notifiers */ uint32_t notify0_offset; /* Buffer moves */ uint32_t m2mf_dma_source; uint32_t m2mf_dma_destin; }; struct nouveau_config { struct { int location; int size; } cmdbuf; }; struct nouveau_instmem_engine { void *priv; int (*init)(struct drm_device *dev); void (*takedown)(struct drm_device *dev); int (*populate)(struct drm_device *, struct nouveau_gpuobj *, uint32_t *size); void (*clear)(struct drm_device *, struct nouveau_gpuobj *); int (*bind)(struct drm_device *, struct nouveau_gpuobj *); int (*unbind)(struct drm_device *, struct nouveau_gpuobj *); }; struct nouveau_mc_engine { int (*init)(struct drm_device *dev); void (*takedown)(struct drm_device *dev); }; struct nouveau_timer_engine { int (*init)(struct drm_device *dev); void (*takedown)(struct drm_device *dev); uint64_t (*read)(struct drm_device *dev); }; struct nouveau_fb_engine { int (*init)(struct drm_device *dev); void (*takedown)(struct drm_device *dev); }; struct nouveau_fifo_engine { void *priv; int channels; int (*init)(struct drm_device *); void (*takedown)(struct drm_device *); int (*channel_id)(struct drm_device *); int (*create_context)(struct nouveau_channel *); void (*destroy_context)(struct nouveau_channel *); int (*load_context)(struct nouveau_channel *); int (*save_context)(struct nouveau_channel *); }; struct nouveau_pgraph_engine { int (*init)(struct drm_device *); void (*takedown)(struct drm_device *); int (*create_context)(struct nouveau_channel *); void (*destroy_context)(struct nouveau_channel *); int (*load_context)(struct nouveau_channel *); int (*save_context)(struct nouveau_channel *); }; struct nouveau_engine { struct nouveau_instmem_engine instmem; struct nouveau_mc_engine mc; struct nouveau_timer_engine timer; struct nouveau_fb_engine fb; struct nouveau_pgraph_engine graph; struct nouveau_fifo_engine fifo; }; #define NOUVEAU_MAX_CHANNEL_NR 128 struct drm_nouveau_private { enum { NOUVEAU_CARD_INIT_DOWN, NOUVEAU_CARD_INIT_DONE, NOUVEAU_CARD_INIT_FAILED } init_state; int ttm; /* the card type, takes NV_* as values */ int card_type; /* exact chipset, derived from NV_PMC_BOOT_0 */ int chipset; int flags; drm_local_map_t *mmio; drm_local_map_t *fb; drm_local_map_t *ramin; /* NV40 onwards */ int fifo_alloc_count; struct nouveau_channel *fifos[NOUVEAU_MAX_CHANNEL_NR]; struct nouveau_engine Engine; struct nouveau_drm_channel channel; /* RAMIN configuration, RAMFC, RAMHT and RAMRO offsets */ struct nouveau_gpuobj *ramht; uint32_t ramin_rsvd_vram; uint32_t ramht_offset; uint32_t ramht_size; uint32_t ramht_bits; uint32_t ramfc_offset; uint32_t ramfc_size; uint32_t ramro_offset; uint32_t ramro_size; /* base physical adresses */ uint64_t fb_phys; uint64_t fb_available_size; struct { enum { NOUVEAU_GART_NONE = 0, NOUVEAU_GART_AGP, NOUVEAU_GART_SGDMA } type; uint64_t aper_base; uint64_t aper_size; struct nouveau_gpuobj *sg_ctxdma; struct page *sg_dummy_page; dma_addr_t sg_dummy_bus; /* nottm hack */ struct drm_ttm_backend *sg_be; unsigned long sg_handle; } gart_info; /* the mtrr covering the FB */ int fb_mtrr; struct mem_block *agp_heap; struct mem_block *fb_heap; struct mem_block *fb_nomap_heap; struct mem_block *ramin_heap; struct mem_block *pci_heap; /* context table pointed to be NV_PGRAPH_CHANNEL_CTX_TABLE (0x400780) */ uint32_t ctx_table_size; struct nouveau_gpuobj_ref *ctx_table; struct nouveau_config config; struct list_head gpuobj_list; }; #define NOUVEAU_CHECK_INITIALISED_WITH_RETURN do { \ struct drm_nouveau_private *nv = dev->dev_private; \ if (nv->init_state != NOUVEAU_CARD_INIT_DONE) { \ DRM_ERROR("called without init\n"); \ return -EINVAL; \ } \ } while(0) #define NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(id,cl,ch) do { \ struct drm_nouveau_private *nv = dev->dev_private; \ if (!nouveau_fifo_owner(dev, (cl), (id))) { \ DRM_ERROR("pid %d doesn't own channel %d\n", \ DRM_CURRENTPID, (id)); \ return -EPERM; \ } \ (ch) = nv->fifos[(id)]; \ } while(0) /* nouveau_state.c */ extern void nouveau_preclose(struct drm_device *dev, struct drm_file *); extern int nouveau_load(struct drm_device *, unsigned long flags); extern int nouveau_firstopen(struct drm_device *); extern void nouveau_lastclose(struct drm_device *); extern int nouveau_unload(struct drm_device *); extern int nouveau_ioctl_getparam(struct drm_device *, void *data, struct drm_file *); extern int nouveau_ioctl_setparam(struct drm_device *, void *data, struct drm_file *); extern void nouveau_wait_for_idle(struct drm_device *); extern int nouveau_card_init(struct drm_device *); extern int nouveau_ioctl_card_init(struct drm_device *, void *data, struct drm_file *); /* nouveau_mem.c */ extern int nouveau_mem_init_heap(struct mem_block **, uint64_t start, uint64_t size); extern struct mem_block *nouveau_mem_alloc_block(struct mem_block *, uint64_t size, int align2, struct drm_file *); extern void nouveau_mem_takedown(struct mem_block **heap); extern void nouveau_mem_free_block(struct mem_block *); extern uint64_t nouveau_mem_fb_amount(struct drm_device *); extern void nouveau_mem_release(struct drm_file *, struct mem_block *heap); extern int nouveau_ioctl_mem_alloc(struct drm_device *, void *data, struct drm_file *); extern int nouveau_ioctl_mem_free(struct drm_device *, void *data, struct drm_file *); extern struct mem_block* nouveau_mem_alloc(struct drm_device *, int alignment, uint64_t size, int flags, struct drm_file *); extern void nouveau_mem_free(struct drm_device *dev, struct mem_block*); extern int nouveau_mem_init(struct drm_device *); extern int nouveau_mem_init_ttm(struct drm_device *); extern void nouveau_mem_close(struct drm_device *); /* nouveau_notifier.c */ extern int nouveau_notifier_init_channel(struct nouveau_channel *); extern void nouveau_notifier_takedown_channel(struct nouveau_channel *); extern int nouveau_notifier_alloc(struct nouveau_channel *, uint32_t handle, int cout, uint32_t *offset); extern int nouveau_ioctl_notifier_alloc(struct drm_device *, void *data, struct drm_file *); extern int nouveau_ioctl_notifier_free(struct drm_device *, void *data, struct drm_file *); /* nouveau_fifo.c */ extern int nouveau_fifo_init(struct drm_device *); extern int nouveau_fifo_ctx_size(struct drm_device *); extern void nouveau_fifo_cleanup(struct drm_device *, struct drm_file *); extern int nouveau_fifo_owner(struct drm_device *, struct drm_file *, int channel); extern int nouveau_fifo_alloc(struct drm_device *dev, struct nouveau_channel **chan, struct drm_file *file_priv, struct mem_block *pushbuf, uint32_t fb_ctxdma, uint32_t tt_ctxdma); extern void nouveau_fifo_free(struct nouveau_channel *); /* nouveau_object.c */ extern int nouveau_gpuobj_early_init(struct drm_device *); extern int nouveau_gpuobj_init(struct drm_device *); extern void nouveau_gpuobj_takedown(struct drm_device *); extern void nouveau_gpuobj_late_takedown(struct drm_device *); extern int nouveau_gpuobj_channel_init(struct nouveau_channel *, uint32_t vram_h, uint32_t tt_h); extern void nouveau_gpuobj_channel_takedown(struct nouveau_channel *); extern int nouveau_gpuobj_new(struct drm_device *, struct nouveau_channel *, int size, int align, uint32_t flags, struct nouveau_gpuobj **); extern int nouveau_gpuobj_del(struct drm_device *, struct nouveau_gpuobj **); extern int nouveau_gpuobj_ref_add(struct drm_device *, struct nouveau_channel *, uint32_t handle, struct nouveau_gpuobj *, struct nouveau_gpuobj_ref **); extern int nouveau_gpuobj_ref_del(struct drm_device *, struct nouveau_gpuobj_ref **); extern int nouveau_gpuobj_ref_find(struct nouveau_channel *, uint32_t handle, struct nouveau_gpuobj_ref **ref_ret); extern int nouveau_gpuobj_new_ref(struct drm_device *, struct nouveau_channel *alloc_chan, struct nouveau_channel *ref_chan, uint32_t handle, int size, int align, uint32_t flags, struct nouveau_gpuobj_ref **); extern int nouveau_gpuobj_new_fake(struct drm_device *, uint32_t p_offset, uint32_t b_offset, uint32_t size, uint32_t flags, struct nouveau_gpuobj **, struct nouveau_gpuobj_ref**); extern int nouveau_gpuobj_dma_new(struct nouveau_channel *, int class, uint64_t offset, uint64_t size, int access, int target, struct nouveau_gpuobj **); extern int nouveau_gpuobj_gart_dma_new(struct nouveau_channel *, uint64_t offset, uint64_t size, int access, struct nouveau_gpuobj **, uint32_t *o_ret); extern int nouveau_gpuobj_gr_new(struct nouveau_channel *, int class, struct nouveau_gpuobj **); extern int nouveau_ioctl_grobj_alloc(struct drm_device *, void *data, struct drm_file *); extern int nouveau_ioctl_gpuobj_free(struct drm_device *, void *data, struct drm_file *); /* nouveau_irq.c */ extern irqreturn_t nouveau_irq_handler(DRM_IRQ_ARGS); extern void nouveau_irq_preinstall(struct drm_device *); extern void nouveau_irq_postinstall(struct drm_device *); extern void nouveau_irq_uninstall(struct drm_device *); /* nouveau_sgdma.c */ extern int nouveau_sgdma_init(struct drm_device *); extern void nouveau_sgdma_takedown(struct drm_device *); extern int nouveau_sgdma_get_page(struct drm_device *, uint32_t offset, uint32_t *page); extern struct drm_ttm_backend *nouveau_sgdma_init_ttm(struct drm_device *); extern int nouveau_sgdma_nottm_hack_init(struct drm_device *); extern void nouveau_sgdma_nottm_hack_takedown(struct drm_device *); /* nouveau_dma.c */ extern int nouveau_dma_channel_init(struct drm_device *); extern void nouveau_dma_channel_takedown(struct drm_device *); extern int nouveau_dma_wait(struct drm_device *, int size); /* nv04_fb.c */ extern int nv04_fb_init(struct drm_device *); extern void nv04_fb_takedown(struct drm_device *); /* nv10_fb.c */ extern int nv10_fb_init(struct drm_device *); extern void nv10_fb_takedown(struct drm_device *); /* nv40_fb.c */ extern int nv40_fb_init(struct drm_device *); extern void nv40_fb_takedown(struct drm_device *); /* nv04_fifo.c */ extern int nv04_fifo_channel_id(struct drm_device *); extern int nv04_fifo_create_context(struct nouveau_channel *); extern void nv04_fifo_destroy_context(struct nouveau_channel *); extern int nv04_fifo_load_context(struct nouveau_channel *); extern int nv04_fifo_save_context(struct nouveau_channel *); /* nv10_fifo.c */ extern int nv10_fifo_channel_id(struct drm_device *); extern int nv10_fifo_create_context(struct nouveau_channel *); extern void nv10_fifo_destroy_context(struct nouveau_channel *); extern int nv10_fifo_load_context(struct nouveau_channel *); extern int nv10_fifo_save_context(struct nouveau_channel *); /* nv40_fifo.c */ extern int nv40_fifo_init(struct drm_device *); extern int nv40_fifo_create_context(struct nouveau_channel *); extern void nv40_fifo_destroy_context(struct nouveau_channel *); extern int nv40_fifo_load_context(struct nouveau_channel *); extern int nv40_fifo_save_context(struct nouveau_channel *); /* nv50_fifo.c */ extern int nv50_fifo_init(struct drm_device *); extern void nv50_fifo_takedown(struct drm_device *); extern int nv50_fifo_channel_id(struct drm_device *); extern int nv50_fifo_create_context(struct nouveau_channel *); extern void nv50_fifo_destroy_context(struct nouveau_channel *); extern int nv50_fifo_load_context(struct nouveau_channel *); extern int nv50_fifo_save_context(struct nouveau_channel *); /* nv04_graph.c */ extern void nouveau_nv04_context_switch(struct drm_device *); extern int nv04_graph_init(struct drm_device *); extern void nv04_graph_takedown(struct drm_device *); extern int nv04_graph_create_context(struct nouveau_channel *); extern void nv04_graph_destroy_context(struct nouveau_channel *); extern int nv04_graph_load_context(struct nouveau_channel *); extern int nv04_graph_save_context(struct nouveau_channel *); /* nv10_graph.c */ extern void nouveau_nv10_context_switch(struct drm_device *); extern int nv10_graph_init(struct drm_device *); extern void nv10_graph_takedown(struct drm_device *); extern int nv10_graph_create_context(struct nouveau_channel *); extern void nv10_graph_destroy_context(struct nouveau_channel *); extern int nv10_graph_load_context(struct nouveau_channel *); extern int nv10_graph_save_context(struct nouveau_channel *); /* nv20_graph.c */ extern int nv20_graph_create_context(struct nouveau_channel *); extern void nv20_graph_destroy_context(struct nouveau_channel *); extern int nv20_graph_load_context(struct nouveau_channel *); extern int nv20_graph_save_context(struct nouveau_channel *); extern int nv20_graph_init(struct drm_device *); extern void nv20_graph_takedown(struct drm_device *); extern int nv30_graph_init(struct drm_device *); /* nv40_graph.c */ extern int nv40_graph_init(struct drm_device *); extern void nv40_graph_takedown(struct drm_device *); extern int nv40_graph_create_context(struct nouveau_channel *); extern void nv40_graph_destroy_context(struct nouveau_channel *); extern int nv40_graph_load_context(struct nouveau_channel *); extern int nv40_graph_save_context(struct nouveau_channel *); /* nv50_graph.c */ extern int nv50_graph_init(struct drm_device *); extern void nv50_graph_takedown(struct drm_device *); extern int nv50_graph_create_context(struct nouveau_channel *); extern void nv50_graph_destroy_context(struct nouveau_channel *); extern int nv50_graph_load_context(struct nouveau_channel *); extern int nv50_graph_save_context(struct nouveau_channel *); /* nv04_instmem.c */ extern int nv04_instmem_init(struct drm_device *); extern void nv04_instmem_takedown(struct drm_device *); extern int nv04_instmem_populate(struct drm_device *, struct nouveau_gpuobj *, uint32_t *size); extern void nv04_instmem_clear(struct drm_device *, struct nouveau_gpuobj *); extern int nv04_instmem_bind(struct drm_device *, struct nouveau_gpuobj *); extern int nv04_instmem_unbind(struct drm_device *, struct nouveau_gpuobj *); /* nv50_instmem.c */ extern int nv50_instmem_init(struct drm_device *); extern void nv50_instmem_takedown(struct drm_device *); extern int nv50_instmem_populate(struct drm_device *, struct nouveau_gpuobj *, uint32_t *size); extern void nv50_instmem_clear(struct drm_device *, struct nouveau_gpuobj *); extern int nv50_instmem_bind(struct drm_device *, struct nouveau_gpuobj *); extern int nv50_instmem_unbind(struct drm_device *, struct nouveau_gpuobj *); /* nv04_mc.c */ extern int nv04_mc_init(struct drm_device *); extern void nv04_mc_takedown(struct drm_device *); /* nv40_mc.c */ extern int nv40_mc_init(struct drm_device *); extern void nv40_mc_takedown(struct drm_device *); /* nv50_mc.c */ extern int nv50_mc_init(struct drm_device *); extern void nv50_mc_takedown(struct drm_device *); /* nv04_timer.c */ extern int nv04_timer_init(struct drm_device *); extern uint64_t nv04_timer_read(struct drm_device *); extern void nv04_timer_takedown(struct drm_device *); extern long nouveau_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg); /* nouveau_buffer.c */ extern struct drm_bo_driver nouveau_bo_driver; /* nouveau_fence.c */ extern struct drm_fence_driver nouveau_fence_driver; extern void nouveau_fence_handler(struct drm_device *dev, int channel); #if defined(__powerpc__) #define NV_READ(reg) in_be32((void __iomem *)(dev_priv->mmio)->handle + (reg) ) #define NV_WRITE(reg,val) out_be32((void __iomem *)(dev_priv->mmio)->handle + (reg) , (val) ) #else #define NV_READ(reg) DRM_READ32( dev_priv->mmio, (reg) ) #define NV_WRITE(reg,val) DRM_WRITE32( dev_priv->mmio, (reg), (val) ) #endif /* PRAMIN access */ #if defined(__powerpc__) #define NV_RI32(o) in_be32((void __iomem *)(dev_priv->ramin)->handle+(o)) #define NV_WI32(o,v) out_be32((void __iomem*)(dev_priv->ramin)->handle+(o), (v)) #else #define NV_RI32(o) DRM_READ32(dev_priv->ramin, (o)) #define NV_WI32(o,v) DRM_WRITE32(dev_priv->ramin, (o), (v)) #endif #define INSTANCE_RD(o,i) NV_RI32((o)->im_pramin->start + ((i)<<2)) #define INSTANCE_WR(o,i,v) NV_WI32((o)->im_pramin->start + ((i)<<2), (v)) #endif /* __NOUVEAU_DRV_H__ */ 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
/*
 * Copyright 2007-8 Advanced Micro Devices, Inc.
 * Copyright 2008 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Dave Airlie
 *          Alex Deucher
 */
#include "drmP.h"
#include "radeon_drm.h"
#include "radeon_drv.h"

#include "drm_crtc_helper.h"

void radeon_restore_common_regs(struct drm_device *dev)
{
	/* don't need this yet */
}

static void radeon_pll_wait_for_read_update_complete(struct drm_device *dev)
{
	struct drm_radeon_private *dev_priv = dev->dev_private;
	int i = 0;

	/* FIXME: Certain revisions of R300 can't recover here.  Not sure of
	   the cause yet, but this workaround will mask the problem for now.
	   Other chips usually will pass at the very first test, so the
	   workaround shouldn't have any effect on them. */
	for (i = 0;
	     (i < 10000 &&
	      RADEON_READ_PLL(dev_priv, RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);
	     i++);
}

static void radeon_pll_write_update(struct drm_device *dev)
{
	struct drm_radeon_private *dev_priv = dev->dev_private;

	while (RADEON_READ_PLL(dev_priv, RADEON_PPLL_REF_DIV) & RADEON_PPLL_ATOMIC_UPDATE_R);

	RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
			   RADEON_PPLL_ATOMIC_UPDATE_W,
			   ~(RADEON_PPLL_ATOMIC_UPDATE_W));
}

static void radeon_pll2_wait_for_read_update_complete(struct drm_device *dev)
{
	struct drm_radeon_private *dev_priv = dev->dev_private;
	int i = 0;


	/* FIXME: Certain revisions of R300 can't recover here.  Not sure of
	   the cause yet, but this workaround will mask the problem for now.
	   Other chips usually will pass at the very first test, so the
	   workaround shouldn't have any effect on them. */
	for (i = 0;
	     (i < 10000 &&
	      RADEON_READ_PLL(dev_priv, RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);
	     i++);
}

static void radeon_pll2_write_update(struct drm_device *dev)
{
	struct drm_radeon_private *dev_priv = dev->dev_private;

	while (RADEON_READ_PLL(dev_priv, RADEON_P2PLL_REF_DIV) & RADEON_P2PLL_ATOMIC_UPDATE_R);

	RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_REF_DIV,
			   RADEON_P2PLL_ATOMIC_UPDATE_W,
			   ~(RADEON_P2PLL_ATOMIC_UPDATE_W));
}

static uint8_t radeon_compute_pll_gain(uint16_t ref_freq, uint16_t ref_div,
				       uint16_t fb_div)
{
	unsigned int vcoFreq;

        if (!ref_div)
		return 1;

	vcoFreq = ((unsigned)ref_freq & fb_div) / ref_div;

	/*
	 * This is horribly crude: the VCO frequency range is divided into
	 * 3 parts, each part having a fixed PLL gain value.
	 */
	if (vcoFreq >= 30000)
		/*
		 * [300..max] MHz : 7
		 */
		return 7;
	else if (vcoFreq >= 18000)
		/*
		 * [180..300) MHz : 4
		 */
		return 4;
	else
		/*
		 * [0..180) MHz : 1
		 */
		return 1;
}

void radeon_crtc_dpms(struct drm_crtc *crtc, int mode)
{
	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	uint32_t mask;

	DRM_DEBUG("\n");

	mask = radeon_crtc->crtc_id ?
		(RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_VSYNC_DIS | RADEON_CRTC2_HSYNC_DIS | RADEON_CRTC2_DISP_REQ_EN_B) :
		(RADEON_CRTC_DISPLAY_DIS | RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS);

	switch(mode) {
	case DRM_MODE_DPMS_ON:
		if (radeon_crtc->crtc_id)
			RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, 0, ~mask);
		else {
			RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, 0, ~RADEON_CRTC_DISP_REQ_EN_B);
			RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, 0, ~mask);
		}
		break;
	case DRM_MODE_DPMS_STANDBY:
		if (radeon_crtc->crtc_id)
			RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, (RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_HSYNC_DIS), ~mask);
		else {
			RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, 0, ~RADEON_CRTC_DISP_REQ_EN_B);
			RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, (RADEON_CRTC_DISPLAY_DIS | RADEON_CRTC_HSYNC_DIS), ~mask);
		}
		break;
	case DRM_MODE_DPMS_SUSPEND:
		if (radeon_crtc->crtc_id)
			RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, (RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_VSYNC_DIS), ~mask);
		else {
			RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, 0, ~RADEON_CRTC_DISP_REQ_EN_B);
			RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, (RADEON_CRTC_DISPLAY_DIS | RADEON_CRTC_VSYNC_DIS), ~mask);
		}
		break;
	case DRM_MODE_DPMS_OFF:
		if (radeon_crtc->crtc_id)
			RADEON_WRITE_P(RADEON_CRTC2_GEN_CNTL, mask, ~mask);
		else {
			RADEON_WRITE_P(RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B, ~RADEON_CRTC_DISP_REQ_EN_B);
			RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, mask, ~mask);
		}
		break;
	}

	if (mode != DRM_MODE_DPMS_OFF) {
		radeon_crtc_load_lut(crtc);
	}
}

/* properly set crtc bpp when using atombios */
void radeon_legacy_atom_set_surface(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
	int format;
	uint32_t crtc_gen_cntl, crtc2_gen_cntl;

	switch (crtc->fb->bits_per_pixel) {
	case 15:      /*  555 */
		format = 3;
		break;
	case 16:      /*  565 */
		format = 4;
		break;
	case 24:      /*  RGB */
		format = 5;
		break;
	case 32:      /* xRGB */
		format = 6;
		break;
	default:
		return;
	}

	switch (radeon_crtc->crtc_id) {
	case 0:
		crtc_gen_cntl = RADEON_READ(RADEON_CRTC_GEN_CNTL) & 0xfffff0ff;
		crtc_gen_cntl |= (format << 8);
		crtc_gen_cntl |= RADEON_CRTC_EXT_DISP_EN;
		RADEON_WRITE(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);
		break;
	case 1:
		crtc2_gen_cntl = RADEON_READ(RADEON_CRTC2_GEN_CNTL) & 0xfffff0ff;
		crtc2_gen_cntl |= (format << 8);
		RADEON_WRITE(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
		// not sure we need these...
		RADEON_WRITE(RADEON_FP_H2_SYNC_STRT_WID,   RADEON_READ(RADEON_CRTC2_H_SYNC_STRT_WID));
		RADEON_WRITE(RADEON_FP_V2_SYNC_STRT_WID,   RADEON_READ(RADEON_CRTC2_V_SYNC_STRT_WID));
		break;
	}
}

static bool radeon_set_crtc1_base(struct drm_crtc *crtc, int x, int y)
{
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_framebuffer *radeon_fb;
	struct drm_gem_object *obj;
	struct drm_radeon_gem_object *obj_priv;
	uint32_t base;
	uint32_t crtc_offset, crtc_offset_cntl, crtc_tile_x0_y0 = 0;
	uint32_t crtc_pitch;
	uint32_t disp_merge_cntl;

	DRM_DEBUG("\n");

	radeon_fb = to_radeon_framebuffer(crtc->fb);

	obj = radeon_fb->obj;
	obj_priv = obj->driver_private;

	crtc_offset = obj_priv->bo->offset;

	crtc_offset_cntl = 0;

	/* TODO tiling */
	if (0) {
		if (radeon_is_r300(dev_priv))
			crtc_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
					     R300_CRTC_MICRO_TILE_BUFFER_DIS |
					     R300_CRTC_MACRO_TILE_EN);
		else
			crtc_offset_cntl |= RADEON_CRTC_TILE_EN;
	} else {
		if (radeon_is_r300(dev_priv))
			crtc_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
					      R300_CRTC_MICRO_TILE_BUFFER_DIS |
					      R300_CRTC_MACRO_TILE_EN);
		else
			crtc_offset_cntl &= ~RADEON_CRTC_TILE_EN;
	}

	base = obj_priv->bo->offset;

	/* TODO more tiling */
	if (0) {
		if (radeon_is_r300(dev_priv)) {
			crtc_tile_x0_y0 = x | (y << 16);
			base &= ~0x7ff;
		} else {
			int byteshift = crtc->fb->bits_per_pixel >> 4;
			int tile_addr = (((y >> 3) * crtc->fb->width + x) >> (8 - byteshift)) << 11;
			base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
			crtc_offset_cntl |= (y % 16);
		}
	} else {
		int offset = y * crtc->fb->pitch + x;
		switch (crtc->fb->bits_per_pixel) {
		case 15:
		case 16:
			offset *= 2;
			break;
		case 24:
			offset *= 3;
			break;
		case 32:
			offset *= 4;
			break;
		default:
			return false;
		}
		base += offset;
	}

	base &= ~7;

	/* update sarea TODO */

	crtc_offset = base;

	crtc_pitch  = ((((crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8)) * crtc->fb->bits_per_pixel) +
			((crtc->fb->bits_per_pixel * 8) - 1)) /
		       (crtc->fb->bits_per_pixel * 8));
	crtc_pitch |= crtc_pitch << 16;

	DRM_DEBUG("mc_fb_location: 0x%x\n", dev_priv->fb_location);

	RADEON_WRITE(RADEON_DISPLAY_BASE_ADDR, dev_priv->fb_location);

	if (radeon_is_r300(dev_priv))
		RADEON_WRITE(R300_CRTC_TILE_X0_Y0, crtc_tile_x0_y0);
	RADEON_WRITE(RADEON_CRTC_OFFSET_CNTL, crtc_offset_cntl);
	RADEON_WRITE(RADEON_CRTC_OFFSET, crtc_offset);
	RADEON_WRITE(RADEON_CRTC_PITCH, crtc_pitch);

	disp_merge_cntl = RADEON_READ(RADEON_DISP_MERGE_CNTL);
	disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
	RADEON_WRITE(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);

	return true;
}

static bool radeon_set_crtc1_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
{
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	int format;
	int hsync_start;
	int hsync_wid;
	int vsync_wid;
	uint32_t crtc_gen_cntl;
	uint32_t crtc_ext_cntl;
	uint32_t crtc_h_total_disp;
	uint32_t crtc_h_sync_strt_wid;
	uint32_t crtc_v_total_disp;
	uint32_t crtc_v_sync_strt_wid;

	DRM_DEBUG("\n");

	switch (crtc->fb->bits_per_pixel) {
	case 15:      /*  555 */
		format = 3;
		break;
	case 16:      /*  565 */
		format = 4;
		break;
	case 24:      /*  RGB */
		format = 5;
		break;
	case 32:      /* xRGB */
		format = 6;
		break;
	default:
		return false;
	}

	crtc_gen_cntl = (RADEON_CRTC_EXT_DISP_EN
			 | RADEON_CRTC_EN
			 | (format << 8)
			 | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
			    ? RADEON_CRTC_DBL_SCAN_EN
			    : 0)
			 | ((mode->flags & DRM_MODE_FLAG_CSYNC)
			    ? RADEON_CRTC_CSYNC_EN
			    : 0)
			 | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
			    ? RADEON_CRTC_INTERLACE_EN
			    : 0));

	crtc_ext_cntl = RADEON_READ(RADEON_CRTC_EXT_CNTL);
	crtc_ext_cntl |= (RADEON_XCRT_CNT_EN |
			  RADEON_CRTC_VSYNC_DIS |
			  RADEON_CRTC_HSYNC_DIS |
			  RADEON_CRTC_DISPLAY_DIS);

	crtc_h_total_disp = ((((mode->crtc_htotal / 8) - 1) & 0x3ff)
			     | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));

	hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
	if (!hsync_wid)
		hsync_wid = 1;
	hsync_start = mode->crtc_hsync_start - 8;

	crtc_h_sync_strt_wid = ((hsync_start & 0x1fff)
				| ((hsync_wid & 0x3f) << 16)
				| ((mode->flags & DRM_MODE_FLAG_NHSYNC)
				   ? RADEON_CRTC_H_SYNC_POL
				   : 0));

	/* This works for double scan mode. */
	crtc_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
			     | ((mode->crtc_vdisplay - 1) << 16));

	vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
	if (!vsync_wid)
		vsync_wid = 1;

	crtc_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
				| ((vsync_wid & 0x1f) << 16)
				| ((mode->flags & DRM_MODE_FLAG_NVSYNC)
				   ? RADEON_CRTC_V_SYNC_POL
				   : 0));

	/* TODO -> Dell Server */
	if (0) {
		uint32_t disp_hw_debug = RADEON_READ(RADEON_DISP_HW_DEBUG);
		uint32_t tv_dac_cntl = RADEON_READ(RADEON_TV_DAC_CNTL);
		uint32_t dac2_cntl = RADEON_READ(RADEON_DAC_CNTL2);
		uint32_t crtc2_gen_cntl = RADEON_READ(RADEON_CRTC2_GEN_CNTL);

		dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
		dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;

		/* For CRT on DAC2, don't turn it on if BIOS didn't
		   enable it, even it's detected.
		*/
		disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
		tv_dac_cntl &= ~((1<<2) | (3<<8) | (7<<24) | (0xff<<16));
		tv_dac_cntl |= (0x03 | (2<<8) | (0x58<<16));

		RADEON_WRITE(RADEON_TV_DAC_CNTL, tv_dac_cntl);
		RADEON_WRITE(RADEON_DISP_HW_DEBUG, disp_hw_debug);
		RADEON_WRITE(RADEON_DAC_CNTL2, dac2_cntl);
		RADEON_WRITE(RADEON_CRTC2_GEN_CNTL, crtc2_gen_cntl);
	}

	RADEON_WRITE(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl |
		     RADEON_CRTC_DISP_REQ_EN_B);

	RADEON_WRITE_P(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl,
		       RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS | RADEON_CRTC_DISPLAY_DIS);

	RADEON_WRITE(RADEON_CRTC_H_TOTAL_DISP, crtc_h_total_disp);
	RADEON_WRITE(RADEON_CRTC_H_SYNC_STRT_WID, crtc_h_sync_strt_wid);
	RADEON_WRITE(RADEON_CRTC_V_TOTAL_DISP, crtc_v_total_disp);
	RADEON_WRITE(RADEON_CRTC_V_SYNC_STRT_WID, crtc_v_sync_strt_wid);

	RADEON_WRITE(RADEON_CRTC_GEN_CNTL, crtc_gen_cntl);

	return true;
}

static void radeon_set_pll1(struct drm_crtc *crtc, struct drm_display_mode *mode)
{
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct drm_encoder *encoder;
	uint32_t feedback_div = 0;
	uint32_t reference_div = 0;
	uint32_t post_divider = 0;
	uint32_t freq = 0;
	uint8_t pll_gain;
	int pll_flags = RADEON_PLL_LEGACY;
	bool use_bios_divs = false;
	/* PLL registers */
	uint32_t ppll_ref_div = 0;
        uint32_t ppll_div_3 = 0;
        uint32_t htotal_cntl = 0;
        uint32_t vclk_ecp_cntl;

	struct radeon_pll *pll = &dev_priv->mode_info.p1pll;

	struct {
		int divider;
		int bitvalue;
	} *post_div, post_divs[]   = {
		/* From RAGE 128 VR/RAGE 128 GL Register
		 * Reference Manual (Technical Reference
		 * Manual P/N RRG-G04100-C Rev. 0.04), page
		 * 3-17 (PLL_DIV_[3:0]).
		 */
		{  1, 0 },              /* VCLK_SRC                 */
		{  2, 1 },              /* VCLK_SRC/2               */
		{  4, 2 },              /* VCLK_SRC/4               */
		{  8, 3 },              /* VCLK_SRC/8               */
		{  3, 4 },              /* VCLK_SRC/3               */
		{ 16, 5 },              /* VCLK_SRC/16              */
		{  6, 6 },              /* VCLK_SRC/6               */
		{ 12, 7 },              /* VCLK_SRC/12              */
		{  0, 0 }
	};

	if (mode->clock > 200000) /* range limits??? */
		pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
	else
		pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;

	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
		if (encoder->crtc == crtc) {
			if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
				pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
			if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS) {
				struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);

				if (radeon_encoder->use_bios_dividers) {
					ppll_ref_div = radeon_encoder->panel_ref_divider;
					ppll_div_3   = (radeon_encoder->panel_fb_divider |
							(radeon_encoder->panel_post_divider << 16));
					htotal_cntl  = 0;
					use_bios_divs = true;
				} else
					pll_flags |= RADEON_PLL_USE_REF_DIV;
			}
		}
	}

	DRM_DEBUG("\n");

	if (!use_bios_divs) {
		radeon_compute_pll(pll, mode->clock, &freq, &feedback_div, &reference_div, &post_divider, pll_flags);

		for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
			if (post_div->divider == post_divider)
				break;
		}

		if (!post_div->divider) {
			post_div = &post_divs[0];
		}

		DRM_DEBUG("dc=%u, fd=%d, rd=%d, pd=%d\n",
			  (unsigned)freq,
			  feedback_div,
			  reference_div,
			  post_divider);

		ppll_ref_div   = reference_div;
#if defined(__powerpc__) && (0) /* TODO */
		/* apparently programming this otherwise causes a hang??? */
		if (info->MacModel == RADEON_MAC_IBOOK)
			state->ppll_div_3 = 0x000600ad;
		else
#endif
			ppll_div_3     = (feedback_div | (post_div->bitvalue << 16));
		htotal_cntl    = mode->htotal & 0x7;

	}

	vclk_ecp_cntl = (RADEON_READ_PLL(dev_priv, RADEON_VCLK_ECP_CNTL) &
			 ~RADEON_VCLK_SRC_SEL_MASK) | RADEON_VCLK_SRC_SEL_PPLLCLK;

	pll_gain = radeon_compute_pll_gain(dev_priv->mode_info.p1pll.reference_freq,
					   ppll_ref_div & RADEON_PPLL_REF_DIV_MASK,
					   ppll_div_3 & RADEON_PPLL_FB3_DIV_MASK);

	if (dev_priv->flags & RADEON_IS_MOBILITY) {
		/* A temporal workaround for the occational blanking on certain laptop panels.
		   This appears to related to the PLL divider registers (fail to lock?).
		   It occurs even when all dividers are the same with their old settings.
		   In this case we really don't need to fiddle with PLL registers.
		   By doing this we can avoid the blanking problem with some panels.
		*/
		if ((ppll_ref_div == (RADEON_READ_PLL(dev_priv, RADEON_PPLL_REF_DIV) & RADEON_PPLL_REF_DIV_MASK)) &&
		    (ppll_div_3 == (RADEON_READ_PLL(dev_priv, RADEON_PPLL_DIV_3) &
					   (RADEON_PPLL_POST3_DIV_MASK | RADEON_PPLL_FB3_DIV_MASK)))) {
			RADEON_WRITE_P(RADEON_CLOCK_CNTL_INDEX,
				       RADEON_PLL_DIV_SEL,
				       ~(RADEON_PLL_DIV_SEL));
			radeon_pll_errata_after_index(dev_priv);
			return;
		}
	}

	RADEON_WRITE_PLL_P(dev_priv, RADEON_VCLK_ECP_CNTL,
			   RADEON_VCLK_SRC_SEL_CPUCLK,
			   ~(RADEON_VCLK_SRC_SEL_MASK));
	RADEON_WRITE_PLL_P(dev_priv,
			   RADEON_PPLL_CNTL,
			   RADEON_PPLL_RESET
			   | RADEON_PPLL_ATOMIC_UPDATE_EN
			   | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
			   | ((uint32_t)pll_gain << RADEON_PPLL_PVG_SHIFT),
			   ~(RADEON_PPLL_RESET
			     | RADEON_PPLL_ATOMIC_UPDATE_EN
			     | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN
			     | RADEON_PPLL_PVG_MASK));

	RADEON_WRITE_P(RADEON_CLOCK_CNTL_INDEX,
		       RADEON_PLL_DIV_SEL,
		       ~(RADEON_PLL_DIV_SEL));
	radeon_pll_errata_after_index(dev_priv);

	if (radeon_is_r300(dev_priv) ||
	    (dev_priv->chip_family == CHIP_RS300) ||
	    (dev_priv->chip_family == CHIP_RS400) ||
	    (dev_priv->chip_family == CHIP_RS480)) {
		if (ppll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
			/* When restoring console mode, use saved PPLL_REF_DIV
			 * setting.
			 */
			RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
					   ppll_ref_div,
					   0);
		} else {
			/* R300 uses ref_div_acc field as real ref divider */
			RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
					   (ppll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
					   ~R300_PPLL_REF_DIV_ACC_MASK);
		}
	} else {
		RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_REF_DIV,
				   ppll_ref_div,
				   ~RADEON_PPLL_REF_DIV_MASK);
	}

	RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_DIV_3,
			   ppll_div_3,
			   ~RADEON_PPLL_FB3_DIV_MASK);

	RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_DIV_3,
			   ppll_div_3,
			   ~RADEON_PPLL_POST3_DIV_MASK);

	radeon_pll_write_update(dev);
	radeon_pll_wait_for_read_update_complete(dev);

	RADEON_WRITE_PLL(dev_priv, RADEON_HTOTAL_CNTL, htotal_cntl);

	RADEON_WRITE_PLL_P(dev_priv, RADEON_PPLL_CNTL,
			   0,
			   ~(RADEON_PPLL_RESET
			     | RADEON_PPLL_SLEEP
			     | RADEON_PPLL_ATOMIC_UPDATE_EN
			     | RADEON_PPLL_VGA_ATOMIC_UPDATE_EN));

	DRM_DEBUG("Wrote: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
		  ppll_ref_div,
		  ppll_div_3,
		  (unsigned)htotal_cntl,
		  RADEON_READ_PLL(dev_priv, RADEON_PPLL_CNTL));
	DRM_DEBUG("Wrote: rd=%d, fd=%d, pd=%d\n",
		  ppll_ref_div & RADEON_PPLL_REF_DIV_MASK,
		  ppll_div_3 & RADEON_PPLL_FB3_DIV_MASK,
		  (ppll_div_3 & RADEON_PPLL_POST3_DIV_MASK) >> 16);

	mdelay(50); /* Let the clock to lock */

	RADEON_WRITE_PLL_P(dev_priv, RADEON_VCLK_ECP_CNTL,
			   RADEON_VCLK_SRC_SEL_PPLLCLK,
			   ~(RADEON_VCLK_SRC_SEL_MASK));

	/*RADEON_WRITE_PLL(dev_priv, RADEON_VCLK_ECP_CNTL, vclk_ecp_cntl);*/

}

static bool radeon_set_crtc2_base(struct drm_crtc *crtc, int x, int y)
{
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_framebuffer *radeon_fb;
	struct drm_gem_object *obj;
	struct drm_radeon_gem_object *obj_priv;
	uint32_t base;
	uint32_t crtc2_offset, crtc2_offset_cntl, crtc2_tile_x0_y0 = 0;
        uint32_t crtc2_pitch;
	uint32_t disp2_merge_cntl;

	DRM_DEBUG("\n");

	radeon_fb = to_radeon_framebuffer(crtc->fb);

	obj = radeon_fb->obj;
	obj_priv = obj->driver_private;

	crtc2_offset = obj_priv->bo->offset;

	crtc2_offset_cntl = 0;

	/* TODO tiling */
	if (0) {
		if (radeon_is_r300(dev_priv))
			crtc2_offset_cntl |= (R300_CRTC_X_Y_MODE_EN |
					      R300_CRTC_MICRO_TILE_BUFFER_DIS |
					      R300_CRTC_MACRO_TILE_EN);
		else
			crtc2_offset_cntl |= RADEON_CRTC_TILE_EN;
	} else {
		if (radeon_is_r300(dev_priv))
			crtc2_offset_cntl &= ~(R300_CRTC_X_Y_MODE_EN |
					       R300_CRTC_MICRO_TILE_BUFFER_DIS |
					       R300_CRTC_MACRO_TILE_EN);
		else
			crtc2_offset_cntl &= ~RADEON_CRTC_TILE_EN;
	}

	base = obj_priv->bo->offset;

	/* TODO more tiling */
	if (0) {
		if (radeon_is_r300(dev_priv)) {
			crtc2_tile_x0_y0 = x | (y << 16);
			base &= ~0x7ff;
		} else {
			int byteshift = crtc->fb->bits_per_pixel >> 4;
			int tile_addr = (((y >> 3) * crtc->fb->width + x) >> (8 - byteshift)) << 11;
			base += tile_addr + ((x << byteshift) % 256) + ((y % 8) << 8);
			crtc2_offset_cntl |= (y % 16);
		}
	} else {
		int offset = y * crtc->fb->pitch + x;
		switch (crtc->fb->bits_per_pixel) {
		case 15:
		case 16:
			offset *= 2;
			break;
		case 24:
			offset *= 3;
			break;
		case 32:
			offset *= 4;
			break;
		default:
			return false;
		}
		base += offset;
	}

	base &= ~7;

	/* update sarea TODO */

	crtc2_offset = base;

	crtc2_pitch  = ((((crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8)) * crtc->fb->bits_per_pixel) +
			((crtc->fb->bits_per_pixel * 8) - 1)) /
		       (crtc->fb->bits_per_pixel * 8));
	crtc2_pitch |= crtc2_pitch << 16;

	RADEON_WRITE(RADEON_DISPLAY2_BASE_ADDR, dev_priv->fb_location);

	if (radeon_is_r300(dev_priv))
		RADEON_WRITE(R300_CRTC2_TILE_X0_Y0, crtc2_tile_x0_y0);
	RADEON_WRITE(RADEON_CRTC2_OFFSET_CNTL, crtc2_offset_cntl);
	RADEON_WRITE(RADEON_CRTC2_OFFSET, crtc2_offset);
	RADEON_WRITE(RADEON_CRTC2_PITCH, crtc2_pitch);

	disp2_merge_cntl = RADEON_READ(RADEON_DISP2_MERGE_CNTL);
	disp2_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
	RADEON_WRITE(RADEON_DISP2_MERGE_CNTL,      disp2_merge_cntl);

	return true;
}

static bool radeon_set_crtc2_timing(struct drm_crtc *crtc, struct drm_display_mode *mode)
{
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	int format;
	int hsync_start;
	int hsync_wid;
	int vsync_wid;
	uint32_t crtc2_gen_cntl;
	uint32_t crtc2_h_total_disp;
        uint32_t crtc2_h_sync_strt_wid;
        uint32_t crtc2_v_total_disp;
        uint32_t crtc2_v_sync_strt_wid;
	uint32_t fp_h2_sync_strt_wid;
	uint32_t fp_v2_sync_strt_wid;

	DRM_DEBUG("\n");

	switch (crtc->fb->bits_per_pixel) {
		
	case 15:      /*  555 */
		format = 3;
		break;
	case 16:      /*  565 */
		format = 4;
		break;
	case 24:      /*  RGB */
		format = 5;
		break;
	case 32:      /* xRGB */
		format = 6;
		break;
	default:
		return false;
	}

	crtc2_h_total_disp =
		((((mode->crtc_htotal / 8) - 1) & 0x3ff)
		 | ((((mode->crtc_hdisplay / 8) - 1) & 0x1ff) << 16));

	hsync_wid = (mode->crtc_hsync_end - mode->crtc_hsync_start) / 8;
	if (!hsync_wid)
		hsync_wid = 1;
	hsync_start = mode->crtc_hsync_start - 8;

	crtc2_h_sync_strt_wid = ((hsync_start & 0x1fff)
				 | ((hsync_wid & 0x3f) << 16)
				 | ((mode->flags & DRM_MODE_FLAG_NHSYNC)
				    ? RADEON_CRTC_H_SYNC_POL
				    : 0));

	/* This works for double scan mode. */
	crtc2_v_total_disp = (((mode->crtc_vtotal - 1) & 0xffff)
			      | ((mode->crtc_vdisplay - 1) << 16));

	vsync_wid = mode->crtc_vsync_end - mode->crtc_vsync_start;
	if (!vsync_wid)
		vsync_wid = 1;

	crtc2_v_sync_strt_wid = (((mode->crtc_vsync_start - 1) & 0xfff)
				 | ((vsync_wid & 0x1f) << 16)
				 | ((mode->flags & DRM_MODE_FLAG_NVSYNC)
				    ? RADEON_CRTC2_V_SYNC_POL
				    : 0));

	/* check to see if TV DAC is enabled for another crtc and keep it enabled */
	if (RADEON_READ(RADEON_CRTC2_GEN_CNTL) & RADEON_CRTC2_CRT2_ON)
		crtc2_gen_cntl = RADEON_CRTC2_CRT2_ON;
	else
		crtc2_gen_cntl = 0;

	crtc2_gen_cntl |= (RADEON_CRTC2_EN
			   | (format << 8)
			   | RADEON_CRTC2_VSYNC_DIS
			   | RADEON_CRTC2_HSYNC_DIS
			   | RADEON_CRTC2_DISP_DIS
			   | ((mode->flags & DRM_MODE_FLAG_DBLSCAN)
			      ? RADEON_CRTC2_DBL_SCAN_EN
			      : 0)
			   | ((mode->flags & DRM_MODE_FLAG_CSYNC)
			      ? RADEON_CRTC2_CSYNC_EN
			      : 0)
			   | ((mode->flags & DRM_MODE_FLAG_INTERLACE)
			      ? RADEON_CRTC2_INTERLACE_EN
			      : 0));

	fp_h2_sync_strt_wid = crtc2_h_sync_strt_wid;
	fp_v2_sync_strt_wid = crtc2_v_sync_strt_wid;

	RADEON_WRITE(RADEON_CRTC2_GEN_CNTL,
		     crtc2_gen_cntl | RADEON_CRTC2_VSYNC_DIS |
		     RADEON_CRTC2_HSYNC_DIS | RADEON_CRTC2_DISP_DIS |
		     RADEON_CRTC2_DISP_REQ_EN_B);

	RADEON_WRITE(RADEON_CRTC2_H_TOTAL_DISP,    crtc2_h_total_disp);
	RADEON_WRITE(RADEON_CRTC2_H_SYNC_STRT_WID, crtc2_h_sync_strt_wid);
	RADEON_WRITE(RADEON_CRTC2_V_TOTAL_DISP,    crtc2_v_total_disp);
	RADEON_WRITE(RADEON_CRTC2_V_SYNC_STRT_WID, crtc2_v_sync_strt_wid);

	RADEON_WRITE(RADEON_FP_H2_SYNC_STRT_WID,   fp_h2_sync_strt_wid);
	RADEON_WRITE(RADEON_FP_V2_SYNC_STRT_WID,   fp_v2_sync_strt_wid);

	RADEON_WRITE(RADEON_CRTC2_GEN_CNTL,        crtc2_gen_cntl);

	return true;

}

static void radeon_set_pll2(struct drm_crtc *crtc, struct drm_display_mode *mode)
{
	struct drm_device *dev = crtc->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct drm_encoder *encoder;
	uint32_t feedback_div = 0;
	uint32_t reference_div = 0;
	uint32_t post_divider = 0;
	uint32_t freq = 0;
	uint8_t pll_gain;
	int pll_flags = RADEON_PLL_LEGACY;
	bool use_bios_divs = false;
	/* PLL2 registers */
	uint32_t p2pll_ref_div = 0;
	uint32_t p2pll_div_0 = 0;
	uint32_t htotal_cntl2 = 0;
	uint32_t pixclks_cntl;

	struct radeon_pll *pll = &dev_priv->mode_info.p2pll;

	struct {
		int divider;
		int bitvalue;
	} *post_div, post_divs[]   = {
		/* From RAGE 128 VR/RAGE 128 GL Register
		 * Reference Manual (Technical Reference
		 * Manual P/N RRG-G04100-C Rev. 0.04), page
		 * 3-17 (PLL_DIV_[3:0]).
		 */
		{  1, 0 },              /* VCLK_SRC                 */
		{  2, 1 },              /* VCLK_SRC/2               */
		{  4, 2 },              /* VCLK_SRC/4               */
		{  8, 3 },              /* VCLK_SRC/8               */
		{  3, 4 },              /* VCLK_SRC/3               */
		{  6, 6 },              /* VCLK_SRC/6               */
		{ 12, 7 },              /* VCLK_SRC/12              */
		{  0, 0 }
	};

	if (mode->clock > 200000) /* range limits??? */
		pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
	else
		pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;

	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
		if (encoder->crtc == crtc) {
			if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
				pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
			if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS) {
				struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);

				if (radeon_encoder->use_bios_dividers) {
					p2pll_ref_div = radeon_encoder->panel_ref_divider;
					p2pll_div_0   = (radeon_encoder->panel_fb_divider |
							(radeon_encoder->panel_post_divider << 16));
					htotal_cntl2  = 0;
					use_bios_divs = true;
				} else
					pll_flags |= RADEON_PLL_USE_REF_DIV;
			}
		}
	}

	DRM_DEBUG("\n");

	if (!use_bios_divs) {
		radeon_compute_pll(pll, mode->clock, &freq, &feedback_div, &reference_div, &post_divider, pll_flags);

		for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
			if (post_div->divider == post_divider)
				break;
		}

		if (!post_div->divider) {
			post_div = &post_divs[0];
		}

		DRM_DEBUG("dc=%u, fd=%d, rd=%d, pd=%d\n",
			  (unsigned)freq,
			  feedback_div,
			  reference_div,
			  post_divider);

		p2pll_ref_div    = reference_div;
		p2pll_div_0      = (feedback_div | (post_div->bitvalue << 16));
		htotal_cntl2     = mode->htotal & 0x7;

	}

	pixclks_cntl     = ((RADEON_READ_PLL(dev_priv, RADEON_PIXCLKS_CNTL) &
			     ~(RADEON_PIX2CLK_SRC_SEL_MASK)) |
			    RADEON_PIX2CLK_SRC_SEL_P2PLLCLK);

	pll_gain = radeon_compute_pll_gain(dev_priv->mode_info.p2pll.reference_freq,
					   p2pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
					   p2pll_div_0 & RADEON_P2PLL_FB0_DIV_MASK);


	RADEON_WRITE_PLL_P(dev_priv, RADEON_PIXCLKS_CNTL,
			   RADEON_PIX2CLK_SRC_SEL_CPUCLK,
			   ~(RADEON_PIX2CLK_SRC_SEL_MASK));

	RADEON_WRITE_PLL_P(dev_priv,
			   RADEON_P2PLL_CNTL,
			   RADEON_P2PLL_RESET
			   | RADEON_P2PLL_ATOMIC_UPDATE_EN
			   | ((uint32_t)pll_gain << RADEON_P2PLL_PVG_SHIFT),
			   ~(RADEON_P2PLL_RESET
			     | RADEON_P2PLL_ATOMIC_UPDATE_EN
			     | RADEON_P2PLL_PVG_MASK));


	RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_REF_DIV,
			   p2pll_ref_div,
			   ~RADEON_P2PLL_REF_DIV_MASK);

	RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_DIV_0,
			   p2pll_div_0,
			   ~RADEON_P2PLL_FB0_DIV_MASK);

	RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_DIV_0,
			   p2pll_div_0,
			   ~RADEON_P2PLL_POST0_DIV_MASK);

	radeon_pll2_write_update(dev);
	radeon_pll2_wait_for_read_update_complete(dev);

	RADEON_WRITE_PLL(dev_priv, RADEON_HTOTAL2_CNTL, htotal_cntl2);

	RADEON_WRITE_PLL_P(dev_priv, RADEON_P2PLL_CNTL,
			   0,
			   ~(RADEON_P2PLL_RESET
			     | RADEON_P2PLL_SLEEP
			     | RADEON_P2PLL_ATOMIC_UPDATE_EN));

	DRM_DEBUG("Wrote2: 0x%08x 0x%08x 0x%08x (0x%08x)\n",
		  (unsigned)p2pll_ref_div,
		  (unsigned)p2pll_div_0,
		  (unsigned)htotal_cntl2,
		  RADEON_READ_PLL(dev_priv, RADEON_P2PLL_CNTL));
	DRM_DEBUG("Wrote2: rd=%u, fd=%u, pd=%u\n",
		  (unsigned)p2pll_ref_div & RADEON_P2PLL_REF_DIV_MASK,
		  (unsigned)p2pll_div_0 & RADEON_P2PLL_FB0_DIV_MASK,
		  (unsigned)((p2pll_div_0 &
			      RADEON_P2PLL_POST0_DIV_MASK) >>16));

	mdelay(50); /* Let the clock to lock */

	RADEON_WRITE_PLL_P(dev_priv, RADEON_PIXCLKS_CNTL,
			   RADEON_PIX2CLK_SRC_SEL_P2PLLCLK,
			   ~(RADEON_PIX2CLK_SRC_SEL_MASK));

	RADEON_WRITE_PLL(dev_priv, RADEON_PIXCLKS_CNTL, pixclks_cntl);

}

static bool radeon_crtc_mode_fixup(struct drm_crtc *crtc,
				   struct drm_display_mode *mode,
				   struct drm_display_mode *adjusted_mode)
{
	return true;
}

void radeon_crtc_set_base(struct drm_crtc *crtc, int x, int y)
{
	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);

	switch(radeon_crtc->crtc_id) {
	case 0:
		radeon_set_crtc1_base(crtc, x, y);
		break;
	case 1:
		radeon_set_crtc2_base(crtc, x, y);
		break;

	}
}

static void radeon_crtc_mode_set(struct drm_crtc *crtc,
				 struct drm_display_mode *mode,
				 struct drm_display_mode *adjusted_mode,
				 int x, int y)
{
	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);

	DRM_DEBUG("\n");

	/* TODO TV */

	radeon_crtc_set_base(crtc, x, y);

	switch(radeon_crtc->crtc_id) {
	case 0:
		radeon_set_crtc1_timing(crtc, adjusted_mode);
		radeon_set_pll1(crtc, adjusted_mode);
		break;
	case 1:
		radeon_set_crtc2_timing(crtc, adjusted_mode);
		radeon_set_pll2(crtc, adjusted_mode);
		break;

	}
}

static void radeon_crtc_prepare(struct drm_crtc *crtc)
{
	radeon_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
}

static void radeon_crtc_commit(struct drm_crtc *crtc)
{
	radeon_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
}

static const struct drm_crtc_helper_funcs legacy_helper_funcs = {
	.dpms = radeon_crtc_dpms,
	.mode_fixup = radeon_crtc_mode_fixup,
	.mode_set = radeon_crtc_mode_set,
	.mode_set_base = radeon_crtc_set_base,
	.prepare = radeon_crtc_prepare,
	.commit = radeon_crtc_commit,
};


void radeon_legacy_init_crtc(struct drm_device *dev,
			       struct radeon_crtc *radeon_crtc)
{
	drm_crtc_helper_add(&radeon_crtc->base, &legacy_helper_funcs);
}