17433874eSRafał Miłecki /* 27433874eSRafał Miłecki * Permission is hereby granted, free of charge, to any person obtaining a 37433874eSRafał Miłecki * copy of this software and associated documentation files (the "Software"), 47433874eSRafał Miłecki * to deal in the Software without restriction, including without limitation 57433874eSRafał Miłecki * the rights to use, copy, modify, merge, publish, distribute, sublicense, 67433874eSRafał Miłecki * and/or sell copies of the Software, and to permit persons to whom the 77433874eSRafał Miłecki * Software is furnished to do so, subject to the following conditions: 87433874eSRafał Miłecki * 97433874eSRafał Miłecki * The above copyright notice and this permission notice shall be included in 107433874eSRafał Miłecki * all copies or substantial portions of the Software. 117433874eSRafał Miłecki * 127433874eSRafał Miłecki * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 137433874eSRafał Miłecki * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 147433874eSRafał Miłecki * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 157433874eSRafał Miłecki * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 167433874eSRafał Miłecki * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 177433874eSRafał Miłecki * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 187433874eSRafał Miłecki * OTHER DEALINGS IN THE SOFTWARE. 197433874eSRafał Miłecki * 207433874eSRafał Miłecki * Authors: Rafał Miłecki <zajec5@gmail.com> 2156278a8eSAlex Deucher * Alex Deucher <alexdeucher@gmail.com> 227433874eSRafał Miłecki */ 237433874eSRafał Miłecki #include "drmP.h" 247433874eSRafał Miłecki #include "radeon.h" 25f735261bSDave Airlie #include "avivod.h" 267433874eSRafał Miłecki 27c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100 28c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200 2973a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200 302031f77cSAlex Deucher #define RADEON_WAIT_IDLE_TIMEOUT 200 31c913e23aSRafał Miłecki 32c913e23aSRafał Miłecki static void radeon_pm_idle_work_handler(struct work_struct *work); 33c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev); 34c913e23aSRafał Miłecki 355876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev) 365876dd24SMatthew Garrett { 375876dd24SMatthew Garrett struct radeon_bo *bo, *n; 385876dd24SMatthew Garrett 395876dd24SMatthew Garrett if (list_empty(&rdev->gem.objects)) 405876dd24SMatthew Garrett return; 415876dd24SMatthew Garrett 425876dd24SMatthew Garrett list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) { 435876dd24SMatthew Garrett if (bo->tbo.mem.mem_type == TTM_PL_VRAM) 445876dd24SMatthew Garrett ttm_bo_unmap_virtual(&bo->tbo); 455876dd24SMatthew Garrett } 465876dd24SMatthew Garrett 475876dd24SMatthew Garrett if (rdev->gart.table.vram.robj) 485876dd24SMatthew Garrett ttm_bo_unmap_virtual(&rdev->gart.table.vram.robj->tbo); 495876dd24SMatthew Garrett 505876dd24SMatthew Garrett if (rdev->stollen_vga_memory) 515876dd24SMatthew Garrett ttm_bo_unmap_virtual(&rdev->stollen_vga_memory->tbo); 525876dd24SMatthew Garrett 535876dd24SMatthew Garrett if (rdev->r600_blit.shader_obj) 545876dd24SMatthew Garrett ttm_bo_unmap_virtual(&rdev->r600_blit.shader_obj->tbo); 555876dd24SMatthew Garrett } 565876dd24SMatthew Garrett 572aba631cSMatthew Garrett static void radeon_pm_set_clocks(struct radeon_device *rdev, int static_switch) 58a424816fSAlex Deucher { 592aba631cSMatthew Garrett int i; 602aba631cSMatthew Garrett 61c37d230aSMatthew Garrett if (!static_switch) 62c37d230aSMatthew Garrett radeon_get_power_state(rdev, rdev->pm.planned_action); 63c37d230aSMatthew Garrett 64612e06ceSMatthew Garrett mutex_lock(&rdev->ddev->struct_mutex); 65612e06ceSMatthew Garrett mutex_lock(&rdev->vram_mutex); 66a424816fSAlex Deucher mutex_lock(&rdev->cp.mutex); 67a424816fSAlex Deucher 68a424816fSAlex Deucher /* wait for GPU idle */ 69a424816fSAlex Deucher rdev->pm.gui_idle = false; 70a424816fSAlex Deucher rdev->irq.gui_idle = true; 71a424816fSAlex Deucher radeon_irq_set(rdev); 72a424816fSAlex Deucher wait_event_interruptible_timeout( 73a424816fSAlex Deucher rdev->irq.idle_queue, rdev->pm.gui_idle, 74a424816fSAlex Deucher msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT)); 75a424816fSAlex Deucher rdev->irq.gui_idle = false; 76a424816fSAlex Deucher radeon_irq_set(rdev); 77a424816fSAlex Deucher 785876dd24SMatthew Garrett radeon_unmap_vram_bos(rdev); 795876dd24SMatthew Garrett 802aba631cSMatthew Garrett if (!static_switch) { 812aba631cSMatthew Garrett for (i = 0; i < rdev->num_crtc; i++) { 822aba631cSMatthew Garrett if (rdev->pm.active_crtcs & (1 << i)) { 832aba631cSMatthew Garrett rdev->pm.req_vblank |= (1 << i); 842aba631cSMatthew Garrett drm_vblank_get(rdev->ddev, i); 852aba631cSMatthew Garrett } 862aba631cSMatthew Garrett } 872aba631cSMatthew Garrett } 882aba631cSMatthew Garrett 892aba631cSMatthew Garrett radeon_set_power_state(rdev, static_switch); 902aba631cSMatthew Garrett 912aba631cSMatthew Garrett if (!static_switch) { 922aba631cSMatthew Garrett for (i = 0; i < rdev->num_crtc; i++) { 932aba631cSMatthew Garrett if (rdev->pm.req_vblank & (1 << i)) { 942aba631cSMatthew Garrett rdev->pm.req_vblank &= ~(1 << i); 952aba631cSMatthew Garrett drm_vblank_put(rdev->ddev, i); 962aba631cSMatthew Garrett } 972aba631cSMatthew Garrett } 982aba631cSMatthew Garrett } 99a424816fSAlex Deucher 100a424816fSAlex Deucher /* update display watermarks based on new power state */ 101a424816fSAlex Deucher radeon_update_bandwidth_info(rdev); 102a424816fSAlex Deucher if (rdev->pm.active_crtc_count) 103a424816fSAlex Deucher radeon_bandwidth_update(rdev); 104a424816fSAlex Deucher 1052aba631cSMatthew Garrett rdev->pm.planned_action = PM_ACTION_NONE; 1062aba631cSMatthew Garrett 107a424816fSAlex Deucher mutex_unlock(&rdev->cp.mutex); 108612e06ceSMatthew Garrett mutex_unlock(&rdev->vram_mutex); 109612e06ceSMatthew Garrett mutex_unlock(&rdev->ddev->struct_mutex); 110a424816fSAlex Deucher } 111a424816fSAlex Deucher 112a424816fSAlex Deucher static ssize_t radeon_get_power_state_static(struct device *dev, 113a424816fSAlex Deucher struct device_attribute *attr, 114a424816fSAlex Deucher char *buf) 115a424816fSAlex Deucher { 116a424816fSAlex Deucher struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev)); 117a424816fSAlex Deucher struct radeon_device *rdev = ddev->dev_private; 118a424816fSAlex Deucher 119a424816fSAlex Deucher return snprintf(buf, PAGE_SIZE, "%d.%d\n", rdev->pm.current_power_state_index, 120a424816fSAlex Deucher rdev->pm.current_clock_mode_index); 121a424816fSAlex Deucher } 122a424816fSAlex Deucher 123a424816fSAlex Deucher static ssize_t radeon_set_power_state_static(struct device *dev, 124a424816fSAlex Deucher struct device_attribute *attr, 125a424816fSAlex Deucher const char *buf, 126a424816fSAlex Deucher size_t count) 127a424816fSAlex Deucher { 128a424816fSAlex Deucher struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev)); 129a424816fSAlex Deucher struct radeon_device *rdev = ddev->dev_private; 130a424816fSAlex Deucher int ps, cm; 131a424816fSAlex Deucher 132a424816fSAlex Deucher if (sscanf(buf, "%u.%u", &ps, &cm) != 2) { 133a424816fSAlex Deucher DRM_ERROR("Invalid power state!\n"); 134a424816fSAlex Deucher return count; 135a424816fSAlex Deucher } 136a424816fSAlex Deucher 137a424816fSAlex Deucher mutex_lock(&rdev->pm.mutex); 138a424816fSAlex Deucher if ((ps >= 0) && (ps < rdev->pm.num_power_states) && 139a424816fSAlex Deucher (cm >= 0) && (cm < rdev->pm.power_state[ps].num_clock_modes)) { 140a424816fSAlex Deucher if ((rdev->pm.active_crtc_count > 1) && 141a424816fSAlex Deucher (rdev->pm.power_state[ps].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)) { 142a424816fSAlex Deucher DRM_ERROR("Invalid power state for multi-head: %d.%d\n", ps, cm); 143a424816fSAlex Deucher } else { 144a424816fSAlex Deucher /* disable dynpm */ 145a424816fSAlex Deucher rdev->pm.state = PM_STATE_DISABLED; 146a424816fSAlex Deucher rdev->pm.planned_action = PM_ACTION_NONE; 147a424816fSAlex Deucher rdev->pm.requested_power_state_index = ps; 148a424816fSAlex Deucher rdev->pm.requested_clock_mode_index = cm; 1492aba631cSMatthew Garrett radeon_pm_set_clocks(rdev, true); 150a424816fSAlex Deucher } 151a424816fSAlex Deucher } else 152a424816fSAlex Deucher DRM_ERROR("Invalid power state: %d.%d\n\n", ps, cm); 153a424816fSAlex Deucher mutex_unlock(&rdev->pm.mutex); 154a424816fSAlex Deucher 155a424816fSAlex Deucher return count; 156a424816fSAlex Deucher } 157a424816fSAlex Deucher 158a424816fSAlex Deucher static ssize_t radeon_get_dynpm(struct device *dev, 159a424816fSAlex Deucher struct device_attribute *attr, 160a424816fSAlex Deucher char *buf) 161a424816fSAlex Deucher { 162a424816fSAlex Deucher struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev)); 163a424816fSAlex Deucher struct radeon_device *rdev = ddev->dev_private; 164a424816fSAlex Deucher 165a424816fSAlex Deucher return snprintf(buf, PAGE_SIZE, "%s\n", 166a424816fSAlex Deucher (rdev->pm.state == PM_STATE_DISABLED) ? "disabled" : "enabled"); 167a424816fSAlex Deucher } 168a424816fSAlex Deucher 169a424816fSAlex Deucher static ssize_t radeon_set_dynpm(struct device *dev, 170a424816fSAlex Deucher struct device_attribute *attr, 171a424816fSAlex Deucher const char *buf, 172a424816fSAlex Deucher size_t count) 173a424816fSAlex Deucher { 174a424816fSAlex Deucher struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev)); 175a424816fSAlex Deucher struct radeon_device *rdev = ddev->dev_private; 176a424816fSAlex Deucher int tmp = simple_strtoul(buf, NULL, 10); 177a424816fSAlex Deucher 178a424816fSAlex Deucher if (tmp == 0) { 179a424816fSAlex Deucher /* update power mode info */ 180a424816fSAlex Deucher radeon_pm_compute_clocks(rdev); 181a424816fSAlex Deucher /* disable dynpm */ 182a424816fSAlex Deucher mutex_lock(&rdev->pm.mutex); 183a424816fSAlex Deucher rdev->pm.state = PM_STATE_DISABLED; 184a424816fSAlex Deucher rdev->pm.planned_action = PM_ACTION_NONE; 185a424816fSAlex Deucher mutex_unlock(&rdev->pm.mutex); 186a424816fSAlex Deucher DRM_INFO("radeon: dynamic power management disabled\n"); 187a424816fSAlex Deucher } else if (tmp == 1) { 188a424816fSAlex Deucher if (rdev->pm.num_power_states > 1) { 189a424816fSAlex Deucher /* enable dynpm */ 190a424816fSAlex Deucher mutex_lock(&rdev->pm.mutex); 191a424816fSAlex Deucher rdev->pm.state = PM_STATE_PAUSED; 192a424816fSAlex Deucher rdev->pm.planned_action = PM_ACTION_DEFAULT; 193a424816fSAlex Deucher radeon_get_power_state(rdev, rdev->pm.planned_action); 194a424816fSAlex Deucher mutex_unlock(&rdev->pm.mutex); 195a424816fSAlex Deucher /* update power mode info */ 196a424816fSAlex Deucher radeon_pm_compute_clocks(rdev); 197a424816fSAlex Deucher DRM_INFO("radeon: dynamic power management enabled\n"); 198a424816fSAlex Deucher } else 199a424816fSAlex Deucher DRM_ERROR("dynpm not valid on this system\n"); 200a424816fSAlex Deucher } else 201a424816fSAlex Deucher DRM_ERROR("Invalid setting: %d\n", tmp); 202a424816fSAlex Deucher 203a424816fSAlex Deucher return count; 204a424816fSAlex Deucher } 205a424816fSAlex Deucher 206a424816fSAlex Deucher static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, radeon_get_power_state_static, radeon_set_power_state_static); 207a424816fSAlex Deucher static DEVICE_ATTR(dynpm, S_IRUGO | S_IWUSR, radeon_get_dynpm, radeon_set_dynpm); 208a424816fSAlex Deucher 209a424816fSAlex Deucher 210c913e23aSRafał Miłecki static const char *pm_state_names[4] = { 211c913e23aSRafał Miłecki "PM_STATE_DISABLED", 212c913e23aSRafał Miłecki "PM_STATE_MINIMUM", 213c913e23aSRafał Miłecki "PM_STATE_PAUSED", 214c913e23aSRafał Miłecki "PM_STATE_ACTIVE" 215c913e23aSRafał Miłecki }; 2167433874eSRafał Miłecki 2170ec0e74fSAlex Deucher static const char *pm_state_types[5] = { 218d91eeb78SAlex Deucher "", 2190ec0e74fSAlex Deucher "Powersave", 2200ec0e74fSAlex Deucher "Battery", 2210ec0e74fSAlex Deucher "Balanced", 2220ec0e74fSAlex Deucher "Performance", 2230ec0e74fSAlex Deucher }; 2240ec0e74fSAlex Deucher 22556278a8eSAlex Deucher static void radeon_print_power_mode_info(struct radeon_device *rdev) 22656278a8eSAlex Deucher { 22756278a8eSAlex Deucher int i, j; 22856278a8eSAlex Deucher bool is_default; 22956278a8eSAlex Deucher 23056278a8eSAlex Deucher DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states); 23156278a8eSAlex Deucher for (i = 0; i < rdev->pm.num_power_states; i++) { 232a48b9b4eSAlex Deucher if (rdev->pm.default_power_state_index == i) 23356278a8eSAlex Deucher is_default = true; 23456278a8eSAlex Deucher else 23556278a8eSAlex Deucher is_default = false; 2360ec0e74fSAlex Deucher DRM_INFO("State %d %s %s\n", i, 2370ec0e74fSAlex Deucher pm_state_types[rdev->pm.power_state[i].type], 2380ec0e74fSAlex Deucher is_default ? "(default)" : ""); 23956278a8eSAlex Deucher if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP)) 24079daedc9SAlex Deucher DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].pcie_lanes); 241a48b9b4eSAlex Deucher if (rdev->pm.power_state[i].flags & RADEON_PM_SINGLE_DISPLAY_ONLY) 242a48b9b4eSAlex Deucher DRM_INFO("\tSingle display only\n"); 24356278a8eSAlex Deucher DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes); 24456278a8eSAlex Deucher for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) { 24556278a8eSAlex Deucher if (rdev->flags & RADEON_IS_IGP) 24656278a8eSAlex Deucher DRM_INFO("\t\t%d engine: %d\n", 24756278a8eSAlex Deucher j, 24856278a8eSAlex Deucher rdev->pm.power_state[i].clock_info[j].sclk * 10); 24956278a8eSAlex Deucher else 25056278a8eSAlex Deucher DRM_INFO("\t\t%d engine/memory: %d/%d\n", 25156278a8eSAlex Deucher j, 25256278a8eSAlex Deucher rdev->pm.power_state[i].clock_info[j].sclk * 10, 25356278a8eSAlex Deucher rdev->pm.power_state[i].clock_info[j].mclk * 10); 25456278a8eSAlex Deucher } 25556278a8eSAlex Deucher } 25656278a8eSAlex Deucher } 25756278a8eSAlex Deucher 258bae6b562SAlex Deucher void radeon_sync_with_vblank(struct radeon_device *rdev) 259d0d6cb81SRafał Miłecki { 260d0d6cb81SRafał Miłecki if (rdev->pm.active_crtcs) { 261d0d6cb81SRafał Miłecki rdev->pm.vblank_sync = false; 262d0d6cb81SRafał Miłecki wait_event_timeout( 263d0d6cb81SRafał Miłecki rdev->irq.vblank_queue, rdev->pm.vblank_sync, 264d0d6cb81SRafał Miłecki msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT)); 265d0d6cb81SRafał Miłecki } 266d0d6cb81SRafał Miłecki } 267d0d6cb81SRafał Miłecki 2687433874eSRafał Miłecki int radeon_pm_init(struct radeon_device *rdev) 2697433874eSRafał Miłecki { 270c913e23aSRafał Miłecki rdev->pm.state = PM_STATE_DISABLED; 271c913e23aSRafał Miłecki rdev->pm.planned_action = PM_ACTION_NONE; 272a48b9b4eSAlex Deucher rdev->pm.can_upclock = true; 273a48b9b4eSAlex Deucher rdev->pm.can_downclock = true; 274c913e23aSRafał Miłecki 27556278a8eSAlex Deucher if (rdev->bios) { 27656278a8eSAlex Deucher if (rdev->is_atom_bios) 27756278a8eSAlex Deucher radeon_atombios_get_power_modes(rdev); 27856278a8eSAlex Deucher else 27956278a8eSAlex Deucher radeon_combios_get_power_modes(rdev); 28056278a8eSAlex Deucher radeon_print_power_mode_info(rdev); 28156278a8eSAlex Deucher } 28256278a8eSAlex Deucher 2837433874eSRafał Miłecki if (radeon_debugfs_pm_init(rdev)) { 284c142c3e5SRafał Miłecki DRM_ERROR("Failed to register debugfs file for PM!\n"); 2857433874eSRafał Miłecki } 2867433874eSRafał Miłecki 287a424816fSAlex Deucher /* where's the best place to put this? */ 288a424816fSAlex Deucher device_create_file(rdev->dev, &dev_attr_power_state); 289a424816fSAlex Deucher device_create_file(rdev->dev, &dev_attr_dynpm); 290a424816fSAlex Deucher 291c913e23aSRafał Miłecki INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler); 292c913e23aSRafał Miłecki 29390c39059SAlex Deucher if ((radeon_dynpm != -1 && radeon_dynpm) && (rdev->pm.num_power_states > 1)) { 294c913e23aSRafał Miłecki rdev->pm.state = PM_STATE_PAUSED; 295c913e23aSRafał Miłecki DRM_INFO("radeon: dynamic power management enabled\n"); 296c913e23aSRafał Miłecki } 297c913e23aSRafał Miłecki 298c913e23aSRafał Miłecki DRM_INFO("radeon: power management initialized\n"); 299c913e23aSRafał Miłecki 3007433874eSRafał Miłecki return 0; 3017433874eSRafał Miłecki } 3027433874eSRafał Miłecki 30329fb52caSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev) 30429fb52caSAlex Deucher { 30558e21dffSAlex Deucher if (rdev->pm.state != PM_STATE_DISABLED) { 30658e21dffSAlex Deucher /* cancel work */ 30758e21dffSAlex Deucher cancel_delayed_work_sync(&rdev->pm.idle_work); 30858e21dffSAlex Deucher /* reset default clocks */ 30958e21dffSAlex Deucher rdev->pm.state = PM_STATE_DISABLED; 31058e21dffSAlex Deucher rdev->pm.planned_action = PM_ACTION_DEFAULT; 3112aba631cSMatthew Garrett radeon_pm_set_clocks(rdev, false); 312a424816fSAlex Deucher } else if ((rdev->pm.current_power_state_index != 313a424816fSAlex Deucher rdev->pm.default_power_state_index) || 314a424816fSAlex Deucher (rdev->pm.current_clock_mode_index != 0)) { 315a424816fSAlex Deucher rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index; 316a424816fSAlex Deucher rdev->pm.requested_clock_mode_index = 0; 317a424816fSAlex Deucher mutex_lock(&rdev->pm.mutex); 3182aba631cSMatthew Garrett radeon_pm_set_clocks(rdev, true); 319a424816fSAlex Deucher mutex_unlock(&rdev->pm.mutex); 32058e21dffSAlex Deucher } 32158e21dffSAlex Deucher 322a424816fSAlex Deucher device_remove_file(rdev->dev, &dev_attr_power_state); 323a424816fSAlex Deucher device_remove_file(rdev->dev, &dev_attr_dynpm); 324a424816fSAlex Deucher 32529fb52caSAlex Deucher if (rdev->pm.i2c_bus) 32629fb52caSAlex Deucher radeon_i2c_destroy(rdev->pm.i2c_bus); 32729fb52caSAlex Deucher } 32829fb52caSAlex Deucher 329c913e23aSRafał Miłecki void radeon_pm_compute_clocks(struct radeon_device *rdev) 330c913e23aSRafał Miłecki { 331c913e23aSRafał Miłecki struct drm_device *ddev = rdev->ddev; 332a48b9b4eSAlex Deucher struct drm_crtc *crtc; 333c913e23aSRafał Miłecki struct radeon_crtc *radeon_crtc; 334c913e23aSRafał Miłecki 335c913e23aSRafał Miłecki if (rdev->pm.state == PM_STATE_DISABLED) 336c913e23aSRafał Miłecki return; 337c913e23aSRafał Miłecki 338c913e23aSRafał Miłecki mutex_lock(&rdev->pm.mutex); 339c913e23aSRafał Miłecki 340c913e23aSRafał Miłecki rdev->pm.active_crtcs = 0; 341a48b9b4eSAlex Deucher rdev->pm.active_crtc_count = 0; 342a48b9b4eSAlex Deucher list_for_each_entry(crtc, 343a48b9b4eSAlex Deucher &ddev->mode_config.crtc_list, head) { 344a48b9b4eSAlex Deucher radeon_crtc = to_radeon_crtc(crtc); 345a48b9b4eSAlex Deucher if (radeon_crtc->enabled) { 346c913e23aSRafał Miłecki rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id); 347a48b9b4eSAlex Deucher rdev->pm.active_crtc_count++; 348c913e23aSRafał Miłecki } 349c913e23aSRafał Miłecki } 350c913e23aSRafał Miłecki 351a48b9b4eSAlex Deucher if (rdev->pm.active_crtc_count > 1) { 352c913e23aSRafał Miłecki if (rdev->pm.state == PM_STATE_ACTIVE) { 353c913e23aSRafał Miłecki cancel_delayed_work(&rdev->pm.idle_work); 354c913e23aSRafał Miłecki 355c913e23aSRafał Miłecki rdev->pm.state = PM_STATE_PAUSED; 356c913e23aSRafał Miłecki rdev->pm.planned_action = PM_ACTION_UPCLOCK; 3572aba631cSMatthew Garrett radeon_pm_set_clocks(rdev, false); 358c913e23aSRafał Miłecki 359c913e23aSRafał Miłecki DRM_DEBUG("radeon: dynamic power management deactivated\n"); 360c913e23aSRafał Miłecki } 361a48b9b4eSAlex Deucher } else if (rdev->pm.active_crtc_count == 1) { 362c913e23aSRafał Miłecki /* TODO: Increase clocks if needed for current mode */ 363c913e23aSRafał Miłecki 364c913e23aSRafał Miłecki if (rdev->pm.state == PM_STATE_MINIMUM) { 365c913e23aSRafał Miłecki rdev->pm.state = PM_STATE_ACTIVE; 366c913e23aSRafał Miłecki rdev->pm.planned_action = PM_ACTION_UPCLOCK; 3672aba631cSMatthew Garrett radeon_pm_set_clocks(rdev, false); 368c913e23aSRafał Miłecki 369c913e23aSRafał Miłecki queue_delayed_work(rdev->wq, &rdev->pm.idle_work, 370c913e23aSRafał Miłecki msecs_to_jiffies(RADEON_IDLE_LOOP_MS)); 371a48b9b4eSAlex Deucher } else if (rdev->pm.state == PM_STATE_PAUSED) { 372c913e23aSRafał Miłecki rdev->pm.state = PM_STATE_ACTIVE; 373c913e23aSRafał Miłecki queue_delayed_work(rdev->wq, &rdev->pm.idle_work, 374c913e23aSRafał Miłecki msecs_to_jiffies(RADEON_IDLE_LOOP_MS)); 375c913e23aSRafał Miłecki DRM_DEBUG("radeon: dynamic power management activated\n"); 376c913e23aSRafał Miłecki } 377a48b9b4eSAlex Deucher } else { /* count == 0 */ 378c913e23aSRafał Miłecki if (rdev->pm.state != PM_STATE_MINIMUM) { 379c913e23aSRafał Miłecki cancel_delayed_work(&rdev->pm.idle_work); 380c913e23aSRafał Miłecki 381c913e23aSRafał Miłecki rdev->pm.state = PM_STATE_MINIMUM; 382c913e23aSRafał Miłecki rdev->pm.planned_action = PM_ACTION_MINIMUM; 3832aba631cSMatthew Garrett radeon_pm_set_clocks(rdev, false); 38473a6d3fcSRafał Miłecki } 385c913e23aSRafał Miłecki } 386c913e23aSRafał Miłecki 387c913e23aSRafał Miłecki mutex_unlock(&rdev->pm.mutex); 388c913e23aSRafał Miłecki } 389c913e23aSRafał Miłecki 390*f81f2024SMatthew Garrett bool radeon_pm_in_vbl(struct radeon_device *rdev) 391f735261bSDave Airlie { 392bae6b562SAlex Deucher u32 stat_crtc = 0; 393f735261bSDave Airlie bool in_vbl = true; 394f735261bSDave Airlie 395bae6b562SAlex Deucher if (ASIC_IS_DCE4(rdev)) { 396f735261bSDave Airlie if (rdev->pm.active_crtcs & (1 << 0)) { 397bae6b562SAlex Deucher stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET); 398bae6b562SAlex Deucher if (!(stat_crtc & 1)) 399f735261bSDave Airlie in_vbl = false; 400f735261bSDave Airlie } 401f735261bSDave Airlie if (rdev->pm.active_crtcs & (1 << 1)) { 402bae6b562SAlex Deucher stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET); 403bae6b562SAlex Deucher if (!(stat_crtc & 1)) 404bae6b562SAlex Deucher in_vbl = false; 405bae6b562SAlex Deucher } 406bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 2)) { 407bae6b562SAlex Deucher stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET); 408bae6b562SAlex Deucher if (!(stat_crtc & 1)) 409bae6b562SAlex Deucher in_vbl = false; 410bae6b562SAlex Deucher } 411bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 3)) { 412bae6b562SAlex Deucher stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET); 413bae6b562SAlex Deucher if (!(stat_crtc & 1)) 414bae6b562SAlex Deucher in_vbl = false; 415bae6b562SAlex Deucher } 416bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 4)) { 417bae6b562SAlex Deucher stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET); 418bae6b562SAlex Deucher if (!(stat_crtc & 1)) 419bae6b562SAlex Deucher in_vbl = false; 420bae6b562SAlex Deucher } 421bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 5)) { 422bae6b562SAlex Deucher stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET); 423bae6b562SAlex Deucher if (!(stat_crtc & 1)) 424bae6b562SAlex Deucher in_vbl = false; 425bae6b562SAlex Deucher } 426bae6b562SAlex Deucher } else if (ASIC_IS_AVIVO(rdev)) { 427bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 0)) { 428bae6b562SAlex Deucher stat_crtc = RREG32(D1CRTC_STATUS); 429bae6b562SAlex Deucher if (!(stat_crtc & 1)) 430bae6b562SAlex Deucher in_vbl = false; 431bae6b562SAlex Deucher } 432bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 1)) { 433bae6b562SAlex Deucher stat_crtc = RREG32(D2CRTC_STATUS); 434bae6b562SAlex Deucher if (!(stat_crtc & 1)) 435bae6b562SAlex Deucher in_vbl = false; 436bae6b562SAlex Deucher } 437bae6b562SAlex Deucher } else { 438bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 0)) { 439bae6b562SAlex Deucher stat_crtc = RREG32(RADEON_CRTC_STATUS); 440bae6b562SAlex Deucher if (!(stat_crtc & 1)) 441bae6b562SAlex Deucher in_vbl = false; 442bae6b562SAlex Deucher } 443bae6b562SAlex Deucher if (rdev->pm.active_crtcs & (1 << 1)) { 444bae6b562SAlex Deucher stat_crtc = RREG32(RADEON_CRTC2_STATUS); 445bae6b562SAlex Deucher if (!(stat_crtc & 1)) 446f735261bSDave Airlie in_vbl = false; 447f735261bSDave Airlie } 448f735261bSDave Airlie } 449*f81f2024SMatthew Garrett 450*f81f2024SMatthew Garrett return in_vbl; 451*f81f2024SMatthew Garrett } 452*f81f2024SMatthew Garrett 453*f81f2024SMatthew Garrett bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish) 454*f81f2024SMatthew Garrett { 455*f81f2024SMatthew Garrett u32 stat_crtc = 0; 456*f81f2024SMatthew Garrett bool in_vbl = radeon_pm_in_vbl(rdev); 457*f81f2024SMatthew Garrett 458f735261bSDave Airlie if (in_vbl == false) 459bae6b562SAlex Deucher DRM_INFO("not in vbl for pm change %08x at %s\n", stat_crtc, 460bae6b562SAlex Deucher finish ? "exit" : "entry"); 461f735261bSDave Airlie return in_vbl; 462f735261bSDave Airlie } 463c913e23aSRafał Miłecki 464c913e23aSRafał Miłecki static void radeon_pm_idle_work_handler(struct work_struct *work) 465c913e23aSRafał Miłecki { 466c913e23aSRafał Miłecki struct radeon_device *rdev; 467d9932a32SMatthew Garrett int resched; 468c913e23aSRafał Miłecki rdev = container_of(work, struct radeon_device, 469c913e23aSRafał Miłecki pm.idle_work.work); 470c913e23aSRafał Miłecki 471d9932a32SMatthew Garrett resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev); 472c913e23aSRafał Miłecki mutex_lock(&rdev->pm.mutex); 47373a6d3fcSRafał Miłecki if (rdev->pm.state == PM_STATE_ACTIVE) { 474c913e23aSRafał Miłecki unsigned long irq_flags; 475c913e23aSRafał Miłecki int not_processed = 0; 476c913e23aSRafał Miłecki 477c913e23aSRafał Miłecki read_lock_irqsave(&rdev->fence_drv.lock, irq_flags); 478c913e23aSRafał Miłecki if (!list_empty(&rdev->fence_drv.emited)) { 479c913e23aSRafał Miłecki struct list_head *ptr; 480c913e23aSRafał Miłecki list_for_each(ptr, &rdev->fence_drv.emited) { 481c913e23aSRafał Miłecki /* count up to 3, that's enought info */ 482c913e23aSRafał Miłecki if (++not_processed >= 3) 483c913e23aSRafał Miłecki break; 484c913e23aSRafał Miłecki } 485c913e23aSRafał Miłecki } 486c913e23aSRafał Miłecki read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags); 487c913e23aSRafał Miłecki 488c913e23aSRafał Miłecki if (not_processed >= 3) { /* should upclock */ 489c913e23aSRafał Miłecki if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) { 490c913e23aSRafał Miłecki rdev->pm.planned_action = PM_ACTION_NONE; 491c913e23aSRafał Miłecki } else if (rdev->pm.planned_action == PM_ACTION_NONE && 492a48b9b4eSAlex Deucher rdev->pm.can_upclock) { 493c913e23aSRafał Miłecki rdev->pm.planned_action = 494c913e23aSRafał Miłecki PM_ACTION_UPCLOCK; 495c913e23aSRafał Miłecki rdev->pm.action_timeout = jiffies + 496c913e23aSRafał Miłecki msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS); 497c913e23aSRafał Miłecki } 498c913e23aSRafał Miłecki } else if (not_processed == 0) { /* should downclock */ 499c913e23aSRafał Miłecki if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) { 500c913e23aSRafał Miłecki rdev->pm.planned_action = PM_ACTION_NONE; 501c913e23aSRafał Miłecki } else if (rdev->pm.planned_action == PM_ACTION_NONE && 502a48b9b4eSAlex Deucher rdev->pm.can_downclock) { 503c913e23aSRafał Miłecki rdev->pm.planned_action = 504c913e23aSRafał Miłecki PM_ACTION_DOWNCLOCK; 505c913e23aSRafał Miłecki rdev->pm.action_timeout = jiffies + 506c913e23aSRafał Miłecki msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS); 507c913e23aSRafał Miłecki } 508c913e23aSRafał Miłecki } 509c913e23aSRafał Miłecki 510c913e23aSRafał Miłecki if (rdev->pm.planned_action != PM_ACTION_NONE && 511c913e23aSRafał Miłecki jiffies > rdev->pm.action_timeout) { 5122aba631cSMatthew Garrett radeon_pm_set_clocks(rdev, false); 513c913e23aSRafał Miłecki } 514c913e23aSRafał Miłecki } 515c913e23aSRafał Miłecki mutex_unlock(&rdev->pm.mutex); 516d9932a32SMatthew Garrett ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched); 517c913e23aSRafał Miłecki 518c913e23aSRafał Miłecki queue_delayed_work(rdev->wq, &rdev->pm.idle_work, 519c913e23aSRafał Miłecki msecs_to_jiffies(RADEON_IDLE_LOOP_MS)); 520c913e23aSRafał Miłecki } 521c913e23aSRafał Miłecki 5227433874eSRafał Miłecki /* 5237433874eSRafał Miłecki * Debugfs info 5247433874eSRafał Miłecki */ 5257433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS) 5267433874eSRafał Miłecki 5277433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data) 5287433874eSRafał Miłecki { 5297433874eSRafał Miłecki struct drm_info_node *node = (struct drm_info_node *) m->private; 5307433874eSRafał Miłecki struct drm_device *dev = node->minor->dev; 5317433874eSRafał Miłecki struct radeon_device *rdev = dev->dev_private; 5327433874eSRafał Miłecki 533c913e23aSRafał Miłecki seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]); 5346234077dSRafał Miłecki seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk); 5356234077dSRafał Miłecki seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev)); 5366234077dSRafał Miłecki seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk); 5376234077dSRafał Miłecki if (rdev->asic->get_memory_clock) 5386234077dSRafał Miłecki seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev)); 539aa5120d2SRafał Miłecki if (rdev->asic->get_pcie_lanes) 540aa5120d2SRafał Miłecki seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev)); 5417433874eSRafał Miłecki 5427433874eSRafał Miłecki return 0; 5437433874eSRafał Miłecki } 5447433874eSRafał Miłecki 5457433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = { 5467433874eSRafał Miłecki {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL}, 5477433874eSRafał Miłecki }; 5487433874eSRafał Miłecki #endif 5497433874eSRafał Miłecki 550c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev) 5517433874eSRafał Miłecki { 5527433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS) 5537433874eSRafał Miłecki return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list)); 5547433874eSRafał Miłecki #else 5557433874eSRafał Miłecki return 0; 5567433874eSRafał Miłecki #endif 5577433874eSRafał Miłecki } 558