Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/atombios_crtc.c.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/atombios_crtc.c
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/atombios_crtc.c.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/atombios_crtc.c	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -441,14 +441,23 @@ static bool atombios_crtc_mode_fixup(str
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 static void atombios_crtc_prepare(struct drm_crtc *crtc)
Jesse Keating 2f82dd
 {
Jesse Keating 2f82dd
+	struct drm_device *dev = crtc->dev;
Jesse Keating 2f82dd
+	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	mutex_lock(&dev_priv->mode_info.power.pll_mutex);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 	atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jesse Keating 2f82dd
 	atombios_lock_crtc(crtc, 1);
Jesse Keating 2f82dd
 }
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 static void atombios_crtc_commit(struct drm_crtc *crtc)
Jesse Keating 2f82dd
 {
Jesse Keating 2f82dd
+	struct drm_device *dev = crtc->dev;
Jesse Keating 2f82dd
+	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 	atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Jesse Keating 2f82dd
 	atombios_lock_crtc(crtc, 0);
Jesse Keating 2f82dd
+	mutex_unlock(&dev_priv->mode_info.power.pll_mutex);
Jesse Keating 2f82dd
 }
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_atombios.c.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_atombios.c
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_atombios.c.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_atombios.c	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -620,6 +620,34 @@ void radeon_atom_static_pwrmgt_setup(str
Jesse Keating 2f82dd
 	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
Jesse Keating 2f82dd
 }
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+void radeon_atom_get_mc_arb_info(struct drm_device *dev)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_mode_info *mode_info = &dev_priv->mode_info;
Jesse Keating 2f82dd
+	struct atom_context *ctx = mode_info->atom_context;
Jesse Keating 2f82dd
+	int index = GetIndexIntoMasterTable(DATA, MC_InitParameter);
Jesse Keating 2f82dd
+	uint8_t frev, crev;
Jesse Keating 2f82dd
+	uint16_t size, data_offset;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset);
Jesse Keating 2f82dd
+	dev_priv->mode_info.power.mc_arb_init_values =
Jesse Keating 2f82dd
+		kmalloc(size*sizeof(int), GFP_KERNEL);
Jesse Keating 2f82dd
+	memcpy(dev_priv->mode_info.power.mc_arb_init_values,
Jesse Keating 2f82dd
+	       ctx->bios + data_offset, size * sizeof(int));
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+void radeon_atom_get_engine_clock(struct drm_device *dev, int *engine_clock)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_mode_info *mode_info = &dev_priv->mode_info;
Jesse Keating 2f82dd
+	struct atom_context *ctx = mode_info->atom_context;
Jesse Keating 2f82dd
+	GET_ENGINE_CLOCK_PS_ALLOCATION args;
Jesse Keating 2f82dd
+	int index = GetIndexIntoMasterTable(COMMAND, GetEngineClock);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	atom_execute_table(ctx, index, (uint32_t *)&args);
Jesse Keating 2f82dd
+	*engine_clock = args.ulReturnEngineClock;
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 void radeon_atom_set_engine_clock(struct drm_device *dev, int eng_clock)
Jesse Keating 2f82dd
 {
Jesse Keating 2f82dd
 	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
@@ -633,6 +661,18 @@ void radeon_atom_set_engine_clock(struct
Jesse Keating 2f82dd
 	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
Jesse Keating 2f82dd
 }
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+void radeon_atom_get_memory_clock(struct drm_device *dev, int *mem_clock)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_mode_info *mode_info = &dev_priv->mode_info;
Jesse Keating 2f82dd
+	struct atom_context *ctx = mode_info->atom_context;
Jesse Keating 2f82dd
+	GET_MEMORY_CLOCK_PS_ALLOCATION args;
Jesse Keating 2f82dd
+	int index = GetIndexIntoMasterTable(COMMAND, GetMemoryClock);
Jesse Keating 2f82dd
+	
Jesse Keating 2f82dd
+	atom_execute_table(ctx, index, (uint32_t *)&args);
Jesse Keating 2f82dd
+	*mem_clock = args.ulReturnMemoryClock;
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 void radeon_atom_set_memory_clock(struct drm_device *dev, int mem_clock)
Jesse Keating 2f82dd
 {
Jesse Keating 2f82dd
 	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
@@ -646,6 +686,16 @@ void radeon_atom_set_memory_clock(struct
Jesse Keating 2f82dd
 	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
Jesse Keating 2f82dd
 }
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+void radeon_atom_initialize_memory_controller(struct drm_device *dev)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct atom_context *ctx = dev_priv->mode_info.atom_context;
Jesse Keating 2f82dd
+	int index = GetIndexIntoMasterTable(COMMAND, MemoryDeviceInit);
Jesse Keating 2f82dd
+	MEMORY_PLLINIT_PS_ALLOCATION args;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	atom_execute_table(ctx, index, (uint32_t *)&args);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 void radeon_atom_initialize_bios_scratch_regs(struct drm_device *dev)
Jesse Keating 2f82dd
 {
Jesse Keating 2f82dd
 	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cp.c.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cp.c
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cp.c.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cp.c	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -3223,6 +3223,8 @@ int radeon_driver_load(struct drm_device
Jesse Keating 2f82dd
 		if (ret)
Jesse Keating 2f82dd
 			goto modeset_fail;
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+		mutex_init(&dev_priv->mode_info.power.pll_mutex);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 		radeon_modeset_init(dev);
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 		radeon_modeset_cp_init(dev);
Jesse Keating 2f82dd
@@ -3231,7 +3233,7 @@ int radeon_driver_load(struct drm_device
Jesse Keating 2f82dd
 		drm_irq_install(dev);
Jesse Keating 2f82dd
 	}
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
-
Jesse Keating 2f82dd
+	radeon_pm_init(dev);
Jesse Keating 2f82dd
 	return ret;
Jesse Keating 2f82dd
 modeset_fail:
Jesse Keating 2f82dd
 	dev->driver->driver_features &= ~DRIVER_MODESET;
Jesse Keating 2f82dd
@@ -3303,6 +3305,8 @@ int radeon_driver_unload(struct drm_devi
Jesse Keating 2f82dd
 {
Jesse Keating 2f82dd
 	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+	radeon_pm_exit(dev);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Keating 2f82dd
 		drm_irq_uninstall(dev);
Jesse Keating 2f82dd
 		radeon_modeset_cleanup(dev);
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cs.c.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cs.c
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cs.c.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_cs.c	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -41,6 +41,8 @@ int radeon_cs_ioctl(struct drm_device *d
Jesse Keating 2f82dd
 	long size;
Jesse Keating 2f82dd
 	int r, i;
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+	radeon_pm_timer_reset(dev);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 	mutex_lock(&dev_priv->cs.cs_mutex);
Jesse Keating 2f82dd
 	/* set command stream id to 0 which is fake id */
Jesse Keating 2f82dd
 	cs_id = 0;
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_drv.h.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_drv.h
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_drv.h.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_drv.h	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -612,6 +612,9 @@ extern int radeon_modeset_cp_resume(stru
Jesse Keating 2f82dd
 /* radeon_pm.c */
Jesse Keating 2f82dd
 int radeon_suspend(struct drm_device *dev, pm_message_t state);
Jesse Keating 2f82dd
 int radeon_resume(struct drm_device *dev);
Jesse Keating 2f82dd
+void radeon_pm_init(struct drm_device *dev);
Jesse Keating 2f82dd
+void radeon_pm_exit(struct drm_device *dev);
Jesse Keating 2f82dd
+void radeon_pm_timer_reset(struct drm_device *dev);
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 /* Flags for stats.boxes
Jesse Keating 2f82dd
  */
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_irq.c.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_irq.c
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_irq.c.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_irq.c	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -185,8 +185,10 @@ irqreturn_t radeon_driver_irq_handler(DR
Jesse Keating 2f82dd
 	struct drm_device *dev = (struct drm_device *) arg;
Jesse Keating 2f82dd
 	drm_radeon_private_t *dev_priv =
Jesse Keating 2f82dd
 	    (drm_radeon_private_t *) dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power = &dev_priv->mode_info.power;
Jesse Keating 2f82dd
 	u32 stat;
Jesse Keating 2f82dd
 	u32 r500_disp_int;
Jesse Keating 2f82dd
+	unsigned long flags;
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 	/* Only consider the bits we're interested in - others could be used
Jesse Keating 2f82dd
 	 * outside the DRM
Jesse Keating 2f82dd
@@ -206,15 +208,47 @@ irqreturn_t radeon_driver_irq_handler(DR
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 	/* VBLANK interrupt */
Jesse Keating 2f82dd
 	if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690) {
Jesse Keating 2f82dd
-		if (r500_disp_int & R500_D1_VBLANK_INTERRUPT)
Jesse Keating 2f82dd
+		if (r500_disp_int & R500_D1_VBLANK_INTERRUPT) {
Jesse Keating 2f82dd
+			spin_lock_irqsave(&power->power_lock, flags);
Jesse Keating 2f82dd
+			if (power->reclock_head & 1) {
Jesse Keating 2f82dd
+				power->reclock_head &= ~1;
Jesse Keating 2f82dd
+				schedule_work(&power->reclock_work);
Jesse Keating 2f82dd
+				drm_vblank_put(dev, 0);
Jesse Keating 2f82dd
+			}
Jesse Keating 2f82dd
+			spin_unlock_irqrestore(&power->power_lock, flags);
Jesse Keating 2f82dd
 			drm_handle_vblank(dev, 0);
Jesse Keating 2f82dd
-		if (r500_disp_int & R500_D2_VBLANK_INTERRUPT)
Jesse Keating 2f82dd
+		}
Jesse Keating 2f82dd
+		if (r500_disp_int & R500_D2_VBLANK_INTERRUPT) {
Jesse Keating 2f82dd
+			spin_lock_irqsave(&power->power_lock, flags);
Jesse Keating 2f82dd
+			if (power->reclock_head & 2) {
Jesse Keating 2f82dd
+				power->reclock_head &= ~2;
Jesse Keating 2f82dd
+				schedule_work(&power->reclock_work);
Jesse Keating 2f82dd
+				drm_vblank_put(dev, 1);
Jesse Keating 2f82dd
+			}
Jesse Keating 2f82dd
+			spin_unlock_irqrestore(&power->power_lock, flags);
Jesse Keating 2f82dd
 			drm_handle_vblank(dev, 1);
Jesse Keating 2f82dd
+		}
Jesse Keating 2f82dd
 	} else {
Jesse Keating 2f82dd
-		if (stat & RADEON_CRTC_VBLANK_STAT)
Jesse Keating 2f82dd
+		if (stat & RADEON_CRTC_VBLANK_STAT) {
Jesse Keating 2f82dd
+			spin_lock_irqsave(&power->power_lock, flags);
Jesse Keating 2f82dd
+			if (power->reclock_head & 1) {
Jesse Keating 2f82dd
+				power->reclock_head &= ~1;
Jesse Keating 2f82dd
+				schedule_work(&power->reclock_work);
Jesse Keating 2f82dd
+				drm_vblank_put(dev, 0);
Jesse Keating 2f82dd
+			}
Jesse Keating 2f82dd
+			spin_unlock_irqrestore(&power->power_lock, flags);
Jesse Keating 2f82dd
 			drm_handle_vblank(dev, 0);
Jesse Keating 2f82dd
-		if (stat & RADEON_CRTC2_VBLANK_STAT)
Jesse Keating 2f82dd
+		}
Jesse Keating 2f82dd
+		if (stat & RADEON_CRTC2_VBLANK_STAT) {
Jesse Keating 2f82dd
+			spin_lock_irqsave(&power->power_lock, flags);
Jesse Keating 2f82dd
+			if (power->reclock_head & 2) {
Jesse Keating 2f82dd
+				power->reclock_head &= ~2;
Jesse Keating 2f82dd
+				schedule_work(&power->reclock_work);
Jesse Keating 2f82dd
+				drm_vblank_put(dev, 1);
Jesse Keating 2f82dd
+			}
Jesse Keating 2f82dd
+			spin_unlock_irqrestore(&power->power_lock, flags);
Jesse Keating 2f82dd
 			drm_handle_vblank(dev, 1);
Jesse Keating 2f82dd
+		}
Jesse Keating 2f82dd
 	}
Jesse Keating 2f82dd
 	return IRQ_HANDLED;
Jesse Keating 2f82dd
 }
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_mode.h.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_mode.h
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_mode.h.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_mode.h	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -173,6 +173,22 @@ struct radeon_i2c_chan {
Jesse Keating 2f82dd
 	struct radeon_i2c_bus_rec rec;
Jesse Keating 2f82dd
 };
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+struct radeon_powermanagement_info {
Jesse Keating 2f82dd
+	struct timer_list idle_power_timer;
Jesse Keating 2f82dd
+	struct work_struct reclock_work;
Jesse Keating 2f82dd
+	struct drm_device *dev;
Jesse Keating 2f82dd
+	uint32_t orig_memory_clock;
Jesse Keating 2f82dd
+	uint32_t orig_engine_clock;
Jesse Keating 2f82dd
+	uint32_t *mc_arb_init_values;
Jesse Keating 2f82dd
+	uint8_t orig_fbdiv;
Jesse Keating 2f82dd
+	int new_mem_clock;
Jesse Keating 2f82dd
+	int new_engine_clock;
Jesse Keating 2f82dd
+	int current_clock_state;
Jesse Keating 2f82dd
+	int reclock_head;
Jesse Keating 2f82dd
+	struct mutex pll_mutex;
Jesse Keating 2f82dd
+	spinlock_t power_lock;
Jesse Keating 2f82dd
+};
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 struct radeon_mode_info {
Jesse Keating 2f82dd
 	struct atom_context *atom_context;
Jesse Keating 2f82dd
 	struct radeon_bios_connector bios_connector[RADEON_MAX_BIOS_CONNECTOR];
Jesse Keating 2f82dd
@@ -182,6 +198,9 @@ struct radeon_mode_info {
Jesse Keating 2f82dd
 	struct radeon_pll mpll;
Jesse Keating 2f82dd
 	uint32_t mclk;
Jesse Keating 2f82dd
 	uint32_t sclk;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	/* power management */
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info power;
Jesse Keating 2f82dd
 };
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 struct radeon_crtc {
Jesse Keating 2f82dd
@@ -307,6 +326,12 @@ extern int radeon_crtc_cursor_move(struc
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 extern bool radeon_atom_get_clock_info(struct drm_device *dev);
Jesse Keating 2f82dd
 extern bool radeon_combios_get_clock_info(struct drm_device *dev);
Jesse Keating 2f82dd
+extern void radeon_atom_get_engine_clock(struct drm_device *dev, int *engine_clock);
Jesse Keating 2f82dd
+extern void radeon_atom_get_memory_clock(struct drm_device *dev, int *memory_clock);
Jesse Keating 2f82dd
+extern void radeon_atom_set_engine_clock(struct drm_device *dev, int engine_clock);
Jesse Keating 2f82dd
+extern void radeon_atom_set_memory_clock(struct drm_device *dev, int memory_clock);
Jesse Keating 2f82dd
+extern void radeon_atom_initialize_memory_controller(struct drm_device *dev);
Jesse Keating 2f82dd
+extern void radeon_atom_get_mc_arb_info(struct drm_device *dev);
Jesse Keating 2f82dd
 extern void radeon_atombios_get_lvds_info(struct radeon_encoder *encoder);
Jesse Keating 2f82dd
 extern void radeon_atombios_get_tmds_info(struct radeon_encoder *encoder);
Jesse Keating 2f82dd
 extern bool radeon_combios_get_lvds_info(struct radeon_encoder *encoder);
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_pm.c.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_pm.c
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_pm.c.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_pm.c	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -31,6 +31,8 @@
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 #include "drm_crtc_helper.h"
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+#define RADEON_DOWNCLOCK_IDLE_MS 30
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
 int radeon_suspend(struct drm_device *dev, pm_message_t state)
Jesse Keating 2f82dd
 {
Jesse Keating 2f82dd
 	struct drm_radeon_private *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
@@ -255,3 +257,214 @@ bool radeon_set_pcie_lanes(struct drm_de
Jesse Keating 2f82dd
 		return false;
Jesse Keating 2f82dd
 }
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
+static void radeon_pm_set_engine_clock(struct drm_device *dev, int freq)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	if (dev_priv->is_atom_bios)
Jesse Keating 2f82dd
+		radeon_atom_set_engine_clock(dev, freq);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+static void radeon_pm_set_memory_clock(struct drm_device *dev, int freq)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power = &dev_priv->mode_info.power;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	mutex_lock(&power->pll_mutex);
Jesse Keating 2f82dd
+	radeon_do_cp_idle(dev_priv);
Jesse Keating 2f82dd
+	if (dev_priv->is_atom_bios) {
Jesse Keating 2f82dd
+		int mpll, spll, hclk, sclk, fbdiv, index, factor;
Jesse Keating 2f82dd
+		switch (dev_priv->chip_family) {
Jesse Keating 2f82dd
+		case CHIP_R520:
Jesse Keating 2f82dd
+		case CHIP_RV530:
Jesse Keating 2f82dd
+		case CHIP_RV560:
Jesse Keating 2f82dd
+		case CHIP_RV570:
Jesse Keating 2f82dd
+		case CHIP_R580:
Jesse Keating 2f82dd
+			mpll = RADEON_READ_PLL(dev_priv, MPLL_FUNC_CNTL);
Jesse Keating 2f82dd
+			fbdiv = (mpll & 0x1fe0) >> 5;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+			/* Set new fbdiv */
Jesse Keating 2f82dd
+			factor = power->orig_memory_clock / freq;
Jesse Keating 2f82dd
+			fbdiv = power->orig_fbdiv / factor;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+			mpll &= ~0x1fe0;
Jesse Keating 2f82dd
+			mpll |= ((fbdiv << 5) | (1 << 24));
Jesse Keating 2f82dd
+			mpll &= ~(1 << 25);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+			spll = RADEON_READ_PLL(dev_priv, SPLL_FUNC_CNTL);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+			hclk = fbdiv << 5;
Jesse Keating 2f82dd
+			hclk += 0x20;
Jesse Keating 2f82dd
+			hclk *= 8;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+			sclk = spll & 0x1fe0;
Jesse Keating 2f82dd
+			sclk += 0x20;
Jesse Keating 2f82dd
+			sclk *= 6;
Jesse Keating 2f82dd
+			sclk = sclk >> 5;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+			index = (hclk/sclk);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+			R500_WRITE_MCIND(R530_MC_ARB_RATIO_CLK_SEQ,
Jesse Keating 2f82dd
+					 power->mc_arb_init_values[index]);
Jesse Keating 2f82dd
+			RADEON_WRITE_PLL(dev_priv, MPLL_FUNC_CNTL, mpll);
Jesse Keating 2f82dd
+			radeon_atom_initialize_memory_controller(dev);
Jesse Keating 2f82dd
+			break;
Jesse Keating 2f82dd
+		}
Jesse Keating 2f82dd
+	}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	mutex_unlock(&power->pll_mutex);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+static int radeon_pm_get_active_crtcs(struct drm_device *dev, int *crtcs)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	struct drm_crtc *crtc;
Jesse Keating 2f82dd
+	int count = 0;
Jesse Keating 2f82dd
+	struct radeon_crtc *radeon_crtc;
Jesse Keating 2f82dd
+	
Jesse Keating 2f82dd
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Keating 2f82dd
+		radeon_crtc = to_radeon_crtc(crtc);
Jesse Keating 2f82dd
+		if (crtc->enabled) {
Jesse Keating 2f82dd
+			count++;
Jesse Keating 2f82dd
+			*crtcs |= (1 << radeon_crtc->crtc_id);
Jesse Keating 2f82dd
+		}
Jesse Keating 2f82dd
+	}
Jesse Keating 2f82dd
+	return count;
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+static void radeon_pm_perform_transition(struct drm_device *dev)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power = &dev_priv->mode_info.power;
Jesse Keating 2f82dd
+	int crtcs = 0, count;
Jesse Keating 2f82dd
+	unsigned long flags;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	count = radeon_pm_get_active_crtcs(dev, &crtcs);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	spin_lock_irqsave(&power->power_lock, flags);
Jesse Keating 2f82dd
+	switch (count) {
Jesse Keating 2f82dd
+	case 0:
Jesse Keating 2f82dd
+		schedule_work(&power->reclock_work);
Jesse Keating 2f82dd
+		break;
Jesse Keating 2f82dd
+	case 1:
Jesse Keating 2f82dd
+		if (power->reclock_head)
Jesse Keating 2f82dd
+			break;
Jesse Keating 2f82dd
+		if (crtcs & 1) {
Jesse Keating 2f82dd
+			power->reclock_head |= 1;
Jesse Keating 2f82dd
+			drm_vblank_get(dev, 0);
Jesse Keating 2f82dd
+		} else {
Jesse Keating 2f82dd
+			power->reclock_head |= 2;
Jesse Keating 2f82dd
+			drm_vblank_get(dev, 1);
Jesse Keating 2f82dd
+		}
Jesse Keating 2f82dd
+		break;
Jesse Keating 2f82dd
+	default:
Jesse Keating 2f82dd
+		/* Too many active heads */
Jesse Keating 2f82dd
+		break;
Jesse Keating 2f82dd
+	}
Jesse Keating 2f82dd
+	spin_unlock_irqrestore(&power->power_lock, flags);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+static int radeon_pm_set_runtime_power(struct drm_device *dev, int value)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power = &dev_priv->mode_info.power;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	if (power->current_clock_state == value)
Jesse Keating 2f82dd
+		return 1;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	switch (value) {
Jesse Keating 2f82dd
+	case 0:
Jesse Keating 2f82dd
+		power->new_engine_clock = 100*100;
Jesse Keating 2f82dd
+		power->new_mem_clock = 100*100;
Jesse Keating 2f82dd
+		break;
Jesse Keating 2f82dd
+	case 1:
Jesse Keating 2f82dd
+		power->new_engine_clock = power->orig_engine_clock;
Jesse Keating 2f82dd
+		power->new_mem_clock = power->orig_memory_clock;
Jesse Keating 2f82dd
+		break;
Jesse Keating 2f82dd
+	}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	power->current_clock_state = value;
Jesse Keating 2f82dd
+	radeon_pm_perform_transition(dev);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	return 0;
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+static void radeon_pm_idle_timeout(unsigned long d)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	struct drm_device *dev = (struct drm_device *)d;
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	radeon_pm_set_runtime_power(dev, 0);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+static void radeon_pm_reclock_callback(struct work_struct *work)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power =
Jesse Keating 2f82dd
+		container_of(work, struct radeon_powermanagement_info,
Jesse Keating 2f82dd
+			     reclock_work);
Jesse Keating 2f82dd
+	struct drm_device *dev = power->dev;	
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	mutex_lock(&dev_priv->cs.cs_mutex);
Jesse Keating 2f82dd
+	radeon_pm_set_memory_clock(dev, power->new_mem_clock);
Jesse Keating 2f82dd
+	radeon_pm_set_engine_clock(dev, power->new_engine_clock);
Jesse Keating 2f82dd
+	mutex_unlock(&dev_priv->cs.cs_mutex);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+void radeon_pm_timer_reset(struct drm_device *dev)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power = &dev_priv->mode_info.power;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	if (!drm_core_check_feature(dev, DRIVER_MODESET))
Jesse Keating 2f82dd
+		return;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	radeon_pm_set_runtime_power(dev, 1);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	mod_timer(&power->idle_power_timer,
Jesse Keating 2f82dd
+		  jiffies + msecs_to_jiffies(RADEON_DOWNCLOCK_IDLE_MS));
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+void radeon_pm_init(struct drm_device *dev)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power = &dev_priv->mode_info.power;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	power->dev = dev;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	if (!drm_core_check_feature(dev, DRIVER_MODESET))
Jesse Keating 2f82dd
+		return;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	if (dev_priv->is_atom_bios) {
Jesse Keating 2f82dd
+		int mpll;
Jesse Keating 2f82dd
+		radeon_atom_get_mc_arb_info(dev);
Jesse Keating 2f82dd
+		radeon_atom_get_engine_clock(dev, &power->orig_engine_clock);
Jesse Keating 2f82dd
+		radeon_atom_get_memory_clock(dev, &power->orig_memory_clock);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+		mpll = RADEON_READ_PLL(dev_priv, MPLL_FUNC_CNTL);
Jesse Keating 2f82dd
+		dev_priv->mode_info.power.orig_fbdiv = (mpll & 0x1fe0) >> 5;
Jesse Keating 2f82dd
+	}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	setup_timer(&power->idle_power_timer, radeon_pm_idle_timeout,
Jesse Keating 2f82dd
+		    (unsigned long)dev);
Jesse Keating 2f82dd
+	INIT_WORK(&power->reclock_work, radeon_pm_reclock_callback);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	spin_lock_init(&power->power_lock);
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	power->current_clock_state = 1;
Jesse Keating 2f82dd
+	power->reclock_head = 0;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	radeon_pm_timer_reset(dev);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+void radeon_pm_exit(struct drm_device *dev)
Jesse Keating 2f82dd
+{
Jesse Keating 2f82dd
+	drm_radeon_private_t *dev_priv = dev->dev_private;
Jesse Keating 2f82dd
+	struct radeon_powermanagement_info *power = &dev_priv->mode_info.power;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	if (!drm_core_check_feature(dev, DRIVER_MODESET))
Jesse Keating 2f82dd
+		return;
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+	del_timer_sync(&power->idle_power_timer);
Jesse Keating 2f82dd
+}
Jesse Keating 2f82dd
diff -up linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_reg.h.mjg linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_reg.h
Jesse Keating 2f82dd
--- linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_reg.h.mjg	2009-03-03 19:41:48.000000000 +0000
Jesse Keating 2f82dd
+++ linux-2.6.28.x86_64/drivers/gpu/drm/radeon/radeon_reg.h	2009-03-03 20:53:05.000000000 +0000
Jesse Keating 2f82dd
@@ -303,6 +303,28 @@
Jesse Keating 2f82dd
 #       define RADEON_PLL_WR_EN             (1 << 7)
Jesse Keating 2f82dd
 #       define RADEON_PLL_DIV_SEL           (3 << 8)
Jesse Keating 2f82dd
 #       define RADEON_PLL2_DIV_SEL_MASK     ~(3 << 8)
Jesse Keating 2f82dd
+#define SPLL_FUNC_CNTL                     0x0000
Jesse Keating 2f82dd
+#define MPLL_FUNC_CNTL                     0x0004
Jesse Keating 2f82dd
+#define GENERAL_PWRMGT                     0x0008
Jesse Keating 2f82dd
+#      define RADEON_GLOBAL_PWRMGT_EN      (1 << 0)
Jesse Keating 2f82dd
+#define SCLK_PWRMGT_CNTL                   0x0009
Jesse Keating 2f82dd
+#      define RADEON_SCLK_PWRMGT_OFF       (1 << 0)
Jesse Keating 2f82dd
+#define MCLK_PWRMGT_CNTL                   0x000a
Jesse Keating 2f82dd
+#      define RADEON_MCLK_PWRMGT_OFF       (1 << 0)
Jesse Keating 2f82dd
+#define DYN_PWRMGT_SCLK_CNTL               0x000b
Jesse Keating 2f82dd
+#      define RADEON_ENGINE_DYNCLK_MODE    (1 << 0)
Jesse Keating 2f82dd
+#      define RADEON_STATIC_SCREEN_EN      (1 << 20)
Jesse Keating 2f82dd
+#      define RADEON_CLIENT_SELECT_POWER_EN (1 << 21)
Jesse Keating 2f82dd
+#define DYN_SCLK_PWMEN_PIPE                0x000d
Jesse Keating 2f82dd
+#      define RADEON_PIPE_3D_NOT_AUTO      (1 << 8)
Jesse Keating 2f82dd
+#define DYN_SCLK_VOL_CNTL                  0x000e
Jesse Keating 2f82dd
+#      define RADEON_IO_CG_VOLTAGE_DROP    (1 << 0)
Jesse Keating 2f82dd
+#      define RADEON_VOLTAGE_DROP_SYNC     (1 << 2)
Jesse Keating 2f82dd
+#define CP_DYN_CNTL                        0x000f
Jesse Keating 2f82dd
+#      define RADEON_CP_FORCEON            (1 << 0)
Jesse Keating 2f82dd
+#      define RADEON_CP_LOWER_POWER_IGNORE (1 << 20)
Jesse Keating 2f82dd
+#      define RADEON_CP_NORMAL_POWER_IGNORE (1 << 21)
Jesse Keating 2f82dd
+#      define RADEON_CP_NORMAL_POWER_BUSY  (1 << 24)
Jesse Keating 2f82dd
 #define RADEON_CLK_PWRMGT_CNTL              0x0014
Jesse Keating 2f82dd
 #       define RADEON_ENGIN_DYNCLK_MODE     (1 << 12)
Jesse Keating 2f82dd
 #       define RADEON_ACTIVE_HILO_LAT_MASK  (3 << 13)
Jesse Keating 2f82dd
@@ -3961,7 +3983,48 @@
Jesse Keating 2f82dd
 #	define AVIVO_I2C_RESET						(1 << 8)
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 #define R600_GENERAL_PWRMGT                                        0x618
Jesse Keating 2f82dd
+#	define R600_GLOBAL_PWRMGT_EN                               (1 << 0)
Jesse Keating 2f82dd
+#	define R600_STATIC_PM_EN                                   (1 << 1)
Jesse Keating 2f82dd
+#	define R600_MOBILE_SU                                      (1 << 2)
Jesse Keating 2f82dd
+#	define R600_THERMAL_PROTECTION_DIS                         (1 << 3)
Jesse Keating 2f82dd
+#	define R600_THERMAL_PROTECTION_TYPE                        (1 << 4)
Jesse Keating 2f82dd
+#	define R600_ENABLE_GEN2PCIE                                (1 << 5)
Jesse Keating 2f82dd
+#	define R600_SW_GPIO_INDEX                                  (1 << 6)
Jesse Keating 2f82dd
+#	define R600_LOW_VOLT_D2_ACPI                               (1 << 8)
Jesse Keating 2f82dd
+#	define R600_LOW_VOLT_D3_ACPI                               (1 << 9)
Jesse Keating 2f82dd
+#	define R600_VOLT_PWRMGT_EN                                 (1 << 10)
Jesse Keating 2f82dd
 #	define R600_OPEN_DRAIN_PADS				   (1 << 11)
Jesse Keating 2f82dd
+#	define R600_AVP_SCLK_EN                                    (1 << 12)
Jesse Keating 2f82dd
+#	define R600_IDCT_SCLK_EN                                   (1 << 13)
Jesse Keating 2f82dd
+#	define R600_GPU_COUNTER_ACPI                               (1 << 14)
Jesse Keating 2f82dd
+#	define R600_COUNTER_CLK                                    (1 << 15)
Jesse Keating 2f82dd
+#	define R600_BACKBIAS_PAD_EN                                (1 << 16)
Jesse Keating 2f82dd
+#	define R600_BACKBIAS_VALUE                                 (1 << 17)
Jesse Keating 2f82dd
+#	define R600_BACKBIAS_DPM_CNTL                              (1 << 18)
Jesse Keating 2f82dd
+#	define R600_SPREAD_SPECTRUM_INDEX                          (1 << 19)
Jesse Keating 2f82dd
+#	define R600_DYN_SPREAD_SPECTRUM_EN                         (1 << 21)
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+#define R600_SCLK_PWRMGT_CNTL                                     0x620
Jesse Keating 2f82dd
+#	define R600_SCLK_PWRMGT_OFF                                (1 << 0)
Jesse Keating 2f82dd
+#	define R600_SCLK_TURNOFF                                   (1 << 1)
Jesse Keating 2f82dd
+#	define R600_SPLL_TURNOFF                                   (1 << 2)
Jesse Keating 2f82dd
+#	define R600_SU_SCLK_USE_BCLK                               (1 << 3)
Jesse Keating 2f82dd
+#	define R600_DYNAMIC_GFX_ISLAND_PWR_DOWN                    (1 << 4)
Jesse Keating 2f82dd
+#	define R600_DYNAMIC_GFX_ISLAND_LP                          (1 << 5)
Jesse Keating 2f82dd
+#	define R600_CLK_TURN_ON_STAGGER                            (1 << 6)
Jesse Keating 2f82dd
+#	define R600_CLK_TURN_OFF_STAGGER                           (1 << 7)
Jesse Keating 2f82dd
+#	define R600_FIR_FORCE_TREND_SEL                            (1 << 8)
Jesse Keating 2f82dd
+#	define R600_FIR_TREND_MODE                                 (1 << 9)
Jesse Keating 2f82dd
+#	define R600_DYN_GFX_CLK_OFF_EN                             (1 << 10)
Jesse Keating 2f82dd
+#	define R600_VDDC3D_TURNOFF_D1                              (1 << 11)
Jesse Keating 2f82dd
+#	define R600_VDDC3D_TURNOFF_D2                              (1 << 12)
Jesse Keating 2f82dd
+#	define R600_VDDC3D_TURNOFF_D3                              (1 << 13)
Jesse Keating 2f82dd
+#	define R600_SPLL_TURNOFF_D2                                (1 << 14)
Jesse Keating 2f82dd
+#	define R600_SCLK_LOW_D1                                    (1 << 15)
Jesse Keating 2f82dd
+#	define R600_DYN_GFX_CLK_OFF_MC_EN                          (1 << 16)
Jesse Keating 2f82dd
+
Jesse Keating 2f82dd
+#define R600_MCLK_PWRMGT_CNTL                                     0x624
Jesse Keating 2f82dd
+#      define R600_MPLL_PWRMGT_OFF                                (1 << 0)
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 #define R600_LOWER_GPIO_ENABLE                                     0x710
Jesse Keating 2f82dd
 #define R600_CTXSW_VID_LOWER_GPIO_CNTL                             0x718
Jesse Keating 2f82dd
@@ -5331,5 +5394,6 @@
Jesse Keating 2f82dd
 #   define R500_RS_IP_OFFSET_EN 			(1 << 31)
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 #define R500_DYN_SCLK_PWMEM_PIPE                        0x000d /* PLL */
Jesse Keating 2f82dd
+#define R530_MC_ARB_RATIO_CLK_SEQ			0x0016 /* MC */
Jesse Keating 2f82dd
 
Jesse Keating 2f82dd
 #endif