Home | History | Annotate | Line # | Download | only in radeon
      1  1.4  riastrad /*	$NetBSD: radeon_pm.c,v 1.6 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.4  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      5  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      6  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      7  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
      9  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     10  1.1  riastrad  *
     11  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     12  1.1  riastrad  * all copies or substantial portions of the Software.
     13  1.1  riastrad  *
     14  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     17  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     18  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     19  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     20  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     21  1.1  riastrad  *
     22  1.1  riastrad  * Authors: Rafa Miecki <zajec5 (at) gmail.com>
     23  1.1  riastrad  *          Alex Deucher <alexdeucher (at) gmail.com>
     24  1.1  riastrad  */
     25  1.6  riastrad 
     26  1.4  riastrad #include <sys/cdefs.h>
     27  1.4  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_pm.c,v 1.6 2021/12/18 23:45:43 riastradh Exp $");
     28  1.4  riastrad 
     29  1.6  riastrad #include <linux/hwmon-sysfs.h>
     30  1.6  riastrad #include <linux/hwmon.h>
     31  1.6  riastrad #include <linux/pci.h>
     32  1.6  riastrad #include <linux/power_supply.h>
     33  1.6  riastrad 
     34  1.6  riastrad #include <drm/drm_debugfs.h>
     35  1.6  riastrad #include <drm/drm_vblank.h>
     36  1.6  riastrad 
     37  1.6  riastrad #include "atom.h"
     38  1.1  riastrad #include "avivod.h"
     39  1.4  riastrad #include "r600_dpm.h"
     40  1.6  riastrad #include "radeon.h"
     41  1.1  riastrad 
     42  1.1  riastrad #define RADEON_IDLE_LOOP_MS 100
     43  1.1  riastrad #define RADEON_RECLOCK_DELAY_MS 200
     44  1.1  riastrad #define RADEON_WAIT_VBLANK_TIMEOUT 200
     45  1.1  riastrad 
     46  1.1  riastrad static const char *radeon_pm_state_type_name[5] = {
     47  1.1  riastrad 	"",
     48  1.1  riastrad 	"Powersave",
     49  1.1  riastrad 	"Battery",
     50  1.1  riastrad 	"Balanced",
     51  1.1  riastrad 	"Performance",
     52  1.1  riastrad };
     53  1.1  riastrad 
     54  1.1  riastrad static void radeon_dynpm_idle_work_handler(struct work_struct *work);
     55  1.1  riastrad static int radeon_debugfs_pm_init(struct radeon_device *rdev);
     56  1.1  riastrad static bool radeon_pm_in_vbl(struct radeon_device *rdev);
     57  1.1  riastrad static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
     58  1.1  riastrad static void radeon_pm_update_profile(struct radeon_device *rdev);
     59  1.1  riastrad static void radeon_pm_set_clocks(struct radeon_device *rdev);
     60  1.1  riastrad 
     61  1.1  riastrad int radeon_pm_get_type_index(struct radeon_device *rdev,
     62  1.1  riastrad 			     enum radeon_pm_state_type ps_type,
     63  1.1  riastrad 			     int instance)
     64  1.1  riastrad {
     65  1.1  riastrad 	int i;
     66  1.1  riastrad 	int found_instance = -1;
     67  1.1  riastrad 
     68  1.1  riastrad 	for (i = 0; i < rdev->pm.num_power_states; i++) {
     69  1.1  riastrad 		if (rdev->pm.power_state[i].type == ps_type) {
     70  1.1  riastrad 			found_instance++;
     71  1.1  riastrad 			if (found_instance == instance)
     72  1.1  riastrad 				return i;
     73  1.1  riastrad 		}
     74  1.1  riastrad 	}
     75  1.1  riastrad 	/* return default if no match */
     76  1.1  riastrad 	return rdev->pm.default_power_state_index;
     77  1.1  riastrad }
     78  1.1  riastrad 
     79  1.1  riastrad void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
     80  1.1  riastrad {
     81  1.1  riastrad 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
     82  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
     83  1.1  riastrad 		if (power_supply_is_system_supplied() > 0)
     84  1.1  riastrad 			rdev->pm.dpm.ac_power = true;
     85  1.1  riastrad 		else
     86  1.1  riastrad 			rdev->pm.dpm.ac_power = false;
     87  1.4  riastrad 		if (rdev->family == CHIP_ARUBA) {
     88  1.4  riastrad 			if (rdev->asic->dpm.enable_bapm)
     89  1.4  riastrad 				radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
     90  1.4  riastrad 		}
     91  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
     92  1.6  riastrad 	} else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
     93  1.1  riastrad 		if (rdev->pm.profile == PM_PROFILE_AUTO) {
     94  1.1  riastrad 			mutex_lock(&rdev->pm.mutex);
     95  1.1  riastrad 			radeon_pm_update_profile(rdev);
     96  1.1  riastrad 			radeon_pm_set_clocks(rdev);
     97  1.1  riastrad 			mutex_unlock(&rdev->pm.mutex);
     98  1.1  riastrad 		}
     99  1.1  riastrad 	}
    100  1.1  riastrad }
    101  1.1  riastrad 
    102  1.1  riastrad static void radeon_pm_update_profile(struct radeon_device *rdev)
    103  1.1  riastrad {
    104  1.1  riastrad 	switch (rdev->pm.profile) {
    105  1.1  riastrad 	case PM_PROFILE_DEFAULT:
    106  1.1  riastrad 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
    107  1.1  riastrad 		break;
    108  1.1  riastrad 	case PM_PROFILE_AUTO:
    109  1.1  riastrad 		if (power_supply_is_system_supplied() > 0) {
    110  1.1  riastrad 			if (rdev->pm.active_crtc_count > 1)
    111  1.1  riastrad 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
    112  1.1  riastrad 			else
    113  1.1  riastrad 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
    114  1.1  riastrad 		} else {
    115  1.1  riastrad 			if (rdev->pm.active_crtc_count > 1)
    116  1.1  riastrad 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
    117  1.1  riastrad 			else
    118  1.1  riastrad 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
    119  1.1  riastrad 		}
    120  1.1  riastrad 		break;
    121  1.1  riastrad 	case PM_PROFILE_LOW:
    122  1.1  riastrad 		if (rdev->pm.active_crtc_count > 1)
    123  1.1  riastrad 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
    124  1.1  riastrad 		else
    125  1.1  riastrad 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
    126  1.1  riastrad 		break;
    127  1.1  riastrad 	case PM_PROFILE_MID:
    128  1.1  riastrad 		if (rdev->pm.active_crtc_count > 1)
    129  1.1  riastrad 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
    130  1.1  riastrad 		else
    131  1.1  riastrad 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
    132  1.1  riastrad 		break;
    133  1.1  riastrad 	case PM_PROFILE_HIGH:
    134  1.1  riastrad 		if (rdev->pm.active_crtc_count > 1)
    135  1.1  riastrad 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
    136  1.1  riastrad 		else
    137  1.1  riastrad 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
    138  1.1  riastrad 		break;
    139  1.1  riastrad 	}
    140  1.1  riastrad 
    141  1.1  riastrad 	if (rdev->pm.active_crtc_count == 0) {
    142  1.1  riastrad 		rdev->pm.requested_power_state_index =
    143  1.1  riastrad 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
    144  1.1  riastrad 		rdev->pm.requested_clock_mode_index =
    145  1.1  riastrad 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
    146  1.1  riastrad 	} else {
    147  1.1  riastrad 		rdev->pm.requested_power_state_index =
    148  1.1  riastrad 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
    149  1.1  riastrad 		rdev->pm.requested_clock_mode_index =
    150  1.1  riastrad 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
    151  1.1  riastrad 	}
    152  1.1  riastrad }
    153  1.1  riastrad 
    154  1.1  riastrad static void radeon_unmap_vram_bos(struct radeon_device *rdev)
    155  1.1  riastrad {
    156  1.1  riastrad 	struct radeon_bo *bo, *n;
    157  1.1  riastrad 
    158  1.1  riastrad 	if (list_empty(&rdev->gem.objects))
    159  1.1  riastrad 		return;
    160  1.1  riastrad 
    161  1.1  riastrad 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
    162  1.1  riastrad 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
    163  1.1  riastrad 			ttm_bo_unmap_virtual(&bo->tbo);
    164  1.1  riastrad 	}
    165  1.1  riastrad }
    166  1.1  riastrad 
    167  1.1  riastrad static void radeon_sync_with_vblank(struct radeon_device *rdev)
    168  1.1  riastrad {
    169  1.1  riastrad 	if (rdev->pm.active_crtcs) {
    170  1.2  riastrad #ifdef __NetBSD__
    171  1.2  riastrad 		int ret __unused;
    172  1.2  riastrad 
    173  1.2  riastrad 		spin_lock(&rdev->irq.vblank_lock);
    174  1.2  riastrad 		rdev->pm.vblank_sync = false;
    175  1.3  riastrad 		DRM_SPIN_TIMED_WAIT_NOINTR_UNTIL(ret, &rdev->irq.vblank_queue,
    176  1.2  riastrad 		    &rdev->irq.vblank_lock,
    177  1.2  riastrad 		    msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT),
    178  1.2  riastrad 		    rdev->pm.vblank_sync);
    179  1.2  riastrad 		spin_unlock(&rdev->irq.vblank_lock);
    180  1.2  riastrad #else
    181  1.1  riastrad 		rdev->pm.vblank_sync = false;
    182  1.1  riastrad 		wait_event_timeout(
    183  1.1  riastrad 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
    184  1.1  riastrad 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
    185  1.2  riastrad #endif
    186  1.1  riastrad 	}
    187  1.1  riastrad }
    188  1.1  riastrad 
    189  1.1  riastrad static void radeon_set_power_state(struct radeon_device *rdev)
    190  1.1  riastrad {
    191  1.1  riastrad 	u32 sclk, mclk;
    192  1.1  riastrad 	bool misc_after = false;
    193  1.1  riastrad 
    194  1.1  riastrad 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
    195  1.1  riastrad 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
    196  1.1  riastrad 		return;
    197  1.1  riastrad 
    198  1.1  riastrad 	if (radeon_gui_idle(rdev)) {
    199  1.1  riastrad 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
    200  1.1  riastrad 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
    201  1.1  riastrad 		if (sclk > rdev->pm.default_sclk)
    202  1.1  riastrad 			sclk = rdev->pm.default_sclk;
    203  1.1  riastrad 
    204  1.1  riastrad 		/* starting with BTC, there is one state that is used for both
    205  1.1  riastrad 		 * MH and SH.  Difference is that we always use the high clock index for
    206  1.1  riastrad 		 * mclk and vddci.
    207  1.1  riastrad 		 */
    208  1.1  riastrad 		if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
    209  1.1  riastrad 		    (rdev->family >= CHIP_BARTS) &&
    210  1.1  riastrad 		    rdev->pm.active_crtc_count &&
    211  1.1  riastrad 		    ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
    212  1.1  riastrad 		     (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
    213  1.1  riastrad 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
    214  1.1  riastrad 				clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
    215  1.1  riastrad 		else
    216  1.1  riastrad 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
    217  1.1  riastrad 				clock_info[rdev->pm.requested_clock_mode_index].mclk;
    218  1.1  riastrad 
    219  1.1  riastrad 		if (mclk > rdev->pm.default_mclk)
    220  1.1  riastrad 			mclk = rdev->pm.default_mclk;
    221  1.1  riastrad 
    222  1.1  riastrad 		/* upvolt before raising clocks, downvolt after lowering clocks */
    223  1.1  riastrad 		if (sclk < rdev->pm.current_sclk)
    224  1.1  riastrad 			misc_after = true;
    225  1.1  riastrad 
    226  1.1  riastrad 		radeon_sync_with_vblank(rdev);
    227  1.1  riastrad 
    228  1.1  riastrad 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
    229  1.1  riastrad 			if (!radeon_pm_in_vbl(rdev))
    230  1.1  riastrad 				return;
    231  1.1  riastrad 		}
    232  1.1  riastrad 
    233  1.1  riastrad 		radeon_pm_prepare(rdev);
    234  1.1  riastrad 
    235  1.1  riastrad 		if (!misc_after)
    236  1.1  riastrad 			/* voltage, pcie lanes, etc.*/
    237  1.1  riastrad 			radeon_pm_misc(rdev);
    238  1.1  riastrad 
    239  1.1  riastrad 		/* set engine clock */
    240  1.1  riastrad 		if (sclk != rdev->pm.current_sclk) {
    241  1.1  riastrad 			radeon_pm_debug_check_in_vbl(rdev, false);
    242  1.1  riastrad 			radeon_set_engine_clock(rdev, sclk);
    243  1.1  riastrad 			radeon_pm_debug_check_in_vbl(rdev, true);
    244  1.1  riastrad 			rdev->pm.current_sclk = sclk;
    245  1.1  riastrad 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
    246  1.1  riastrad 		}
    247  1.1  riastrad 
    248  1.1  riastrad 		/* set memory clock */
    249  1.1  riastrad 		if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
    250  1.1  riastrad 			radeon_pm_debug_check_in_vbl(rdev, false);
    251  1.1  riastrad 			radeon_set_memory_clock(rdev, mclk);
    252  1.1  riastrad 			radeon_pm_debug_check_in_vbl(rdev, true);
    253  1.1  riastrad 			rdev->pm.current_mclk = mclk;
    254  1.1  riastrad 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
    255  1.1  riastrad 		}
    256  1.1  riastrad 
    257  1.1  riastrad 		if (misc_after)
    258  1.1  riastrad 			/* voltage, pcie lanes, etc.*/
    259  1.1  riastrad 			radeon_pm_misc(rdev);
    260  1.1  riastrad 
    261  1.1  riastrad 		radeon_pm_finish(rdev);
    262  1.1  riastrad 
    263  1.1  riastrad 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
    264  1.1  riastrad 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
    265  1.1  riastrad 	} else
    266  1.1  riastrad 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
    267  1.1  riastrad }
    268  1.1  riastrad 
    269  1.1  riastrad static void radeon_pm_set_clocks(struct radeon_device *rdev)
    270  1.1  riastrad {
    271  1.6  riastrad 	struct drm_crtc *crtc;
    272  1.1  riastrad 	int i, r;
    273  1.1  riastrad 
    274  1.1  riastrad 	/* no need to take locks, etc. if nothing's going to change */
    275  1.1  riastrad 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
    276  1.1  riastrad 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
    277  1.1  riastrad 		return;
    278  1.1  riastrad 
    279  1.1  riastrad 	down_write(&rdev->pm.mclk_lock);
    280  1.1  riastrad 	mutex_lock(&rdev->ring_lock);
    281  1.1  riastrad 
    282  1.1  riastrad 	/* wait for the rings to drain */
    283  1.1  riastrad 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
    284  1.1  riastrad 		struct radeon_ring *ring = &rdev->ring[i];
    285  1.1  riastrad 		if (!ring->ready) {
    286  1.1  riastrad 			continue;
    287  1.1  riastrad 		}
    288  1.1  riastrad 		r = radeon_fence_wait_empty(rdev, i);
    289  1.1  riastrad 		if (r) {
    290  1.1  riastrad 			/* needs a GPU reset dont reset here */
    291  1.1  riastrad 			mutex_unlock(&rdev->ring_lock);
    292  1.1  riastrad 			up_write(&rdev->pm.mclk_lock);
    293  1.1  riastrad 			return;
    294  1.1  riastrad 		}
    295  1.1  riastrad 	}
    296  1.1  riastrad 
    297  1.1  riastrad 	radeon_unmap_vram_bos(rdev);
    298  1.1  riastrad 
    299  1.1  riastrad 	if (rdev->irq.installed) {
    300  1.6  riastrad 		i = 0;
    301  1.6  riastrad 		drm_for_each_crtc(crtc, rdev->ddev) {
    302  1.1  riastrad 			if (rdev->pm.active_crtcs & (1 << i)) {
    303  1.6  riastrad 				/* This can fail if a modeset is in progress */
    304  1.6  riastrad 				if (drm_crtc_vblank_get(crtc) == 0)
    305  1.6  riastrad 					rdev->pm.req_vblank |= (1 << i);
    306  1.6  riastrad 				else
    307  1.6  riastrad 					DRM_DEBUG_DRIVER("crtc %d no vblank, can glitch\n",
    308  1.6  riastrad 							 i);
    309  1.1  riastrad 			}
    310  1.6  riastrad 			i++;
    311  1.1  riastrad 		}
    312  1.1  riastrad 	}
    313  1.1  riastrad 
    314  1.1  riastrad 	radeon_set_power_state(rdev);
    315  1.1  riastrad 
    316  1.1  riastrad 	if (rdev->irq.installed) {
    317  1.6  riastrad 		i = 0;
    318  1.6  riastrad 		drm_for_each_crtc(crtc, rdev->ddev) {
    319  1.1  riastrad 			if (rdev->pm.req_vblank & (1 << i)) {
    320  1.1  riastrad 				rdev->pm.req_vblank &= ~(1 << i);
    321  1.6  riastrad 				drm_crtc_vblank_put(crtc);
    322  1.1  riastrad 			}
    323  1.6  riastrad 			i++;
    324  1.1  riastrad 		}
    325  1.1  riastrad 	}
    326  1.1  riastrad 
    327  1.1  riastrad 	/* update display watermarks based on new power state */
    328  1.1  riastrad 	radeon_update_bandwidth_info(rdev);
    329  1.1  riastrad 	if (rdev->pm.active_crtc_count)
    330  1.1  riastrad 		radeon_bandwidth_update(rdev);
    331  1.1  riastrad 
    332  1.1  riastrad 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
    333  1.1  riastrad 
    334  1.1  riastrad 	mutex_unlock(&rdev->ring_lock);
    335  1.1  riastrad 	up_write(&rdev->pm.mclk_lock);
    336  1.1  riastrad }
    337  1.1  riastrad 
    338  1.1  riastrad static void radeon_pm_print_states(struct radeon_device *rdev)
    339  1.1  riastrad {
    340  1.1  riastrad 	int i, j;
    341  1.1  riastrad 	struct radeon_power_state *power_state;
    342  1.1  riastrad 	struct radeon_pm_clock_info *clock_info;
    343  1.1  riastrad 
    344  1.1  riastrad 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
    345  1.1  riastrad 	for (i = 0; i < rdev->pm.num_power_states; i++) {
    346  1.1  riastrad 		power_state = &rdev->pm.power_state[i];
    347  1.1  riastrad 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
    348  1.1  riastrad 			radeon_pm_state_type_name[power_state->type]);
    349  1.1  riastrad 		if (i == rdev->pm.default_power_state_index)
    350  1.1  riastrad 			DRM_DEBUG_DRIVER("\tDefault");
    351  1.1  riastrad 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
    352  1.1  riastrad 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
    353  1.1  riastrad 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
    354  1.1  riastrad 			DRM_DEBUG_DRIVER("\tSingle display only\n");
    355  1.1  riastrad 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
    356  1.1  riastrad 		for (j = 0; j < power_state->num_clock_modes; j++) {
    357  1.1  riastrad 			clock_info = &(power_state->clock_info[j]);
    358  1.1  riastrad 			if (rdev->flags & RADEON_IS_IGP)
    359  1.1  riastrad 				DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
    360  1.1  riastrad 						 j,
    361  1.1  riastrad 						 clock_info->sclk * 10);
    362  1.1  riastrad 			else
    363  1.1  riastrad 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
    364  1.1  riastrad 						 j,
    365  1.1  riastrad 						 clock_info->sclk * 10,
    366  1.1  riastrad 						 clock_info->mclk * 10,
    367  1.1  riastrad 						 clock_info->voltage.voltage);
    368  1.1  riastrad 		}
    369  1.1  riastrad 	}
    370  1.1  riastrad }
    371  1.1  riastrad 
    372  1.2  riastrad #ifndef __NetBSD__		/* XXX radeon power */
    373  1.1  riastrad static ssize_t radeon_get_pm_profile(struct device *dev,
    374  1.1  riastrad 				     struct device_attribute *attr,
    375  1.1  riastrad 				     char *buf)
    376  1.1  riastrad {
    377  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    378  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    379  1.1  riastrad 	int cp = rdev->pm.profile;
    380  1.1  riastrad 
    381  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%s\n",
    382  1.1  riastrad 			(cp == PM_PROFILE_AUTO) ? "auto" :
    383  1.1  riastrad 			(cp == PM_PROFILE_LOW) ? "low" :
    384  1.1  riastrad 			(cp == PM_PROFILE_MID) ? "mid" :
    385  1.1  riastrad 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
    386  1.1  riastrad }
    387  1.1  riastrad 
    388  1.1  riastrad static ssize_t radeon_set_pm_profile(struct device *dev,
    389  1.1  riastrad 				     struct device_attribute *attr,
    390  1.1  riastrad 				     const char *buf,
    391  1.1  riastrad 				     size_t count)
    392  1.1  riastrad {
    393  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    394  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    395  1.1  riastrad 
    396  1.1  riastrad 	/* Can't set profile when the card is off */
    397  1.1  riastrad 	if  ((rdev->flags & RADEON_IS_PX) &&
    398  1.1  riastrad 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
    399  1.1  riastrad 		return -EINVAL;
    400  1.1  riastrad 
    401  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
    402  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
    403  1.1  riastrad 		if (strncmp("default", buf, strlen("default")) == 0)
    404  1.1  riastrad 			rdev->pm.profile = PM_PROFILE_DEFAULT;
    405  1.1  riastrad 		else if (strncmp("auto", buf, strlen("auto")) == 0)
    406  1.1  riastrad 			rdev->pm.profile = PM_PROFILE_AUTO;
    407  1.1  riastrad 		else if (strncmp("low", buf, strlen("low")) == 0)
    408  1.1  riastrad 			rdev->pm.profile = PM_PROFILE_LOW;
    409  1.1  riastrad 		else if (strncmp("mid", buf, strlen("mid")) == 0)
    410  1.1  riastrad 			rdev->pm.profile = PM_PROFILE_MID;
    411  1.1  riastrad 		else if (strncmp("high", buf, strlen("high")) == 0)
    412  1.1  riastrad 			rdev->pm.profile = PM_PROFILE_HIGH;
    413  1.1  riastrad 		else {
    414  1.1  riastrad 			count = -EINVAL;
    415  1.1  riastrad 			goto fail;
    416  1.1  riastrad 		}
    417  1.1  riastrad 		radeon_pm_update_profile(rdev);
    418  1.1  riastrad 		radeon_pm_set_clocks(rdev);
    419  1.1  riastrad 	} else
    420  1.1  riastrad 		count = -EINVAL;
    421  1.1  riastrad 
    422  1.1  riastrad fail:
    423  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
    424  1.1  riastrad 
    425  1.1  riastrad 	return count;
    426  1.1  riastrad }
    427  1.1  riastrad 
    428  1.1  riastrad static ssize_t radeon_get_pm_method(struct device *dev,
    429  1.1  riastrad 				    struct device_attribute *attr,
    430  1.1  riastrad 				    char *buf)
    431  1.1  riastrad {
    432  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    433  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    434  1.1  riastrad 	int pm = rdev->pm.pm_method;
    435  1.1  riastrad 
    436  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%s\n",
    437  1.1  riastrad 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
    438  1.1  riastrad 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
    439  1.1  riastrad }
    440  1.1  riastrad 
    441  1.1  riastrad static ssize_t radeon_set_pm_method(struct device *dev,
    442  1.1  riastrad 				    struct device_attribute *attr,
    443  1.1  riastrad 				    const char *buf,
    444  1.1  riastrad 				    size_t count)
    445  1.1  riastrad {
    446  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    447  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    448  1.1  riastrad 
    449  1.1  riastrad 	/* Can't set method when the card is off */
    450  1.1  riastrad 	if  ((rdev->flags & RADEON_IS_PX) &&
    451  1.1  riastrad 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
    452  1.1  riastrad 		count = -EINVAL;
    453  1.1  riastrad 		goto fail;
    454  1.1  riastrad 	}
    455  1.1  riastrad 
    456  1.1  riastrad 	/* we don't support the legacy modes with dpm */
    457  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
    458  1.1  riastrad 		count = -EINVAL;
    459  1.1  riastrad 		goto fail;
    460  1.1  riastrad 	}
    461  1.1  riastrad 
    462  1.1  riastrad 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
    463  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
    464  1.1  riastrad 		rdev->pm.pm_method = PM_METHOD_DYNPM;
    465  1.1  riastrad 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
    466  1.1  riastrad 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
    467  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
    468  1.1  riastrad 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
    469  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
    470  1.1  riastrad 		/* disable dynpm */
    471  1.1  riastrad 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
    472  1.1  riastrad 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
    473  1.1  riastrad 		rdev->pm.pm_method = PM_METHOD_PROFILE;
    474  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
    475  1.1  riastrad 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
    476  1.1  riastrad 	} else {
    477  1.1  riastrad 		count = -EINVAL;
    478  1.1  riastrad 		goto fail;
    479  1.1  riastrad 	}
    480  1.1  riastrad 	radeon_pm_compute_clocks(rdev);
    481  1.1  riastrad fail:
    482  1.1  riastrad 	return count;
    483  1.1  riastrad }
    484  1.1  riastrad 
    485  1.1  riastrad static ssize_t radeon_get_dpm_state(struct device *dev,
    486  1.1  riastrad 				    struct device_attribute *attr,
    487  1.1  riastrad 				    char *buf)
    488  1.1  riastrad {
    489  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    490  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    491  1.1  riastrad 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
    492  1.1  riastrad 
    493  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%s\n",
    494  1.1  riastrad 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
    495  1.1  riastrad 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
    496  1.1  riastrad }
    497  1.1  riastrad 
    498  1.1  riastrad static ssize_t radeon_set_dpm_state(struct device *dev,
    499  1.1  riastrad 				    struct device_attribute *attr,
    500  1.1  riastrad 				    const char *buf,
    501  1.1  riastrad 				    size_t count)
    502  1.1  riastrad {
    503  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    504  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    505  1.1  riastrad 
    506  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
    507  1.1  riastrad 	if (strncmp("battery", buf, strlen("battery")) == 0)
    508  1.1  riastrad 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
    509  1.1  riastrad 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
    510  1.1  riastrad 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
    511  1.1  riastrad 	else if (strncmp("performance", buf, strlen("performance")) == 0)
    512  1.1  riastrad 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
    513  1.1  riastrad 	else {
    514  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
    515  1.1  riastrad 		count = -EINVAL;
    516  1.1  riastrad 		goto fail;
    517  1.1  riastrad 	}
    518  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
    519  1.4  riastrad 
    520  1.4  riastrad 	/* Can't set dpm state when the card is off */
    521  1.4  riastrad 	if (!(rdev->flags & RADEON_IS_PX) ||
    522  1.4  riastrad 	    (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
    523  1.4  riastrad 		radeon_pm_compute_clocks(rdev);
    524  1.4  riastrad 
    525  1.1  riastrad fail:
    526  1.1  riastrad 	return count;
    527  1.1  riastrad }
    528  1.1  riastrad 
    529  1.1  riastrad static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
    530  1.1  riastrad 						       struct device_attribute *attr,
    531  1.1  riastrad 						       char *buf)
    532  1.1  riastrad {
    533  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    534  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    535  1.1  riastrad 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
    536  1.1  riastrad 
    537  1.1  riastrad 	if  ((rdev->flags & RADEON_IS_PX) &&
    538  1.1  riastrad 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
    539  1.1  riastrad 		return snprintf(buf, PAGE_SIZE, "off\n");
    540  1.1  riastrad 
    541  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%s\n",
    542  1.1  riastrad 			(level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
    543  1.1  riastrad 			(level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
    544  1.1  riastrad }
    545  1.1  riastrad 
    546  1.1  riastrad static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
    547  1.1  riastrad 						       struct device_attribute *attr,
    548  1.1  riastrad 						       const char *buf,
    549  1.1  riastrad 						       size_t count)
    550  1.1  riastrad {
    551  1.1  riastrad 	struct drm_device *ddev = dev_get_drvdata(dev);
    552  1.1  riastrad 	struct radeon_device *rdev = ddev->dev_private;
    553  1.1  riastrad 	enum radeon_dpm_forced_level level;
    554  1.1  riastrad 	int ret = 0;
    555  1.1  riastrad 
    556  1.1  riastrad 	/* Can't force performance level when the card is off */
    557  1.1  riastrad 	if  ((rdev->flags & RADEON_IS_PX) &&
    558  1.1  riastrad 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
    559  1.1  riastrad 		return -EINVAL;
    560  1.1  riastrad 
    561  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
    562  1.1  riastrad 	if (strncmp("low", buf, strlen("low")) == 0) {
    563  1.1  riastrad 		level = RADEON_DPM_FORCED_LEVEL_LOW;
    564  1.1  riastrad 	} else if (strncmp("high", buf, strlen("high")) == 0) {
    565  1.1  riastrad 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
    566  1.1  riastrad 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
    567  1.1  riastrad 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
    568  1.1  riastrad 	} else {
    569  1.1  riastrad 		count = -EINVAL;
    570  1.1  riastrad 		goto fail;
    571  1.1  riastrad 	}
    572  1.1  riastrad 	if (rdev->asic->dpm.force_performance_level) {
    573  1.1  riastrad 		if (rdev->pm.dpm.thermal_active) {
    574  1.1  riastrad 			count = -EINVAL;
    575  1.1  riastrad 			goto fail;
    576  1.1  riastrad 		}
    577  1.1  riastrad 		ret = radeon_dpm_force_performance_level(rdev, level);
    578  1.1  riastrad 		if (ret)
    579  1.1  riastrad 			count = -EINVAL;
    580  1.1  riastrad 	}
    581  1.1  riastrad fail:
    582  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
    583  1.1  riastrad 
    584  1.1  riastrad 	return count;
    585  1.1  riastrad }
    586  1.1  riastrad 
    587  1.4  riastrad static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
    588  1.4  riastrad 					    struct device_attribute *attr,
    589  1.4  riastrad 					    char *buf)
    590  1.4  riastrad {
    591  1.4  riastrad 	struct radeon_device *rdev = dev_get_drvdata(dev);
    592  1.4  riastrad 	u32 pwm_mode = 0;
    593  1.4  riastrad 
    594  1.4  riastrad 	if (rdev->asic->dpm.fan_ctrl_get_mode)
    595  1.4  riastrad 		pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
    596  1.4  riastrad 
    597  1.4  riastrad 	/* never 0 (full-speed), fuse or smc-controlled always */
    598  1.4  riastrad 	return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
    599  1.4  riastrad }
    600  1.4  riastrad 
    601  1.4  riastrad static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
    602  1.4  riastrad 					    struct device_attribute *attr,
    603  1.4  riastrad 					    const char *buf,
    604  1.4  riastrad 					    size_t count)
    605  1.4  riastrad {
    606  1.4  riastrad 	struct radeon_device *rdev = dev_get_drvdata(dev);
    607  1.4  riastrad 	int err;
    608  1.4  riastrad 	int value;
    609  1.4  riastrad 
    610  1.4  riastrad 	if(!rdev->asic->dpm.fan_ctrl_set_mode)
    611  1.4  riastrad 		return -EINVAL;
    612  1.4  riastrad 
    613  1.4  riastrad 	err = kstrtoint(buf, 10, &value);
    614  1.4  riastrad 	if (err)
    615  1.4  riastrad 		return err;
    616  1.4  riastrad 
    617  1.4  riastrad 	switch (value) {
    618  1.4  riastrad 	case 1: /* manual, percent-based */
    619  1.4  riastrad 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
    620  1.4  riastrad 		break;
    621  1.4  riastrad 	default: /* disable */
    622  1.4  riastrad 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
    623  1.4  riastrad 		break;
    624  1.4  riastrad 	}
    625  1.4  riastrad 
    626  1.4  riastrad 	return count;
    627  1.4  riastrad }
    628  1.4  riastrad 
    629  1.4  riastrad static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
    630  1.4  riastrad 					 struct device_attribute *attr,
    631  1.4  riastrad 					 char *buf)
    632  1.4  riastrad {
    633  1.4  riastrad 	return sprintf(buf, "%i\n", 0);
    634  1.4  riastrad }
    635  1.4  riastrad 
    636  1.4  riastrad static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
    637  1.4  riastrad 					 struct device_attribute *attr,
    638  1.4  riastrad 					 char *buf)
    639  1.4  riastrad {
    640  1.4  riastrad 	return sprintf(buf, "%i\n", 255);
    641  1.4  riastrad }
    642  1.4  riastrad 
    643  1.4  riastrad static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
    644  1.4  riastrad 				     struct device_attribute *attr,
    645  1.4  riastrad 				     const char *buf, size_t count)
    646  1.4  riastrad {
    647  1.4  riastrad 	struct radeon_device *rdev = dev_get_drvdata(dev);
    648  1.4  riastrad 	int err;
    649  1.4  riastrad 	u32 value;
    650  1.4  riastrad 
    651  1.4  riastrad 	err = kstrtou32(buf, 10, &value);
    652  1.4  riastrad 	if (err)
    653  1.4  riastrad 		return err;
    654  1.4  riastrad 
    655  1.4  riastrad 	value = (value * 100) / 255;
    656  1.4  riastrad 
    657  1.4  riastrad 	err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
    658  1.4  riastrad 	if (err)
    659  1.4  riastrad 		return err;
    660  1.4  riastrad 
    661  1.4  riastrad 	return count;
    662  1.4  riastrad }
    663  1.4  riastrad 
    664  1.4  riastrad static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
    665  1.4  riastrad 				     struct device_attribute *attr,
    666  1.4  riastrad 				     char *buf)
    667  1.4  riastrad {
    668  1.4  riastrad 	struct radeon_device *rdev = dev_get_drvdata(dev);
    669  1.4  riastrad 	int err;
    670  1.4  riastrad 	u32 speed;
    671  1.4  riastrad 
    672  1.4  riastrad 	err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
    673  1.4  riastrad 	if (err)
    674  1.4  riastrad 		return err;
    675  1.4  riastrad 
    676  1.4  riastrad 	speed = (speed * 255) / 100;
    677  1.4  riastrad 
    678  1.4  riastrad 	return sprintf(buf, "%i\n", speed);
    679  1.4  riastrad }
    680  1.4  riastrad 
    681  1.1  riastrad static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
    682  1.1  riastrad static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
    683  1.1  riastrad static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
    684  1.1  riastrad static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
    685  1.1  riastrad 		   radeon_get_dpm_forced_performance_level,
    686  1.1  riastrad 		   radeon_set_dpm_forced_performance_level);
    687  1.2  riastrad #endif
    688  1.1  riastrad 
    689  1.2  riastrad #ifndef __NetBSD__		/* XXX radeon hwmon */
    690  1.1  riastrad static ssize_t radeon_hwmon_show_temp(struct device *dev,
    691  1.1  riastrad 				      struct device_attribute *attr,
    692  1.1  riastrad 				      char *buf)
    693  1.1  riastrad {
    694  1.1  riastrad 	struct radeon_device *rdev = dev_get_drvdata(dev);
    695  1.1  riastrad 	struct drm_device *ddev = rdev->ddev;
    696  1.1  riastrad 	int temp;
    697  1.1  riastrad 
    698  1.1  riastrad 	/* Can't get temperature when the card is off */
    699  1.1  riastrad 	if  ((rdev->flags & RADEON_IS_PX) &&
    700  1.1  riastrad 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
    701  1.1  riastrad 		return -EINVAL;
    702  1.1  riastrad 
    703  1.1  riastrad 	if (rdev->asic->pm.get_temperature)
    704  1.1  riastrad 		temp = radeon_get_temperature(rdev);
    705  1.1  riastrad 	else
    706  1.1  riastrad 		temp = 0;
    707  1.1  riastrad 
    708  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
    709  1.1  riastrad }
    710  1.1  riastrad 
    711  1.1  riastrad static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
    712  1.1  riastrad 					     struct device_attribute *attr,
    713  1.1  riastrad 					     char *buf)
    714  1.1  riastrad {
    715  1.1  riastrad 	struct radeon_device *rdev = dev_get_drvdata(dev);
    716  1.1  riastrad 	int hyst = to_sensor_dev_attr(attr)->index;
    717  1.1  riastrad 	int temp;
    718  1.1  riastrad 
    719  1.1  riastrad 	if (hyst)
    720  1.1  riastrad 		temp = rdev->pm.dpm.thermal.min_temp;
    721  1.1  riastrad 	else
    722  1.1  riastrad 		temp = rdev->pm.dpm.thermal.max_temp;
    723  1.1  riastrad 
    724  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
    725  1.1  riastrad }
    726  1.1  riastrad 
    727  1.1  riastrad static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
    728  1.1  riastrad static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
    729  1.1  riastrad static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
    730  1.4  riastrad static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
    731  1.4  riastrad static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
    732  1.4  riastrad static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
    733  1.4  riastrad static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
    734  1.4  riastrad 
    735  1.1  riastrad 
    736  1.1  riastrad static struct attribute *hwmon_attributes[] = {
    737  1.1  riastrad 	&sensor_dev_attr_temp1_input.dev_attr.attr,
    738  1.1  riastrad 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
    739  1.1  riastrad 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
    740  1.4  riastrad 	&sensor_dev_attr_pwm1.dev_attr.attr,
    741  1.4  riastrad 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
    742  1.4  riastrad 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
    743  1.4  riastrad 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
    744  1.1  riastrad 	NULL
    745  1.1  riastrad };
    746  1.1  riastrad 
    747  1.1  riastrad static umode_t hwmon_attributes_visible(struct kobject *kobj,
    748  1.1  riastrad 					struct attribute *attr, int index)
    749  1.1  riastrad {
    750  1.6  riastrad 	struct device *dev = kobj_to_dev(kobj);
    751  1.1  riastrad 	struct radeon_device *rdev = dev_get_drvdata(dev);
    752  1.4  riastrad 	umode_t effective_mode = attr->mode;
    753  1.1  riastrad 
    754  1.4  riastrad 	/* Skip attributes if DPM is not enabled */
    755  1.1  riastrad 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
    756  1.1  riastrad 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
    757  1.4  riastrad 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
    758  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
    759  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
    760  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
    761  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
    762  1.4  riastrad 		return 0;
    763  1.4  riastrad 
    764  1.4  riastrad 	/* Skip fan attributes if fan is not present */
    765  1.4  riastrad 	if (rdev->pm.no_fan &&
    766  1.4  riastrad 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
    767  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
    768  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
    769  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
    770  1.4  riastrad 		return 0;
    771  1.4  riastrad 
    772  1.4  riastrad 	/* mask fan attributes if we have no bindings for this asic to expose */
    773  1.4  riastrad 	if ((!rdev->asic->dpm.get_fan_speed_percent &&
    774  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
    775  1.4  riastrad 	    (!rdev->asic->dpm.fan_ctrl_get_mode &&
    776  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
    777  1.4  riastrad 		effective_mode &= ~S_IRUGO;
    778  1.4  riastrad 
    779  1.4  riastrad 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
    780  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
    781  1.4  riastrad 	    (!rdev->asic->dpm.fan_ctrl_set_mode &&
    782  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
    783  1.4  riastrad 		effective_mode &= ~S_IWUSR;
    784  1.4  riastrad 
    785  1.4  riastrad 	/* hide max/min values if we can't both query and manage the fan */
    786  1.4  riastrad 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
    787  1.4  riastrad 	     !rdev->asic->dpm.get_fan_speed_percent) &&
    788  1.4  riastrad 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
    789  1.4  riastrad 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
    790  1.1  riastrad 		return 0;
    791  1.1  riastrad 
    792  1.4  riastrad 	return effective_mode;
    793  1.1  riastrad }
    794  1.1  riastrad 
    795  1.1  riastrad static const struct attribute_group hwmon_attrgroup = {
    796  1.1  riastrad 	.attrs = hwmon_attributes,
    797  1.1  riastrad 	.is_visible = hwmon_attributes_visible,
    798  1.1  riastrad };
    799  1.1  riastrad 
    800  1.1  riastrad static const struct attribute_group *hwmon_groups[] = {
    801  1.1  riastrad 	&hwmon_attrgroup,
    802  1.1  riastrad 	NULL
    803  1.1  riastrad };
    804  1.2  riastrad #endif
    805  1.1  riastrad 
    806  1.1  riastrad static int radeon_hwmon_init(struct radeon_device *rdev)
    807  1.1  riastrad {
    808  1.1  riastrad 	int err = 0;
    809  1.1  riastrad 
    810  1.2  riastrad #ifndef __NetBSD__		/* XXX radeon hwmon */
    811  1.1  riastrad 	switch (rdev->pm.int_thermal_type) {
    812  1.1  riastrad 	case THERMAL_TYPE_RV6XX:
    813  1.1  riastrad 	case THERMAL_TYPE_RV770:
    814  1.1  riastrad 	case THERMAL_TYPE_EVERGREEN:
    815  1.1  riastrad 	case THERMAL_TYPE_NI:
    816  1.1  riastrad 	case THERMAL_TYPE_SUMO:
    817  1.1  riastrad 	case THERMAL_TYPE_SI:
    818  1.1  riastrad 	case THERMAL_TYPE_CI:
    819  1.1  riastrad 	case THERMAL_TYPE_KV:
    820  1.1  riastrad 		if (rdev->asic->pm.get_temperature == NULL)
    821  1.1  riastrad 			return err;
    822  1.1  riastrad 		rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
    823  1.1  riastrad 									   "radeon", rdev,
    824  1.1  riastrad 									   hwmon_groups);
    825  1.1  riastrad 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
    826  1.1  riastrad 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
    827  1.1  riastrad 			dev_err(rdev->dev,
    828  1.1  riastrad 				"Unable to register hwmon device: %d\n", err);
    829  1.1  riastrad 		}
    830  1.1  riastrad 		break;
    831  1.1  riastrad 	default:
    832  1.1  riastrad 		break;
    833  1.1  riastrad 	}
    834  1.2  riastrad #endif
    835  1.1  riastrad 
    836  1.1  riastrad 	return err;
    837  1.1  riastrad }
    838  1.1  riastrad 
    839  1.1  riastrad static void radeon_hwmon_fini(struct radeon_device *rdev)
    840  1.1  riastrad {
    841  1.2  riastrad #ifndef __NetBSD__		/* XXX radeon hwmon */
    842  1.1  riastrad 	if (rdev->pm.int_hwmon_dev)
    843  1.1  riastrad 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
    844  1.2  riastrad #endif
    845  1.1  riastrad }
    846  1.1  riastrad 
    847  1.1  riastrad static void radeon_dpm_thermal_work_handler(struct work_struct *work)
    848  1.1  riastrad {
    849  1.1  riastrad 	struct radeon_device *rdev =
    850  1.1  riastrad 		container_of(work, struct radeon_device,
    851  1.1  riastrad 			     pm.dpm.thermal.work);
    852  1.1  riastrad 	/* switch to the thermal state */
    853  1.1  riastrad 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
    854  1.1  riastrad 
    855  1.1  riastrad 	if (!rdev->pm.dpm_enabled)
    856  1.1  riastrad 		return;
    857  1.1  riastrad 
    858  1.1  riastrad 	if (rdev->asic->pm.get_temperature) {
    859  1.1  riastrad 		int temp = radeon_get_temperature(rdev);
    860  1.1  riastrad 
    861  1.1  riastrad 		if (temp < rdev->pm.dpm.thermal.min_temp)
    862  1.1  riastrad 			/* switch back the user state */
    863  1.1  riastrad 			dpm_state = rdev->pm.dpm.user_state;
    864  1.1  riastrad 	} else {
    865  1.1  riastrad 		if (rdev->pm.dpm.thermal.high_to_low)
    866  1.1  riastrad 			/* switch back the user state */
    867  1.1  riastrad 			dpm_state = rdev->pm.dpm.user_state;
    868  1.1  riastrad 	}
    869  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
    870  1.1  riastrad 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
    871  1.1  riastrad 		rdev->pm.dpm.thermal_active = true;
    872  1.1  riastrad 	else
    873  1.1  riastrad 		rdev->pm.dpm.thermal_active = false;
    874  1.1  riastrad 	rdev->pm.dpm.state = dpm_state;
    875  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
    876  1.1  riastrad 
    877  1.1  riastrad 	radeon_pm_compute_clocks(rdev);
    878  1.1  riastrad }
    879  1.1  riastrad 
    880  1.4  riastrad static bool radeon_dpm_single_display(struct radeon_device *rdev)
    881  1.1  riastrad {
    882  1.1  riastrad 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
    883  1.1  riastrad 		true : false;
    884  1.1  riastrad 
    885  1.1  riastrad 	/* check if the vblank period is too short to adjust the mclk */
    886  1.1  riastrad 	if (single_display && rdev->asic->dpm.vblank_too_short) {
    887  1.1  riastrad 		if (radeon_dpm_vblank_too_short(rdev))
    888  1.1  riastrad 			single_display = false;
    889  1.1  riastrad 	}
    890  1.1  riastrad 
    891  1.4  riastrad 	/* 120hz tends to be problematic even if they are under the
    892  1.4  riastrad 	 * vblank limit.
    893  1.4  riastrad 	 */
    894  1.4  riastrad 	if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
    895  1.4  riastrad 		single_display = false;
    896  1.4  riastrad 
    897  1.4  riastrad 	return single_display;
    898  1.4  riastrad }
    899  1.4  riastrad 
    900  1.4  riastrad static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
    901  1.4  riastrad 						     enum radeon_pm_state_type dpm_state)
    902  1.4  riastrad {
    903  1.4  riastrad 	int i;
    904  1.4  riastrad 	struct radeon_ps *ps;
    905  1.4  riastrad 	u32 ui_class;
    906  1.4  riastrad 	bool single_display = radeon_dpm_single_display(rdev);
    907  1.4  riastrad 
    908  1.1  riastrad 	/* certain older asics have a separare 3D performance state,
    909  1.1  riastrad 	 * so try that first if the user selected performance
    910  1.1  riastrad 	 */
    911  1.1  riastrad 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
    912  1.1  riastrad 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
    913  1.1  riastrad 	/* balanced states don't exist at the moment */
    914  1.1  riastrad 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
    915  1.1  riastrad 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
    916  1.1  riastrad 
    917  1.1  riastrad restart_search:
    918  1.1  riastrad 	/* Pick the best power state based on current conditions */
    919  1.1  riastrad 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
    920  1.1  riastrad 		ps = &rdev->pm.dpm.ps[i];
    921  1.1  riastrad 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
    922  1.1  riastrad 		switch (dpm_state) {
    923  1.1  riastrad 		/* user states */
    924  1.1  riastrad 		case POWER_STATE_TYPE_BATTERY:
    925  1.1  riastrad 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
    926  1.1  riastrad 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
    927  1.1  riastrad 					if (single_display)
    928  1.1  riastrad 						return ps;
    929  1.1  riastrad 				} else
    930  1.1  riastrad 					return ps;
    931  1.1  riastrad 			}
    932  1.1  riastrad 			break;
    933  1.1  riastrad 		case POWER_STATE_TYPE_BALANCED:
    934  1.1  riastrad 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
    935  1.1  riastrad 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
    936  1.1  riastrad 					if (single_display)
    937  1.1  riastrad 						return ps;
    938  1.1  riastrad 				} else
    939  1.1  riastrad 					return ps;
    940  1.1  riastrad 			}
    941  1.1  riastrad 			break;
    942  1.1  riastrad 		case POWER_STATE_TYPE_PERFORMANCE:
    943  1.1  riastrad 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
    944  1.1  riastrad 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
    945  1.1  riastrad 					if (single_display)
    946  1.1  riastrad 						return ps;
    947  1.1  riastrad 				} else
    948  1.1  riastrad 					return ps;
    949  1.1  riastrad 			}
    950  1.1  riastrad 			break;
    951  1.1  riastrad 		/* internal states */
    952  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_UVD:
    953  1.1  riastrad 			if (rdev->pm.dpm.uvd_ps)
    954  1.1  riastrad 				return rdev->pm.dpm.uvd_ps;
    955  1.1  riastrad 			else
    956  1.1  riastrad 				break;
    957  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
    958  1.1  riastrad 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
    959  1.1  riastrad 				return ps;
    960  1.1  riastrad 			break;
    961  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
    962  1.1  riastrad 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
    963  1.1  riastrad 				return ps;
    964  1.1  riastrad 			break;
    965  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
    966  1.1  riastrad 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
    967  1.1  riastrad 				return ps;
    968  1.1  riastrad 			break;
    969  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
    970  1.1  riastrad 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
    971  1.1  riastrad 				return ps;
    972  1.1  riastrad 			break;
    973  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_BOOT:
    974  1.1  riastrad 			return rdev->pm.dpm.boot_ps;
    975  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
    976  1.1  riastrad 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
    977  1.1  riastrad 				return ps;
    978  1.1  riastrad 			break;
    979  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_ACPI:
    980  1.1  riastrad 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
    981  1.1  riastrad 				return ps;
    982  1.1  riastrad 			break;
    983  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_ULV:
    984  1.1  riastrad 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
    985  1.1  riastrad 				return ps;
    986  1.1  riastrad 			break;
    987  1.1  riastrad 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
    988  1.1  riastrad 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
    989  1.1  riastrad 				return ps;
    990  1.1  riastrad 			break;
    991  1.1  riastrad 		default:
    992  1.1  riastrad 			break;
    993  1.1  riastrad 		}
    994  1.1  riastrad 	}
    995  1.1  riastrad 	/* use a fallback state if we didn't match */
    996  1.1  riastrad 	switch (dpm_state) {
    997  1.1  riastrad 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
    998  1.1  riastrad 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
    999  1.1  riastrad 		goto restart_search;
   1000  1.1  riastrad 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
   1001  1.1  riastrad 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
   1002  1.1  riastrad 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
   1003  1.1  riastrad 		if (rdev->pm.dpm.uvd_ps) {
   1004  1.1  riastrad 			return rdev->pm.dpm.uvd_ps;
   1005  1.1  riastrad 		} else {
   1006  1.1  riastrad 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
   1007  1.1  riastrad 			goto restart_search;
   1008  1.1  riastrad 		}
   1009  1.1  riastrad 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
   1010  1.1  riastrad 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
   1011  1.1  riastrad 		goto restart_search;
   1012  1.1  riastrad 	case POWER_STATE_TYPE_INTERNAL_ACPI:
   1013  1.1  riastrad 		dpm_state = POWER_STATE_TYPE_BATTERY;
   1014  1.1  riastrad 		goto restart_search;
   1015  1.1  riastrad 	case POWER_STATE_TYPE_BATTERY:
   1016  1.1  riastrad 	case POWER_STATE_TYPE_BALANCED:
   1017  1.1  riastrad 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
   1018  1.1  riastrad 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
   1019  1.1  riastrad 		goto restart_search;
   1020  1.1  riastrad 	default:
   1021  1.1  riastrad 		break;
   1022  1.1  riastrad 	}
   1023  1.1  riastrad 
   1024  1.1  riastrad 	return NULL;
   1025  1.1  riastrad }
   1026  1.1  riastrad 
   1027  1.1  riastrad static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
   1028  1.1  riastrad {
   1029  1.1  riastrad 	int i;
   1030  1.1  riastrad 	struct radeon_ps *ps;
   1031  1.1  riastrad 	enum radeon_pm_state_type dpm_state;
   1032  1.1  riastrad 	int ret;
   1033  1.4  riastrad 	bool single_display = radeon_dpm_single_display(rdev);
   1034  1.1  riastrad 
   1035  1.1  riastrad 	/* if dpm init failed */
   1036  1.1  riastrad 	if (!rdev->pm.dpm_enabled)
   1037  1.1  riastrad 		return;
   1038  1.1  riastrad 
   1039  1.1  riastrad 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
   1040  1.1  riastrad 		/* add other state override checks here */
   1041  1.1  riastrad 		if ((!rdev->pm.dpm.thermal_active) &&
   1042  1.1  riastrad 		    (!rdev->pm.dpm.uvd_active))
   1043  1.1  riastrad 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
   1044  1.1  riastrad 	}
   1045  1.1  riastrad 	dpm_state = rdev->pm.dpm.state;
   1046  1.1  riastrad 
   1047  1.1  riastrad 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
   1048  1.1  riastrad 	if (ps)
   1049  1.1  riastrad 		rdev->pm.dpm.requested_ps = ps;
   1050  1.1  riastrad 	else
   1051  1.1  riastrad 		return;
   1052  1.1  riastrad 
   1053  1.1  riastrad 	/* no need to reprogram if nothing changed unless we are on BTC+ */
   1054  1.1  riastrad 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
   1055  1.1  riastrad 		/* vce just modifies an existing state so force a change */
   1056  1.1  riastrad 		if (ps->vce_active != rdev->pm.dpm.vce_active)
   1057  1.1  riastrad 			goto force;
   1058  1.4  riastrad 		/* user has made a display change (such as timing) */
   1059  1.4  riastrad 		if (rdev->pm.dpm.single_display != single_display)
   1060  1.4  riastrad 			goto force;
   1061  1.1  riastrad 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
   1062  1.1  riastrad 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
   1063  1.1  riastrad 			 * all we need to do is update the display configuration.
   1064  1.1  riastrad 			 */
   1065  1.1  riastrad 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
   1066  1.1  riastrad 				/* update display watermarks based on new power state */
   1067  1.1  riastrad 				radeon_bandwidth_update(rdev);
   1068  1.1  riastrad 				/* update displays */
   1069  1.1  riastrad 				radeon_dpm_display_configuration_changed(rdev);
   1070  1.1  riastrad 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
   1071  1.1  riastrad 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
   1072  1.1  riastrad 			}
   1073  1.1  riastrad 			return;
   1074  1.1  riastrad 		} else {
   1075  1.1  riastrad 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
   1076  1.1  riastrad 			 * nothing to do, if the num crtcs is > 1 and state is the same,
   1077  1.1  riastrad 			 * update display configuration.
   1078  1.1  riastrad 			 */
   1079  1.1  riastrad 			if (rdev->pm.dpm.new_active_crtcs ==
   1080  1.1  riastrad 			    rdev->pm.dpm.current_active_crtcs) {
   1081  1.1  riastrad 				return;
   1082  1.1  riastrad 			} else {
   1083  1.1  riastrad 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
   1084  1.1  riastrad 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
   1085  1.1  riastrad 					/* update display watermarks based on new power state */
   1086  1.1  riastrad 					radeon_bandwidth_update(rdev);
   1087  1.1  riastrad 					/* update displays */
   1088  1.1  riastrad 					radeon_dpm_display_configuration_changed(rdev);
   1089  1.1  riastrad 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
   1090  1.1  riastrad 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
   1091  1.1  riastrad 					return;
   1092  1.1  riastrad 				}
   1093  1.1  riastrad 			}
   1094  1.1  riastrad 		}
   1095  1.1  riastrad 	}
   1096  1.1  riastrad 
   1097  1.1  riastrad force:
   1098  1.1  riastrad 	if (radeon_dpm == 1) {
   1099  1.1  riastrad 		printk("switching from power state:\n");
   1100  1.1  riastrad 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
   1101  1.1  riastrad 		printk("switching to power state:\n");
   1102  1.1  riastrad 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
   1103  1.1  riastrad 	}
   1104  1.1  riastrad 
   1105  1.1  riastrad 	down_write(&rdev->pm.mclk_lock);
   1106  1.1  riastrad 	mutex_lock(&rdev->ring_lock);
   1107  1.1  riastrad 
   1108  1.1  riastrad 	/* update whether vce is active */
   1109  1.1  riastrad 	ps->vce_active = rdev->pm.dpm.vce_active;
   1110  1.1  riastrad 
   1111  1.1  riastrad 	ret = radeon_dpm_pre_set_power_state(rdev);
   1112  1.1  riastrad 	if (ret)
   1113  1.1  riastrad 		goto done;
   1114  1.1  riastrad 
   1115  1.1  riastrad 	/* update display watermarks based on new power state */
   1116  1.1  riastrad 	radeon_bandwidth_update(rdev);
   1117  1.1  riastrad 	/* update displays */
   1118  1.1  riastrad 	radeon_dpm_display_configuration_changed(rdev);
   1119  1.1  riastrad 
   1120  1.1  riastrad 	/* wait for the rings to drain */
   1121  1.1  riastrad 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
   1122  1.1  riastrad 		struct radeon_ring *ring = &rdev->ring[i];
   1123  1.1  riastrad 		if (ring->ready)
   1124  1.1  riastrad 			radeon_fence_wait_empty(rdev, i);
   1125  1.1  riastrad 	}
   1126  1.1  riastrad 
   1127  1.1  riastrad 	/* program the new power state */
   1128  1.1  riastrad 	radeon_dpm_set_power_state(rdev);
   1129  1.1  riastrad 
   1130  1.1  riastrad 	/* update current power state */
   1131  1.1  riastrad 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
   1132  1.1  riastrad 
   1133  1.1  riastrad 	radeon_dpm_post_set_power_state(rdev);
   1134  1.1  riastrad 
   1135  1.4  riastrad 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
   1136  1.4  riastrad 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
   1137  1.4  riastrad 	rdev->pm.dpm.single_display = single_display;
   1138  1.4  riastrad 
   1139  1.1  riastrad 	if (rdev->asic->dpm.force_performance_level) {
   1140  1.1  riastrad 		if (rdev->pm.dpm.thermal_active) {
   1141  1.1  riastrad 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
   1142  1.1  riastrad 			/* force low perf level for thermal */
   1143  1.1  riastrad 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
   1144  1.1  riastrad 			/* save the user's level */
   1145  1.1  riastrad 			rdev->pm.dpm.forced_level = level;
   1146  1.1  riastrad 		} else {
   1147  1.1  riastrad 			/* otherwise, user selected level */
   1148  1.1  riastrad 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
   1149  1.1  riastrad 		}
   1150  1.1  riastrad 	}
   1151  1.1  riastrad 
   1152  1.1  riastrad done:
   1153  1.1  riastrad 	mutex_unlock(&rdev->ring_lock);
   1154  1.1  riastrad 	up_write(&rdev->pm.mclk_lock);
   1155  1.1  riastrad }
   1156  1.1  riastrad 
   1157  1.1  riastrad void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
   1158  1.1  riastrad {
   1159  1.1  riastrad 	enum radeon_pm_state_type dpm_state;
   1160  1.1  riastrad 
   1161  1.1  riastrad 	if (rdev->asic->dpm.powergate_uvd) {
   1162  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
   1163  1.1  riastrad 		/* don't powergate anything if we
   1164  1.1  riastrad 		   have active but pause streams */
   1165  1.1  riastrad 		enable |= rdev->pm.dpm.sd > 0;
   1166  1.1  riastrad 		enable |= rdev->pm.dpm.hd > 0;
   1167  1.1  riastrad 		/* enable/disable UVD */
   1168  1.1  riastrad 		radeon_dpm_powergate_uvd(rdev, !enable);
   1169  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
   1170  1.1  riastrad 	} else {
   1171  1.1  riastrad 		if (enable) {
   1172  1.1  riastrad 			mutex_lock(&rdev->pm.mutex);
   1173  1.1  riastrad 			rdev->pm.dpm.uvd_active = true;
   1174  1.4  riastrad 			/* disable this for now */
   1175  1.4  riastrad #if 0
   1176  1.1  riastrad 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
   1177  1.1  riastrad 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
   1178  1.1  riastrad 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
   1179  1.1  riastrad 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
   1180  1.1  riastrad 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
   1181  1.1  riastrad 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
   1182  1.1  riastrad 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
   1183  1.1  riastrad 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
   1184  1.1  riastrad 			else
   1185  1.4  riastrad #endif
   1186  1.1  riastrad 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
   1187  1.1  riastrad 			rdev->pm.dpm.state = dpm_state;
   1188  1.1  riastrad 			mutex_unlock(&rdev->pm.mutex);
   1189  1.1  riastrad 		} else {
   1190  1.1  riastrad 			mutex_lock(&rdev->pm.mutex);
   1191  1.1  riastrad 			rdev->pm.dpm.uvd_active = false;
   1192  1.1  riastrad 			mutex_unlock(&rdev->pm.mutex);
   1193  1.1  riastrad 		}
   1194  1.1  riastrad 
   1195  1.1  riastrad 		radeon_pm_compute_clocks(rdev);
   1196  1.1  riastrad 	}
   1197  1.1  riastrad }
   1198  1.1  riastrad 
   1199  1.1  riastrad void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
   1200  1.1  riastrad {
   1201  1.1  riastrad 	if (enable) {
   1202  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
   1203  1.1  riastrad 		rdev->pm.dpm.vce_active = true;
   1204  1.1  riastrad 		/* XXX select vce level based on ring/task */
   1205  1.1  riastrad 		rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
   1206  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
   1207  1.1  riastrad 	} else {
   1208  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
   1209  1.1  riastrad 		rdev->pm.dpm.vce_active = false;
   1210  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
   1211  1.1  riastrad 	}
   1212  1.1  riastrad 
   1213  1.1  riastrad 	radeon_pm_compute_clocks(rdev);
   1214  1.1  riastrad }
   1215  1.1  riastrad 
   1216  1.1  riastrad static void radeon_pm_suspend_old(struct radeon_device *rdev)
   1217  1.1  riastrad {
   1218  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1219  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
   1220  1.1  riastrad 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
   1221  1.1  riastrad 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
   1222  1.1  riastrad 	}
   1223  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1224  1.1  riastrad 
   1225  1.1  riastrad 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
   1226  1.1  riastrad }
   1227  1.1  riastrad 
   1228  1.1  riastrad static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
   1229  1.1  riastrad {
   1230  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1231  1.1  riastrad 	/* disable dpm */
   1232  1.1  riastrad 	radeon_dpm_disable(rdev);
   1233  1.1  riastrad 	/* reset the power state */
   1234  1.1  riastrad 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
   1235  1.1  riastrad 	rdev->pm.dpm_enabled = false;
   1236  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1237  1.1  riastrad }
   1238  1.1  riastrad 
   1239  1.1  riastrad void radeon_pm_suspend(struct radeon_device *rdev)
   1240  1.1  riastrad {
   1241  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DPM)
   1242  1.1  riastrad 		radeon_pm_suspend_dpm(rdev);
   1243  1.1  riastrad 	else
   1244  1.1  riastrad 		radeon_pm_suspend_old(rdev);
   1245  1.1  riastrad }
   1246  1.1  riastrad 
   1247  1.1  riastrad static void radeon_pm_resume_old(struct radeon_device *rdev)
   1248  1.1  riastrad {
   1249  1.1  riastrad 	/* set up the default clocks if the MC ucode is loaded */
   1250  1.1  riastrad 	if ((rdev->family >= CHIP_BARTS) &&
   1251  1.1  riastrad 	    (rdev->family <= CHIP_CAYMAN) &&
   1252  1.1  riastrad 	    rdev->mc_fw) {
   1253  1.1  riastrad 		if (rdev->pm.default_vddc)
   1254  1.1  riastrad 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
   1255  1.1  riastrad 						SET_VOLTAGE_TYPE_ASIC_VDDC);
   1256  1.1  riastrad 		if (rdev->pm.default_vddci)
   1257  1.1  riastrad 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
   1258  1.1  riastrad 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
   1259  1.1  riastrad 		if (rdev->pm.default_sclk)
   1260  1.1  riastrad 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
   1261  1.1  riastrad 		if (rdev->pm.default_mclk)
   1262  1.1  riastrad 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
   1263  1.1  riastrad 	}
   1264  1.1  riastrad 	/* asic init will reset the default power state */
   1265  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1266  1.1  riastrad 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
   1267  1.1  riastrad 	rdev->pm.current_clock_mode_index = 0;
   1268  1.1  riastrad 	rdev->pm.current_sclk = rdev->pm.default_sclk;
   1269  1.1  riastrad 	rdev->pm.current_mclk = rdev->pm.default_mclk;
   1270  1.1  riastrad 	if (rdev->pm.power_state) {
   1271  1.1  riastrad 		rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
   1272  1.1  riastrad 		rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
   1273  1.1  riastrad 	}
   1274  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
   1275  1.1  riastrad 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
   1276  1.1  riastrad 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
   1277  1.1  riastrad 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
   1278  1.1  riastrad 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
   1279  1.1  riastrad 	}
   1280  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1281  1.1  riastrad 	radeon_pm_compute_clocks(rdev);
   1282  1.1  riastrad }
   1283  1.1  riastrad 
   1284  1.1  riastrad static void radeon_pm_resume_dpm(struct radeon_device *rdev)
   1285  1.1  riastrad {
   1286  1.1  riastrad 	int ret;
   1287  1.1  riastrad 
   1288  1.1  riastrad 	/* asic init will reset to the boot state */
   1289  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1290  1.1  riastrad 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
   1291  1.1  riastrad 	radeon_dpm_setup_asic(rdev);
   1292  1.1  riastrad 	ret = radeon_dpm_enable(rdev);
   1293  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1294  1.1  riastrad 	if (ret)
   1295  1.1  riastrad 		goto dpm_resume_fail;
   1296  1.1  riastrad 	rdev->pm.dpm_enabled = true;
   1297  1.1  riastrad 	return;
   1298  1.1  riastrad 
   1299  1.1  riastrad dpm_resume_fail:
   1300  1.1  riastrad 	DRM_ERROR("radeon: dpm resume failed\n");
   1301  1.1  riastrad 	if ((rdev->family >= CHIP_BARTS) &&
   1302  1.1  riastrad 	    (rdev->family <= CHIP_CAYMAN) &&
   1303  1.1  riastrad 	    rdev->mc_fw) {
   1304  1.1  riastrad 		if (rdev->pm.default_vddc)
   1305  1.1  riastrad 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
   1306  1.1  riastrad 						SET_VOLTAGE_TYPE_ASIC_VDDC);
   1307  1.1  riastrad 		if (rdev->pm.default_vddci)
   1308  1.1  riastrad 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
   1309  1.1  riastrad 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
   1310  1.1  riastrad 		if (rdev->pm.default_sclk)
   1311  1.1  riastrad 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
   1312  1.1  riastrad 		if (rdev->pm.default_mclk)
   1313  1.1  riastrad 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
   1314  1.1  riastrad 	}
   1315  1.1  riastrad }
   1316  1.1  riastrad 
   1317  1.1  riastrad void radeon_pm_resume(struct radeon_device *rdev)
   1318  1.1  riastrad {
   1319  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DPM)
   1320  1.1  riastrad 		radeon_pm_resume_dpm(rdev);
   1321  1.1  riastrad 	else
   1322  1.1  riastrad 		radeon_pm_resume_old(rdev);
   1323  1.1  riastrad }
   1324  1.1  riastrad 
   1325  1.1  riastrad static int radeon_pm_init_old(struct radeon_device *rdev)
   1326  1.1  riastrad {
   1327  1.1  riastrad 	int ret;
   1328  1.1  riastrad 
   1329  1.1  riastrad 	rdev->pm.profile = PM_PROFILE_DEFAULT;
   1330  1.1  riastrad 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
   1331  1.1  riastrad 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
   1332  1.1  riastrad 	rdev->pm.dynpm_can_upclock = true;
   1333  1.1  riastrad 	rdev->pm.dynpm_can_downclock = true;
   1334  1.1  riastrad 	rdev->pm.default_sclk = rdev->clock.default_sclk;
   1335  1.1  riastrad 	rdev->pm.default_mclk = rdev->clock.default_mclk;
   1336  1.1  riastrad 	rdev->pm.current_sclk = rdev->clock.default_sclk;
   1337  1.1  riastrad 	rdev->pm.current_mclk = rdev->clock.default_mclk;
   1338  1.1  riastrad 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
   1339  1.1  riastrad 
   1340  1.1  riastrad 	if (rdev->bios) {
   1341  1.1  riastrad 		if (rdev->is_atom_bios)
   1342  1.1  riastrad 			radeon_atombios_get_power_modes(rdev);
   1343  1.1  riastrad 		else
   1344  1.1  riastrad 			radeon_combios_get_power_modes(rdev);
   1345  1.1  riastrad 		radeon_pm_print_states(rdev);
   1346  1.1  riastrad 		radeon_pm_init_profile(rdev);
   1347  1.1  riastrad 		/* set up the default clocks if the MC ucode is loaded */
   1348  1.1  riastrad 		if ((rdev->family >= CHIP_BARTS) &&
   1349  1.1  riastrad 		    (rdev->family <= CHIP_CAYMAN) &&
   1350  1.1  riastrad 		    rdev->mc_fw) {
   1351  1.1  riastrad 			if (rdev->pm.default_vddc)
   1352  1.1  riastrad 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
   1353  1.1  riastrad 							SET_VOLTAGE_TYPE_ASIC_VDDC);
   1354  1.1  riastrad 			if (rdev->pm.default_vddci)
   1355  1.1  riastrad 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
   1356  1.1  riastrad 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
   1357  1.1  riastrad 			if (rdev->pm.default_sclk)
   1358  1.1  riastrad 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
   1359  1.1  riastrad 			if (rdev->pm.default_mclk)
   1360  1.1  riastrad 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
   1361  1.1  riastrad 		}
   1362  1.1  riastrad 	}
   1363  1.1  riastrad 
   1364  1.1  riastrad 	/* set up the internal thermal sensor if applicable */
   1365  1.1  riastrad 	ret = radeon_hwmon_init(rdev);
   1366  1.1  riastrad 	if (ret)
   1367  1.1  riastrad 		return ret;
   1368  1.1  riastrad 
   1369  1.1  riastrad 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
   1370  1.1  riastrad 
   1371  1.2  riastrad #ifndef __NetBSD__		/* XXX radeon power */
   1372  1.1  riastrad 	if (rdev->pm.num_power_states > 1) {
   1373  1.1  riastrad 		if (radeon_debugfs_pm_init(rdev)) {
   1374  1.1  riastrad 			DRM_ERROR("Failed to register debugfs file for PM!\n");
   1375  1.1  riastrad 		}
   1376  1.1  riastrad 
   1377  1.1  riastrad 		DRM_INFO("radeon: power management initialized\n");
   1378  1.1  riastrad 	}
   1379  1.2  riastrad #endif
   1380  1.1  riastrad 
   1381  1.1  riastrad 	return 0;
   1382  1.1  riastrad }
   1383  1.1  riastrad 
   1384  1.1  riastrad static void radeon_dpm_print_power_states(struct radeon_device *rdev)
   1385  1.1  riastrad {
   1386  1.1  riastrad 	int i;
   1387  1.1  riastrad 
   1388  1.1  riastrad 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
   1389  1.1  riastrad 		printk("== power state %d ==\n", i);
   1390  1.1  riastrad 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
   1391  1.1  riastrad 	}
   1392  1.1  riastrad }
   1393  1.1  riastrad 
   1394  1.1  riastrad static int radeon_pm_init_dpm(struct radeon_device *rdev)
   1395  1.1  riastrad {
   1396  1.1  riastrad 	int ret;
   1397  1.1  riastrad 
   1398  1.1  riastrad 	/* default to balanced state */
   1399  1.1  riastrad 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
   1400  1.1  riastrad 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
   1401  1.1  riastrad 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
   1402  1.1  riastrad 	rdev->pm.default_sclk = rdev->clock.default_sclk;
   1403  1.1  riastrad 	rdev->pm.default_mclk = rdev->clock.default_mclk;
   1404  1.1  riastrad 	rdev->pm.current_sclk = rdev->clock.default_sclk;
   1405  1.1  riastrad 	rdev->pm.current_mclk = rdev->clock.default_mclk;
   1406  1.1  riastrad 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
   1407  1.1  riastrad 
   1408  1.1  riastrad 	if (rdev->bios && rdev->is_atom_bios)
   1409  1.1  riastrad 		radeon_atombios_get_power_modes(rdev);
   1410  1.1  riastrad 	else
   1411  1.1  riastrad 		return -EINVAL;
   1412  1.1  riastrad 
   1413  1.1  riastrad 	/* set up the internal thermal sensor if applicable */
   1414  1.1  riastrad 	ret = radeon_hwmon_init(rdev);
   1415  1.1  riastrad 	if (ret)
   1416  1.1  riastrad 		return ret;
   1417  1.1  riastrad 
   1418  1.1  riastrad 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
   1419  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1420  1.1  riastrad 	radeon_dpm_init(rdev);
   1421  1.1  riastrad 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
   1422  1.1  riastrad 	if (radeon_dpm == 1)
   1423  1.1  riastrad 		radeon_dpm_print_power_states(rdev);
   1424  1.1  riastrad 	radeon_dpm_setup_asic(rdev);
   1425  1.1  riastrad 	ret = radeon_dpm_enable(rdev);
   1426  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1427  1.1  riastrad 	if (ret)
   1428  1.1  riastrad 		goto dpm_failed;
   1429  1.1  riastrad 	rdev->pm.dpm_enabled = true;
   1430  1.1  riastrad 
   1431  1.1  riastrad 	if (radeon_debugfs_pm_init(rdev)) {
   1432  1.1  riastrad 		DRM_ERROR("Failed to register debugfs file for dpm!\n");
   1433  1.1  riastrad 	}
   1434  1.1  riastrad 
   1435  1.1  riastrad 	DRM_INFO("radeon: dpm initialized\n");
   1436  1.1  riastrad 
   1437  1.1  riastrad 	return 0;
   1438  1.1  riastrad 
   1439  1.1  riastrad dpm_failed:
   1440  1.1  riastrad 	rdev->pm.dpm_enabled = false;
   1441  1.1  riastrad 	if ((rdev->family >= CHIP_BARTS) &&
   1442  1.1  riastrad 	    (rdev->family <= CHIP_CAYMAN) &&
   1443  1.1  riastrad 	    rdev->mc_fw) {
   1444  1.1  riastrad 		if (rdev->pm.default_vddc)
   1445  1.1  riastrad 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
   1446  1.1  riastrad 						SET_VOLTAGE_TYPE_ASIC_VDDC);
   1447  1.1  riastrad 		if (rdev->pm.default_vddci)
   1448  1.1  riastrad 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
   1449  1.1  riastrad 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
   1450  1.1  riastrad 		if (rdev->pm.default_sclk)
   1451  1.1  riastrad 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
   1452  1.1  riastrad 		if (rdev->pm.default_mclk)
   1453  1.1  riastrad 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
   1454  1.1  riastrad 	}
   1455  1.1  riastrad 	DRM_ERROR("radeon: dpm initialization failed\n");
   1456  1.1  riastrad 	return ret;
   1457  1.1  riastrad }
   1458  1.1  riastrad 
   1459  1.4  riastrad struct radeon_dpm_quirk {
   1460  1.4  riastrad 	u32 chip_vendor;
   1461  1.4  riastrad 	u32 chip_device;
   1462  1.4  riastrad 	u32 subsys_vendor;
   1463  1.4  riastrad 	u32 subsys_device;
   1464  1.4  riastrad };
   1465  1.4  riastrad 
   1466  1.4  riastrad /* cards with dpm stability problems */
   1467  1.4  riastrad static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
   1468  1.4  riastrad 	/* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
   1469  1.4  riastrad 	{ PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
   1470  1.4  riastrad 	/* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
   1471  1.4  riastrad 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
   1472  1.4  riastrad 	{ 0, 0, 0, 0 },
   1473  1.4  riastrad };
   1474  1.4  riastrad 
   1475  1.1  riastrad int radeon_pm_init(struct radeon_device *rdev)
   1476  1.1  riastrad {
   1477  1.4  riastrad 	struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
   1478  1.4  riastrad 	bool disable_dpm = false;
   1479  1.4  riastrad 
   1480  1.4  riastrad 	/* Apply dpm quirks */
   1481  1.4  riastrad 	while (p && p->chip_device != 0) {
   1482  1.4  riastrad 		if (rdev->pdev->vendor == p->chip_vendor &&
   1483  1.4  riastrad 		    rdev->pdev->device == p->chip_device &&
   1484  1.4  riastrad 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
   1485  1.4  riastrad 		    rdev->pdev->subsystem_device == p->subsys_device) {
   1486  1.4  riastrad 			disable_dpm = true;
   1487  1.4  riastrad 			break;
   1488  1.4  riastrad 		}
   1489  1.4  riastrad 		++p;
   1490  1.4  riastrad 	}
   1491  1.4  riastrad 
   1492  1.1  riastrad 	/* enable dpm on rv6xx+ */
   1493  1.1  riastrad 	switch (rdev->family) {
   1494  1.1  riastrad 	case CHIP_RV610:
   1495  1.1  riastrad 	case CHIP_RV630:
   1496  1.1  riastrad 	case CHIP_RV620:
   1497  1.1  riastrad 	case CHIP_RV635:
   1498  1.1  riastrad 	case CHIP_RV670:
   1499  1.1  riastrad 	case CHIP_RS780:
   1500  1.1  riastrad 	case CHIP_RS880:
   1501  1.1  riastrad 	case CHIP_RV770:
   1502  1.1  riastrad 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
   1503  1.1  riastrad 		if (!rdev->rlc_fw)
   1504  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_PROFILE;
   1505  1.1  riastrad 		else if ((rdev->family >= CHIP_RV770) &&
   1506  1.1  riastrad 			 (!(rdev->flags & RADEON_IS_IGP)) &&
   1507  1.1  riastrad 			 (!rdev->smc_fw))
   1508  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_PROFILE;
   1509  1.1  riastrad 		else if (radeon_dpm == 1)
   1510  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_DPM;
   1511  1.1  riastrad 		else
   1512  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_PROFILE;
   1513  1.1  riastrad 		break;
   1514  1.1  riastrad 	case CHIP_RV730:
   1515  1.1  riastrad 	case CHIP_RV710:
   1516  1.1  riastrad 	case CHIP_RV740:
   1517  1.1  riastrad 	case CHIP_CEDAR:
   1518  1.1  riastrad 	case CHIP_REDWOOD:
   1519  1.1  riastrad 	case CHIP_JUNIPER:
   1520  1.1  riastrad 	case CHIP_CYPRESS:
   1521  1.1  riastrad 	case CHIP_HEMLOCK:
   1522  1.1  riastrad 	case CHIP_PALM:
   1523  1.1  riastrad 	case CHIP_SUMO:
   1524  1.1  riastrad 	case CHIP_SUMO2:
   1525  1.4  riastrad 	case CHIP_BARTS:
   1526  1.4  riastrad 	case CHIP_TURKS:
   1527  1.4  riastrad 	case CHIP_CAICOS:
   1528  1.4  riastrad 	case CHIP_CAYMAN:
   1529  1.1  riastrad 	case CHIP_ARUBA:
   1530  1.1  riastrad 	case CHIP_TAHITI:
   1531  1.1  riastrad 	case CHIP_PITCAIRN:
   1532  1.1  riastrad 	case CHIP_VERDE:
   1533  1.1  riastrad 	case CHIP_OLAND:
   1534  1.1  riastrad 	case CHIP_HAINAN:
   1535  1.1  riastrad 	case CHIP_BONAIRE:
   1536  1.1  riastrad 	case CHIP_KABINI:
   1537  1.1  riastrad 	case CHIP_KAVERI:
   1538  1.1  riastrad 	case CHIP_HAWAII:
   1539  1.1  riastrad 	case CHIP_MULLINS:
   1540  1.1  riastrad 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
   1541  1.1  riastrad 		if (!rdev->rlc_fw)
   1542  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_PROFILE;
   1543  1.1  riastrad 		else if ((rdev->family >= CHIP_RV770) &&
   1544  1.1  riastrad 			 (!(rdev->flags & RADEON_IS_IGP)) &&
   1545  1.1  riastrad 			 (!rdev->smc_fw))
   1546  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_PROFILE;
   1547  1.4  riastrad 		else if (disable_dpm && (radeon_dpm == -1))
   1548  1.4  riastrad 			rdev->pm.pm_method = PM_METHOD_PROFILE;
   1549  1.1  riastrad 		else if (radeon_dpm == 0)
   1550  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_PROFILE;
   1551  1.1  riastrad 		else
   1552  1.1  riastrad 			rdev->pm.pm_method = PM_METHOD_DPM;
   1553  1.1  riastrad 		break;
   1554  1.1  riastrad 	default:
   1555  1.1  riastrad 		/* default to profile method */
   1556  1.1  riastrad 		rdev->pm.pm_method = PM_METHOD_PROFILE;
   1557  1.1  riastrad 		break;
   1558  1.1  riastrad 	}
   1559  1.1  riastrad 
   1560  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DPM)
   1561  1.1  riastrad 		return radeon_pm_init_dpm(rdev);
   1562  1.1  riastrad 	else
   1563  1.1  riastrad 		return radeon_pm_init_old(rdev);
   1564  1.1  riastrad }
   1565  1.1  riastrad 
   1566  1.1  riastrad int radeon_pm_late_init(struct radeon_device *rdev)
   1567  1.1  riastrad {
   1568  1.1  riastrad 	int ret = 0;
   1569  1.1  riastrad 
   1570  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
   1571  1.4  riastrad 		if (rdev->pm.dpm_enabled) {
   1572  1.5  riastrad #ifndef __NetBSD__		/* XXX radeon sysfs */
   1573  1.4  riastrad 			if (!rdev->pm.sysfs_initialized) {
   1574  1.4  riastrad 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
   1575  1.4  riastrad 				if (ret)
   1576  1.4  riastrad 					DRM_ERROR("failed to create device file for dpm state\n");
   1577  1.4  riastrad 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
   1578  1.4  riastrad 				if (ret)
   1579  1.4  riastrad 					DRM_ERROR("failed to create device file for dpm state\n");
   1580  1.4  riastrad 				/* XXX: these are noops for dpm but are here for backwards compat */
   1581  1.4  riastrad 				ret = device_create_file(rdev->dev, &dev_attr_power_profile);
   1582  1.4  riastrad 				if (ret)
   1583  1.4  riastrad 					DRM_ERROR("failed to create device file for power profile\n");
   1584  1.4  riastrad 				ret = device_create_file(rdev->dev, &dev_attr_power_method);
   1585  1.4  riastrad 				if (ret)
   1586  1.4  riastrad 					DRM_ERROR("failed to create device file for power method\n");
   1587  1.4  riastrad 				rdev->pm.sysfs_initialized = true;
   1588  1.4  riastrad 			}
   1589  1.5  riastrad #endif
   1590  1.4  riastrad 
   1591  1.4  riastrad 			mutex_lock(&rdev->pm.mutex);
   1592  1.4  riastrad 			ret = radeon_dpm_late_enable(rdev);
   1593  1.4  riastrad 			mutex_unlock(&rdev->pm.mutex);
   1594  1.4  riastrad 			if (ret) {
   1595  1.4  riastrad 				rdev->pm.dpm_enabled = false;
   1596  1.4  riastrad 				DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
   1597  1.4  riastrad 			} else {
   1598  1.4  riastrad 				/* set the dpm state for PX since there won't be
   1599  1.4  riastrad 				 * a modeset to call this.
   1600  1.4  riastrad 				 */
   1601  1.4  riastrad 				radeon_pm_compute_clocks(rdev);
   1602  1.4  riastrad 			}
   1603  1.4  riastrad 		}
   1604  1.4  riastrad 	} else {
   1605  1.4  riastrad 		if ((rdev->pm.num_power_states > 1) &&
   1606  1.4  riastrad 		    (!rdev->pm.sysfs_initialized)) {
   1607  1.5  riastrad #ifndef __NetBSD__	     /* XXX radeon sysfs */
   1608  1.4  riastrad 			/* where's the best place to put these? */
   1609  1.4  riastrad 			ret = device_create_file(rdev->dev, &dev_attr_power_profile);
   1610  1.4  riastrad 			if (ret)
   1611  1.4  riastrad 				DRM_ERROR("failed to create device file for power profile\n");
   1612  1.4  riastrad 			ret = device_create_file(rdev->dev, &dev_attr_power_method);
   1613  1.4  riastrad 			if (ret)
   1614  1.4  riastrad 				DRM_ERROR("failed to create device file for power method\n");
   1615  1.4  riastrad 			if (!ret)
   1616  1.4  riastrad 				rdev->pm.sysfs_initialized = true;
   1617  1.5  riastrad #endif
   1618  1.4  riastrad 		}
   1619  1.1  riastrad 	}
   1620  1.1  riastrad 	return ret;
   1621  1.1  riastrad }
   1622  1.1  riastrad 
   1623  1.1  riastrad static void radeon_pm_fini_old(struct radeon_device *rdev)
   1624  1.1  riastrad {
   1625  1.1  riastrad 	if (rdev->pm.num_power_states > 1) {
   1626  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
   1627  1.1  riastrad 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
   1628  1.1  riastrad 			rdev->pm.profile = PM_PROFILE_DEFAULT;
   1629  1.1  riastrad 			radeon_pm_update_profile(rdev);
   1630  1.1  riastrad 			radeon_pm_set_clocks(rdev);
   1631  1.1  riastrad 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
   1632  1.1  riastrad 			/* reset default clocks */
   1633  1.1  riastrad 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
   1634  1.1  riastrad 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
   1635  1.1  riastrad 			radeon_pm_set_clocks(rdev);
   1636  1.1  riastrad 		}
   1637  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
   1638  1.1  riastrad 
   1639  1.1  riastrad 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
   1640  1.1  riastrad 
   1641  1.2  riastrad #ifndef __NetBSD__		/* XXX radeon power */
   1642  1.1  riastrad 		device_remove_file(rdev->dev, &dev_attr_power_profile);
   1643  1.1  riastrad 		device_remove_file(rdev->dev, &dev_attr_power_method);
   1644  1.2  riastrad #endif
   1645  1.1  riastrad 	}
   1646  1.1  riastrad 
   1647  1.1  riastrad 	radeon_hwmon_fini(rdev);
   1648  1.4  riastrad 	kfree(rdev->pm.power_state);
   1649  1.1  riastrad }
   1650  1.1  riastrad 
   1651  1.1  riastrad static void radeon_pm_fini_dpm(struct radeon_device *rdev)
   1652  1.1  riastrad {
   1653  1.1  riastrad 	if (rdev->pm.num_power_states > 1) {
   1654  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
   1655  1.1  riastrad 		radeon_dpm_disable(rdev);
   1656  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
   1657  1.1  riastrad 
   1658  1.2  riastrad #ifndef __NetBSD__		/* XXX radeon power */
   1659  1.1  riastrad 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
   1660  1.1  riastrad 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
   1661  1.1  riastrad 		/* XXX backwards compat */
   1662  1.1  riastrad 		device_remove_file(rdev->dev, &dev_attr_power_profile);
   1663  1.1  riastrad 		device_remove_file(rdev->dev, &dev_attr_power_method);
   1664  1.2  riastrad #endif
   1665  1.1  riastrad 	}
   1666  1.1  riastrad 	radeon_dpm_fini(rdev);
   1667  1.1  riastrad 
   1668  1.1  riastrad 	radeon_hwmon_fini(rdev);
   1669  1.4  riastrad 	kfree(rdev->pm.power_state);
   1670  1.1  riastrad }
   1671  1.1  riastrad 
   1672  1.1  riastrad void radeon_pm_fini(struct radeon_device *rdev)
   1673  1.1  riastrad {
   1674  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DPM)
   1675  1.1  riastrad 		radeon_pm_fini_dpm(rdev);
   1676  1.1  riastrad 	else
   1677  1.1  riastrad 		radeon_pm_fini_old(rdev);
   1678  1.1  riastrad }
   1679  1.1  riastrad 
   1680  1.1  riastrad static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
   1681  1.1  riastrad {
   1682  1.1  riastrad 	struct drm_device *ddev = rdev->ddev;
   1683  1.1  riastrad 	struct drm_crtc *crtc;
   1684  1.1  riastrad 	struct radeon_crtc *radeon_crtc;
   1685  1.1  riastrad 
   1686  1.1  riastrad 	if (rdev->pm.num_power_states < 2)
   1687  1.1  riastrad 		return;
   1688  1.1  riastrad 
   1689  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1690  1.1  riastrad 
   1691  1.1  riastrad 	rdev->pm.active_crtcs = 0;
   1692  1.1  riastrad 	rdev->pm.active_crtc_count = 0;
   1693  1.1  riastrad 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
   1694  1.1  riastrad 		list_for_each_entry(crtc,
   1695  1.1  riastrad 				    &ddev->mode_config.crtc_list, head) {
   1696  1.1  riastrad 			radeon_crtc = to_radeon_crtc(crtc);
   1697  1.1  riastrad 			if (radeon_crtc->enabled) {
   1698  1.1  riastrad 				rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
   1699  1.1  riastrad 				rdev->pm.active_crtc_count++;
   1700  1.1  riastrad 			}
   1701  1.1  riastrad 		}
   1702  1.1  riastrad 	}
   1703  1.1  riastrad 
   1704  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
   1705  1.1  riastrad 		radeon_pm_update_profile(rdev);
   1706  1.1  riastrad 		radeon_pm_set_clocks(rdev);
   1707  1.1  riastrad 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
   1708  1.1  riastrad 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
   1709  1.1  riastrad 			if (rdev->pm.active_crtc_count > 1) {
   1710  1.1  riastrad 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
   1711  1.1  riastrad 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
   1712  1.1  riastrad 
   1713  1.1  riastrad 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
   1714  1.1  riastrad 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
   1715  1.1  riastrad 					radeon_pm_get_dynpm_state(rdev);
   1716  1.1  riastrad 					radeon_pm_set_clocks(rdev);
   1717  1.1  riastrad 
   1718  1.1  riastrad 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
   1719  1.1  riastrad 				}
   1720  1.1  riastrad 			} else if (rdev->pm.active_crtc_count == 1) {
   1721  1.1  riastrad 				/* TODO: Increase clocks if needed for current mode */
   1722  1.1  riastrad 
   1723  1.1  riastrad 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
   1724  1.1  riastrad 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
   1725  1.1  riastrad 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
   1726  1.1  riastrad 					radeon_pm_get_dynpm_state(rdev);
   1727  1.1  riastrad 					radeon_pm_set_clocks(rdev);
   1728  1.1  riastrad 
   1729  1.1  riastrad 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
   1730  1.1  riastrad 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
   1731  1.1  riastrad 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
   1732  1.1  riastrad 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
   1733  1.1  riastrad 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
   1734  1.1  riastrad 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
   1735  1.1  riastrad 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
   1736  1.1  riastrad 				}
   1737  1.1  riastrad 			} else { /* count == 0 */
   1738  1.1  riastrad 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
   1739  1.1  riastrad 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
   1740  1.1  riastrad 
   1741  1.1  riastrad 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
   1742  1.1  riastrad 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
   1743  1.1  riastrad 					radeon_pm_get_dynpm_state(rdev);
   1744  1.1  riastrad 					radeon_pm_set_clocks(rdev);
   1745  1.1  riastrad 				}
   1746  1.1  riastrad 			}
   1747  1.1  riastrad 		}
   1748  1.1  riastrad 	}
   1749  1.1  riastrad 
   1750  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1751  1.1  riastrad }
   1752  1.1  riastrad 
   1753  1.1  riastrad static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
   1754  1.1  riastrad {
   1755  1.1  riastrad 	struct drm_device *ddev = rdev->ddev;
   1756  1.1  riastrad 	struct drm_crtc *crtc;
   1757  1.1  riastrad 	struct radeon_crtc *radeon_crtc;
   1758  1.1  riastrad 
   1759  1.1  riastrad 	if (!rdev->pm.dpm_enabled)
   1760  1.1  riastrad 		return;
   1761  1.1  riastrad 
   1762  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1763  1.1  riastrad 
   1764  1.1  riastrad 	/* update active crtc counts */
   1765  1.1  riastrad 	rdev->pm.dpm.new_active_crtcs = 0;
   1766  1.1  riastrad 	rdev->pm.dpm.new_active_crtc_count = 0;
   1767  1.1  riastrad 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
   1768  1.1  riastrad 		list_for_each_entry(crtc,
   1769  1.1  riastrad 				    &ddev->mode_config.crtc_list, head) {
   1770  1.1  riastrad 			radeon_crtc = to_radeon_crtc(crtc);
   1771  1.1  riastrad 			if (crtc->enabled) {
   1772  1.1  riastrad 				rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
   1773  1.1  riastrad 				rdev->pm.dpm.new_active_crtc_count++;
   1774  1.1  riastrad 			}
   1775  1.1  riastrad 		}
   1776  1.1  riastrad 	}
   1777  1.1  riastrad 
   1778  1.1  riastrad 	/* update battery/ac status */
   1779  1.1  riastrad 	if (power_supply_is_system_supplied() > 0)
   1780  1.1  riastrad 		rdev->pm.dpm.ac_power = true;
   1781  1.1  riastrad 	else
   1782  1.1  riastrad 		rdev->pm.dpm.ac_power = false;
   1783  1.1  riastrad 
   1784  1.1  riastrad 	radeon_dpm_change_power_state_locked(rdev);
   1785  1.1  riastrad 
   1786  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1787  1.1  riastrad 
   1788  1.1  riastrad }
   1789  1.1  riastrad 
   1790  1.1  riastrad void radeon_pm_compute_clocks(struct radeon_device *rdev)
   1791  1.1  riastrad {
   1792  1.1  riastrad 	if (rdev->pm.pm_method == PM_METHOD_DPM)
   1793  1.1  riastrad 		radeon_pm_compute_clocks_dpm(rdev);
   1794  1.1  riastrad 	else
   1795  1.1  riastrad 		radeon_pm_compute_clocks_old(rdev);
   1796  1.1  riastrad }
   1797  1.1  riastrad 
   1798  1.1  riastrad static bool radeon_pm_in_vbl(struct radeon_device *rdev)
   1799  1.1  riastrad {
   1800  1.1  riastrad 	int  crtc, vpos, hpos, vbl_status;
   1801  1.1  riastrad 	bool in_vbl = true;
   1802  1.1  riastrad 
   1803  1.1  riastrad 	/* Iterate over all active crtc's. All crtc's must be in vblank,
   1804  1.1  riastrad 	 * otherwise return in_vbl == false.
   1805  1.1  riastrad 	 */
   1806  1.1  riastrad 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
   1807  1.1  riastrad 		if (rdev->pm.active_crtcs & (1 << crtc)) {
   1808  1.4  riastrad 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev,
   1809  1.4  riastrad 								crtc,
   1810  1.4  riastrad 								USE_REAL_VBLANKSTART,
   1811  1.4  riastrad 								&vpos, &hpos, NULL, NULL,
   1812  1.4  riastrad 								&rdev->mode_info.crtcs[crtc]->base.hwmode);
   1813  1.1  riastrad 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
   1814  1.4  riastrad 			    !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
   1815  1.1  riastrad 				in_vbl = false;
   1816  1.1  riastrad 		}
   1817  1.1  riastrad 	}
   1818  1.1  riastrad 
   1819  1.1  riastrad 	return in_vbl;
   1820  1.1  riastrad }
   1821  1.1  riastrad 
   1822  1.1  riastrad static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
   1823  1.1  riastrad {
   1824  1.1  riastrad 	u32 stat_crtc = 0;
   1825  1.1  riastrad 	bool in_vbl = radeon_pm_in_vbl(rdev);
   1826  1.1  riastrad 
   1827  1.6  riastrad 	if (!in_vbl)
   1828  1.1  riastrad 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
   1829  1.1  riastrad 			 finish ? "exit" : "entry");
   1830  1.1  riastrad 	return in_vbl;
   1831  1.1  riastrad }
   1832  1.1  riastrad 
   1833  1.1  riastrad static void radeon_dynpm_idle_work_handler(struct work_struct *work)
   1834  1.1  riastrad {
   1835  1.1  riastrad 	struct radeon_device *rdev;
   1836  1.1  riastrad 	int resched;
   1837  1.1  riastrad 	rdev = container_of(work, struct radeon_device,
   1838  1.1  riastrad 				pm.dynpm_idle_work.work);
   1839  1.1  riastrad 
   1840  1.1  riastrad 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
   1841  1.1  riastrad 	mutex_lock(&rdev->pm.mutex);
   1842  1.1  riastrad 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
   1843  1.1  riastrad 		int not_processed = 0;
   1844  1.1  riastrad 		int i;
   1845  1.1  riastrad 
   1846  1.1  riastrad 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
   1847  1.1  riastrad 			struct radeon_ring *ring = &rdev->ring[i];
   1848  1.1  riastrad 
   1849  1.1  riastrad 			if (ring->ready) {
   1850  1.1  riastrad 				not_processed += radeon_fence_count_emitted(rdev, i);
   1851  1.1  riastrad 				if (not_processed >= 3)
   1852  1.1  riastrad 					break;
   1853  1.1  riastrad 			}
   1854  1.1  riastrad 		}
   1855  1.1  riastrad 
   1856  1.1  riastrad 		if (not_processed >= 3) { /* should upclock */
   1857  1.1  riastrad 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
   1858  1.1  riastrad 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
   1859  1.1  riastrad 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
   1860  1.1  riastrad 				   rdev->pm.dynpm_can_upclock) {
   1861  1.1  riastrad 				rdev->pm.dynpm_planned_action =
   1862  1.1  riastrad 					DYNPM_ACTION_UPCLOCK;
   1863  1.1  riastrad 				rdev->pm.dynpm_action_timeout = jiffies +
   1864  1.1  riastrad 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
   1865  1.1  riastrad 			}
   1866  1.1  riastrad 		} else if (not_processed == 0) { /* should downclock */
   1867  1.1  riastrad 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
   1868  1.1  riastrad 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
   1869  1.1  riastrad 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
   1870  1.1  riastrad 				   rdev->pm.dynpm_can_downclock) {
   1871  1.1  riastrad 				rdev->pm.dynpm_planned_action =
   1872  1.1  riastrad 					DYNPM_ACTION_DOWNCLOCK;
   1873  1.1  riastrad 				rdev->pm.dynpm_action_timeout = jiffies +
   1874  1.1  riastrad 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
   1875  1.1  riastrad 			}
   1876  1.1  riastrad 		}
   1877  1.1  riastrad 
   1878  1.1  riastrad 		/* Note, radeon_pm_set_clocks is called with static_switch set
   1879  1.1  riastrad 		 * to false since we want to wait for vbl to avoid flicker.
   1880  1.1  riastrad 		 */
   1881  1.1  riastrad 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
   1882  1.1  riastrad 		    jiffies > rdev->pm.dynpm_action_timeout) {
   1883  1.1  riastrad 			radeon_pm_get_dynpm_state(rdev);
   1884  1.1  riastrad 			radeon_pm_set_clocks(rdev);
   1885  1.1  riastrad 		}
   1886  1.1  riastrad 
   1887  1.1  riastrad 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
   1888  1.1  riastrad 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
   1889  1.1  riastrad 	}
   1890  1.1  riastrad 	mutex_unlock(&rdev->pm.mutex);
   1891  1.1  riastrad 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
   1892  1.1  riastrad }
   1893  1.1  riastrad 
   1894  1.1  riastrad /*
   1895  1.1  riastrad  * Debugfs info
   1896  1.1  riastrad  */
   1897  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
   1898  1.1  riastrad 
   1899  1.1  riastrad static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
   1900  1.1  riastrad {
   1901  1.1  riastrad 	struct drm_info_node *node = (struct drm_info_node *) m->private;
   1902  1.1  riastrad 	struct drm_device *dev = node->minor->dev;
   1903  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
   1904  1.1  riastrad 	struct drm_device *ddev = rdev->ddev;
   1905  1.1  riastrad 
   1906  1.1  riastrad 	if  ((rdev->flags & RADEON_IS_PX) &&
   1907  1.1  riastrad 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
   1908  1.1  riastrad 		seq_printf(m, "PX asic powered off\n");
   1909  1.1  riastrad 	} else if (rdev->pm.dpm_enabled) {
   1910  1.1  riastrad 		mutex_lock(&rdev->pm.mutex);
   1911  1.1  riastrad 		if (rdev->asic->dpm.debugfs_print_current_performance_level)
   1912  1.1  riastrad 			radeon_dpm_debugfs_print_current_performance_level(rdev, m);
   1913  1.1  riastrad 		else
   1914  1.1  riastrad 			seq_printf(m, "Debugfs support not implemented for this asic\n");
   1915  1.1  riastrad 		mutex_unlock(&rdev->pm.mutex);
   1916  1.1  riastrad 	} else {
   1917  1.1  riastrad 		seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
   1918  1.1  riastrad 		/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
   1919  1.1  riastrad 		if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
   1920  1.1  riastrad 			seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
   1921  1.1  riastrad 		else
   1922  1.1  riastrad 			seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
   1923  1.1  riastrad 		seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
   1924  1.1  riastrad 		if (rdev->asic->pm.get_memory_clock)
   1925  1.1  riastrad 			seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
   1926  1.1  riastrad 		if (rdev->pm.current_vddc)
   1927  1.1  riastrad 			seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
   1928  1.1  riastrad 		if (rdev->asic->pm.get_pcie_lanes)
   1929  1.1  riastrad 			seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
   1930  1.1  riastrad 	}
   1931  1.1  riastrad 
   1932  1.1  riastrad 	return 0;
   1933  1.1  riastrad }
   1934  1.1  riastrad 
   1935  1.1  riastrad static struct drm_info_list radeon_pm_info_list[] = {
   1936  1.1  riastrad 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
   1937  1.1  riastrad };
   1938  1.1  riastrad #endif
   1939  1.1  riastrad 
   1940  1.1  riastrad static int radeon_debugfs_pm_init(struct radeon_device *rdev)
   1941  1.1  riastrad {
   1942  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
   1943  1.1  riastrad 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
   1944  1.1  riastrad #else
   1945  1.1  riastrad 	return 0;
   1946  1.1  riastrad #endif
   1947  1.1  riastrad }
   1948