tz.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:16k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  *
  3.  * Module Name: tz.c
  4.  *   $Revision: 44 $
  5.  *
  6.  *****************************************************************************/
  7. /*
  8.  *  Copyright (C) 2000, 2001 Andrew Grover
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23.  */
  24. #include <acpi.h>
  25. #include <bm.h>
  26. #include "tz.h"
  27. #define _COMPONENT ACPI_THERMAL
  28. MODULE_NAME ("tz")
  29. /****************************************************************************
  30.  *                                  Globals
  31.  ****************************************************************************/
  32. extern int TZP;
  33. /****************************************************************************
  34.  *                            Internal Functions
  35.  ****************************************************************************/
  36. /****************************************************************************
  37.  *
  38.  * FUNCTION:    tz_print
  39.  *
  40.  ****************************************************************************/
  41. void
  42. tz_print (
  43. TZ_CONTEXT *tz)
  44. {
  45. #ifdef ACPI_DEBUG
  46. acpi_buffer buffer;
  47. u32 i,j = 0;
  48. TZ_THRESHOLDS *thresholds = NULL;
  49. FUNCTION_TRACE("tz_print");
  50. if (!tz)
  51. return;
  52. thresholds = &(tz->policy.thresholds);
  53. buffer.length = 256;
  54. buffer.pointer = acpi_os_callocate(buffer.length);
  55. if (!buffer.pointer)
  56. return;
  57. /*
  58.  * Get the full pathname for this ACPI object.
  59.  */
  60. acpi_get_name(tz->acpi_handle, ACPI_FULL_PATHNAME, &buffer);
  61. /*
  62.  * Print out basic thermal zone information.
  63.  */
  64. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "+------------------------------------------------------------n"));
  65. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "| Thermal_zone[%02x]:[%p] %sn", tz->device_handle, tz->acpi_handle, (char*)buffer.pointer));
  66. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|   temperature[%d] state[%08x]n", tz->policy.temperature, tz->policy.state));
  67. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|   cooling_mode[%08x] polling_freq[%d]n", tz->policy.cooling_mode, tz->policy.polling_freq));
  68. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|   critical[%d]n", thresholds->critical.temperature));
  69. if (thresholds->hot.is_valid)
  70. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|   hot[%d]n", thresholds->hot.temperature));
  71. if (thresholds->passive.is_valid) {
  72. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|   passive[%d]: tc1[%d] tc2[%d] tsp[%d]n", thresholds->passive.temperature, thresholds->passive.tc1, thresholds->passive.tc2, thresholds->passive.tsp));
  73. if (thresholds->passive.devices.count > 0) {
  74. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|     devices"));
  75. for (j=0; (j<thresholds->passive.devices.count && j<10); j++) {
  76. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "[%02x]", thresholds->passive.devices.handles[j]));
  77. }
  78. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "n"));
  79. }
  80. }
  81. for (i=0; i<TZ_MAX_ACTIVE_THRESHOLDS; i++) {
  82. if (!thresholds->active[i].is_valid)
  83. break;
  84. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|   active[%d]: index[%d]n", thresholds->active[i].temperature, i));
  85. if (thresholds->active[i].devices.count > 0) {
  86. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "|     devices"));
  87. for (j=0; (j<thresholds->active[i].devices.count && j<10); j++) {
  88. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "[%02x]", thresholds->active[i].devices.handles[j]));
  89. }
  90. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "n"));
  91. }
  92. }
  93. ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO, "+------------------------------------------------------------n"));
  94. acpi_os_free(buffer.pointer);
  95. #endif /*ACPI_DEBUG*/
  96. return;
  97. }
  98. /****************************************************************************
  99.  *
  100.  * FUNCTION:    tz_get_temperaturee
  101.  *
  102.  ****************************************************************************/
  103. acpi_status
  104. tz_get_temperature (
  105. TZ_CONTEXT *tz)
  106. {
  107. acpi_status             status = AE_OK;
  108. FUNCTION_TRACE("tz_get_temperature");
  109. if (!tz) {
  110. return_ACPI_STATUS(AE_BAD_PARAMETER);
  111. }
  112. /*
  113.  * Evaluate the _TMP method to get the current temperature.
  114.  */
  115. status = bm_evaluate_simple_integer(tz->acpi_handle, "_TMP", &(tz->policy.temperature));
  116. if (ACPI_FAILURE(status)) {
  117. return_ACPI_STATUS(status);
  118. }
  119. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %d d_kn", tz->policy.temperature));
  120. return_ACPI_STATUS(status);
  121. }
  122. /****************************************************************************
  123.  *
  124.  * FUNCTION:    tz_set_cooling_preference
  125.  *
  126.  ****************************************************************************/
  127. acpi_status
  128. tz_set_cooling_preference (
  129. TZ_CONTEXT              *tz,
  130. TZ_COOLING_MODE         cooling_mode)
  131. {
  132. acpi_status             status = AE_OK;
  133. acpi_object_list        arg_list;
  134. acpi_object             arg0;
  135. FUNCTION_TRACE("tz_set_cooling_preference");
  136. if (!tz || ((cooling_mode != TZ_COOLING_MODE_ACTIVE) && (cooling_mode != TZ_COOLING_MODE_PASSIVE))) {
  137. return_ACPI_STATUS(AE_BAD_PARAMETER);
  138. }
  139. /*
  140.  * Build the argument list, which simply consists of the current
  141.  * cooling preference.
  142.  */
  143. memset(&arg_list, 0, sizeof(acpi_object));
  144. arg_list.count = 1;
  145. arg_list.pointer = &arg0;
  146. memset(&arg0, 0, sizeof(acpi_object));
  147. arg0.type = ACPI_TYPE_INTEGER;
  148. arg0.integer.value = cooling_mode;
  149. /*
  150.  * Evaluate "_SCP" - setting the new cooling preference.
  151.  */
  152. status = acpi_evaluate_object(tz->acpi_handle, "_SCP", &arg_list, NULL);
  153. if (ACPI_FAILURE(status)) {
  154. tz->policy.cooling_mode = -1;
  155. return_ACPI_STATUS(status);
  156. }
  157. tz->policy.cooling_mode = cooling_mode;
  158. return_ACPI_STATUS(status);
  159. }
  160. /****************************************************************************
  161.  *
  162.  * FUNCTION:    tz_get_thresholds
  163.  *
  164.  ****************************************************************************/
  165. acpi_status
  166. tz_get_thresholds (
  167. TZ_CONTEXT *tz)
  168. {
  169. acpi_status status = AE_OK;
  170. TZ_THRESHOLDS *thresholds = NULL;
  171. u32 value = 0;
  172. u32                     i = 0;
  173. FUNCTION_TRACE("acpi_tz_get_thresholds");
  174. if (!tz) {
  175. return_ACPI_STATUS(AE_BAD_PARAMETER);
  176. }
  177. thresholds = &(tz->policy.thresholds);
  178. /* Critical Shutdown (required) */
  179. status = bm_evaluate_simple_integer(tz->acpi_handle, "_CRT", &value);
  180. if (ACPI_FAILURE(status)) {
  181. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "No critical thresholdn"));
  182. return_ACPI_STATUS(status);
  183. }
  184. else {
  185. thresholds->critical.temperature = value;
  186. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found critical threshold [%d]n", thresholds->critical.temperature));
  187. }
  188. /* Critical Sleep (optional) */
  189. status = bm_evaluate_simple_integer(tz->acpi_handle, "_HOT", &value);
  190. if (ACPI_FAILURE(status)) {
  191. thresholds->hot.is_valid = 0;
  192. thresholds->hot.temperature = 0;
  193. }
  194. else {
  195. thresholds->hot.is_valid = 1;
  196. thresholds->hot.temperature = value;
  197. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%d]n", thresholds->hot.temperature));
  198. }
  199. /* Passive: Processors (optional) */
  200. status = bm_evaluate_simple_integer(tz->acpi_handle, "_PSV", &value);
  201. if (ACPI_FAILURE(status)) {
  202. thresholds->passive.is_valid = 0;
  203. thresholds->passive.temperature = 0;
  204. }
  205. else {
  206. thresholds->passive.is_valid = 1;
  207. thresholds->passive.temperature = value;
  208. status = bm_evaluate_simple_integer(tz->acpi_handle, "_TC1", &value);
  209. if (ACPI_FAILURE(status)) {
  210. thresholds->passive.is_valid = 0;
  211. }
  212. thresholds->passive.tc1 = value;
  213. status = bm_evaluate_simple_integer(tz->acpi_handle, "_TC2", &value);
  214. if (ACPI_FAILURE(status)) {
  215. thresholds->passive.is_valid = 0;
  216. }
  217. thresholds->passive.tc2 = value;
  218. status = bm_evaluate_simple_integer(tz->acpi_handle, "_TSP", &value);
  219. if (ACPI_FAILURE(status)) {
  220. thresholds->passive.is_valid = 0;
  221. }
  222. thresholds->passive.tsp = value;
  223. status = bm_evaluate_reference_list(tz->acpi_handle, "_PSL", &(thresholds->passive.devices));
  224. if (ACPI_FAILURE(status)) {
  225. thresholds->passive.is_valid = 0;
  226. }
  227. if (thresholds->passive.is_valid) {
  228. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found passive threshold [%d]n", thresholds->passive.temperature));
  229. }
  230. else {
  231. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid passive thresholdn"));
  232. }
  233. }
  234. /* Active: Fans, etc. (optional) */
  235. for (i=0; i<TZ_MAX_ACTIVE_THRESHOLDS; i++) {
  236. char name[5] = {'_','A','C',('0'+i),''};
  237. status = bm_evaluate_simple_integer(tz->acpi_handle, name, &value);
  238. if (ACPI_FAILURE(status)) {
  239. thresholds->active[i].is_valid = 0;
  240. thresholds->active[i].temperature = 0;
  241. break;
  242. }
  243. thresholds->active[i].temperature = value;
  244. name[2] = 'L';
  245. status = bm_evaluate_reference_list(tz->acpi_handle, name, &(thresholds->active[i].devices));
  246. if (ACPI_SUCCESS(status)) {
  247. thresholds->active[i].is_valid = 1;
  248. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found active threshold [%d]:[%d]n", i, thresholds->active[i].temperature));
  249. }
  250. else {
  251. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid active threshold [%d]n", i));
  252. }
  253. }
  254. return_ACPI_STATUS(AE_OK);
  255. }
  256. /****************************************************************************
  257.  *
  258.  * FUNCTION:    tz_add_device
  259.  *
  260.  ****************************************************************************/
  261. acpi_status
  262. tz_add_device (
  263. BM_HANDLE               device_handle,
  264. void                    **context)
  265. {
  266. acpi_status             status = AE_OK;
  267. TZ_CONTEXT              *tz = NULL;
  268. BM_DEVICE *device = NULL;
  269. acpi_handle             tmp_handle = NULL;
  270. static u32 zone_count = 0;
  271. FUNCTION_TRACE("tz_add_device");
  272. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Adding thermal zone [%02x].n", device_handle));
  273. if (!context || *context) {
  274. ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Invalid context for device [%02x].n", device_handle));
  275. return_ACPI_STATUS(AE_BAD_PARAMETER);
  276. }
  277. /*
  278.  * Get information on this device.
  279.  */
  280. status = bm_get_device_info(device_handle, &device);
  281. if (ACPI_FAILURE(status)) {
  282. return_ACPI_STATUS(status);
  283. }
  284. /*
  285.  * Allocate a new Thermal Zone device.
  286.  */
  287. tz = acpi_os_callocate(sizeof(TZ_CONTEXT));
  288. if (!tz) {
  289. return_ACPI_STATUS(AE_NO_MEMORY);
  290. }
  291. tz->device_handle = device->handle;
  292. tz->acpi_handle = device->acpi_handle;
  293. /* TBD: How to manage 'uid' when zones are Pn_p? */
  294. sprintf(tz->uid, "%d", zone_count++);
  295. /*
  296.  * Temperature:
  297.  * ------------
  298.  * Make sure we can read the zone's current temperature (_TMP).
  299.  * If we can't, there's no use in doing any policy (abort).
  300.  */
  301. status = tz_get_temperature(tz);
  302. if (ACPI_FAILURE(status))
  303. goto end;
  304. /*
  305.  * Polling Frequency:
  306.  * ------------------
  307.  * If _TZP doesn't exist use the OS default polling frequency.
  308.  */
  309. status = bm_evaluate_simple_integer(tz->acpi_handle, "_TZP", &(tz->policy.polling_freq));
  310. if (ACPI_FAILURE(status)) {
  311. tz->policy.polling_freq = TZP;
  312. }
  313. status = AE_OK;
  314. /*
  315.  * Cooling Preference:
  316.  * -------------------
  317.  * Default to ACTIVE (noisy) cooling until policy decides otherwise.
  318.  * Note that _SCP is optional.
  319.  */
  320. tz_set_cooling_preference(tz, TZ_COOLING_MODE_ACTIVE);
  321. /*
  322.  * Start Policy:
  323.  * -------------
  324.  * Thermal policy is included in the kernel (this driver) because
  325.  * of the critical role it plays in avoiding nuclear meltdown. =O
  326.  */
  327. status = tz_policy_add_device(tz);
  328. if (ACPI_FAILURE(status))
  329. goto end;
  330. status = tz_osl_add_device(tz);
  331. if (ACPI_FAILURE(status))
  332. goto end;
  333. *context = tz;
  334. tz_print(tz);
  335. end:
  336. if (ACPI_FAILURE(status))
  337. acpi_os_free(tz);
  338. return_ACPI_STATUS(status);
  339. }
  340. /****************************************************************************
  341.  *
  342.  * FUNCTION:    tz_remove_device
  343.  *
  344.  ****************************************************************************/
  345. acpi_status
  346. tz_remove_device (
  347. void **context)
  348. {
  349. acpi_status status = AE_OK;
  350. TZ_CONTEXT *tz = NULL;
  351. FUNCTION_TRACE("tz_remove_device");
  352. if (!context || !*context) {
  353. return_ACPI_STATUS(AE_BAD_PARAMETER);
  354. }
  355. tz = (TZ_CONTEXT*)(*context);
  356. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Removing thermal zone [%02x].n", tz->device_handle));
  357. status = tz_osl_remove_device(tz);
  358. /*
  359.  * Remove Policy:
  360.  * --------------
  361.  * TBD: Move all thermal zone policy to user-mode daemon...
  362.  */
  363. status = tz_policy_remove_device(tz);
  364. if (ACPI_FAILURE(status)) {
  365. return_ACPI_STATUS(status);
  366. }
  367. acpi_os_free(tz);
  368. return_ACPI_STATUS(status);
  369. }
  370. /****************************************************************************
  371.  *                            External Functions
  372.  ****************************************************************************/
  373. /****************************************************************************
  374.  *
  375.  * FUNCTION:    tz_initialize
  376.  *
  377.  ****************************************************************************/
  378. acpi_status
  379. tz_initialize (void)
  380. {
  381. acpi_status             status = AE_OK;
  382. BM_DEVICE_ID criteria;
  383. BM_DRIVER driver;
  384. FUNCTION_TRACE("tz_initialize");
  385. memset(&criteria, 0, sizeof(BM_DEVICE_ID));
  386. memset(&driver, 0, sizeof(BM_DRIVER));
  387. /*
  388.  * Register driver for thermal zone devices.
  389.  */
  390. criteria.type = BM_TYPE_THERMAL_ZONE;
  391. driver.notify = &tz_notify;
  392. driver.request = &tz_request;
  393. status = bm_register_driver(&criteria, &driver);
  394. return_ACPI_STATUS(status);
  395. }
  396. /****************************************************************************
  397.  *
  398.  * FUNCTION:    tz_terminate
  399.  *
  400.  ****************************************************************************/
  401. acpi_status
  402. tz_terminate (void)
  403. {
  404. acpi_status             status = AE_OK;
  405. BM_DEVICE_ID criteria;
  406. BM_DRIVER driver;
  407. FUNCTION_TRACE("tz_terminate");
  408. memset(&criteria, 0, sizeof(BM_DEVICE_ID));
  409. memset(&driver, 0, sizeof(BM_DRIVER));
  410. /*
  411.  * Unregister driver for thermal zone devices.
  412.  */
  413. criteria.type = BM_TYPE_THERMAL_ZONE;
  414. driver.notify = &tz_notify;
  415. driver.request = &tz_request;
  416. status = bm_unregister_driver(&criteria, &driver);
  417. return_ACPI_STATUS(status);
  418. }
  419. /****************************************************************************
  420.  *
  421.  * FUNCTION:    tz_notify
  422.  *
  423.  ****************************************************************************/
  424. acpi_status
  425. tz_notify (
  426. BM_NOTIFY               notify_type,
  427. BM_HANDLE               device_handle,
  428. void                    **context)
  429. {
  430. acpi_status             status = AE_OK;
  431. TZ_CONTEXT *tz = NULL;
  432. FUNCTION_TRACE("tz_notify");
  433. if (!context) {
  434. return_ACPI_STATUS(AE_BAD_PARAMETER);
  435. }
  436. tz = (TZ_CONTEXT*)*context;
  437. switch (notify_type) {
  438. case BM_NOTIFY_DEVICE_ADDED:
  439. status = tz_add_device(device_handle, context);
  440. break;
  441. case BM_NOTIFY_DEVICE_REMOVED:
  442. status = tz_remove_device(context);
  443. break;
  444. case TZ_NOTIFY_TEMPERATURE_CHANGE:
  445. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Temperature (_TMP) change event detected.n"));
  446. tz_policy_check(*context);
  447. status = tz_get_temperature(tz);
  448. if (ACPI_SUCCESS(status)) {
  449. status = tz_osl_generate_event(notify_type, tz);
  450. }
  451. break;
  452. case TZ_NOTIFY_THRESHOLD_CHANGE:
  453. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Threshold (_SCP) change event detected.n"));
  454. status = tz_policy_remove_device(tz);
  455. if (ACPI_SUCCESS(status)) {
  456. status = tz_policy_add_device(tz);
  457. }
  458. status = tz_osl_generate_event(notify_type, tz);
  459. break;
  460. case TZ_NOTIFY_DEVICE_LISTS_CHANGE:
  461. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Device lists (_ALx, _PSL, _TZD) change event detected.n"));
  462. status = tz_policy_remove_device(tz);
  463. if (ACPI_SUCCESS(status)) {
  464. status = tz_policy_add_device(tz);
  465. }
  466. status = tz_osl_generate_event(notify_type, tz);
  467. break;
  468. default:
  469. status = AE_SUPPORT;
  470. break;
  471. }
  472. return_ACPI_STATUS(status);
  473. }
  474. /****************************************************************************
  475.  *
  476.  * FUNCTION:    tz_request
  477.  *
  478.  ****************************************************************************/
  479. acpi_status
  480. tz_request (
  481. BM_REQUEST *request,
  482. void                    *context)
  483. {
  484. acpi_status             status = AE_OK;
  485. TZ_CONTEXT              *tz = NULL;
  486. FUNCTION_TRACE("tz_request");
  487. /*
  488.  * Must have a valid request structure and context.
  489.  */
  490. if (!request || !context) {
  491. return_ACPI_STATUS(AE_BAD_PARAMETER);
  492. }
  493. tz = (TZ_CONTEXT*)context;
  494. /*
  495.  * Handle request:
  496.  * ---------------
  497.  */
  498. switch (request->command) {
  499. default:
  500. status = AE_SUPPORT;
  501. break;
  502. }
  503. request->status = status;
  504. return_ACPI_STATUS(status);
  505. }