2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
16 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
18 * OTHER DEALINGS IN THE SOFTWARE.
20 * Authors: Rafał Miłecki <zajec5@gmail.com>
21 * Alex Deucher <alexdeucher@gmail.com>
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include <dev/drm2/drmP.h>
32 #define RADEON_IDLE_LOOP_MS 100
33 #define RADEON_RECLOCK_DELAY_MS 200
34 #define RADEON_WAIT_VBLANK_TIMEOUT 200
36 static const char *radeon_pm_state_type_name[5] = {
45 static void radeon_dynpm_idle_work_handler(struct work_struct *work);
46 #endif /* DUMBBELL_WIP */
47 static int radeon_debugfs_pm_init(struct radeon_device *rdev);
48 static bool radeon_pm_in_vbl(struct radeon_device *rdev);
49 static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
50 static void radeon_pm_update_profile(struct radeon_device *rdev);
51 static void radeon_pm_set_clocks(struct radeon_device *rdev);
53 int radeon_pm_get_type_index(struct radeon_device *rdev,
54 enum radeon_pm_state_type ps_type,
58 int found_instance = -1;
60 for (i = 0; i < rdev->pm.num_power_states; i++) {
61 if (rdev->pm.power_state[i].type == ps_type) {
63 if (found_instance == instance)
67 /* return default if no match */
68 return rdev->pm.default_power_state_index;
71 void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
73 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
74 if (rdev->pm.profile == PM_PROFILE_AUTO) {
75 sx_xlock(&rdev->pm.mutex);
76 radeon_pm_update_profile(rdev);
77 radeon_pm_set_clocks(rdev);
78 sx_xunlock(&rdev->pm.mutex);
83 static void radeon_pm_update_profile(struct radeon_device *rdev)
85 switch (rdev->pm.profile) {
86 case PM_PROFILE_DEFAULT:
87 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
91 if (power_supply_is_system_supplied() > 0) {
92 if (rdev->pm.active_crtc_count > 1)
93 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
95 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
97 if (rdev->pm.active_crtc_count > 1)
98 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
100 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
102 #endif /* DUMBBELL_WIP */
105 if (rdev->pm.active_crtc_count > 1)
106 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
108 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
111 if (rdev->pm.active_crtc_count > 1)
112 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
114 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
116 case PM_PROFILE_HIGH:
117 if (rdev->pm.active_crtc_count > 1)
118 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
120 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
124 if (rdev->pm.active_crtc_count == 0) {
125 rdev->pm.requested_power_state_index =
126 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
127 rdev->pm.requested_clock_mode_index =
128 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
130 rdev->pm.requested_power_state_index =
131 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
132 rdev->pm.requested_clock_mode_index =
133 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
137 static void radeon_unmap_vram_bos(struct radeon_device *rdev)
139 struct radeon_bo *bo, *n;
141 if (list_empty(&rdev->gem.objects))
144 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
145 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
146 ttm_bo_unmap_virtual(&bo->tbo);
150 static void radeon_sync_with_vblank(struct radeon_device *rdev)
152 if (rdev->pm.active_crtcs) {
153 rdev->pm.vblank_sync = false;
156 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
157 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
158 #endif /* DUMBBELL_WIP */
162 static void radeon_set_power_state(struct radeon_device *rdev)
165 bool misc_after = false;
167 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
168 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
171 if (radeon_gui_idle(rdev)) {
172 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
173 clock_info[rdev->pm.requested_clock_mode_index].sclk;
174 if (sclk > rdev->pm.default_sclk)
175 sclk = rdev->pm.default_sclk;
177 /* starting with BTC, there is one state that is used for both
178 * MH and SH. Difference is that we always use the high clock index for
181 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
182 (rdev->family >= CHIP_BARTS) &&
183 rdev->pm.active_crtc_count &&
184 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
185 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
186 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
187 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
189 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
190 clock_info[rdev->pm.requested_clock_mode_index].mclk;
192 if (mclk > rdev->pm.default_mclk)
193 mclk = rdev->pm.default_mclk;
195 /* upvolt before raising clocks, downvolt after lowering clocks */
196 if (sclk < rdev->pm.current_sclk)
199 radeon_sync_with_vblank(rdev);
201 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
202 if (!radeon_pm_in_vbl(rdev))
206 radeon_pm_prepare(rdev);
209 /* voltage, pcie lanes, etc.*/
210 radeon_pm_misc(rdev);
212 /* set engine clock */
213 if (sclk != rdev->pm.current_sclk) {
214 radeon_pm_debug_check_in_vbl(rdev, false);
215 radeon_set_engine_clock(rdev, sclk);
216 radeon_pm_debug_check_in_vbl(rdev, true);
217 rdev->pm.current_sclk = sclk;
218 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
221 /* set memory clock */
222 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
223 radeon_pm_debug_check_in_vbl(rdev, false);
224 radeon_set_memory_clock(rdev, mclk);
225 radeon_pm_debug_check_in_vbl(rdev, true);
226 rdev->pm.current_mclk = mclk;
227 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
231 /* voltage, pcie lanes, etc.*/
232 radeon_pm_misc(rdev);
234 radeon_pm_finish(rdev);
236 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
237 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
239 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
242 static void radeon_pm_set_clocks(struct radeon_device *rdev)
246 /* no need to take locks, etc. if nothing's going to change */
247 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
248 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
251 //DRM_LOCK(rdev->ddev); XXX Recursion, already locked in drm_attach/drm_load -- dumbbell@
252 sx_xlock(&rdev->pm.mclk_lock);
253 sx_xlock(&rdev->ring_lock);
255 /* wait for the rings to drain */
256 for (i = 0; i < RADEON_NUM_RINGS; i++) {
257 struct radeon_ring *ring = &rdev->ring[i];
261 r = radeon_fence_wait_empty_locked(rdev, i);
263 /* needs a GPU reset dont reset here */
264 sx_xunlock(&rdev->ring_lock);
265 sx_xunlock(&rdev->pm.mclk_lock);
266 //DRM_UNLOCK(rdev->ddev); XXX Recursion, already locked in drm_attach/drm_load -- dumbbell@
271 radeon_unmap_vram_bos(rdev);
273 if (rdev->irq.installed) {
274 for (i = 0; i < rdev->num_crtc; i++) {
275 if (rdev->pm.active_crtcs & (1 << i)) {
276 rdev->pm.req_vblank |= (1 << i);
277 drm_vblank_get(rdev->ddev, i);
282 radeon_set_power_state(rdev);
284 if (rdev->irq.installed) {
285 for (i = 0; i < rdev->num_crtc; i++) {
286 if (rdev->pm.req_vblank & (1 << i)) {
287 rdev->pm.req_vblank &= ~(1 << i);
288 drm_vblank_put(rdev->ddev, i);
293 /* update display watermarks based on new power state */
294 radeon_update_bandwidth_info(rdev);
295 if (rdev->pm.active_crtc_count)
296 radeon_bandwidth_update(rdev);
298 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
300 sx_xunlock(&rdev->ring_lock);
301 sx_xunlock(&rdev->pm.mclk_lock);
302 //DRM_UNLOCK(rdev->ddev); XXX Recursion, already locked in drm_attach/drm_load -- dumbbell@
305 static void radeon_pm_print_states(struct radeon_device *rdev)
308 struct radeon_power_state *power_state;
309 struct radeon_pm_clock_info *clock_info;
311 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
312 for (i = 0; i < rdev->pm.num_power_states; i++) {
313 power_state = &rdev->pm.power_state[i];
314 DRM_DEBUG_DRIVER("State %d: %s\n", i,
315 radeon_pm_state_type_name[power_state->type]);
316 if (i == rdev->pm.default_power_state_index)
317 DRM_DEBUG_DRIVER("\tDefault");
318 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
319 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
320 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
321 DRM_DEBUG_DRIVER("\tSingle display only\n");
322 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
323 for (j = 0; j < power_state->num_clock_modes; j++) {
324 clock_info = &(power_state->clock_info[j]);
325 if (rdev->flags & RADEON_IS_IGP)
326 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
328 clock_info->sclk * 10);
330 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
332 clock_info->sclk * 10,
333 clock_info->mclk * 10,
334 clock_info->voltage.voltage);
340 static ssize_t radeon_get_pm_profile(struct device *dev,
341 struct device_attribute *attr,
344 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
345 struct radeon_device *rdev = ddev->dev_private;
346 int cp = rdev->pm.profile;
348 return snprintf(buf, PAGE_SIZE, "%s\n",
349 (cp == PM_PROFILE_AUTO) ? "auto" :
350 (cp == PM_PROFILE_LOW) ? "low" :
351 (cp == PM_PROFILE_MID) ? "mid" :
352 (cp == PM_PROFILE_HIGH) ? "high" : "default");
355 static ssize_t radeon_set_pm_profile(struct device *dev,
356 struct device_attribute *attr,
360 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
361 struct radeon_device *rdev = ddev->dev_private;
363 sx_xlock(&rdev->pm.mutex);
364 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
365 if (strncmp("default", buf, strlen("default")) == 0)
366 rdev->pm.profile = PM_PROFILE_DEFAULT;
367 else if (strncmp("auto", buf, strlen("auto")) == 0)
368 rdev->pm.profile = PM_PROFILE_AUTO;
369 else if (strncmp("low", buf, strlen("low")) == 0)
370 rdev->pm.profile = PM_PROFILE_LOW;
371 else if (strncmp("mid", buf, strlen("mid")) == 0)
372 rdev->pm.profile = PM_PROFILE_MID;
373 else if (strncmp("high", buf, strlen("high")) == 0)
374 rdev->pm.profile = PM_PROFILE_HIGH;
379 radeon_pm_update_profile(rdev);
380 radeon_pm_set_clocks(rdev);
385 sx_xunlock(&rdev->pm.mutex);
390 static ssize_t radeon_get_pm_method(struct device *dev,
391 struct device_attribute *attr,
394 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
395 struct radeon_device *rdev = ddev->dev_private;
396 int pm = rdev->pm.pm_method;
398 return snprintf(buf, PAGE_SIZE, "%s\n",
399 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
402 static ssize_t radeon_set_pm_method(struct device *dev,
403 struct device_attribute *attr,
407 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
408 struct radeon_device *rdev = ddev->dev_private;
411 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
412 sx_xlock(&rdev->pm.mutex);
413 rdev->pm.pm_method = PM_METHOD_DYNPM;
414 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
415 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
416 sx_xunlock(&rdev->pm.mutex);
417 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
418 sx_xlock(&rdev->pm.mutex);
420 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
421 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
422 rdev->pm.pm_method = PM_METHOD_PROFILE;
423 sx_xunlock(&rdev->pm.mutex);
425 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
426 #endif /* DUMBBELL_WIP */
431 radeon_pm_compute_clocks(rdev);
436 static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
437 static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
439 static ssize_t radeon_hwmon_show_temp(struct device *dev,
440 struct device_attribute *attr,
443 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
444 struct radeon_device *rdev = ddev->dev_private;
447 switch (rdev->pm.int_thermal_type) {
448 case THERMAL_TYPE_RV6XX:
449 temp = rv6xx_get_temp(rdev);
451 case THERMAL_TYPE_RV770:
452 temp = rv770_get_temp(rdev);
454 case THERMAL_TYPE_EVERGREEN:
455 case THERMAL_TYPE_NI:
456 temp = evergreen_get_temp(rdev);
458 case THERMAL_TYPE_SUMO:
459 temp = sumo_get_temp(rdev);
461 case THERMAL_TYPE_SI:
462 temp = si_get_temp(rdev);
469 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
472 static ssize_t radeon_hwmon_show_name(struct device *dev,
473 struct device_attribute *attr,
476 return sprintf(buf, "radeon\n");
479 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
480 static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
482 static struct attribute *hwmon_attributes[] = {
483 &sensor_dev_attr_temp1_input.dev_attr.attr,
484 &sensor_dev_attr_name.dev_attr.attr,
488 static const struct attribute_group hwmon_attrgroup = {
489 .attrs = hwmon_attributes,
491 #endif /* DUMBBELL_WIP */
493 static int radeon_hwmon_init(struct radeon_device *rdev)
498 rdev->pm.int_hwmon_dev = NULL;
499 #endif /* DUMBBELL_WIP */
501 switch (rdev->pm.int_thermal_type) {
502 case THERMAL_TYPE_RV6XX:
503 case THERMAL_TYPE_RV770:
504 case THERMAL_TYPE_EVERGREEN:
505 case THERMAL_TYPE_NI:
506 case THERMAL_TYPE_SUMO:
507 case THERMAL_TYPE_SI:
508 /* No support for TN yet */
509 if (rdev->family == CHIP_ARUBA)
512 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
513 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
514 err = PTR_ERR(rdev->pm.int_hwmon_dev);
516 "Unable to register hwmon device: %d\n", err);
519 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
520 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
524 "Unable to create hwmon sysfs file: %d\n", err);
525 hwmon_device_unregister(rdev->dev);
527 #endif /* DUMBBELL_WIP */
536 static void radeon_hwmon_fini(struct radeon_device *rdev)
539 if (rdev->pm.int_hwmon_dev) {
540 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
541 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
543 #endif /* DUMBBELL_WIP */
546 void radeon_pm_suspend(struct radeon_device *rdev)
548 sx_xlock(&rdev->pm.mutex);
549 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
550 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
551 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
553 sx_xunlock(&rdev->pm.mutex);
556 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
557 #endif /* DUMBBELL_WIP */
560 void radeon_pm_resume(struct radeon_device *rdev)
562 /* set up the default clocks if the MC ucode is loaded */
563 if ((rdev->family >= CHIP_BARTS) &&
564 (rdev->family <= CHIP_CAYMAN) &&
566 if (rdev->pm.default_vddc)
567 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
568 SET_VOLTAGE_TYPE_ASIC_VDDC);
569 if (rdev->pm.default_vddci)
570 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
571 SET_VOLTAGE_TYPE_ASIC_VDDCI);
572 if (rdev->pm.default_sclk)
573 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
574 if (rdev->pm.default_mclk)
575 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
577 /* asic init will reset the default power state */
578 sx_xlock(&rdev->pm.mutex);
579 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
580 rdev->pm.current_clock_mode_index = 0;
581 rdev->pm.current_sclk = rdev->pm.default_sclk;
582 rdev->pm.current_mclk = rdev->pm.default_mclk;
583 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
584 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
585 if (rdev->pm.pm_method == PM_METHOD_DYNPM
586 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
587 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
589 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
590 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
591 #endif /* DUMBBELL_WIP */
593 sx_xunlock(&rdev->pm.mutex);
594 radeon_pm_compute_clocks(rdev);
597 int radeon_pm_init(struct radeon_device *rdev)
601 /* default to profile method */
602 rdev->pm.pm_method = PM_METHOD_PROFILE;
603 rdev->pm.profile = PM_PROFILE_DEFAULT;
604 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
605 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
606 rdev->pm.dynpm_can_upclock = true;
607 rdev->pm.dynpm_can_downclock = true;
608 rdev->pm.default_sclk = rdev->clock.default_sclk;
609 rdev->pm.default_mclk = rdev->clock.default_mclk;
610 rdev->pm.current_sclk = rdev->clock.default_sclk;
611 rdev->pm.current_mclk = rdev->clock.default_mclk;
612 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
615 if (rdev->is_atom_bios)
616 radeon_atombios_get_power_modes(rdev);
618 radeon_combios_get_power_modes(rdev);
619 radeon_pm_print_states(rdev);
620 radeon_pm_init_profile(rdev);
621 /* set up the default clocks if the MC ucode is loaded */
622 if ((rdev->family >= CHIP_BARTS) &&
623 (rdev->family <= CHIP_CAYMAN) &&
625 if (rdev->pm.default_vddc)
626 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
627 SET_VOLTAGE_TYPE_ASIC_VDDC);
628 if (rdev->pm.default_vddci)
629 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
630 SET_VOLTAGE_TYPE_ASIC_VDDCI);
631 if (rdev->pm.default_sclk)
632 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
633 if (rdev->pm.default_mclk)
634 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
638 /* set up the internal thermal sensor if applicable */
639 ret = radeon_hwmon_init(rdev);
644 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
645 #endif /* DUMBBELL_WIP */
647 if (rdev->pm.num_power_states > 1) {
648 /* where's the best place to put these? */
650 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
651 #endif /* DUMBBELL_WIP */
653 DRM_ERROR("failed to create device file for power profile\n");
655 ret = device_create_file(rdev->dev, &dev_attr_power_method);
656 #endif /* DUMBBELL_WIP */
658 DRM_ERROR("failed to create device file for power method\n");
660 if (radeon_debugfs_pm_init(rdev)) {
661 DRM_ERROR("Failed to register debugfs file for PM!\n");
664 DRM_INFO("radeon: power management initialized\n");
670 void radeon_pm_fini(struct radeon_device *rdev)
672 if (rdev->pm.num_power_states > 1) {
673 DRM_UNLOCK(rdev->ddev); /* Work around LOR. */
674 sx_xlock(&rdev->pm.mutex);
675 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
676 rdev->pm.profile = PM_PROFILE_DEFAULT;
677 radeon_pm_update_profile(rdev);
678 radeon_pm_set_clocks(rdev);
679 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
680 /* reset default clocks */
681 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
682 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
683 radeon_pm_set_clocks(rdev);
685 sx_xunlock(&rdev->pm.mutex);
686 DRM_LOCK(rdev->ddev);
689 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
691 device_remove_file(rdev->dev, &dev_attr_power_profile);
692 device_remove_file(rdev->dev, &dev_attr_power_method);
693 #endif /* DUMBBELL_WIP */
696 if (rdev->pm.power_state) {
698 for (i = 0; i < rdev->pm.num_power_states; ++i) {
699 free(rdev->pm.power_state[i].clock_info, DRM_MEM_DRIVER);
701 free(rdev->pm.power_state, DRM_MEM_DRIVER);
702 rdev->pm.power_state = NULL;
703 rdev->pm.num_power_states = 0;
706 radeon_hwmon_fini(rdev);
709 void radeon_pm_compute_clocks(struct radeon_device *rdev)
711 struct drm_device *ddev = rdev->ddev;
712 struct drm_crtc *crtc;
713 struct radeon_crtc *radeon_crtc;
715 if (rdev->pm.num_power_states < 2)
718 sx_xlock(&rdev->pm.mutex);
720 rdev->pm.active_crtcs = 0;
721 rdev->pm.active_crtc_count = 0;
722 list_for_each_entry(crtc,
723 &ddev->mode_config.crtc_list, head) {
724 radeon_crtc = to_radeon_crtc(crtc);
725 if (radeon_crtc->enabled) {
726 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
727 rdev->pm.active_crtc_count++;
731 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
732 radeon_pm_update_profile(rdev);
733 radeon_pm_set_clocks(rdev);
734 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
735 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
736 if (rdev->pm.active_crtc_count > 1) {
737 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
739 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
740 #endif /* DUMBBELL_WIP */
742 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
743 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
744 radeon_pm_get_dynpm_state(rdev);
745 radeon_pm_set_clocks(rdev);
747 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
749 } else if (rdev->pm.active_crtc_count == 1) {
750 /* TODO: Increase clocks if needed for current mode */
752 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
753 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
754 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
755 radeon_pm_get_dynpm_state(rdev);
756 radeon_pm_set_clocks(rdev);
759 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
760 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
761 #endif /* DUMBBELL_WIP */
762 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
763 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
765 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
766 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
767 #endif /* DUMBBELL_WIP */
768 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
770 } else { /* count == 0 */
771 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
773 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
774 #endif /* DUMBBELL_WIP */
776 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
777 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
778 radeon_pm_get_dynpm_state(rdev);
779 radeon_pm_set_clocks(rdev);
785 sx_xunlock(&rdev->pm.mutex);
788 static bool radeon_pm_in_vbl(struct radeon_device *rdev)
790 int crtc, vpos, hpos, vbl_status;
793 /* Iterate over all active crtc's. All crtc's must be in vblank,
794 * otherwise return in_vbl == false.
796 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
797 if (rdev->pm.active_crtcs & (1 << crtc)) {
798 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
799 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
800 !(vbl_status & DRM_SCANOUTPOS_INVBL))
808 static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
811 bool in_vbl = radeon_pm_in_vbl(rdev);
814 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
815 finish ? "exit" : "entry");
820 static void radeon_dynpm_idle_work_handler(struct work_struct *work)
822 struct radeon_device *rdev;
824 rdev = container_of(work, struct radeon_device,
825 pm.dynpm_idle_work.work);
827 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
828 sx_xlock(&rdev->pm.mutex);
829 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
830 int not_processed = 0;
833 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
834 struct radeon_ring *ring = &rdev->ring[i];
837 not_processed += radeon_fence_count_emitted(rdev, i);
838 if (not_processed >= 3)
843 if (not_processed >= 3) { /* should upclock */
844 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
845 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
846 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
847 rdev->pm.dynpm_can_upclock) {
848 rdev->pm.dynpm_planned_action =
849 DYNPM_ACTION_UPCLOCK;
850 rdev->pm.dynpm_action_timeout = jiffies +
851 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
853 } else if (not_processed == 0) { /* should downclock */
854 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
855 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
856 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
857 rdev->pm.dynpm_can_downclock) {
858 rdev->pm.dynpm_planned_action =
859 DYNPM_ACTION_DOWNCLOCK;
860 rdev->pm.dynpm_action_timeout = jiffies +
861 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
865 /* Note, radeon_pm_set_clocks is called with static_switch set
866 * to false since we want to wait for vbl to avoid flicker.
868 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
869 jiffies > rdev->pm.dynpm_action_timeout) {
870 radeon_pm_get_dynpm_state(rdev);
871 radeon_pm_set_clocks(rdev);
874 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
875 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
877 sx_xunlock(&rdev->pm.mutex);
878 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
880 #endif /* DUMBBELL_WIP */
885 #if defined(CONFIG_DEBUG_FS)
887 static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
889 struct drm_info_node *node = (struct drm_info_node *) m->private;
890 struct drm_device *dev = node->minor->dev;
891 struct radeon_device *rdev = dev->dev_private;
893 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
894 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
895 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
896 if (rdev->asic->pm.get_memory_clock)
897 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
898 if (rdev->pm.current_vddc)
899 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
900 if (rdev->asic->pm.get_pcie_lanes)
901 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
906 static struct drm_info_list radeon_pm_info_list[] = {
907 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
911 static int radeon_debugfs_pm_init(struct radeon_device *rdev)
913 #if defined(CONFIG_DEBUG_FS)
914 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));