· 7 years ago · Dec 23, 2018, 01:02 PM
1# PlayStation Classic power management system, documented
2
3This document describes the power management system on the PlayStation Classic.
4It is mostly focused on the `power_manage` executable: the command line arguments,
5control files, and processes. It'll also touch upon the system service setup
6and startup script.
7
8There are a few components to the power management system: the startup script,
9the power management service, thermal kernel modules, and supporting programs
10such as the USB reset script and thermal handling scripts. Let's start with
11the `power_manage` executable, which is the main service of the power management
12system.
13
14# `/bin/power_manage`
15
16`power_manage` is a daemon for managing power-related tasks. It sits in the
17background and waits for incoming commands from control files, monitors temperature
18for throttling, and monitors key presses to respond to reset and power button
19press from the console.
20
21## Command line arguments
22
23You may pass a number of command line arguments to `power_manage` to modify its
24behavior. Pass as many or few arguments as you want from the following table, in
25any order.
26
27`%` denotes additional an additional argument you need to pass.
28
29**Argument**|**Description**
30-----|-----
31noapps|Do not attempt to shut down frontend apps before suspending.
32abeled|Sets the LED mode.
33softled|Sets the LED mode.
34nothermal|Disable auto throttling and response to overtemperature.
35thermallog|Logs temperature, auto throttle temperature and counters, and other thermal-related messages
36nokeytime %timeout=3600|Power save timeout in seconds. Must be greater than or equal to 30.
37ipt %timeout=15|Minimum amount of time in seconds after resume before system can be suspended again using the power button. Must be less than or equal to 300.
38temp %start\_temp=80000 %end\_temp=55000 %start\_time=3 %end\_time=5|Auto throttle params. `start_temp` is temperature when throttling starts, `end_temp` is temperature below which throttling ends, both in 1/1000 degree Celsius. `start_time` is sustained high temperature before throttling starts, `end_time` is sustained low temperature before throttling ends, both in minutes.
39stopweston|Stop Weston when suspending.
40rtcon|Handle resume caused by alarm wake (otherwise system resume will not be run until you press the power button). Practically, sets keypress polling timeout to 1000 instead of infinite.
41safereboot|Notifies frontend and unmounts `/dev/sda1` and `/dev/sdb1` before rebooting.
42keylog|Log input events.
43usbreset|Check and reset USB devices when reset button is pressed.
44
45By default (per startup script) the service is run with `usbreset rtcon safereboot softled`.
46
47## Key functions
48`power_manage` is responsible for the following:
49
50- Managing system suspend/resume
51- Handling thermal throttling
52- Handling overheating
53- Handling power save timeout and suspend
54- Responding to power button press
55- Initiate reset of USB devices
56- Setting LED lights
57
58### Suspend/resume
59Before suspending, the service will signal to the frontend by way of `/data/power/prepare_suspend`
60that the system is going to suspend. It waits for `/data/power/apps_timeout`
61seconds for app to respond with OK (`/data/power/apps_okay`) or cancel
62(`/data/power/apps_cancel`). If canceled, suspend is canceled, unless suspend
63was initiated by overheating. Otherwise, Weston will be stopped if selected,
64the LEDs set (under `softled` mode, yellow for normal suspend, red for overheating),
65and the system put into suspend mode. Suspend control file is set accordingly.
66
67When system is resuming, the LEDs are reset (under `softled` mode, green), the
68power flag and resume count updated, and `/usr/bin/resume_run` is executed.
69
70### Thermal throttling
71When CPU temperature exceeds throttle start temperature for a period, `cpu_mode auto` is called and the LEDs set (under `softled` mode, flashing yellow and green). When CPU temperature goes below throttle end temperature for a period,
72`cpu_mode full` is called and the LEDs set (under `softled` mode, green).
73
74### Overheat protection
75If thermal management is not disabled, the service will hook into the console's
76thermal notification and suspend if overheat has been signaled.
77`/dev/shm/thermal/mtkthermal_signal` is executed then the system is supended
78when overheat is asserted.
79
80### Power save
81If power save is enabled, waits for the timeout that no input has been received
82and initiates suspend when the timeout has been exceeded. The time it has been
83since last input is written to `/data/power/time`.
84
85### Power and reset button presses
86If the reset button has been pressed, a USB reset will be triggered. Any presses
87within 3 seconds will be ignored. If the power button is pressed, unless disabled,
88a suspend is initiated.
89
90### USB reset
91It calls `/usr/bin/usbreset_auto` (covered later) when USB reset is triggered.
92
93### Background operations
94Every 60 seconds, thermal checks will be performed (auto throttle, thermal
95interval script). Every `usbreset_auto` seconds (2 or more), USB reset is
96triggered. Every second, CPU temperature file is updated, and control files
97are checked.
98
99## Control files
100
101When `power_manage` starts, it will create a directory at `/dev/shm/power`, and
102symlink `/data/power` to it. This means the following control files can be
103accessed through `/data/power` as well.
104
105### `control`
106**Write-only**
107Master control file.
108
109`%` in the following table indicates an integer value. Make sure the length of
110what you're writing to the file is no longer than 9 characters.
111
112**Value**|**Description**
113-----|-----
114aoff%|Sets power save timeout in seconds. Must be greater than 30.
115aon%|Automatic resume timeout in seconds. Must be greater than 3. (You should issue a standby after this.)
116usbr%|USB reset interval in seconds. 0 to disable USB reset, 1 to enable when pressing reset button, greater than 1 to set auto USB reset interval.
117reboot|Reboots the system. May do it safely (see `safereboot` command line arg).
118off|Suspends the system.
119
120### `disable`
121**Read/write**
122Whether power save is disabled.
123
124**Value**|**Description**
125-----|-----
1260|Power save enabled.
1271|Power save disabled.
1282|Power save and power button disabled (for suspend).
129
130### `time`
131**Read-only**
132Seconds since last input received. Content is a binary `int` value.
133
134### `powerflag`
135**Read-only**
136System power status.
137
138**Value**|**Description**
139-----|-----
1400|Normal operations.
1411|Suspending.
142
143### `resume_count`
144**Read-write**
145The number of times the system has been resumed.
146
147### `usbreset_count`
148**Read-write**
149The number of times USB reset has been performed.
150
151### `apps_timeout`
152**Read-write**
153The number of seconds to wait for the frontend to acknowledge suspending. Values
154between 0 and 3600, inclusive. Invalid range will default setting to 5.
155
156Additionally, the following will set the value to 5 and carry out additional
157function:
158
159**Value**|**Description**
160-----|-----
161debugon|Redirect logging to `/dev/tty`.
162debugoff|Redirect logging to `stdout`.
163
164### `prepare_suspend`
165**Presence**
166Signals to frontend apps that the system is preparing to suspend.
167
168### `apps_okay`
169**Presence**
170Response to service that system is OK to continue suspend.
171
172### `apps_cancel`
173**Presence**
174Response to service that system should not suspend. Ignored if system is suspending
175due to overheating.
176
177### `cpu_temp`
178**Read-only**
179Reports the current CPU temperature in 1/1000 degree Celsius and the current
180throttling mode. `0` is throttled, `1` is full speed. Format is two numbers
181delimited by a space. Only present when thermal management is enabled.
182
183### `temp_limit`
184**Read-only**
185Reports current throttling settings. Contents look like this:
186```
187CPU_AUTO_START_TEMP 80000
188CPU_AUTO_START_TIME 3
189CPU_AUTO_END_TEMP 55000
190CPU_AUTO_END_TIME 5
191```
192Only present when thermal management is enabled.
193
194## LED mode
195
196The following table summarizes how the LEDs behave when set on from within the
197service, depending on the mode:
198
199**LED**|**Default**|**softled**|**abeled**
200-----|-----|-----|-----
201Red|Off|On|Off
202Green|On|On|Off
203
204Invert table values for set off from within the service.
205
206# `/usr/bin/start_pman`
207Although the file name claims it starts `power_manage`, it's actually an extended
208startup script. It is started by the `systemd` unit `powermanage.service`.
209Here's what it does:
210
211## Shutdown if standard downstream USB port detected
212Simply put, if it thinks the console is connected to a computer through the
213Micro USB port, and the current unit is the user variant (from `/etc/versions/variant`),
214it will light the LED red and shut down.
215
216## Shutdown if unauthorized kernel detected
217If the current unit is the user variant and the kernel name is not `aiv8167-rockman-emmc`,
218the unit will light the LED red and shut down.
219
220## Update `/data`
221It sets up symlinks as necessary to PCSX config files and BIOS and whatnot.
222See `/usr/sony/bin/sonyapp-copylink` for details.
223
224## Perform system integrity check
225Certain files are protected, and will be restored by SIC if it's detected to
226be modified. See `/usr/bin/sic` and `/etc/sic.db`.
227
228## Configure custom thermal profile
229If thermal config and `/data/thermal` exists, copy thermal config and restart
230thermal manager. `/data/thermal` does not exist on a stock system. Also, the
231`systemd` unit overrides the config file path, so this really doesn't do anything.
232
233## Set LED mode
234If not DVT version (I don't know what that means), set LEDs yellow and pass
235`softled` mode for `power_manage`.
236
237## Suspend the system
238No, we don't use `power_manage` to suspend on boot, instead we do it ourselves
239because nothing should be running yet. If `/etc/autostart` exists, the value
240in it is used to auto wake the system.
241
242## After power button is pressed, start Weston
243I thought `systemd` or one of the other scripts was supposed to handle this?
244
245## Finally, set `cpu_mode full` and start `power_manage` service
246
247# `/usr/bin/usbreset_auto`
248
249This program resets USB devices in case the hub puts them to sleep. It will
250only reset devices if the devices are suspended. It calls `/usr/bin/usbreset`
251to do the actual resetting.
252
253## Options
254
255The following options are flags (i.e. presence only to enable).
256
257### `/data/power/mtk_hubreset`
258Reset the hub when resetting USB.
259
260### `/data/power/no_usbreset`
261Disable resetting USB devices (but does not disable hub reset, if enabled).
262
263### `/data/power/force_usbreset`
264Forces USB reset even when not necessary.
265
266# Supporting files
267
268The following files are used by the power management system, but are not managed
269by the `power_manage` service.
270
271## `/dev/shm/thermal/mtkthermal_interval`
272Copied from `/usr/bin/mtkthermal_interval`. It prints the contents of each file
273matching `/proc/mtktz/mtkts*` if the system is not a retail release model.
274
275## `/dev/shm/thermal/mtkthermal_signal`
276Copied from `/usr/bin/mtkthermal_signal`. Ran in preparation for thermal shutdown.
277It prints the contents of `/proc/mtktz/mtkts*`, runs `fsck` on `/data` in dry
278mode, and prints a list of zero-length files in `/data`. Why is it doing all
279of this when the system should be suspending to prevent damage? No one knows.
280
281## `/usr/bin/resume_run`
282Resumes system from suspend. Closes error message instance of Weston, restarts
283Weston if not running, waits for SDL to be inittable, logrotates, and restarts
284`sonyapp` (but not by using `systemd` for some reason).
285
286## `/proc/driver/thermal/clsd_pid`
287Actually part of the system thermal driver, write your process ID to register
288to receive `SIGIO` with `si_code=1` when the system asserts overheating.
289
290## `/usr/bin/cpu_mode`
291Sets CPU throttling mode. `full` and `auto` are the most commonly used options,
292but there's a lot of other ones. Read the script if you want to know more.
293
294## `/usr/bin/thermal_manager`
295Loads up thermal config. Looks by default at `/data/thermal/thermal.conf`, but
296the `systemd` unit associated with it actually specifies `/etc/.tp/thermal.conf`.
297Although this is a compiled binary that loads another library, what it actually
298does is fairly straightforward. It reads the contents of the conf file and writes
299lines to the files specified.
300
301Conf file format is as follows: one line of path of the file to write to, another
302line of the content to write. Rinse and repeat. End the file with the string `EOF`.