Home | History | Annotate | Line # | Download | only in radeon
      1  1.12  riastrad /*	$NetBSD: radeon_display.c,v 1.12 2021/12/18 23:45:43 riastradh Exp $	*/
      2   1.6  riastrad 
      3   1.1  riastrad /*
      4   1.1  riastrad  * Copyright 2007-8 Advanced Micro Devices, Inc.
      5   1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6   1.1  riastrad  *
      7   1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      8   1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      9   1.1  riastrad  * to deal in the Software without restriction, including without limitation
     10   1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11   1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     12   1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     13   1.1  riastrad  *
     14   1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     15   1.1  riastrad  * all copies or substantial portions of the Software.
     16   1.1  riastrad  *
     17   1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18   1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19   1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20   1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     21   1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     22   1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     23   1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     24   1.1  riastrad  *
     25   1.1  riastrad  * Authors: Dave Airlie
     26   1.1  riastrad  *          Alex Deucher
     27   1.1  riastrad  */
     28  1.12  riastrad 
     29   1.6  riastrad #include <sys/cdefs.h>
     30  1.12  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_display.c,v 1.12 2021/12/18 23:45:43 riastradh Exp $");
     31   1.6  riastrad 
     32  1.12  riastrad #include <linux/pci.h>
     33  1.12  riastrad #include <linux/pm_runtime.h>
     34  1.12  riastrad #include <linux/gcd.h>
     35   1.1  riastrad 
     36   1.1  riastrad #include <asm/div64.h>
     37   1.1  riastrad 
     38   1.1  riastrad #include <drm/drm_crtc_helper.h>
     39  1.12  riastrad #include <drm/drm_device.h>
     40  1.12  riastrad #include <drm/drm_drv.h>
     41  1.12  riastrad #include <drm/drm_edid.h>
     42  1.12  riastrad #include <drm/drm_fb_helper.h>
     43  1.12  riastrad #include <drm/drm_fourcc.h>
     44  1.12  riastrad #include <drm/drm_gem_framebuffer_helper.h>
     45   1.6  riastrad #include <drm/drm_plane_helper.h>
     46  1.12  riastrad #include <drm/drm_probe_helper.h>
     47  1.12  riastrad #include <drm/drm_vblank.h>
     48  1.12  riastrad #include <drm/radeon_drm.h>
     49   1.1  riastrad 
     50  1.12  riastrad #include "atom.h"
     51  1.12  riastrad #include "radeon.h"
     52   1.1  riastrad 
     53   1.1  riastrad static void avivo_crtc_load_lut(struct drm_crtc *crtc)
     54   1.1  riastrad {
     55   1.1  riastrad 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
     56   1.1  riastrad 	struct drm_device *dev = crtc->dev;
     57   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
     58  1.12  riastrad 	u16 *r, *g, *b;
     59   1.1  riastrad 	int i;
     60   1.1  riastrad 
     61   1.1  riastrad 	DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
     62   1.1  riastrad 	WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
     63   1.1  riastrad 
     64   1.1  riastrad 	WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
     65   1.1  riastrad 	WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
     66   1.1  riastrad 	WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
     67   1.1  riastrad 
     68   1.1  riastrad 	WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
     69   1.1  riastrad 	WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
     70   1.1  riastrad 	WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
     71   1.1  riastrad 
     72   1.1  riastrad 	WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
     73   1.1  riastrad 	WREG32(AVIVO_DC_LUT_RW_MODE, 0);
     74   1.1  riastrad 	WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
     75   1.1  riastrad 
     76   1.1  riastrad 	WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
     77  1.12  riastrad 	r = crtc->gamma_store;
     78  1.12  riastrad 	g = r + crtc->gamma_size;
     79  1.12  riastrad 	b = g + crtc->gamma_size;
     80   1.1  riastrad 	for (i = 0; i < 256; i++) {
     81   1.1  riastrad 		WREG32(AVIVO_DC_LUT_30_COLOR,
     82  1.12  riastrad 		       ((*r++ & 0xffc0) << 14) |
     83  1.12  riastrad 		       ((*g++ & 0xffc0) << 4) |
     84  1.12  riastrad 		       (*b++ >> 6));
     85   1.1  riastrad 	}
     86   1.1  riastrad 
     87   1.6  riastrad 	/* Only change bit 0 of LUT_SEL, other bits are set elsewhere */
     88   1.6  riastrad 	WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id, ~1);
     89   1.1  riastrad }
     90   1.1  riastrad 
     91   1.1  riastrad static void dce4_crtc_load_lut(struct drm_crtc *crtc)
     92   1.1  riastrad {
     93   1.1  riastrad 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
     94   1.1  riastrad 	struct drm_device *dev = crtc->dev;
     95   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
     96  1.12  riastrad 	u16 *r, *g, *b;
     97   1.1  riastrad 	int i;
     98   1.1  riastrad 
     99   1.1  riastrad 	DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
    100   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
    101   1.1  riastrad 
    102   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
    103   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
    104   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
    105   1.1  riastrad 
    106   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
    107   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
    108   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
    109   1.1  riastrad 
    110   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
    111   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
    112   1.1  riastrad 
    113   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
    114  1.12  riastrad 	r = crtc->gamma_store;
    115  1.12  riastrad 	g = r + crtc->gamma_size;
    116  1.12  riastrad 	b = g + crtc->gamma_size;
    117   1.1  riastrad 	for (i = 0; i < 256; i++) {
    118   1.1  riastrad 		WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
    119  1.12  riastrad 		       ((*r++ & 0xffc0) << 14) |
    120  1.12  riastrad 		       ((*g++ & 0xffc0) << 4) |
    121  1.12  riastrad 		       (*b++ >> 6));
    122   1.1  riastrad 	}
    123   1.1  riastrad }
    124   1.1  riastrad 
    125   1.1  riastrad static void dce5_crtc_load_lut(struct drm_crtc *crtc)
    126   1.1  riastrad {
    127   1.1  riastrad 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
    128   1.1  riastrad 	struct drm_device *dev = crtc->dev;
    129   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    130  1.12  riastrad 	u16 *r, *g, *b;
    131   1.1  riastrad 	int i;
    132   1.1  riastrad 
    133   1.1  riastrad 	DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
    134   1.1  riastrad 
    135  1.12  riastrad 	msleep(10);
    136  1.12  riastrad 
    137   1.1  riastrad 	WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
    138   1.1  riastrad 	       (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
    139   1.1  riastrad 		NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
    140   1.1  riastrad 	WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
    141   1.1  riastrad 	       NI_GRPH_PRESCALE_BYPASS);
    142   1.1  riastrad 	WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
    143   1.1  riastrad 	       NI_OVL_PRESCALE_BYPASS);
    144   1.1  riastrad 	WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
    145   1.1  riastrad 	       (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
    146   1.1  riastrad 		NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
    147   1.1  riastrad 
    148   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
    149   1.1  riastrad 
    150   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
    151   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
    152   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
    153   1.1  riastrad 
    154   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
    155   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
    156   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
    157   1.1  riastrad 
    158   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
    159   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
    160   1.1  riastrad 
    161   1.1  riastrad 	WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
    162  1.12  riastrad 	r = crtc->gamma_store;
    163  1.12  riastrad 	g = r + crtc->gamma_size;
    164  1.12  riastrad 	b = g + crtc->gamma_size;
    165   1.1  riastrad 	for (i = 0; i < 256; i++) {
    166   1.1  riastrad 		WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
    167  1.12  riastrad 		       ((*r++ & 0xffc0) << 14) |
    168  1.12  riastrad 		       ((*g++ & 0xffc0) << 4) |
    169  1.12  riastrad 		       (*b++ >> 6));
    170   1.1  riastrad 	}
    171   1.1  riastrad 
    172   1.1  riastrad 	WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
    173   1.1  riastrad 	       (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
    174   1.1  riastrad 		NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
    175   1.1  riastrad 		NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
    176   1.1  riastrad 		NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
    177   1.1  riastrad 	WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
    178   1.1  riastrad 	       (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
    179   1.1  riastrad 		NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
    180   1.1  riastrad 	WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
    181   1.1  riastrad 	       (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
    182   1.1  riastrad 		NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
    183   1.1  riastrad 	WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
    184   1.6  riastrad 	       (NI_OUTPUT_CSC_GRPH_MODE(radeon_crtc->output_csc) |
    185   1.1  riastrad 		NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
    186   1.1  riastrad 	/* XXX match this to the depth of the crtc fmt block, move to modeset? */
    187   1.1  riastrad 	WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
    188   1.1  riastrad 	if (ASIC_IS_DCE8(rdev)) {
    189   1.1  riastrad 		/* XXX this only needs to be programmed once per crtc at startup,
    190   1.1  riastrad 		 * not sure where the best place for it is
    191   1.1  riastrad 		 */
    192   1.1  riastrad 		WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
    193   1.1  riastrad 		       CIK_CURSOR_ALPHA_BLND_ENA);
    194   1.1  riastrad 	}
    195   1.1  riastrad }
    196   1.1  riastrad 
    197   1.1  riastrad static void legacy_crtc_load_lut(struct drm_crtc *crtc)
    198   1.1  riastrad {
    199   1.1  riastrad 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
    200   1.1  riastrad 	struct drm_device *dev = crtc->dev;
    201   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    202  1.12  riastrad 	u16 *r, *g, *b;
    203   1.1  riastrad 	int i;
    204   1.1  riastrad 	uint32_t dac2_cntl;
    205   1.1  riastrad 
    206   1.1  riastrad 	dac2_cntl = RREG32(RADEON_DAC_CNTL2);
    207   1.1  riastrad 	if (radeon_crtc->crtc_id == 0)
    208   1.1  riastrad 		dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
    209   1.1  riastrad 	else
    210   1.1  riastrad 		dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
    211   1.1  riastrad 	WREG32(RADEON_DAC_CNTL2, dac2_cntl);
    212   1.1  riastrad 
    213  1.10       mrg 	WREG8(RADEON_PALETTE_INDEX, 0);
    214  1.12  riastrad 	r = crtc->gamma_store;
    215  1.12  riastrad 	g = r + crtc->gamma_size;
    216  1.12  riastrad 	b = g + crtc->gamma_size;
    217  1.10       mrg 	for (i = 0; i < 256; i++) {
    218  1.10       mrg 		WREG32(RADEON_PALETTE_30_DATA,
    219  1.12  riastrad 		       ((*r++ & 0xffc0) << 14) |
    220  1.12  riastrad 		       ((*g++ & 0xffc0) << 4) |
    221  1.12  riastrad 		       (*b++ >> 6));
    222   1.1  riastrad 	}
    223   1.1  riastrad }
    224   1.1  riastrad 
    225   1.1  riastrad void radeon_crtc_load_lut(struct drm_crtc *crtc)
    226   1.1  riastrad {
    227   1.1  riastrad 	struct drm_device *dev = crtc->dev;
    228   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    229   1.1  riastrad 
    230   1.1  riastrad 	if (!crtc->enabled)
    231   1.1  riastrad 		return;
    232   1.1  riastrad 
    233   1.1  riastrad 	if (ASIC_IS_DCE5(rdev))
    234   1.1  riastrad 		dce5_crtc_load_lut(crtc);
    235   1.1  riastrad 	else if (ASIC_IS_DCE4(rdev))
    236   1.1  riastrad 		dce4_crtc_load_lut(crtc);
    237   1.1  riastrad 	else if (ASIC_IS_AVIVO(rdev))
    238   1.1  riastrad 		avivo_crtc_load_lut(crtc);
    239   1.1  riastrad 	else
    240   1.1  riastrad 		legacy_crtc_load_lut(crtc);
    241   1.1  riastrad }
    242   1.1  riastrad 
    243  1.12  riastrad static int radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
    244  1.12  riastrad 				 u16 *blue, uint32_t size,
    245  1.12  riastrad 				 struct drm_modeset_acquire_ctx *ctx)
    246   1.1  riastrad {
    247  1.12  riastrad 	radeon_crtc_load_lut(crtc);
    248   1.1  riastrad 
    249  1.12  riastrad 	return 0;
    250   1.1  riastrad }
    251   1.1  riastrad 
    252   1.1  riastrad static void radeon_crtc_destroy(struct drm_crtc *crtc)
    253   1.1  riastrad {
    254   1.1  riastrad 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
    255   1.1  riastrad 
    256   1.1  riastrad 	drm_crtc_cleanup(crtc);
    257   1.6  riastrad 	destroy_workqueue(radeon_crtc->flip_queue);
    258   1.1  riastrad 	kfree(radeon_crtc);
    259   1.1  riastrad }
    260   1.1  riastrad 
    261   1.6  riastrad /**
    262   1.6  riastrad  * radeon_unpin_work_func - unpin old buffer object
    263   1.6  riastrad  *
    264   1.6  riastrad  * @__work - kernel work item
    265   1.6  riastrad  *
    266   1.6  riastrad  * Unpin the old frame buffer object outside of the interrupt handler
    267   1.1  riastrad  */
    268   1.1  riastrad static void radeon_unpin_work_func(struct work_struct *__work)
    269   1.1  riastrad {
    270   1.6  riastrad 	struct radeon_flip_work *work =
    271   1.6  riastrad 		container_of(__work, struct radeon_flip_work, unpin_work);
    272   1.1  riastrad 	int r;
    273   1.1  riastrad 
    274   1.1  riastrad 	/* unpin of the old buffer */
    275   1.1  riastrad 	r = radeon_bo_reserve(work->old_rbo, false);
    276   1.1  riastrad 	if (likely(r == 0)) {
    277   1.1  riastrad 		r = radeon_bo_unpin(work->old_rbo);
    278   1.1  riastrad 		if (unlikely(r != 0)) {
    279   1.1  riastrad 			DRM_ERROR("failed to unpin buffer after flip\n");
    280   1.1  riastrad 		}
    281   1.1  riastrad 		radeon_bo_unreserve(work->old_rbo);
    282   1.1  riastrad 	} else
    283   1.1  riastrad 		DRM_ERROR("failed to reserve buffer after flip\n");
    284   1.1  riastrad 
    285  1.12  riastrad 	drm_gem_object_put_unlocked(&work->old_rbo->tbo.base);
    286   1.1  riastrad 	kfree(work);
    287   1.1  riastrad }
    288   1.1  riastrad 
    289   1.6  riastrad void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
    290   1.1  riastrad {
    291   1.1  riastrad 	struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
    292   1.1  riastrad 	unsigned long flags;
    293   1.1  riastrad 	u32 update_pending;
    294   1.1  riastrad 	int vpos, hpos;
    295   1.1  riastrad 
    296   1.1  riastrad 	/* can happen during initialization */
    297   1.1  riastrad 	if (radeon_crtc == NULL)
    298   1.1  riastrad 		return;
    299   1.1  riastrad 
    300   1.6  riastrad 	/* Skip the pageflip completion check below (based on polling) on
    301   1.6  riastrad 	 * asics which reliably support hw pageflip completion irqs. pflip
    302   1.6  riastrad 	 * irqs are a reliable and race-free method of handling pageflip
    303   1.6  riastrad 	 * completion detection. A use_pflipirq module parameter < 2 allows
    304   1.6  riastrad 	 * to override this in case of asics with faulty pflip irqs.
    305   1.6  riastrad 	 * A module parameter of 0 would only use this polling based path,
    306   1.6  riastrad 	 * a parameter of 1 would use pflip irq only as a backup to this
    307   1.6  riastrad 	 * path, as in Linux 3.16.
    308   1.6  riastrad 	 */
    309   1.6  riastrad 	if ((radeon_use_pflipirq == 2) && ASIC_IS_DCE4(rdev))
    310   1.6  riastrad 		return;
    311   1.6  riastrad 
    312   1.1  riastrad 	spin_lock_irqsave(&rdev->ddev->event_lock, flags);
    313   1.6  riastrad 	if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
    314   1.6  riastrad 		DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
    315   1.6  riastrad 				 "RADEON_FLIP_SUBMITTED(%d)\n",
    316   1.6  riastrad 				 radeon_crtc->flip_status,
    317   1.6  riastrad 				 RADEON_FLIP_SUBMITTED);
    318   1.1  riastrad 		spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
    319   1.1  riastrad 		return;
    320   1.1  riastrad 	}
    321   1.6  riastrad 
    322   1.6  riastrad 	update_pending = radeon_page_flip_pending(rdev, crtc_id);
    323   1.1  riastrad 
    324   1.1  riastrad 	/* Has the pageflip already completed in crtc, or is it certain
    325  1.12  riastrad 	 * to complete in this vblank? GET_DISTANCE_TO_VBLANKSTART provides
    326  1.12  riastrad 	 * distance to start of "fudged earlier" vblank in vpos, distance to
    327  1.12  riastrad 	 * start of real vblank in hpos. vpos >= 0 && hpos < 0 means we are in
    328  1.12  riastrad 	 * the last few scanlines before start of real vblank, where the vblank
    329  1.12  riastrad 	 * irq can fire, so we have sampled update_pending a bit too early and
    330  1.12  riastrad 	 * know the flip will complete at leading edge of the upcoming real
    331  1.12  riastrad 	 * vblank. On pre-AVIVO hardware, flips also complete inside the real
    332  1.12  riastrad 	 * vblank, not only at leading edge, so if update_pending for hpos >= 0
    333  1.12  riastrad 	 *  == inside real vblank, the flip will complete almost immediately.
    334  1.12  riastrad 	 * Note that this method of completion handling is still not 100% race
    335  1.12  riastrad 	 * free, as we could execute before the radeon_flip_work_func managed
    336  1.12  riastrad 	 * to run and set the RADEON_FLIP_SUBMITTED status, thereby we no-op,
    337  1.12  riastrad 	 * but the flip still gets programmed into hw and completed during
    338  1.12  riastrad 	 * vblank, leading to a delayed emission of the flip completion event.
    339  1.12  riastrad 	 * This applies at least to pre-AVIVO hardware, where flips are always
    340  1.12  riastrad 	 * completing inside vblank, not only at leading edge of vblank.
    341   1.1  riastrad 	 */
    342   1.1  riastrad 	if (update_pending &&
    343  1.12  riastrad 	    (DRM_SCANOUTPOS_VALID &
    344  1.12  riastrad 	     radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id,
    345  1.12  riastrad 					GET_DISTANCE_TO_VBLANKSTART,
    346  1.12  riastrad 					&vpos, &hpos, NULL, NULL,
    347  1.12  riastrad 					&rdev->mode_info.crtcs[crtc_id]->base.hwmode)) &&
    348  1.12  riastrad 	    ((vpos >= 0 && hpos < 0) || (hpos >= 0 && !ASIC_IS_AVIVO(rdev)))) {
    349   1.1  riastrad 		/* crtc didn't flip in this target vblank interval,
    350   1.1  riastrad 		 * but flip is pending in crtc. Based on the current
    351   1.1  riastrad 		 * scanout position we know that the current frame is
    352   1.1  riastrad 		 * (nearly) complete and the flip will (likely)
    353   1.1  riastrad 		 * complete before the start of the next frame.
    354   1.1  riastrad 		 */
    355   1.1  riastrad 		update_pending = 0;
    356   1.1  riastrad 	}
    357   1.6  riastrad 	spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
    358   1.6  riastrad 	if (!update_pending)
    359   1.6  riastrad 		radeon_crtc_handle_flip(rdev, crtc_id);
    360   1.6  riastrad }
    361   1.6  riastrad 
    362   1.6  riastrad /**
    363   1.6  riastrad  * radeon_crtc_handle_flip - page flip completed
    364   1.6  riastrad  *
    365   1.6  riastrad  * @rdev: radeon device pointer
    366   1.6  riastrad  * @crtc_id: crtc number this event is for
    367   1.6  riastrad  *
    368   1.6  riastrad  * Called when we are sure that a page flip for this crtc is completed.
    369   1.6  riastrad  */
    370   1.6  riastrad void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
    371   1.6  riastrad {
    372   1.6  riastrad 	struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
    373   1.6  riastrad 	struct radeon_flip_work *work;
    374   1.6  riastrad 	unsigned long flags;
    375   1.6  riastrad 
    376   1.6  riastrad 	/* this can happen at init */
    377   1.6  riastrad 	if (radeon_crtc == NULL)
    378   1.6  riastrad 		return;
    379   1.6  riastrad 
    380   1.6  riastrad 	spin_lock_irqsave(&rdev->ddev->event_lock, flags);
    381   1.6  riastrad 	work = radeon_crtc->flip_work;
    382   1.6  riastrad 	if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
    383   1.6  riastrad 		DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
    384   1.6  riastrad 				 "RADEON_FLIP_SUBMITTED(%d)\n",
    385   1.6  riastrad 				 radeon_crtc->flip_status,
    386   1.6  riastrad 				 RADEON_FLIP_SUBMITTED);
    387   1.1  riastrad 		spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
    388   1.1  riastrad 		return;
    389   1.1  riastrad 	}
    390   1.1  riastrad 
    391   1.6  riastrad 	/* Pageflip completed. Clean up. */
    392   1.6  riastrad 	radeon_crtc->flip_status = RADEON_FLIP_NONE;
    393   1.6  riastrad 	radeon_crtc->flip_work = NULL;
    394   1.1  riastrad 
    395   1.1  riastrad 	/* wakeup userspace */
    396   1.1  riastrad 	if (work->event)
    397  1.12  riastrad 		drm_crtc_send_vblank_event(&radeon_crtc->base, work->event);
    398   1.1  riastrad 
    399   1.1  riastrad 	spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
    400   1.1  riastrad 
    401  1.12  riastrad 	drm_crtc_vblank_put(&radeon_crtc->base);
    402   1.6  riastrad 	radeon_irq_kms_pflip_irq_put(rdev, work->crtc_id);
    403   1.6  riastrad 	queue_work(radeon_crtc->flip_queue, &work->unpin_work);
    404   1.6  riastrad }
    405   1.6  riastrad 
    406   1.6  riastrad /**
    407   1.6  riastrad  * radeon_flip_work_func - page flip framebuffer
    408   1.6  riastrad  *
    409   1.6  riastrad  * @work - kernel work item
    410   1.6  riastrad  *
    411   1.6  riastrad  * Wait for the buffer object to become idle and do the actual page flip
    412   1.6  riastrad  */
    413   1.6  riastrad static void radeon_flip_work_func(struct work_struct *__work)
    414   1.6  riastrad {
    415   1.6  riastrad 	struct radeon_flip_work *work =
    416   1.6  riastrad 		container_of(__work, struct radeon_flip_work, flip_work);
    417   1.6  riastrad 	struct radeon_device *rdev = work->rdev;
    418  1.12  riastrad 	struct drm_device *dev = rdev->ddev;
    419   1.6  riastrad 	struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
    420   1.6  riastrad 
    421   1.6  riastrad 	struct drm_crtc *crtc = &radeon_crtc->base;
    422   1.6  riastrad 	unsigned long flags;
    423   1.6  riastrad 	int r;
    424  1.12  riastrad 	int vpos, hpos;
    425   1.6  riastrad 
    426  1.12  riastrad 	down_read(&rdev->exclusive_lock);
    427   1.6  riastrad 	if (work->fence) {
    428   1.6  riastrad 		struct radeon_fence *fence;
    429   1.6  riastrad 
    430   1.6  riastrad 		fence = to_radeon_fence(work->fence);
    431   1.6  riastrad 		if (fence && fence->rdev == rdev) {
    432   1.6  riastrad 			r = radeon_fence_wait(fence, false);
    433   1.6  riastrad 			if (r == -EDEADLK) {
    434   1.6  riastrad 				up_read(&rdev->exclusive_lock);
    435   1.6  riastrad 				do {
    436   1.6  riastrad 					r = radeon_gpu_reset(rdev);
    437   1.6  riastrad 				} while (r == -EAGAIN);
    438   1.6  riastrad 				down_read(&rdev->exclusive_lock);
    439   1.6  riastrad 			}
    440   1.6  riastrad 		} else
    441  1.12  riastrad 			r = dma_fence_wait(work->fence, false);
    442   1.6  riastrad 
    443   1.6  riastrad 		if (r)
    444   1.6  riastrad 			DRM_ERROR("failed to wait on page flip fence (%d)!\n", r);
    445   1.6  riastrad 
    446   1.6  riastrad 		/* We continue with the page flip even if we failed to wait on
    447   1.6  riastrad 		 * the fence, otherwise the DRM core and userspace will be
    448   1.6  riastrad 		 * confused about which BO the CRTC is scanning out
    449   1.6  riastrad 		 */
    450   1.6  riastrad 
    451  1.12  riastrad 		dma_fence_put(work->fence);
    452   1.6  riastrad 		work->fence = NULL;
    453   1.6  riastrad 	}
    454   1.6  riastrad 
    455  1.12  riastrad 	/* Wait until we're out of the vertical blank period before the one
    456  1.12  riastrad 	 * targeted by the flip. Always wait on pre DCE4 to avoid races with
    457  1.12  riastrad 	 * flip completion handling from vblank irq, as these old asics don't
    458  1.12  riastrad 	 * have reliable pageflip completion interrupts.
    459  1.12  riastrad 	 */
    460  1.12  riastrad 	while (radeon_crtc->enabled &&
    461  1.12  riastrad 		(radeon_get_crtc_scanoutpos(dev, work->crtc_id, 0,
    462  1.12  riastrad 					    &vpos, &hpos, NULL, NULL,
    463  1.12  riastrad 					    &crtc->hwmode)
    464  1.12  riastrad 		& (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
    465  1.12  riastrad 		(DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
    466  1.12  riastrad 		(!ASIC_IS_AVIVO(rdev) ||
    467  1.12  riastrad 		((int) (work->target_vblank -
    468  1.12  riastrad 		dev->driver->get_vblank_counter(dev, work->crtc_id)) > 0)))
    469  1.12  riastrad 		usleep_range(1000, 2000);
    470  1.12  riastrad 
    471   1.6  riastrad 	/* We borrow the event spin lock for protecting flip_status */
    472   1.6  riastrad 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
    473   1.6  riastrad 
    474   1.6  riastrad 	/* set the proper interrupt */
    475   1.6  riastrad 	radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
    476   1.6  riastrad 
    477   1.6  riastrad 	/* do the flip (mmio) */
    478  1.12  riastrad 	radeon_page_flip(rdev, radeon_crtc->crtc_id, work->base, work->async);
    479   1.6  riastrad 
    480   1.6  riastrad 	radeon_crtc->flip_status = RADEON_FLIP_SUBMITTED;
    481   1.6  riastrad 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
    482   1.6  riastrad 	up_read(&rdev->exclusive_lock);
    483   1.1  riastrad }
    484   1.1  riastrad 
    485  1.12  riastrad static int radeon_crtc_page_flip_target(struct drm_crtc *crtc,
    486  1.12  riastrad 					struct drm_framebuffer *fb,
    487  1.12  riastrad 					struct drm_pending_vblank_event *event,
    488  1.12  riastrad 					uint32_t page_flip_flags,
    489  1.12  riastrad 					uint32_t target,
    490  1.12  riastrad 					struct drm_modeset_acquire_ctx *ctx)
    491   1.1  riastrad {
    492   1.1  riastrad 	struct drm_device *dev = crtc->dev;
    493   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    494   1.1  riastrad 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
    495   1.1  riastrad 	struct drm_gem_object *obj;
    496   1.6  riastrad 	struct radeon_flip_work *work;
    497   1.6  riastrad 	struct radeon_bo *new_rbo;
    498   1.6  riastrad 	uint32_t tiling_flags, pitch_pixels;
    499   1.6  riastrad 	uint64_t base;
    500   1.1  riastrad 	unsigned long flags;
    501   1.1  riastrad 	int r;
    502   1.1  riastrad 
    503   1.1  riastrad 	work = kzalloc(sizeof *work, GFP_KERNEL);
    504   1.1  riastrad 	if (work == NULL)
    505   1.1  riastrad 		return -ENOMEM;
    506   1.1  riastrad 
    507   1.6  riastrad 	INIT_WORK(&work->flip_work, radeon_flip_work_func);
    508   1.6  riastrad 	INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
    509   1.6  riastrad 
    510   1.1  riastrad 	work->rdev = rdev;
    511   1.1  riastrad 	work->crtc_id = radeon_crtc->crtc_id;
    512   1.6  riastrad 	work->event = event;
    513  1.12  riastrad 	work->async = (page_flip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
    514   1.6  riastrad 
    515   1.6  riastrad 	/* schedule unpin of the old buffer */
    516  1.12  riastrad 	obj = crtc->primary->fb->obj[0];
    517   1.6  riastrad 
    518   1.1  riastrad 	/* take a reference to the old object */
    519  1.12  riastrad 	drm_gem_object_get(obj);
    520   1.6  riastrad 	work->old_rbo = gem_to_radeon_bo(obj);
    521   1.6  riastrad 
    522  1.12  riastrad 	obj = fb->obj[0];
    523   1.6  riastrad 	new_rbo = gem_to_radeon_bo(obj);
    524   1.1  riastrad 
    525   1.1  riastrad 	/* pin the new buffer */
    526   1.6  riastrad 	DRM_DEBUG_DRIVER("flip-ioctl() cur_rbo = %p, new_rbo = %p\n",
    527   1.6  riastrad 			 work->old_rbo, new_rbo);
    528   1.1  riastrad 
    529   1.6  riastrad 	r = radeon_bo_reserve(new_rbo, false);
    530   1.1  riastrad 	if (unlikely(r != 0)) {
    531   1.1  riastrad 		DRM_ERROR("failed to reserve new rbo buffer before flip\n");
    532   1.6  riastrad 		goto cleanup;
    533   1.1  riastrad 	}
    534   1.1  riastrad 	/* Only 27 bit offset for legacy CRTC */
    535   1.6  riastrad 	r = radeon_bo_pin_restricted(new_rbo, RADEON_GEM_DOMAIN_VRAM,
    536   1.1  riastrad 				     ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
    537   1.1  riastrad 	if (unlikely(r != 0)) {
    538   1.6  riastrad 		radeon_bo_unreserve(new_rbo);
    539   1.1  riastrad 		r = -EINVAL;
    540   1.1  riastrad 		DRM_ERROR("failed to pin new rbo buffer before flip\n");
    541   1.6  riastrad 		goto cleanup;
    542   1.1  riastrad 	}
    543  1.12  riastrad 	work->fence = dma_fence_get(dma_resv_get_excl(new_rbo->tbo.base.resv));
    544   1.6  riastrad 	radeon_bo_get_tiling_flags(new_rbo, &tiling_flags, NULL);
    545   1.6  riastrad 	radeon_bo_unreserve(new_rbo);
    546   1.1  riastrad 
    547   1.1  riastrad 	if (!ASIC_IS_AVIVO(rdev)) {
    548   1.1  riastrad 		/* crtc offset is from display base addr not FB location */
    549   1.1  riastrad 		base -= radeon_crtc->legacy_display_base_addr;
    550  1.12  riastrad 		pitch_pixels = fb->pitches[0] / fb->format->cpp[0];
    551   1.1  riastrad 
    552   1.1  riastrad 		if (tiling_flags & RADEON_TILING_MACRO) {
    553   1.1  riastrad 			if (ASIC_IS_R300(rdev)) {
    554   1.1  riastrad 				base &= ~0x7ff;
    555   1.1  riastrad 			} else {
    556  1.12  riastrad 				int byteshift = fb->format->cpp[0] * 8 >> 4;
    557   1.1  riastrad 				int tile_addr = (((crtc->y >> 3) * pitch_pixels +  crtc->x) >> (8 - byteshift)) << 11;
    558   1.1  riastrad 				base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
    559   1.1  riastrad 			}
    560   1.1  riastrad 		} else {
    561   1.1  riastrad 			int offset = crtc->y * pitch_pixels + crtc->x;
    562  1.12  riastrad 			switch (fb->format->cpp[0] * 8) {
    563   1.1  riastrad 			case 8:
    564   1.1  riastrad 			default:
    565   1.1  riastrad 				offset *= 1;
    566   1.1  riastrad 				break;
    567   1.1  riastrad 			case 15:
    568   1.1  riastrad 			case 16:
    569   1.1  riastrad 				offset *= 2;
    570   1.1  riastrad 				break;
    571   1.1  riastrad 			case 24:
    572   1.1  riastrad 				offset *= 3;
    573   1.1  riastrad 				break;
    574   1.1  riastrad 			case 32:
    575   1.1  riastrad 				offset *= 4;
    576   1.1  riastrad 				break;
    577   1.1  riastrad 			}
    578   1.1  riastrad 			base += offset;
    579   1.1  riastrad 		}
    580   1.1  riastrad 		base &= ~7;
    581   1.1  riastrad 	}
    582   1.6  riastrad 	work->base = base;
    583  1.12  riastrad 	work->target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
    584  1.12  riastrad 		dev->driver->get_vblank_counter(dev, work->crtc_id);
    585   1.6  riastrad 
    586   1.6  riastrad 	/* We borrow the event spin lock for protecting flip_work */
    587   1.6  riastrad 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
    588   1.1  riastrad 
    589   1.6  riastrad 	if (radeon_crtc->flip_status != RADEON_FLIP_NONE) {
    590   1.6  riastrad 		DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
    591   1.6  riastrad 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
    592   1.6  riastrad 		r = -EBUSY;
    593  1.12  riastrad 		goto pflip_cleanup;
    594   1.6  riastrad 	}
    595   1.6  riastrad 	radeon_crtc->flip_status = RADEON_FLIP_PENDING;
    596   1.6  riastrad 	radeon_crtc->flip_work = work;
    597   1.1  riastrad 
    598   1.1  riastrad 	/* update crtc fb */
    599   1.1  riastrad 	crtc->primary->fb = fb;
    600   1.1  riastrad 
    601   1.6  riastrad 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
    602   1.1  riastrad 
    603   1.6  riastrad 	queue_work(radeon_crtc->flip_queue, &work->flip_work);
    604   1.6  riastrad 	return 0;
    605   1.1  riastrad 
    606   1.6  riastrad pflip_cleanup:
    607   1.6  riastrad 	if (unlikely(radeon_bo_reserve(new_rbo, false) != 0)) {
    608   1.1  riastrad 		DRM_ERROR("failed to reserve new rbo in error path\n");
    609   1.6  riastrad 		goto cleanup;
    610   1.1  riastrad 	}
    611   1.6  riastrad 	if (unlikely(radeon_bo_unpin(new_rbo) != 0)) {
    612   1.1  riastrad 		DRM_ERROR("failed to unpin new rbo in error path\n");
    613   1.1  riastrad 	}
    614   1.6  riastrad 	radeon_bo_unreserve(new_rbo);
    615   1.1  riastrad 
    616   1.6  riastrad cleanup:
    617  1.12  riastrad 	drm_gem_object_put_unlocked(&work->old_rbo->tbo.base);
    618  1.12  riastrad 	dma_fence_put(work->fence);
    619   1.1  riastrad 	kfree(work);
    620   1.1  riastrad 	return r;
    621   1.1  riastrad }
    622   1.1  riastrad 
    623   1.1  riastrad static int
    624  1.12  riastrad radeon_crtc_set_config(struct drm_mode_set *set,
    625  1.12  riastrad 		       struct drm_modeset_acquire_ctx *ctx)
    626   1.1  riastrad {
    627   1.1  riastrad 	struct drm_device *dev;
    628   1.1  riastrad 	struct radeon_device *rdev;
    629   1.1  riastrad 	struct drm_crtc *crtc;
    630   1.1  riastrad 	bool active = false;
    631   1.1  riastrad 	int ret;
    632   1.1  riastrad 
    633   1.1  riastrad 	if (!set || !set->crtc)
    634   1.1  riastrad 		return -EINVAL;
    635   1.1  riastrad 
    636   1.1  riastrad 	dev = set->crtc->dev;
    637   1.1  riastrad 
    638   1.1  riastrad 	ret = pm_runtime_get_sync(dev->dev);
    639   1.1  riastrad 	if (ret < 0)
    640   1.1  riastrad 		return ret;
    641   1.1  riastrad 
    642  1.12  riastrad 	ret = drm_crtc_helper_set_config(set, ctx);
    643   1.1  riastrad 
    644   1.1  riastrad 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
    645   1.1  riastrad 		if (crtc->enabled)
    646   1.1  riastrad 			active = true;
    647   1.1  riastrad 
    648   1.1  riastrad 	pm_runtime_mark_last_busy(dev->dev);
    649   1.1  riastrad 
    650   1.1  riastrad 	rdev = dev->dev_private;
    651   1.1  riastrad 	/* if we have active crtcs and we don't have a power ref,
    652   1.1  riastrad 	   take the current one */
    653   1.1  riastrad 	if (active && !rdev->have_disp_power_ref) {
    654   1.1  riastrad 		rdev->have_disp_power_ref = true;
    655   1.1  riastrad 		return ret;
    656   1.1  riastrad 	}
    657   1.1  riastrad 	/* if we have no active crtcs, then drop the power ref
    658   1.1  riastrad 	   we got before */
    659   1.1  riastrad 	if (!active && rdev->have_disp_power_ref) {
    660   1.1  riastrad 		pm_runtime_put_autosuspend(dev->dev);
    661   1.1  riastrad 		rdev->have_disp_power_ref = false;
    662   1.1  riastrad 	}
    663   1.1  riastrad 
    664   1.1  riastrad 	/* drop the power reference we got coming in here */
    665   1.1  riastrad 	pm_runtime_put_autosuspend(dev->dev);
    666   1.1  riastrad 	return ret;
    667   1.1  riastrad }
    668  1.12  riastrad 
    669   1.1  riastrad static const struct drm_crtc_funcs radeon_crtc_funcs = {
    670   1.6  riastrad 	.cursor_set2 = radeon_crtc_cursor_set2,
    671   1.1  riastrad 	.cursor_move = radeon_crtc_cursor_move,
    672   1.1  riastrad 	.gamma_set = radeon_crtc_gamma_set,
    673   1.1  riastrad 	.set_config = radeon_crtc_set_config,
    674   1.1  riastrad 	.destroy = radeon_crtc_destroy,
    675  1.12  riastrad 	.page_flip_target = radeon_crtc_page_flip_target,
    676   1.1  riastrad };
    677   1.1  riastrad 
    678   1.1  riastrad static void radeon_crtc_init(struct drm_device *dev, int index)
    679   1.1  riastrad {
    680   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    681   1.1  riastrad 	struct radeon_crtc *radeon_crtc;
    682   1.1  riastrad 
    683   1.1  riastrad 	radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
    684   1.1  riastrad 	if (radeon_crtc == NULL)
    685   1.1  riastrad 		return;
    686   1.1  riastrad 
    687   1.1  riastrad 	drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
    688   1.1  riastrad 
    689   1.1  riastrad 	drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
    690   1.1  riastrad 	radeon_crtc->crtc_id = index;
    691  1.12  riastrad 	radeon_crtc->flip_queue = alloc_workqueue("radeon-crtc", WQ_HIGHPRI, 0);
    692   1.1  riastrad 	rdev->mode_info.crtcs[index] = radeon_crtc;
    693   1.1  riastrad 
    694   1.1  riastrad 	if (rdev->family >= CHIP_BONAIRE) {
    695   1.1  riastrad 		radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
    696   1.1  riastrad 		radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
    697   1.1  riastrad 	} else {
    698   1.1  riastrad 		radeon_crtc->max_cursor_width = CURSOR_WIDTH;
    699   1.1  riastrad 		radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
    700   1.1  riastrad 	}
    701   1.1  riastrad 	dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
    702   1.1  riastrad 	dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
    703   1.1  riastrad 
    704   1.1  riastrad #if 0
    705   1.1  riastrad 	radeon_crtc->mode_set.crtc = &radeon_crtc->base;
    706   1.1  riastrad 	radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
    707   1.1  riastrad 	radeon_crtc->mode_set.num_connectors = 0;
    708   1.1  riastrad #endif
    709   1.1  riastrad 
    710   1.1  riastrad 	if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
    711   1.1  riastrad 		radeon_atombios_init_crtc(dev, radeon_crtc);
    712   1.1  riastrad 	else
    713   1.1  riastrad 		radeon_legacy_init_crtc(dev, radeon_crtc);
    714   1.1  riastrad }
    715   1.1  riastrad 
    716   1.1  riastrad static const char *encoder_names[38] = {
    717   1.1  riastrad 	"NONE",
    718   1.1  riastrad 	"INTERNAL_LVDS",
    719   1.1  riastrad 	"INTERNAL_TMDS1",
    720   1.1  riastrad 	"INTERNAL_TMDS2",
    721   1.1  riastrad 	"INTERNAL_DAC1",
    722   1.1  riastrad 	"INTERNAL_DAC2",
    723   1.1  riastrad 	"INTERNAL_SDVOA",
    724   1.1  riastrad 	"INTERNAL_SDVOB",
    725   1.1  riastrad 	"SI170B",
    726   1.1  riastrad 	"CH7303",
    727   1.1  riastrad 	"CH7301",
    728   1.1  riastrad 	"INTERNAL_DVO1",
    729   1.1  riastrad 	"EXTERNAL_SDVOA",
    730   1.1  riastrad 	"EXTERNAL_SDVOB",
    731   1.1  riastrad 	"TITFP513",
    732   1.1  riastrad 	"INTERNAL_LVTM1",
    733   1.1  riastrad 	"VT1623",
    734   1.1  riastrad 	"HDMI_SI1930",
    735   1.1  riastrad 	"HDMI_INTERNAL",
    736   1.1  riastrad 	"INTERNAL_KLDSCP_TMDS1",
    737   1.1  riastrad 	"INTERNAL_KLDSCP_DVO1",
    738   1.1  riastrad 	"INTERNAL_KLDSCP_DAC1",
    739   1.1  riastrad 	"INTERNAL_KLDSCP_DAC2",
    740   1.1  riastrad 	"SI178",
    741   1.1  riastrad 	"MVPU_FPGA",
    742   1.1  riastrad 	"INTERNAL_DDI",
    743   1.1  riastrad 	"VT1625",
    744   1.1  riastrad 	"HDMI_SI1932",
    745   1.1  riastrad 	"DP_AN9801",
    746   1.1  riastrad 	"DP_DP501",
    747   1.1  riastrad 	"INTERNAL_UNIPHY",
    748   1.1  riastrad 	"INTERNAL_KLDSCP_LVTMA",
    749   1.1  riastrad 	"INTERNAL_UNIPHY1",
    750   1.1  riastrad 	"INTERNAL_UNIPHY2",
    751   1.1  riastrad 	"NUTMEG",
    752   1.1  riastrad 	"TRAVIS",
    753   1.1  riastrad 	"INTERNAL_VCE",
    754   1.1  riastrad 	"INTERNAL_UNIPHY3",
    755   1.1  riastrad };
    756   1.1  riastrad 
    757   1.1  riastrad static const char *hpd_names[6] = {
    758   1.1  riastrad 	"HPD1",
    759   1.1  riastrad 	"HPD2",
    760   1.1  riastrad 	"HPD3",
    761   1.1  riastrad 	"HPD4",
    762   1.1  riastrad 	"HPD5",
    763   1.1  riastrad 	"HPD6",
    764   1.1  riastrad };
    765   1.1  riastrad 
    766   1.1  riastrad static void radeon_print_display_setup(struct drm_device *dev)
    767   1.1  riastrad {
    768   1.1  riastrad 	struct drm_connector *connector;
    769   1.1  riastrad 	struct radeon_connector *radeon_connector;
    770   1.1  riastrad 	struct drm_encoder *encoder;
    771   1.1  riastrad 	struct radeon_encoder *radeon_encoder;
    772   1.1  riastrad 	uint32_t devices;
    773   1.1  riastrad 	int i = 0;
    774   1.1  riastrad 
    775   1.1  riastrad 	DRM_INFO("Radeon Display Connectors\n");
    776   1.1  riastrad 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
    777   1.1  riastrad 		radeon_connector = to_radeon_connector(connector);
    778   1.1  riastrad 		DRM_INFO("Connector %d:\n", i);
    779   1.6  riastrad 		DRM_INFO("  %s\n", connector->name);
    780   1.1  riastrad 		if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
    781   1.1  riastrad 			DRM_INFO("  %s\n", hpd_names[radeon_connector->hpd.hpd]);
    782   1.1  riastrad 		if (radeon_connector->ddc_bus) {
    783   1.1  riastrad 			DRM_INFO("  DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
    784   1.1  riastrad 				 radeon_connector->ddc_bus->rec.mask_clk_reg,
    785   1.1  riastrad 				 radeon_connector->ddc_bus->rec.mask_data_reg,
    786   1.1  riastrad 				 radeon_connector->ddc_bus->rec.a_clk_reg,
    787   1.1  riastrad 				 radeon_connector->ddc_bus->rec.a_data_reg,
    788   1.1  riastrad 				 radeon_connector->ddc_bus->rec.en_clk_reg,
    789   1.1  riastrad 				 radeon_connector->ddc_bus->rec.en_data_reg,
    790   1.1  riastrad 				 radeon_connector->ddc_bus->rec.y_clk_reg,
    791   1.1  riastrad 				 radeon_connector->ddc_bus->rec.y_data_reg);
    792   1.1  riastrad 			if (radeon_connector->router.ddc_valid)
    793   1.1  riastrad 				DRM_INFO("  DDC Router 0x%x/0x%x\n",
    794   1.1  riastrad 					 radeon_connector->router.ddc_mux_control_pin,
    795   1.1  riastrad 					 radeon_connector->router.ddc_mux_state);
    796   1.1  riastrad 			if (radeon_connector->router.cd_valid)
    797   1.1  riastrad 				DRM_INFO("  Clock/Data Router 0x%x/0x%x\n",
    798   1.1  riastrad 					 radeon_connector->router.cd_mux_control_pin,
    799   1.1  riastrad 					 radeon_connector->router.cd_mux_state);
    800   1.1  riastrad 		} else {
    801   1.1  riastrad 			if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
    802   1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
    803   1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
    804   1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
    805   1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
    806   1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
    807   1.1  riastrad 				DRM_INFO("  DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati (at) lists.x.org\n");
    808   1.1  riastrad 		}
    809   1.1  riastrad 		DRM_INFO("  Encoders:\n");
    810   1.1  riastrad 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
    811   1.1  riastrad 			radeon_encoder = to_radeon_encoder(encoder);
    812   1.1  riastrad 			devices = radeon_encoder->devices & radeon_connector->devices;
    813   1.1  riastrad 			if (devices) {
    814   1.1  riastrad 				if (devices & ATOM_DEVICE_CRT1_SUPPORT)
    815   1.1  riastrad 					DRM_INFO("    CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
    816   1.1  riastrad 				if (devices & ATOM_DEVICE_CRT2_SUPPORT)
    817   1.1  riastrad 					DRM_INFO("    CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
    818   1.1  riastrad 				if (devices & ATOM_DEVICE_LCD1_SUPPORT)
    819   1.1  riastrad 					DRM_INFO("    LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
    820   1.1  riastrad 				if (devices & ATOM_DEVICE_DFP1_SUPPORT)
    821   1.1  riastrad 					DRM_INFO("    DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
    822   1.1  riastrad 				if (devices & ATOM_DEVICE_DFP2_SUPPORT)
    823   1.1  riastrad 					DRM_INFO("    DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
    824   1.1  riastrad 				if (devices & ATOM_DEVICE_DFP3_SUPPORT)
    825   1.1  riastrad 					DRM_INFO("    DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
    826   1.1  riastrad 				if (devices & ATOM_DEVICE_DFP4_SUPPORT)
    827   1.1  riastrad 					DRM_INFO("    DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
    828   1.1  riastrad 				if (devices & ATOM_DEVICE_DFP5_SUPPORT)
    829   1.1  riastrad 					DRM_INFO("    DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
    830   1.1  riastrad 				if (devices & ATOM_DEVICE_DFP6_SUPPORT)
    831   1.1  riastrad 					DRM_INFO("    DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
    832   1.1  riastrad 				if (devices & ATOM_DEVICE_TV1_SUPPORT)
    833   1.1  riastrad 					DRM_INFO("    TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
    834   1.1  riastrad 				if (devices & ATOM_DEVICE_CV_SUPPORT)
    835   1.1  riastrad 					DRM_INFO("    CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
    836   1.1  riastrad 			}
    837   1.1  riastrad 		}
    838   1.1  riastrad 		i++;
    839   1.1  riastrad 	}
    840   1.1  riastrad }
    841   1.1  riastrad 
    842   1.1  riastrad static bool radeon_setup_enc_conn(struct drm_device *dev)
    843   1.1  riastrad {
    844   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    845   1.1  riastrad 	bool ret = false;
    846   1.1  riastrad 
    847   1.1  riastrad 	if (rdev->bios) {
    848   1.1  riastrad 		if (rdev->is_atom_bios) {
    849   1.1  riastrad 			ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
    850  1.12  riastrad 			if (!ret)
    851   1.1  riastrad 				ret = radeon_get_atom_connector_info_from_object_table(dev);
    852   1.1  riastrad 		} else {
    853   1.1  riastrad 			ret = radeon_get_legacy_connector_info_from_bios(dev);
    854  1.12  riastrad 			if (!ret)
    855   1.1  riastrad 				ret = radeon_get_legacy_connector_info_from_table(dev);
    856   1.1  riastrad 		}
    857   1.1  riastrad 	} else {
    858   1.1  riastrad 		if (!ASIC_IS_AVIVO(rdev))
    859   1.1  riastrad 			ret = radeon_get_legacy_connector_info_from_table(dev);
    860   1.1  riastrad 	}
    861   1.1  riastrad 	if (ret) {
    862   1.1  riastrad 		radeon_setup_encoder_clones(dev);
    863   1.1  riastrad 		radeon_print_display_setup(dev);
    864   1.1  riastrad 	}
    865   1.1  riastrad 
    866   1.1  riastrad 	return ret;
    867   1.1  riastrad }
    868   1.1  riastrad 
    869   1.1  riastrad /* avivo */
    870   1.1  riastrad 
    871   1.1  riastrad /**
    872   1.1  riastrad  * avivo_reduce_ratio - fractional number reduction
    873   1.1  riastrad  *
    874   1.1  riastrad  * @nom: nominator
    875   1.1  riastrad  * @den: denominator
    876   1.1  riastrad  * @nom_min: minimum value for nominator
    877   1.1  riastrad  * @den_min: minimum value for denominator
    878   1.1  riastrad  *
    879   1.1  riastrad  * Find the greatest common divisor and apply it on both nominator and
    880   1.1  riastrad  * denominator, but make nominator and denominator are at least as large
    881   1.1  riastrad  * as their minimum values.
    882   1.1  riastrad  */
    883   1.1  riastrad static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
    884   1.1  riastrad 			       unsigned nom_min, unsigned den_min)
    885   1.1  riastrad {
    886   1.1  riastrad 	unsigned tmp;
    887   1.1  riastrad 
    888   1.1  riastrad 	/* reduce the numbers to a simpler ratio */
    889   1.1  riastrad 	tmp = gcd(*nom, *den);
    890   1.1  riastrad 	*nom /= tmp;
    891   1.1  riastrad 	*den /= tmp;
    892   1.1  riastrad 
    893   1.1  riastrad 	/* make sure nominator is large enough */
    894  1.12  riastrad 	if (*nom < nom_min) {
    895   1.1  riastrad 		tmp = DIV_ROUND_UP(nom_min, *nom);
    896   1.1  riastrad 		*nom *= tmp;
    897   1.1  riastrad 		*den *= tmp;
    898   1.1  riastrad 	}
    899   1.1  riastrad 
    900   1.1  riastrad 	/* make sure the denominator is large enough */
    901   1.1  riastrad 	if (*den < den_min) {
    902   1.1  riastrad 		tmp = DIV_ROUND_UP(den_min, *den);
    903   1.1  riastrad 		*nom *= tmp;
    904   1.1  riastrad 		*den *= tmp;
    905   1.1  riastrad 	}
    906   1.1  riastrad }
    907   1.1  riastrad 
    908   1.1  riastrad /**
    909   1.1  riastrad  * avivo_get_fb_ref_div - feedback and ref divider calculation
    910   1.1  riastrad  *
    911   1.1  riastrad  * @nom: nominator
    912   1.1  riastrad  * @den: denominator
    913   1.1  riastrad  * @post_div: post divider
    914   1.1  riastrad  * @fb_div_max: feedback divider maximum
    915   1.1  riastrad  * @ref_div_max: reference divider maximum
    916   1.1  riastrad  * @fb_div: resulting feedback divider
    917   1.1  riastrad  * @ref_div: resulting reference divider
    918   1.1  riastrad  *
    919   1.1  riastrad  * Calculate feedback and reference divider for a given post divider. Makes
    920   1.1  riastrad  * sure we stay within the limits.
    921   1.1  riastrad  */
    922   1.1  riastrad static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
    923   1.1  riastrad 				 unsigned fb_div_max, unsigned ref_div_max,
    924   1.1  riastrad 				 unsigned *fb_div, unsigned *ref_div)
    925   1.1  riastrad {
    926   1.1  riastrad 	/* limit reference * post divider to a maximum */
    927   1.1  riastrad 	ref_div_max = max(min(100 / post_div, ref_div_max), 1u);
    928   1.1  riastrad 
    929   1.1  riastrad 	/* get matching reference and feedback divider */
    930  1.12  riastrad 	*ref_div = min(max(den/post_div, 1u), ref_div_max);
    931   1.1  riastrad 	*fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
    932   1.1  riastrad 
    933   1.1  riastrad 	/* limit fb divider to its maximum */
    934  1.12  riastrad 	if (*fb_div > fb_div_max) {
    935  1.12  riastrad 		*ref_div = (*ref_div * fb_div_max)/(*fb_div);
    936   1.1  riastrad 		*fb_div = fb_div_max;
    937   1.1  riastrad 	}
    938   1.1  riastrad }
    939   1.1  riastrad 
    940   1.1  riastrad /**
    941   1.1  riastrad  * radeon_compute_pll_avivo - compute PLL paramaters
    942   1.1  riastrad  *
    943   1.1  riastrad  * @pll: information about the PLL
    944   1.1  riastrad  * @dot_clock_p: resulting pixel clock
    945   1.1  riastrad  * fb_div_p: resulting feedback divider
    946   1.1  riastrad  * frac_fb_div_p: fractional part of the feedback divider
    947   1.1  riastrad  * ref_div_p: resulting reference divider
    948   1.1  riastrad  * post_div_p: resulting reference divider
    949   1.1  riastrad  *
    950   1.1  riastrad  * Try to calculate the PLL parameters to generate the given frequency:
    951   1.1  riastrad  * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
    952   1.1  riastrad  */
    953   1.1  riastrad void radeon_compute_pll_avivo(struct radeon_pll *pll,
    954   1.1  riastrad 			      u32 freq,
    955   1.1  riastrad 			      u32 *dot_clock_p,
    956   1.1  riastrad 			      u32 *fb_div_p,
    957   1.1  riastrad 			      u32 *frac_fb_div_p,
    958   1.1  riastrad 			      u32 *ref_div_p,
    959   1.1  riastrad 			      u32 *post_div_p)
    960   1.1  riastrad {
    961   1.1  riastrad 	unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
    962   1.1  riastrad 		freq : freq / 10;
    963   1.1  riastrad 
    964   1.1  riastrad 	unsigned fb_div_min, fb_div_max, fb_div;
    965   1.1  riastrad 	unsigned post_div_min, post_div_max, post_div;
    966   1.1  riastrad 	unsigned ref_div_min, ref_div_max, ref_div;
    967   1.1  riastrad 	unsigned post_div_best, diff_best;
    968   1.1  riastrad 	unsigned nom, den;
    969   1.1  riastrad 
    970   1.1  riastrad 	/* determine allowed feedback divider range */
    971   1.1  riastrad 	fb_div_min = pll->min_feedback_div;
    972   1.1  riastrad 	fb_div_max = pll->max_feedback_div;
    973   1.1  riastrad 
    974   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
    975   1.1  riastrad 		fb_div_min *= 10;
    976   1.1  riastrad 		fb_div_max *= 10;
    977   1.1  riastrad 	}
    978   1.1  riastrad 
    979   1.1  riastrad 	/* determine allowed ref divider range */
    980   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_REF_DIV)
    981   1.1  riastrad 		ref_div_min = pll->reference_div;
    982   1.1  riastrad 	else
    983   1.1  riastrad 		ref_div_min = pll->min_ref_div;
    984   1.1  riastrad 
    985   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
    986   1.1  riastrad 	    pll->flags & RADEON_PLL_USE_REF_DIV)
    987   1.1  riastrad 		ref_div_max = pll->reference_div;
    988   1.6  riastrad 	else if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
    989   1.6  riastrad 		/* fix for problems on RS880 */
    990   1.6  riastrad 		ref_div_max = min(pll->max_ref_div, 7u);
    991   1.1  riastrad 	else
    992   1.1  riastrad 		ref_div_max = pll->max_ref_div;
    993   1.1  riastrad 
    994   1.1  riastrad 	/* determine allowed post divider range */
    995   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_POST_DIV) {
    996   1.1  riastrad 		post_div_min = pll->post_div;
    997   1.1  riastrad 		post_div_max = pll->post_div;
    998   1.1  riastrad 	} else {
    999   1.1  riastrad 		unsigned vco_min, vco_max;
   1000   1.1  riastrad 
   1001   1.1  riastrad 		if (pll->flags & RADEON_PLL_IS_LCD) {
   1002   1.1  riastrad 			vco_min = pll->lcd_pll_out_min;
   1003   1.1  riastrad 			vco_max = pll->lcd_pll_out_max;
   1004   1.1  riastrad 		} else {
   1005   1.1  riastrad 			vco_min = pll->pll_out_min;
   1006   1.1  riastrad 			vco_max = pll->pll_out_max;
   1007   1.1  riastrad 		}
   1008   1.1  riastrad 
   1009   1.1  riastrad 		if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
   1010   1.1  riastrad 			vco_min *= 10;
   1011   1.1  riastrad 			vco_max *= 10;
   1012   1.1  riastrad 		}
   1013   1.1  riastrad 
   1014   1.1  riastrad 		post_div_min = vco_min / target_clock;
   1015   1.1  riastrad 		if ((target_clock * post_div_min) < vco_min)
   1016   1.1  riastrad 			++post_div_min;
   1017   1.1  riastrad 		if (post_div_min < pll->min_post_div)
   1018   1.1  riastrad 			post_div_min = pll->min_post_div;
   1019   1.1  riastrad 
   1020   1.1  riastrad 		post_div_max = vco_max / target_clock;
   1021   1.1  riastrad 		if ((target_clock * post_div_max) > vco_max)
   1022   1.1  riastrad 			--post_div_max;
   1023   1.1  riastrad 		if (post_div_max > pll->max_post_div)
   1024   1.1  riastrad 			post_div_max = pll->max_post_div;
   1025   1.1  riastrad 	}
   1026   1.1  riastrad 
   1027   1.1  riastrad 	/* represent the searched ratio as fractional number */
   1028   1.1  riastrad 	nom = target_clock;
   1029   1.1  riastrad 	den = pll->reference_freq;
   1030   1.1  riastrad 
   1031   1.1  riastrad 	/* reduce the numbers to a simpler ratio */
   1032   1.1  riastrad 	avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
   1033   1.1  riastrad 
   1034   1.1  riastrad 	/* now search for a post divider */
   1035   1.1  riastrad 	if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
   1036   1.1  riastrad 		post_div_best = post_div_min;
   1037   1.1  riastrad 	else
   1038   1.1  riastrad 		post_div_best = post_div_max;
   1039   1.1  riastrad 	diff_best = ~0;
   1040   1.1  riastrad 
   1041   1.1  riastrad 	for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
   1042   1.1  riastrad 		unsigned diff;
   1043   1.1  riastrad 		avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
   1044   1.1  riastrad 				     ref_div_max, &fb_div, &ref_div);
   1045   1.1  riastrad 		diff = abs(target_clock - (pll->reference_freq * fb_div) /
   1046   1.1  riastrad 			(ref_div * post_div));
   1047   1.1  riastrad 
   1048   1.1  riastrad 		if (diff < diff_best || (diff == diff_best &&
   1049   1.1  riastrad 		    !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
   1050   1.1  riastrad 
   1051   1.1  riastrad 			post_div_best = post_div;
   1052   1.1  riastrad 			diff_best = diff;
   1053   1.1  riastrad 		}
   1054   1.1  riastrad 	}
   1055   1.1  riastrad 	post_div = post_div_best;
   1056   1.1  riastrad 
   1057   1.1  riastrad 	/* get the feedback and reference divider for the optimal value */
   1058   1.1  riastrad 	avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
   1059   1.1  riastrad 			     &fb_div, &ref_div);
   1060   1.1  riastrad 
   1061   1.1  riastrad 	/* reduce the numbers to a simpler ratio once more */
   1062   1.1  riastrad 	/* this also makes sure that the reference divider is large enough */
   1063   1.1  riastrad 	avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
   1064   1.1  riastrad 
   1065   1.1  riastrad 	/* avoid high jitter with small fractional dividers */
   1066   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
   1067   1.1  riastrad 		fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 50);
   1068   1.1  riastrad 		if (fb_div < fb_div_min) {
   1069   1.1  riastrad 			unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
   1070   1.1  riastrad 			fb_div *= tmp;
   1071   1.1  riastrad 			ref_div *= tmp;
   1072   1.1  riastrad 		}
   1073   1.1  riastrad 	}
   1074   1.1  riastrad 
   1075   1.1  riastrad 	/* and finally save the result */
   1076   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
   1077   1.1  riastrad 		*fb_div_p = fb_div / 10;
   1078   1.1  riastrad 		*frac_fb_div_p = fb_div % 10;
   1079   1.1  riastrad 	} else {
   1080   1.1  riastrad 		*fb_div_p = fb_div;
   1081   1.1  riastrad 		*frac_fb_div_p = 0;
   1082   1.1  riastrad 	}
   1083   1.1  riastrad 
   1084   1.1  riastrad 	*dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
   1085   1.1  riastrad 			(pll->reference_freq * *frac_fb_div_p)) /
   1086   1.1  riastrad 		       (ref_div * post_div * 10);
   1087   1.1  riastrad 	*ref_div_p = ref_div;
   1088   1.1  riastrad 	*post_div_p = post_div;
   1089   1.1  riastrad 
   1090   1.1  riastrad 	DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
   1091   1.1  riastrad 		      freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
   1092   1.1  riastrad 		      ref_div, post_div);
   1093   1.1  riastrad }
   1094   1.1  riastrad 
   1095   1.1  riastrad /* pre-avivo */
   1096   1.1  riastrad static inline uint32_t radeon_div(uint64_t n, uint32_t d)
   1097   1.1  riastrad {
   1098   1.2  riastrad 	uint64_t mod __unused;
   1099   1.1  riastrad 
   1100   1.1  riastrad 	n += d / 2;
   1101   1.1  riastrad 
   1102   1.1  riastrad 	mod = do_div(n, d);
   1103   1.1  riastrad 	return n;
   1104   1.1  riastrad }
   1105   1.1  riastrad 
   1106   1.1  riastrad void radeon_compute_pll_legacy(struct radeon_pll *pll,
   1107   1.1  riastrad 			       uint64_t freq,
   1108   1.1  riastrad 			       uint32_t *dot_clock_p,
   1109   1.1  riastrad 			       uint32_t *fb_div_p,
   1110   1.1  riastrad 			       uint32_t *frac_fb_div_p,
   1111   1.1  riastrad 			       uint32_t *ref_div_p,
   1112   1.1  riastrad 			       uint32_t *post_div_p)
   1113   1.1  riastrad {
   1114   1.1  riastrad 	uint32_t min_ref_div = pll->min_ref_div;
   1115   1.1  riastrad 	uint32_t max_ref_div = pll->max_ref_div;
   1116   1.1  riastrad 	uint32_t min_post_div = pll->min_post_div;
   1117   1.1  riastrad 	uint32_t max_post_div = pll->max_post_div;
   1118   1.1  riastrad 	uint32_t min_fractional_feed_div = 0;
   1119   1.1  riastrad 	uint32_t max_fractional_feed_div = 0;
   1120   1.1  riastrad 	uint32_t best_vco = pll->best_vco;
   1121   1.1  riastrad 	uint32_t best_post_div = 1;
   1122   1.1  riastrad 	uint32_t best_ref_div = 1;
   1123   1.1  riastrad 	uint32_t best_feedback_div = 1;
   1124   1.1  riastrad 	uint32_t best_frac_feedback_div = 0;
   1125   1.1  riastrad 	uint32_t best_freq = -1;
   1126   1.1  riastrad 	uint32_t best_error = 0xffffffff;
   1127   1.1  riastrad 	uint32_t best_vco_diff = 1;
   1128   1.1  riastrad 	uint32_t post_div;
   1129   1.1  riastrad 	u32 pll_out_min, pll_out_max;
   1130   1.1  riastrad 
   1131   1.2  riastrad 	DRM_DEBUG_KMS("PLL freq %"PRIu64" %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
   1132   1.1  riastrad 	freq = freq * 1000;
   1133   1.1  riastrad 
   1134   1.1  riastrad 	if (pll->flags & RADEON_PLL_IS_LCD) {
   1135   1.1  riastrad 		pll_out_min = pll->lcd_pll_out_min;
   1136   1.1  riastrad 		pll_out_max = pll->lcd_pll_out_max;
   1137   1.1  riastrad 	} else {
   1138   1.1  riastrad 		pll_out_min = pll->pll_out_min;
   1139   1.1  riastrad 		pll_out_max = pll->pll_out_max;
   1140   1.1  riastrad 	}
   1141   1.1  riastrad 
   1142   1.1  riastrad 	if (pll_out_min > 64800)
   1143   1.1  riastrad 		pll_out_min = 64800;
   1144   1.1  riastrad 
   1145   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_REF_DIV)
   1146   1.1  riastrad 		min_ref_div = max_ref_div = pll->reference_div;
   1147   1.1  riastrad 	else {
   1148   1.1  riastrad 		while (min_ref_div < max_ref_div-1) {
   1149   1.1  riastrad 			uint32_t mid = (min_ref_div + max_ref_div) / 2;
   1150   1.1  riastrad 			uint32_t pll_in = pll->reference_freq / mid;
   1151   1.1  riastrad 			if (pll_in < pll->pll_in_min)
   1152   1.1  riastrad 				max_ref_div = mid;
   1153   1.1  riastrad 			else if (pll_in > pll->pll_in_max)
   1154   1.1  riastrad 				min_ref_div = mid;
   1155   1.1  riastrad 			else
   1156   1.1  riastrad 				break;
   1157   1.1  riastrad 		}
   1158   1.1  riastrad 	}
   1159   1.1  riastrad 
   1160   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_POST_DIV)
   1161   1.1  riastrad 		min_post_div = max_post_div = pll->post_div;
   1162   1.1  riastrad 
   1163   1.1  riastrad 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
   1164   1.1  riastrad 		min_fractional_feed_div = pll->min_frac_feedback_div;
   1165   1.1  riastrad 		max_fractional_feed_div = pll->max_frac_feedback_div;
   1166   1.1  riastrad 	}
   1167   1.1  riastrad 
   1168   1.1  riastrad 	for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
   1169   1.1  riastrad 		uint32_t ref_div;
   1170   1.1  riastrad 
   1171   1.1  riastrad 		if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
   1172   1.1  riastrad 			continue;
   1173   1.1  riastrad 
   1174   1.1  riastrad 		/* legacy radeons only have a few post_divs */
   1175   1.1  riastrad 		if (pll->flags & RADEON_PLL_LEGACY) {
   1176   1.1  riastrad 			if ((post_div == 5) ||
   1177   1.1  riastrad 			    (post_div == 7) ||
   1178   1.1  riastrad 			    (post_div == 9) ||
   1179   1.1  riastrad 			    (post_div == 10) ||
   1180   1.1  riastrad 			    (post_div == 11) ||
   1181   1.1  riastrad 			    (post_div == 13) ||
   1182   1.1  riastrad 			    (post_div == 14) ||
   1183   1.1  riastrad 			    (post_div == 15))
   1184   1.1  riastrad 				continue;
   1185   1.1  riastrad 		}
   1186   1.1  riastrad 
   1187   1.1  riastrad 		for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
   1188   1.1  riastrad 			uint32_t feedback_div, current_freq = 0, error, vco_diff;
   1189   1.1  riastrad 			uint32_t pll_in = pll->reference_freq / ref_div;
   1190   1.1  riastrad 			uint32_t min_feed_div = pll->min_feedback_div;
   1191   1.1  riastrad 			uint32_t max_feed_div = pll->max_feedback_div + 1;
   1192   1.1  riastrad 
   1193   1.1  riastrad 			if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
   1194   1.1  riastrad 				continue;
   1195   1.1  riastrad 
   1196   1.1  riastrad 			while (min_feed_div < max_feed_div) {
   1197   1.1  riastrad 				uint32_t vco;
   1198   1.1  riastrad 				uint32_t min_frac_feed_div = min_fractional_feed_div;
   1199   1.1  riastrad 				uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
   1200   1.1  riastrad 				uint32_t frac_feedback_div;
   1201   1.1  riastrad 				uint64_t tmp;
   1202   1.1  riastrad 
   1203   1.1  riastrad 				feedback_div = (min_feed_div + max_feed_div) / 2;
   1204   1.1  riastrad 
   1205   1.1  riastrad 				tmp = (uint64_t)pll->reference_freq * feedback_div;
   1206   1.1  riastrad 				vco = radeon_div(tmp, ref_div);
   1207   1.1  riastrad 
   1208   1.1  riastrad 				if (vco < pll_out_min) {
   1209   1.1  riastrad 					min_feed_div = feedback_div + 1;
   1210   1.1  riastrad 					continue;
   1211   1.1  riastrad 				} else if (vco > pll_out_max) {
   1212   1.1  riastrad 					max_feed_div = feedback_div;
   1213   1.1  riastrad 					continue;
   1214   1.1  riastrad 				}
   1215   1.1  riastrad 
   1216   1.1  riastrad 				while (min_frac_feed_div < max_frac_feed_div) {
   1217   1.1  riastrad 					frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
   1218   1.1  riastrad 					tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
   1219   1.1  riastrad 					tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
   1220   1.1  riastrad 					current_freq = radeon_div(tmp, ref_div * post_div);
   1221   1.1  riastrad 
   1222   1.1  riastrad 					if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
   1223   1.1  riastrad 						if (freq < current_freq)
   1224   1.1  riastrad 							error = 0xffffffff;
   1225   1.1  riastrad 						else
   1226   1.1  riastrad 							error = freq - current_freq;
   1227   1.1  riastrad 					} else
   1228   1.1  riastrad 						error = abs(current_freq - freq);
   1229   1.1  riastrad 					vco_diff = abs(vco - best_vco);
   1230   1.1  riastrad 
   1231   1.1  riastrad 					if ((best_vco == 0 && error < best_error) ||
   1232   1.1  riastrad 					    (best_vco != 0 &&
   1233   1.1  riastrad 					     ((best_error > 100 && error < best_error - 100) ||
   1234   1.1  riastrad 					      (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
   1235   1.1  riastrad 						best_post_div = post_div;
   1236   1.1  riastrad 						best_ref_div = ref_div;
   1237   1.1  riastrad 						best_feedback_div = feedback_div;
   1238   1.1  riastrad 						best_frac_feedback_div = frac_feedback_div;
   1239   1.1  riastrad 						best_freq = current_freq;
   1240   1.1  riastrad 						best_error = error;
   1241   1.1  riastrad 						best_vco_diff = vco_diff;
   1242   1.1  riastrad 					} else if (current_freq == freq) {
   1243   1.1  riastrad 						if (best_freq == -1) {
   1244   1.1  riastrad 							best_post_div = post_div;
   1245   1.1  riastrad 							best_ref_div = ref_div;
   1246   1.1  riastrad 							best_feedback_div = feedback_div;
   1247   1.1  riastrad 							best_frac_feedback_div = frac_feedback_div;
   1248   1.1  riastrad 							best_freq = current_freq;
   1249   1.1  riastrad 							best_error = error;
   1250   1.1  riastrad 							best_vco_diff = vco_diff;
   1251   1.1  riastrad 						} else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
   1252   1.1  riastrad 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
   1253   1.1  riastrad 							   ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
   1254   1.1  riastrad 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
   1255   1.1  riastrad 							   ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
   1256   1.1  riastrad 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
   1257   1.1  riastrad 							best_post_div = post_div;
   1258   1.1  riastrad 							best_ref_div = ref_div;
   1259   1.1  riastrad 							best_feedback_div = feedback_div;
   1260   1.1  riastrad 							best_frac_feedback_div = frac_feedback_div;
   1261   1.1  riastrad 							best_freq = current_freq;
   1262   1.1  riastrad 							best_error = error;
   1263   1.1  riastrad 							best_vco_diff = vco_diff;
   1264   1.1  riastrad 						}
   1265   1.1  riastrad 					}
   1266   1.1  riastrad 					if (current_freq < freq)
   1267   1.1  riastrad 						min_frac_feed_div = frac_feedback_div + 1;
   1268   1.1  riastrad 					else
   1269   1.1  riastrad 						max_frac_feed_div = frac_feedback_div;
   1270   1.1  riastrad 				}
   1271   1.1  riastrad 				if (current_freq < freq)
   1272   1.1  riastrad 					min_feed_div = feedback_div + 1;
   1273   1.1  riastrad 				else
   1274   1.1  riastrad 					max_feed_div = feedback_div;
   1275   1.1  riastrad 			}
   1276   1.1  riastrad 		}
   1277   1.1  riastrad 	}
   1278   1.1  riastrad 
   1279   1.1  riastrad 	*dot_clock_p = best_freq / 10000;
   1280   1.1  riastrad 	*fb_div_p = best_feedback_div;
   1281   1.1  riastrad 	*frac_fb_div_p = best_frac_feedback_div;
   1282   1.1  riastrad 	*ref_div_p = best_ref_div;
   1283   1.1  riastrad 	*post_div_p = best_post_div;
   1284   1.1  riastrad 	DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
   1285   1.1  riastrad 		      (long long)freq,
   1286   1.1  riastrad 		      best_freq / 1000, best_feedback_div, best_frac_feedback_div,
   1287   1.1  riastrad 		      best_ref_div, best_post_div);
   1288   1.1  riastrad 
   1289   1.1  riastrad }
   1290   1.1  riastrad 
   1291   1.1  riastrad static const struct drm_framebuffer_funcs radeon_fb_funcs = {
   1292  1.12  riastrad 	.destroy = drm_gem_fb_destroy,
   1293  1.12  riastrad 	.create_handle = drm_gem_fb_create_handle,
   1294   1.1  riastrad };
   1295   1.1  riastrad 
   1296   1.1  riastrad int
   1297   1.1  riastrad radeon_framebuffer_init(struct drm_device *dev,
   1298  1.12  riastrad 			struct drm_framebuffer *fb,
   1299  1.12  riastrad 			const struct drm_mode_fb_cmd2 *mode_cmd,
   1300   1.1  riastrad 			struct drm_gem_object *obj)
   1301   1.1  riastrad {
   1302   1.1  riastrad 	int ret;
   1303  1.12  riastrad 	fb->obj[0] = obj;
   1304  1.12  riastrad 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
   1305  1.12  riastrad 	ret = drm_framebuffer_init(dev, fb, &radeon_fb_funcs);
   1306   1.1  riastrad 	if (ret) {
   1307  1.12  riastrad 		fb->obj[0] = NULL;
   1308   1.1  riastrad 		return ret;
   1309   1.1  riastrad 	}
   1310   1.1  riastrad 	return 0;
   1311   1.1  riastrad }
   1312   1.1  riastrad 
   1313   1.1  riastrad static struct drm_framebuffer *
   1314   1.1  riastrad radeon_user_framebuffer_create(struct drm_device *dev,
   1315   1.1  riastrad 			       struct drm_file *file_priv,
   1316  1.12  riastrad 			       const struct drm_mode_fb_cmd2 *mode_cmd)
   1317   1.1  riastrad {
   1318   1.1  riastrad 	struct drm_gem_object *obj;
   1319  1.12  riastrad 	struct drm_framebuffer *fb;
   1320   1.1  riastrad 	int ret;
   1321   1.1  riastrad 
   1322  1.12  riastrad 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
   1323   1.1  riastrad 	if (obj ==  NULL) {
   1324   1.5  riastrad 		dev_err(dev->dev, "No GEM object associated to handle 0x%08X, "
   1325   1.1  riastrad 			"can't create framebuffer\n", mode_cmd->handles[0]);
   1326   1.1  riastrad 		return ERR_PTR(-ENOENT);
   1327   1.1  riastrad 	}
   1328   1.1  riastrad 
   1329   1.6  riastrad 	/* Handle is imported dma-buf, so cannot be migrated to VRAM for scanout */
   1330   1.6  riastrad 	if (obj->import_attach) {
   1331   1.6  riastrad 		DRM_DEBUG_KMS("Cannot create framebuffer from imported dma_buf\n");
   1332   1.6  riastrad 		return ERR_PTR(-EINVAL);
   1333   1.6  riastrad 	}
   1334   1.6  riastrad 
   1335  1.12  riastrad 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
   1336  1.12  riastrad 	if (fb == NULL) {
   1337  1.12  riastrad 		drm_gem_object_put_unlocked(obj);
   1338   1.1  riastrad 		return ERR_PTR(-ENOMEM);
   1339   1.1  riastrad 	}
   1340   1.1  riastrad 
   1341  1.12  riastrad 	ret = radeon_framebuffer_init(dev, fb, mode_cmd, obj);
   1342   1.1  riastrad 	if (ret) {
   1343  1.12  riastrad 		kfree(fb);
   1344  1.12  riastrad 		drm_gem_object_put_unlocked(obj);
   1345   1.1  riastrad 		return ERR_PTR(ret);
   1346   1.1  riastrad 	}
   1347   1.1  riastrad 
   1348  1.12  riastrad 	return fb;
   1349   1.1  riastrad }
   1350   1.1  riastrad 
   1351   1.1  riastrad static const struct drm_mode_config_funcs radeon_mode_funcs = {
   1352   1.1  riastrad 	.fb_create = radeon_user_framebuffer_create,
   1353  1.12  riastrad 	.output_poll_changed = drm_fb_helper_output_poll_changed,
   1354   1.1  riastrad };
   1355   1.1  riastrad 
   1356  1.12  riastrad static const struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
   1357   1.1  riastrad {	{ 0, "driver" },
   1358   1.1  riastrad 	{ 1, "bios" },
   1359   1.1  riastrad };
   1360   1.1  riastrad 
   1361  1.12  riastrad static const struct drm_prop_enum_list radeon_tv_std_enum_list[] =
   1362   1.1  riastrad {	{ TV_STD_NTSC, "ntsc" },
   1363   1.1  riastrad 	{ TV_STD_PAL, "pal" },
   1364   1.1  riastrad 	{ TV_STD_PAL_M, "pal-m" },
   1365   1.1  riastrad 	{ TV_STD_PAL_60, "pal-60" },
   1366   1.1  riastrad 	{ TV_STD_NTSC_J, "ntsc-j" },
   1367   1.1  riastrad 	{ TV_STD_SCART_PAL, "scart-pal" },
   1368   1.1  riastrad 	{ TV_STD_PAL_CN, "pal-cn" },
   1369   1.1  riastrad 	{ TV_STD_SECAM, "secam" },
   1370   1.1  riastrad };
   1371   1.1  riastrad 
   1372  1.12  riastrad static const struct drm_prop_enum_list radeon_underscan_enum_list[] =
   1373   1.1  riastrad {	{ UNDERSCAN_OFF, "off" },
   1374   1.1  riastrad 	{ UNDERSCAN_ON, "on" },
   1375   1.1  riastrad 	{ UNDERSCAN_AUTO, "auto" },
   1376   1.1  riastrad };
   1377   1.1  riastrad 
   1378  1.12  riastrad static const struct drm_prop_enum_list radeon_audio_enum_list[] =
   1379   1.1  riastrad {	{ RADEON_AUDIO_DISABLE, "off" },
   1380   1.1  riastrad 	{ RADEON_AUDIO_ENABLE, "on" },
   1381   1.1  riastrad 	{ RADEON_AUDIO_AUTO, "auto" },
   1382   1.1  riastrad };
   1383   1.1  riastrad 
   1384   1.1  riastrad /* XXX support different dither options? spatial, temporal, both, etc. */
   1385  1.12  riastrad static const struct drm_prop_enum_list radeon_dither_enum_list[] =
   1386   1.1  riastrad {	{ RADEON_FMT_DITHER_DISABLE, "off" },
   1387   1.1  riastrad 	{ RADEON_FMT_DITHER_ENABLE, "on" },
   1388   1.1  riastrad };
   1389   1.1  riastrad 
   1390  1.12  riastrad static const struct drm_prop_enum_list radeon_output_csc_enum_list[] =
   1391   1.6  riastrad {	{ RADEON_OUTPUT_CSC_BYPASS, "bypass" },
   1392   1.6  riastrad 	{ RADEON_OUTPUT_CSC_TVRGB, "tvrgb" },
   1393   1.6  riastrad 	{ RADEON_OUTPUT_CSC_YCBCR601, "ycbcr601" },
   1394   1.6  riastrad 	{ RADEON_OUTPUT_CSC_YCBCR709, "ycbcr709" },
   1395   1.6  riastrad };
   1396   1.6  riastrad 
   1397   1.1  riastrad static int radeon_modeset_create_props(struct radeon_device *rdev)
   1398   1.1  riastrad {
   1399   1.1  riastrad 	int sz;
   1400   1.1  riastrad 
   1401   1.1  riastrad 	if (rdev->is_atom_bios) {
   1402   1.1  riastrad 		rdev->mode_info.coherent_mode_property =
   1403   1.1  riastrad 			drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
   1404   1.1  riastrad 		if (!rdev->mode_info.coherent_mode_property)
   1405   1.1  riastrad 			return -ENOMEM;
   1406   1.1  riastrad 	}
   1407   1.1  riastrad 
   1408   1.1  riastrad 	if (!ASIC_IS_AVIVO(rdev)) {
   1409   1.1  riastrad 		sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
   1410   1.1  riastrad 		rdev->mode_info.tmds_pll_property =
   1411   1.1  riastrad 			drm_property_create_enum(rdev->ddev, 0,
   1412   1.1  riastrad 					    "tmds_pll",
   1413   1.1  riastrad 					    radeon_tmds_pll_enum_list, sz);
   1414   1.1  riastrad 	}
   1415   1.1  riastrad 
   1416   1.1  riastrad 	rdev->mode_info.load_detect_property =
   1417   1.1  riastrad 		drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
   1418   1.1  riastrad 	if (!rdev->mode_info.load_detect_property)
   1419   1.1  riastrad 		return -ENOMEM;
   1420   1.1  riastrad 
   1421   1.1  riastrad 	drm_mode_create_scaling_mode_property(rdev->ddev);
   1422   1.1  riastrad 
   1423   1.1  riastrad 	sz = ARRAY_SIZE(radeon_tv_std_enum_list);
   1424   1.1  riastrad 	rdev->mode_info.tv_std_property =
   1425   1.1  riastrad 		drm_property_create_enum(rdev->ddev, 0,
   1426   1.1  riastrad 				    "tv standard",
   1427   1.1  riastrad 				    radeon_tv_std_enum_list, sz);
   1428   1.1  riastrad 
   1429   1.1  riastrad 	sz = ARRAY_SIZE(radeon_underscan_enum_list);
   1430   1.1  riastrad 	rdev->mode_info.underscan_property =
   1431   1.1  riastrad 		drm_property_create_enum(rdev->ddev, 0,
   1432   1.1  riastrad 				    "underscan",
   1433   1.1  riastrad 				    radeon_underscan_enum_list, sz);
   1434   1.1  riastrad 
   1435   1.1  riastrad 	rdev->mode_info.underscan_hborder_property =
   1436   1.1  riastrad 		drm_property_create_range(rdev->ddev, 0,
   1437   1.1  riastrad 					"underscan hborder", 0, 128);
   1438   1.1  riastrad 	if (!rdev->mode_info.underscan_hborder_property)
   1439   1.1  riastrad 		return -ENOMEM;
   1440   1.1  riastrad 
   1441   1.1  riastrad 	rdev->mode_info.underscan_vborder_property =
   1442   1.1  riastrad 		drm_property_create_range(rdev->ddev, 0,
   1443   1.1  riastrad 					"underscan vborder", 0, 128);
   1444   1.1  riastrad 	if (!rdev->mode_info.underscan_vborder_property)
   1445   1.1  riastrad 		return -ENOMEM;
   1446   1.1  riastrad 
   1447   1.1  riastrad 	sz = ARRAY_SIZE(radeon_audio_enum_list);
   1448   1.1  riastrad 	rdev->mode_info.audio_property =
   1449   1.1  riastrad 		drm_property_create_enum(rdev->ddev, 0,
   1450   1.1  riastrad 					 "audio",
   1451   1.1  riastrad 					 radeon_audio_enum_list, sz);
   1452   1.1  riastrad 
   1453   1.1  riastrad 	sz = ARRAY_SIZE(radeon_dither_enum_list);
   1454   1.1  riastrad 	rdev->mode_info.dither_property =
   1455   1.1  riastrad 		drm_property_create_enum(rdev->ddev, 0,
   1456   1.1  riastrad 					 "dither",
   1457   1.1  riastrad 					 radeon_dither_enum_list, sz);
   1458   1.1  riastrad 
   1459   1.6  riastrad 	sz = ARRAY_SIZE(radeon_output_csc_enum_list);
   1460   1.6  riastrad 	rdev->mode_info.output_csc_property =
   1461   1.6  riastrad 		drm_property_create_enum(rdev->ddev, 0,
   1462   1.6  riastrad 					 "output_csc",
   1463   1.6  riastrad 					 radeon_output_csc_enum_list, sz);
   1464   1.6  riastrad 
   1465   1.1  riastrad 	return 0;
   1466   1.1  riastrad }
   1467   1.1  riastrad 
   1468   1.1  riastrad void radeon_update_display_priority(struct radeon_device *rdev)
   1469   1.1  riastrad {
   1470   1.1  riastrad 	/* adjustment options for the display watermarks */
   1471   1.1  riastrad 	if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
   1472   1.1  riastrad 		/* set display priority to high for r3xx, rv515 chips
   1473   1.1  riastrad 		 * this avoids flickering due to underflow to the
   1474   1.1  riastrad 		 * display controllers during heavy acceleration.
   1475   1.1  riastrad 		 * Don't force high on rs4xx igp chips as it seems to
   1476   1.1  riastrad 		 * affect the sound card.  See kernel bug 15982.
   1477   1.1  riastrad 		 */
   1478   1.1  riastrad 		if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
   1479   1.1  riastrad 		    !(rdev->flags & RADEON_IS_IGP))
   1480   1.1  riastrad 			rdev->disp_priority = 2;
   1481   1.1  riastrad 		else
   1482   1.1  riastrad 			rdev->disp_priority = 0;
   1483   1.1  riastrad 	} else
   1484   1.1  riastrad 		rdev->disp_priority = radeon_disp_priority;
   1485   1.1  riastrad 
   1486   1.1  riastrad }
   1487   1.1  riastrad 
   1488   1.1  riastrad /*
   1489   1.1  riastrad  * Allocate hdmi structs and determine register offsets
   1490   1.1  riastrad  */
   1491   1.1  riastrad static void radeon_afmt_init(struct radeon_device *rdev)
   1492   1.1  riastrad {
   1493   1.1  riastrad 	int i;
   1494   1.1  riastrad 
   1495   1.1  riastrad 	for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
   1496   1.1  riastrad 		rdev->mode_info.afmt[i] = NULL;
   1497   1.1  riastrad 
   1498   1.1  riastrad 	if (ASIC_IS_NODCE(rdev)) {
   1499   1.1  riastrad 		/* nothing to do */
   1500   1.1  riastrad 	} else if (ASIC_IS_DCE4(rdev)) {
   1501   1.1  riastrad 		static uint32_t eg_offsets[] = {
   1502   1.1  riastrad 			EVERGREEN_CRTC0_REGISTER_OFFSET,
   1503   1.1  riastrad 			EVERGREEN_CRTC1_REGISTER_OFFSET,
   1504   1.1  riastrad 			EVERGREEN_CRTC2_REGISTER_OFFSET,
   1505   1.1  riastrad 			EVERGREEN_CRTC3_REGISTER_OFFSET,
   1506   1.1  riastrad 			EVERGREEN_CRTC4_REGISTER_OFFSET,
   1507   1.1  riastrad 			EVERGREEN_CRTC5_REGISTER_OFFSET,
   1508   1.1  riastrad 			0x13830 - 0x7030,
   1509   1.1  riastrad 		};
   1510   1.1  riastrad 		int num_afmt;
   1511   1.1  riastrad 
   1512   1.1  riastrad 		/* DCE8 has 7 audio blocks tied to DIG encoders */
   1513   1.1  riastrad 		/* DCE6 has 6 audio blocks tied to DIG encoders */
   1514   1.1  riastrad 		/* DCE4/5 has 6 audio blocks tied to DIG encoders */
   1515   1.1  riastrad 		/* DCE4.1 has 2 audio blocks tied to DIG encoders */
   1516   1.1  riastrad 		if (ASIC_IS_DCE8(rdev))
   1517   1.1  riastrad 			num_afmt = 7;
   1518   1.1  riastrad 		else if (ASIC_IS_DCE6(rdev))
   1519   1.1  riastrad 			num_afmt = 6;
   1520   1.1  riastrad 		else if (ASIC_IS_DCE5(rdev))
   1521   1.1  riastrad 			num_afmt = 6;
   1522   1.1  riastrad 		else if (ASIC_IS_DCE41(rdev))
   1523   1.1  riastrad 			num_afmt = 2;
   1524   1.1  riastrad 		else /* DCE4 */
   1525   1.1  riastrad 			num_afmt = 6;
   1526   1.1  riastrad 
   1527   1.1  riastrad 		BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
   1528   1.1  riastrad 		for (i = 0; i < num_afmt; i++) {
   1529   1.1  riastrad 			rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
   1530   1.1  riastrad 			if (rdev->mode_info.afmt[i]) {
   1531   1.1  riastrad 				rdev->mode_info.afmt[i]->offset = eg_offsets[i];
   1532   1.1  riastrad 				rdev->mode_info.afmt[i]->id = i;
   1533   1.1  riastrad 			}
   1534   1.1  riastrad 		}
   1535   1.1  riastrad 	} else if (ASIC_IS_DCE3(rdev)) {
   1536   1.1  riastrad 		/* DCE3.x has 2 audio blocks tied to DIG encoders */
   1537   1.1  riastrad 		rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
   1538   1.1  riastrad 		if (rdev->mode_info.afmt[0]) {
   1539   1.1  riastrad 			rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
   1540   1.1  riastrad 			rdev->mode_info.afmt[0]->id = 0;
   1541   1.1  riastrad 		}
   1542   1.1  riastrad 		rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
   1543   1.1  riastrad 		if (rdev->mode_info.afmt[1]) {
   1544   1.1  riastrad 			rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
   1545   1.1  riastrad 			rdev->mode_info.afmt[1]->id = 1;
   1546   1.1  riastrad 		}
   1547   1.1  riastrad 	} else if (ASIC_IS_DCE2(rdev)) {
   1548   1.1  riastrad 		/* DCE2 has at least 1 routable audio block */
   1549   1.1  riastrad 		rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
   1550   1.1  riastrad 		if (rdev->mode_info.afmt[0]) {
   1551   1.1  riastrad 			rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
   1552   1.1  riastrad 			rdev->mode_info.afmt[0]->id = 0;
   1553   1.1  riastrad 		}
   1554   1.1  riastrad 		/* r6xx has 2 routable audio blocks */
   1555   1.1  riastrad 		if (rdev->family >= CHIP_R600) {
   1556   1.1  riastrad 			rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
   1557   1.1  riastrad 			if (rdev->mode_info.afmt[1]) {
   1558   1.1  riastrad 				rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
   1559   1.1  riastrad 				rdev->mode_info.afmt[1]->id = 1;
   1560   1.1  riastrad 			}
   1561   1.1  riastrad 		}
   1562   1.1  riastrad 	}
   1563   1.1  riastrad }
   1564   1.1  riastrad 
   1565   1.1  riastrad static void radeon_afmt_fini(struct radeon_device *rdev)
   1566   1.1  riastrad {
   1567   1.1  riastrad 	int i;
   1568   1.1  riastrad 
   1569   1.1  riastrad 	for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
   1570   1.1  riastrad 		kfree(rdev->mode_info.afmt[i]);
   1571   1.1  riastrad 		rdev->mode_info.afmt[i] = NULL;
   1572   1.1  riastrad 	}
   1573   1.1  riastrad }
   1574   1.1  riastrad 
   1575   1.1  riastrad int radeon_modeset_init(struct radeon_device *rdev)
   1576   1.1  riastrad {
   1577   1.1  riastrad 	int i;
   1578   1.1  riastrad 	int ret;
   1579   1.1  riastrad 
   1580   1.1  riastrad 	drm_mode_config_init(rdev->ddev);
   1581   1.1  riastrad 	rdev->mode_info.mode_config_initialized = true;
   1582   1.1  riastrad 
   1583   1.1  riastrad 	rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
   1584   1.1  riastrad 
   1585  1.12  riastrad 	if (radeon_use_pflipirq == 2 && rdev->family >= CHIP_R600)
   1586  1.12  riastrad 		rdev->ddev->mode_config.async_page_flip = true;
   1587  1.12  riastrad 
   1588   1.1  riastrad 	if (ASIC_IS_DCE5(rdev)) {
   1589   1.1  riastrad 		rdev->ddev->mode_config.max_width = 16384;
   1590   1.1  riastrad 		rdev->ddev->mode_config.max_height = 16384;
   1591   1.1  riastrad 	} else if (ASIC_IS_AVIVO(rdev)) {
   1592   1.1  riastrad 		rdev->ddev->mode_config.max_width = 8192;
   1593   1.1  riastrad 		rdev->ddev->mode_config.max_height = 8192;
   1594   1.1  riastrad 	} else {
   1595   1.1  riastrad 		rdev->ddev->mode_config.max_width = 4096;
   1596   1.1  riastrad 		rdev->ddev->mode_config.max_height = 4096;
   1597   1.1  riastrad 	}
   1598   1.1  riastrad 
   1599   1.1  riastrad 	rdev->ddev->mode_config.preferred_depth = 24;
   1600   1.1  riastrad 	rdev->ddev->mode_config.prefer_shadow = 1;
   1601   1.1  riastrad 
   1602   1.1  riastrad 	rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
   1603   1.1  riastrad 
   1604   1.1  riastrad 	ret = radeon_modeset_create_props(rdev);
   1605   1.1  riastrad 	if (ret) {
   1606   1.1  riastrad 		return ret;
   1607   1.1  riastrad 	}
   1608   1.1  riastrad 
   1609   1.1  riastrad 	/* init i2c buses */
   1610   1.1  riastrad 	radeon_i2c_init(rdev);
   1611   1.1  riastrad 
   1612   1.1  riastrad 	/* check combios for a valid hardcoded EDID - Sun servers */
   1613   1.1  riastrad 	if (!rdev->is_atom_bios) {
   1614   1.1  riastrad 		/* check for hardcoded EDID in BIOS */
   1615   1.1  riastrad 		radeon_combios_check_hardcoded_edid(rdev);
   1616   1.1  riastrad 	}
   1617   1.1  riastrad 
   1618   1.1  riastrad 	/* allocate crtcs */
   1619   1.1  riastrad 	for (i = 0; i < rdev->num_crtc; i++) {
   1620   1.1  riastrad 		radeon_crtc_init(rdev->ddev, i);
   1621   1.1  riastrad 	}
   1622   1.1  riastrad 
   1623   1.1  riastrad 	/* okay we should have all the bios connectors */
   1624   1.1  riastrad 	ret = radeon_setup_enc_conn(rdev->ddev);
   1625   1.1  riastrad 	if (!ret) {
   1626   1.1  riastrad 		return ret;
   1627   1.1  riastrad 	}
   1628   1.1  riastrad 
   1629   1.1  riastrad 	/* init dig PHYs, disp eng pll */
   1630   1.1  riastrad 	if (rdev->is_atom_bios) {
   1631   1.1  riastrad 		radeon_atom_encoder_init(rdev);
   1632   1.1  riastrad 		radeon_atom_disp_eng_pll_init(rdev);
   1633   1.1  riastrad 	}
   1634   1.1  riastrad 
   1635   1.1  riastrad 	/* initialize hpd */
   1636   1.1  riastrad 	radeon_hpd_init(rdev);
   1637   1.1  riastrad 
   1638   1.1  riastrad 	/* setup afmt */
   1639   1.1  riastrad 	radeon_afmt_init(rdev);
   1640   1.1  riastrad 
   1641   1.1  riastrad 	radeon_fbdev_init(rdev);
   1642   1.1  riastrad 	drm_kms_helper_poll_init(rdev->ddev);
   1643   1.1  riastrad 
   1644   1.6  riastrad 	/* do pm late init */
   1645   1.6  riastrad 	ret = radeon_pm_late_init(rdev);
   1646   1.1  riastrad 
   1647   1.1  riastrad 	return 0;
   1648   1.1  riastrad }
   1649   1.1  riastrad 
   1650   1.1  riastrad void radeon_modeset_fini(struct radeon_device *rdev)
   1651   1.1  riastrad {
   1652   1.1  riastrad 	if (rdev->mode_info.mode_config_initialized) {
   1653   1.1  riastrad 		drm_kms_helper_poll_fini(rdev->ddev);
   1654   1.1  riastrad 		radeon_hpd_fini(rdev);
   1655  1.12  riastrad 		drm_helper_force_disable_all(rdev->ddev);
   1656  1.12  riastrad 		radeon_fbdev_fini(rdev);
   1657  1.12  riastrad 		radeon_afmt_fini(rdev);
   1658   1.1  riastrad 		drm_mode_config_cleanup(rdev->ddev);
   1659   1.1  riastrad 		rdev->mode_info.mode_config_initialized = false;
   1660   1.1  riastrad 	}
   1661  1.12  riastrad 
   1662  1.12  riastrad 	kfree(rdev->mode_info.bios_hardcoded_edid);
   1663  1.12  riastrad 
   1664   1.1  riastrad 	/* free i2c buses */
   1665   1.1  riastrad 	radeon_i2c_fini(rdev);
   1666   1.1  riastrad }
   1667   1.1  riastrad 
   1668   1.1  riastrad static bool is_hdtv_mode(const struct drm_display_mode *mode)
   1669   1.1  riastrad {
   1670   1.1  riastrad 	/* try and guess if this is a tv or a monitor */
   1671   1.1  riastrad 	if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
   1672   1.1  riastrad 	    (mode->vdisplay == 576) || /* 576p */
   1673   1.1  riastrad 	    (mode->vdisplay == 720) || /* 720p */
   1674   1.1  riastrad 	    (mode->vdisplay == 1080)) /* 1080p */
   1675   1.1  riastrad 		return true;
   1676   1.1  riastrad 	else
   1677   1.1  riastrad 		return false;
   1678   1.1  riastrad }
   1679   1.1  riastrad 
   1680   1.1  riastrad bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
   1681   1.1  riastrad 				const struct drm_display_mode *mode,
   1682   1.1  riastrad 				struct drm_display_mode *adjusted_mode)
   1683   1.1  riastrad {
   1684   1.1  riastrad 	struct drm_device *dev = crtc->dev;
   1685   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
   1686   1.1  riastrad 	struct drm_encoder *encoder;
   1687   1.1  riastrad 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
   1688   1.1  riastrad 	struct radeon_encoder *radeon_encoder;
   1689   1.1  riastrad 	struct drm_connector *connector;
   1690   1.1  riastrad 	bool first = true;
   1691   1.1  riastrad 	u32 src_v = 1, dst_v = 1;
   1692   1.1  riastrad 	u32 src_h = 1, dst_h = 1;
   1693   1.1  riastrad 
   1694   1.1  riastrad 	radeon_crtc->h_border = 0;
   1695   1.1  riastrad 	radeon_crtc->v_border = 0;
   1696   1.1  riastrad 
   1697   1.1  riastrad 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
   1698   1.1  riastrad 		if (encoder->crtc != crtc)
   1699   1.1  riastrad 			continue;
   1700   1.1  riastrad 		radeon_encoder = to_radeon_encoder(encoder);
   1701   1.1  riastrad 		connector = radeon_get_connector_for_encoder(encoder);
   1702   1.1  riastrad 
   1703   1.1  riastrad 		if (first) {
   1704   1.1  riastrad 			/* set scaling */
   1705   1.1  riastrad 			if (radeon_encoder->rmx_type == RMX_OFF)
   1706   1.1  riastrad 				radeon_crtc->rmx_type = RMX_OFF;
   1707   1.1  riastrad 			else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
   1708   1.1  riastrad 				 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
   1709   1.1  riastrad 				radeon_crtc->rmx_type = radeon_encoder->rmx_type;
   1710   1.1  riastrad 			else
   1711   1.1  riastrad 				radeon_crtc->rmx_type = RMX_OFF;
   1712   1.1  riastrad 			/* copy native mode */
   1713   1.1  riastrad 			memcpy(&radeon_crtc->native_mode,
   1714   1.1  riastrad 			       &radeon_encoder->native_mode,
   1715   1.1  riastrad 				sizeof(struct drm_display_mode));
   1716   1.1  riastrad 			src_v = crtc->mode.vdisplay;
   1717   1.1  riastrad 			dst_v = radeon_crtc->native_mode.vdisplay;
   1718   1.1  riastrad 			src_h = crtc->mode.hdisplay;
   1719   1.1  riastrad 			dst_h = radeon_crtc->native_mode.hdisplay;
   1720   1.1  riastrad 
   1721   1.1  riastrad 			/* fix up for overscan on hdmi */
   1722   1.1  riastrad 			if (ASIC_IS_AVIVO(rdev) &&
   1723   1.1  riastrad 			    (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
   1724   1.1  riastrad 			    ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
   1725   1.1  riastrad 			     ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
   1726   1.6  riastrad 			      drm_detect_hdmi_monitor(radeon_connector_edid(connector)) &&
   1727   1.1  riastrad 			      is_hdtv_mode(mode)))) {
   1728   1.1  riastrad 				if (radeon_encoder->underscan_hborder != 0)
   1729   1.1  riastrad 					radeon_crtc->h_border = radeon_encoder->underscan_hborder;
   1730   1.1  riastrad 				else
   1731   1.1  riastrad 					radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
   1732   1.1  riastrad 				if (radeon_encoder->underscan_vborder != 0)
   1733   1.1  riastrad 					radeon_crtc->v_border = radeon_encoder->underscan_vborder;
   1734   1.1  riastrad 				else
   1735   1.1  riastrad 					radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
   1736   1.1  riastrad 				radeon_crtc->rmx_type = RMX_FULL;
   1737   1.1  riastrad 				src_v = crtc->mode.vdisplay;
   1738   1.1  riastrad 				dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
   1739   1.1  riastrad 				src_h = crtc->mode.hdisplay;
   1740   1.1  riastrad 				dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
   1741   1.1  riastrad 			}
   1742   1.1  riastrad 			first = false;
   1743   1.1  riastrad 		} else {
   1744   1.1  riastrad 			if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
   1745   1.1  riastrad 				/* WARNING: Right now this can't happen but
   1746   1.1  riastrad 				 * in the future we need to check that scaling
   1747   1.1  riastrad 				 * are consistent across different encoder
   1748   1.1  riastrad 				 * (ie all encoder can work with the same
   1749   1.1  riastrad 				 *  scaling).
   1750   1.1  riastrad 				 */
   1751   1.1  riastrad 				DRM_ERROR("Scaling not consistent across encoder.\n");
   1752   1.1  riastrad 				return false;
   1753   1.1  riastrad 			}
   1754   1.1  riastrad 		}
   1755   1.1  riastrad 	}
   1756   1.1  riastrad 	if (radeon_crtc->rmx_type != RMX_OFF) {
   1757   1.1  riastrad 		fixed20_12 a, b;
   1758   1.1  riastrad 		a.full = dfixed_const(src_v);
   1759   1.1  riastrad 		b.full = dfixed_const(dst_v);
   1760   1.1  riastrad 		radeon_crtc->vsc.full = dfixed_div(a, b);
   1761   1.1  riastrad 		a.full = dfixed_const(src_h);
   1762   1.1  riastrad 		b.full = dfixed_const(dst_h);
   1763   1.1  riastrad 		radeon_crtc->hsc.full = dfixed_div(a, b);
   1764   1.1  riastrad 	} else {
   1765   1.1  riastrad 		radeon_crtc->vsc.full = dfixed_const(1);
   1766   1.1  riastrad 		radeon_crtc->hsc.full = dfixed_const(1);
   1767   1.1  riastrad 	}
   1768   1.1  riastrad 	return true;
   1769   1.1  riastrad }
   1770   1.1  riastrad 
   1771   1.1  riastrad /*
   1772   1.1  riastrad  * Retrieve current video scanout position of crtc on a given gpu, and
   1773   1.1  riastrad  * an optional accurate timestamp of when query happened.
   1774   1.1  riastrad  *
   1775   1.1  riastrad  * \param dev Device to query.
   1776   1.1  riastrad  * \param crtc Crtc to query.
   1777   1.1  riastrad  * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
   1778   1.6  riastrad  *              For driver internal use only also supports these flags:
   1779   1.6  riastrad  *
   1780   1.6  riastrad  *              USE_REAL_VBLANKSTART to use the real start of vblank instead
   1781   1.6  riastrad  *              of a fudged earlier start of vblank.
   1782   1.6  riastrad  *
   1783   1.6  riastrad  *              GET_DISTANCE_TO_VBLANKSTART to return distance to the
   1784   1.6  riastrad  *              fudged earlier start of vblank in *vpos and the distance
   1785   1.6  riastrad  *              to true start of vblank in *hpos.
   1786   1.6  riastrad  *
   1787   1.1  riastrad  * \param *vpos Location where vertical scanout position should be stored.
   1788   1.1  riastrad  * \param *hpos Location where horizontal scanout position should go.
   1789   1.1  riastrad  * \param *stime Target location for timestamp taken immediately before
   1790   1.1  riastrad  *               scanout position query. Can be NULL to skip timestamp.
   1791   1.1  riastrad  * \param *etime Target location for timestamp taken immediately after
   1792   1.1  riastrad  *               scanout position query. Can be NULL to skip timestamp.
   1793   1.1  riastrad  *
   1794   1.1  riastrad  * Returns vpos as a positive number while in active scanout area.
   1795   1.1  riastrad  * Returns vpos as a negative number inside vblank, counting the number
   1796   1.1  riastrad  * of scanlines to go until end of vblank, e.g., -1 means "one scanline
   1797   1.1  riastrad  * until start of active scanout / end of vblank."
   1798   1.1  riastrad  *
   1799   1.1  riastrad  * \return Flags, or'ed together as follows:
   1800   1.1  riastrad  *
   1801   1.1  riastrad  * DRM_SCANOUTPOS_VALID = Query successful.
   1802   1.1  riastrad  * DRM_SCANOUTPOS_INVBL = Inside vblank.
   1803   1.1  riastrad  * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
   1804   1.1  riastrad  * this flag means that returned position may be offset by a constant but
   1805   1.1  riastrad  * unknown small number of scanlines wrt. real scanout position.
   1806   1.1  riastrad  *
   1807   1.1  riastrad  */
   1808   1.6  riastrad int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
   1809   1.6  riastrad 			       unsigned int flags, int *vpos, int *hpos,
   1810   1.6  riastrad 			       ktime_t *stime, ktime_t *etime,
   1811   1.6  riastrad 			       const struct drm_display_mode *mode)
   1812   1.1  riastrad {
   1813   1.1  riastrad 	u32 stat_crtc = 0, vbl = 0, position = 0;
   1814   1.1  riastrad 	int vbl_start, vbl_end, vtotal, ret = 0;
   1815   1.1  riastrad 	bool in_vbl = true;
   1816   1.1  riastrad 
   1817   1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
   1818   1.1  riastrad 
   1819   1.1  riastrad 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
   1820   1.1  riastrad 
   1821   1.1  riastrad 	/* Get optional system timestamp before query. */
   1822   1.1  riastrad 	if (stime)
   1823   1.1  riastrad 		*stime = ktime_get();
   1824   1.1  riastrad 
   1825   1.1  riastrad 	if (ASIC_IS_DCE4(rdev)) {
   1826   1.6  riastrad 		if (pipe == 0) {
   1827   1.1  riastrad 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
   1828   1.1  riastrad 				     EVERGREEN_CRTC0_REGISTER_OFFSET);
   1829   1.1  riastrad 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
   1830   1.1  riastrad 					  EVERGREEN_CRTC0_REGISTER_OFFSET);
   1831   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1832   1.1  riastrad 		}
   1833   1.6  riastrad 		if (pipe == 1) {
   1834   1.1  riastrad 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
   1835   1.1  riastrad 				     EVERGREEN_CRTC1_REGISTER_OFFSET);
   1836   1.1  riastrad 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
   1837   1.1  riastrad 					  EVERGREEN_CRTC1_REGISTER_OFFSET);
   1838   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1839   1.1  riastrad 		}
   1840   1.6  riastrad 		if (pipe == 2) {
   1841   1.1  riastrad 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
   1842   1.1  riastrad 				     EVERGREEN_CRTC2_REGISTER_OFFSET);
   1843   1.1  riastrad 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
   1844   1.1  riastrad 					  EVERGREEN_CRTC2_REGISTER_OFFSET);
   1845   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1846   1.1  riastrad 		}
   1847   1.6  riastrad 		if (pipe == 3) {
   1848   1.1  riastrad 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
   1849   1.1  riastrad 				     EVERGREEN_CRTC3_REGISTER_OFFSET);
   1850   1.1  riastrad 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
   1851   1.1  riastrad 					  EVERGREEN_CRTC3_REGISTER_OFFSET);
   1852   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1853   1.1  riastrad 		}
   1854   1.6  riastrad 		if (pipe == 4) {
   1855   1.1  riastrad 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
   1856   1.1  riastrad 				     EVERGREEN_CRTC4_REGISTER_OFFSET);
   1857   1.1  riastrad 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
   1858   1.1  riastrad 					  EVERGREEN_CRTC4_REGISTER_OFFSET);
   1859   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1860   1.1  riastrad 		}
   1861   1.6  riastrad 		if (pipe == 5) {
   1862   1.1  riastrad 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
   1863   1.1  riastrad 				     EVERGREEN_CRTC5_REGISTER_OFFSET);
   1864   1.1  riastrad 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
   1865   1.1  riastrad 					  EVERGREEN_CRTC5_REGISTER_OFFSET);
   1866   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1867   1.1  riastrad 		}
   1868   1.1  riastrad 	} else if (ASIC_IS_AVIVO(rdev)) {
   1869   1.6  riastrad 		if (pipe == 0) {
   1870   1.1  riastrad 			vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
   1871   1.1  riastrad 			position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
   1872   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1873   1.1  riastrad 		}
   1874   1.6  riastrad 		if (pipe == 1) {
   1875   1.1  riastrad 			vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
   1876   1.1  riastrad 			position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
   1877   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1878   1.1  riastrad 		}
   1879   1.1  riastrad 	} else {
   1880   1.1  riastrad 		/* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
   1881   1.6  riastrad 		if (pipe == 0) {
   1882   1.1  riastrad 			/* Assume vbl_end == 0, get vbl_start from
   1883   1.1  riastrad 			 * upper 16 bits.
   1884   1.1  riastrad 			 */
   1885   1.1  riastrad 			vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
   1886   1.1  riastrad 				RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
   1887   1.1  riastrad 			/* Only retrieve vpos from upper 16 bits, set hpos == 0. */
   1888   1.1  riastrad 			position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
   1889   1.1  riastrad 			stat_crtc = RREG32(RADEON_CRTC_STATUS);
   1890   1.1  riastrad 			if (!(stat_crtc & 1))
   1891   1.1  riastrad 				in_vbl = false;
   1892   1.1  riastrad 
   1893   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1894   1.1  riastrad 		}
   1895   1.6  riastrad 		if (pipe == 1) {
   1896   1.1  riastrad 			vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
   1897   1.1  riastrad 				RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
   1898   1.1  riastrad 			position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
   1899   1.1  riastrad 			stat_crtc = RREG32(RADEON_CRTC2_STATUS);
   1900   1.1  riastrad 			if (!(stat_crtc & 1))
   1901   1.1  riastrad 				in_vbl = false;
   1902   1.1  riastrad 
   1903   1.1  riastrad 			ret |= DRM_SCANOUTPOS_VALID;
   1904   1.1  riastrad 		}
   1905   1.1  riastrad 	}
   1906   1.1  riastrad 
   1907   1.1  riastrad 	/* Get optional system timestamp after query. */
   1908   1.1  riastrad 	if (etime)
   1909   1.1  riastrad 		*etime = ktime_get();
   1910   1.1  riastrad 
   1911   1.1  riastrad 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
   1912   1.1  riastrad 
   1913   1.1  riastrad 	/* Decode into vertical and horizontal scanout position. */
   1914   1.1  riastrad 	*vpos = position & 0x1fff;
   1915   1.1  riastrad 	*hpos = (position >> 16) & 0x1fff;
   1916   1.1  riastrad 
   1917   1.1  riastrad 	/* Valid vblank area boundaries from gpu retrieved? */
   1918   1.1  riastrad 	if (vbl > 0) {
   1919   1.1  riastrad 		/* Yes: Decode. */
   1920   1.1  riastrad 		ret |= DRM_SCANOUTPOS_ACCURATE;
   1921   1.1  riastrad 		vbl_start = vbl & 0x1fff;
   1922   1.1  riastrad 		vbl_end = (vbl >> 16) & 0x1fff;
   1923   1.1  riastrad 	}
   1924   1.1  riastrad 	else {
   1925   1.1  riastrad 		/* No: Fake something reasonable which gives at least ok results. */
   1926   1.6  riastrad 		vbl_start = mode->crtc_vdisplay;
   1927   1.1  riastrad 		vbl_end = 0;
   1928   1.1  riastrad 	}
   1929   1.1  riastrad 
   1930   1.6  riastrad 	/* Called from driver internal vblank counter query code? */
   1931   1.6  riastrad 	if (flags & GET_DISTANCE_TO_VBLANKSTART) {
   1932   1.6  riastrad 	    /* Caller wants distance from real vbl_start in *hpos */
   1933   1.6  riastrad 	    *hpos = *vpos - vbl_start;
   1934   1.6  riastrad 	}
   1935   1.6  riastrad 
   1936   1.6  riastrad 	/* Fudge vblank to start a few scanlines earlier to handle the
   1937   1.6  riastrad 	 * problem that vblank irqs fire a few scanlines before start
   1938   1.6  riastrad 	 * of vblank. Some driver internal callers need the true vblank
   1939   1.6  riastrad 	 * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
   1940   1.6  riastrad 	 *
   1941   1.6  riastrad 	 * The cause of the "early" vblank irq is that the irq is triggered
   1942   1.6  riastrad 	 * by the line buffer logic when the line buffer read position enters
   1943   1.6  riastrad 	 * the vblank, whereas our crtc scanout position naturally lags the
   1944   1.6  riastrad 	 * line buffer read position.
   1945   1.6  riastrad 	 */
   1946   1.6  riastrad 	if (!(flags & USE_REAL_VBLANKSTART))
   1947   1.6  riastrad 		vbl_start -= rdev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
   1948   1.6  riastrad 
   1949   1.1  riastrad 	/* Test scanout position against vblank region. */
   1950   1.1  riastrad 	if ((*vpos < vbl_start) && (*vpos >= vbl_end))
   1951   1.1  riastrad 		in_vbl = false;
   1952   1.1  riastrad 
   1953   1.6  riastrad 	/* In vblank? */
   1954   1.6  riastrad 	if (in_vbl)
   1955   1.6  riastrad 	    ret |= DRM_SCANOUTPOS_IN_VBLANK;
   1956   1.6  riastrad 
   1957   1.6  riastrad 	/* Called from driver internal vblank counter query code? */
   1958   1.6  riastrad 	if (flags & GET_DISTANCE_TO_VBLANKSTART) {
   1959   1.6  riastrad 		/* Caller wants distance from fudged earlier vbl_start */
   1960   1.6  riastrad 		*vpos -= vbl_start;
   1961   1.6  riastrad 		return ret;
   1962   1.6  riastrad 	}
   1963   1.6  riastrad 
   1964   1.1  riastrad 	/* Check if inside vblank area and apply corrective offsets:
   1965   1.1  riastrad 	 * vpos will then be >=0 in video scanout area, but negative
   1966   1.1  riastrad 	 * within vblank area, counting down the number of lines until
   1967   1.1  riastrad 	 * start of scanout.
   1968   1.1  riastrad 	 */
   1969   1.1  riastrad 
   1970   1.1  riastrad 	/* Inside "upper part" of vblank area? Apply corrective offset if so: */
   1971   1.1  riastrad 	if (in_vbl && (*vpos >= vbl_start)) {
   1972   1.6  riastrad 		vtotal = mode->crtc_vtotal;
   1973   1.1  riastrad 		*vpos = *vpos - vtotal;
   1974   1.1  riastrad 	}
   1975   1.1  riastrad 
   1976   1.1  riastrad 	/* Correct for shifted end of vbl at vbl_end. */
   1977   1.1  riastrad 	*vpos = *vpos - vbl_end;
   1978   1.1  riastrad 
   1979   1.1  riastrad 	return ret;
   1980   1.1  riastrad }
   1981