evhandler.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /******************************************************************************
  2. *
  3. * Module Name: evhandler - Support for Address Space handlers
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acevents.h"
  45. #include "acnamesp.h"
  46. #include "acinterp.h"
  47. #define _COMPONENT ACPI_EVENTS
  48. ACPI_MODULE_NAME("evhandler")
  49. /* Local prototypes */
  50. static acpi_status
  51. acpi_ev_install_handler(acpi_handle obj_handle,
  52. u32 level, void *context, void **return_value);
  53. /* These are the address spaces that will get default handlers */
  54. u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
  55. ACPI_ADR_SPACE_SYSTEM_MEMORY,
  56. ACPI_ADR_SPACE_SYSTEM_IO,
  57. ACPI_ADR_SPACE_PCI_CONFIG,
  58. ACPI_ADR_SPACE_DATA_TABLE
  59. };
  60. /*******************************************************************************
  61. *
  62. * FUNCTION: acpi_ev_install_region_handlers
  63. *
  64. * PARAMETERS: None
  65. *
  66. * RETURN: Status
  67. *
  68. * DESCRIPTION: Installs the core subsystem default address space handlers.
  69. *
  70. ******************************************************************************/
  71. acpi_status acpi_ev_install_region_handlers(void)
  72. {
  73. acpi_status status;
  74. u32 i;
  75. ACPI_FUNCTION_TRACE(ev_install_region_handlers);
  76. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  77. if (ACPI_FAILURE(status)) {
  78. return_ACPI_STATUS(status);
  79. }
  80. /*
  81. * All address spaces (PCI Config, EC, SMBus) are scope dependent and
  82. * registration must occur for a specific device.
  83. *
  84. * In the case of the system memory and IO address spaces there is
  85. * currently no device associated with the address space. For these we
  86. * use the root.
  87. *
  88. * We install the default PCI config space handler at the root so that
  89. * this space is immediately available even though the we have not
  90. * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
  91. * specification which states that the PCI config space must be always
  92. * available -- even though we are nowhere near ready to find the PCI root
  93. * buses at this point.
  94. *
  95. * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
  96. * has already been installed (via acpi_install_address_space_handler).
  97. * Similar for AE_SAME_HANDLER.
  98. */
  99. for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
  100. status = acpi_ev_install_space_handler(acpi_gbl_root_node,
  101. acpi_gbl_default_address_spaces
  102. [i],
  103. ACPI_DEFAULT_HANDLER,
  104. NULL, NULL);
  105. switch (status) {
  106. case AE_OK:
  107. case AE_SAME_HANDLER:
  108. case AE_ALREADY_EXISTS:
  109. /* These exceptions are all OK */
  110. status = AE_OK;
  111. break;
  112. default:
  113. goto unlock_and_exit;
  114. }
  115. }
  116. unlock_and_exit:
  117. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  118. return_ACPI_STATUS(status);
  119. }
  120. /*******************************************************************************
  121. *
  122. * FUNCTION: acpi_ev_has_default_handler
  123. *
  124. * PARAMETERS: node - Namespace node for the device
  125. * space_id - The address space ID
  126. *
  127. * RETURN: TRUE if default handler is installed, FALSE otherwise
  128. *
  129. * DESCRIPTION: Check if the default handler is installed for the requested
  130. * space ID.
  131. *
  132. ******************************************************************************/
  133. u8
  134. acpi_ev_has_default_handler(struct acpi_namespace_node *node,
  135. acpi_adr_space_type space_id)
  136. {
  137. union acpi_operand_object *obj_desc;
  138. union acpi_operand_object *handler_obj;
  139. /* Must have an existing internal object */
  140. obj_desc = acpi_ns_get_attached_object(node);
  141. if (obj_desc) {
  142. handler_obj = obj_desc->common_notify.handler;
  143. /* Walk the linked list of handlers for this object */
  144. while (handler_obj) {
  145. if (handler_obj->address_space.space_id == space_id) {
  146. if (handler_obj->address_space.handler_flags &
  147. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
  148. return (TRUE);
  149. }
  150. }
  151. handler_obj = handler_obj->address_space.next;
  152. }
  153. }
  154. return (FALSE);
  155. }
  156. /*******************************************************************************
  157. *
  158. * FUNCTION: acpi_ev_install_handler
  159. *
  160. * PARAMETERS: walk_namespace callback
  161. *
  162. * DESCRIPTION: This routine installs an address handler into objects that are
  163. * of type Region or Device.
  164. *
  165. * If the Object is a Device, and the device has a handler of
  166. * the same type then the search is terminated in that branch.
  167. *
  168. * This is because the existing handler is closer in proximity
  169. * to any more regions than the one we are trying to install.
  170. *
  171. ******************************************************************************/
  172. static acpi_status
  173. acpi_ev_install_handler(acpi_handle obj_handle,
  174. u32 level, void *context, void **return_value)
  175. {
  176. union acpi_operand_object *handler_obj;
  177. union acpi_operand_object *next_handler_obj;
  178. union acpi_operand_object *obj_desc;
  179. struct acpi_namespace_node *node;
  180. acpi_status status;
  181. ACPI_FUNCTION_NAME(ev_install_handler);
  182. handler_obj = (union acpi_operand_object *)context;
  183. /* Parameter validation */
  184. if (!handler_obj) {
  185. return (AE_OK);
  186. }
  187. /* Convert and validate the device handle */
  188. node = acpi_ns_validate_handle(obj_handle);
  189. if (!node) {
  190. return (AE_BAD_PARAMETER);
  191. }
  192. /*
  193. * We only care about regions and objects that are allowed to have
  194. * address space handlers
  195. */
  196. if ((node->type != ACPI_TYPE_DEVICE) &&
  197. (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  198. return (AE_OK);
  199. }
  200. /* Check for an existing internal object */
  201. obj_desc = acpi_ns_get_attached_object(node);
  202. if (!obj_desc) {
  203. /* No object, just exit */
  204. return (AE_OK);
  205. }
  206. /* Devices are handled different than regions */
  207. if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
  208. /* Check if this Device already has a handler for this address space */
  209. next_handler_obj =
  210. acpi_ev_find_region_handler(handler_obj->address_space.
  211. space_id,
  212. obj_desc->common_notify.
  213. handler);
  214. if (next_handler_obj) {
  215. /* Found a handler, is it for the same address space? */
  216. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  217. "Found handler for region [%s] in device %p(%p) handler %p\n",
  218. acpi_ut_get_region_name(handler_obj->
  219. address_space.
  220. space_id),
  221. obj_desc, next_handler_obj,
  222. handler_obj));
  223. /*
  224. * Since the object we found it on was a device, then it means
  225. * that someone has already installed a handler for the branch
  226. * of the namespace from this device on. Just bail out telling
  227. * the walk routine to not traverse this branch. This preserves
  228. * the scoping rule for handlers.
  229. */
  230. return (AE_CTRL_DEPTH);
  231. }
  232. /*
  233. * As long as the device didn't have a handler for this space we
  234. * don't care about it. We just ignore it and proceed.
  235. */
  236. return (AE_OK);
  237. }
  238. /* Object is a Region */
  239. if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
  240. /* This region is for a different address space, just ignore it */
  241. return (AE_OK);
  242. }
  243. /*
  244. * Now we have a region and it is for the handler's address space type.
  245. *
  246. * First disconnect region for any previous handler (if any)
  247. */
  248. acpi_ev_detach_region(obj_desc, FALSE);
  249. /* Connect the region to the new handler */
  250. status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
  251. return (status);
  252. }
  253. /*******************************************************************************
  254. *
  255. * FUNCTION: acpi_ev_find_region_handler
  256. *
  257. * PARAMETERS: space_id - The address space ID
  258. * handler_obj - Head of the handler object list
  259. *
  260. * RETURN: Matching handler object. NULL if space ID not matched
  261. *
  262. * DESCRIPTION: Search a handler object list for a match on the address
  263. * space ID.
  264. *
  265. ******************************************************************************/
  266. union acpi_operand_object *acpi_ev_find_region_handler(acpi_adr_space_type
  267. space_id,
  268. union acpi_operand_object
  269. *handler_obj)
  270. {
  271. /* Walk the handler list for this device */
  272. while (handler_obj) {
  273. /* Same space_id indicates a handler is installed */
  274. if (handler_obj->address_space.space_id == space_id) {
  275. return (handler_obj);
  276. }
  277. /* Next handler object */
  278. handler_obj = handler_obj->address_space.next;
  279. }
  280. return (NULL);
  281. }
  282. /*******************************************************************************
  283. *
  284. * FUNCTION: acpi_ev_install_space_handler
  285. *
  286. * PARAMETERS: node - Namespace node for the device
  287. * space_id - The address space ID
  288. * handler - Address of the handler
  289. * setup - Address of the setup function
  290. * context - Value passed to the handler on each access
  291. *
  292. * RETURN: Status
  293. *
  294. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  295. * Assumes namespace is locked
  296. *
  297. ******************************************************************************/
  298. acpi_status
  299. acpi_ev_install_space_handler(struct acpi_namespace_node *node,
  300. acpi_adr_space_type space_id,
  301. acpi_adr_space_handler handler,
  302. acpi_adr_space_setup setup, void *context)
  303. {
  304. union acpi_operand_object *obj_desc;
  305. union acpi_operand_object *handler_obj;
  306. acpi_status status = AE_OK;
  307. acpi_object_type type;
  308. u8 flags = 0;
  309. ACPI_FUNCTION_TRACE(ev_install_space_handler);
  310. /*
  311. * This registration is valid for only the types below and the root.
  312. * The root node is where the default handlers get installed.
  313. */
  314. if ((node->type != ACPI_TYPE_DEVICE) &&
  315. (node->type != ACPI_TYPE_PROCESSOR) &&
  316. (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
  317. status = AE_BAD_PARAMETER;
  318. goto unlock_and_exit;
  319. }
  320. if (handler == ACPI_DEFAULT_HANDLER) {
  321. flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
  322. switch (space_id) {
  323. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  324. handler = acpi_ex_system_memory_space_handler;
  325. setup = acpi_ev_system_memory_region_setup;
  326. break;
  327. case ACPI_ADR_SPACE_SYSTEM_IO:
  328. handler = acpi_ex_system_io_space_handler;
  329. setup = acpi_ev_io_space_region_setup;
  330. break;
  331. case ACPI_ADR_SPACE_PCI_CONFIG:
  332. handler = acpi_ex_pci_config_space_handler;
  333. setup = acpi_ev_pci_config_region_setup;
  334. break;
  335. case ACPI_ADR_SPACE_CMOS:
  336. handler = acpi_ex_cmos_space_handler;
  337. setup = acpi_ev_cmos_region_setup;
  338. break;
  339. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  340. handler = acpi_ex_pci_bar_space_handler;
  341. setup = acpi_ev_pci_bar_region_setup;
  342. break;
  343. case ACPI_ADR_SPACE_DATA_TABLE:
  344. handler = acpi_ex_data_table_space_handler;
  345. setup = NULL;
  346. break;
  347. default:
  348. status = AE_BAD_PARAMETER;
  349. goto unlock_and_exit;
  350. }
  351. }
  352. /* If the caller hasn't specified a setup routine, use the default */
  353. if (!setup) {
  354. setup = acpi_ev_default_region_setup;
  355. }
  356. /* Check for an existing internal object */
  357. obj_desc = acpi_ns_get_attached_object(node);
  358. if (obj_desc) {
  359. /*
  360. * The attached device object already exists. Now make sure
  361. * the handler is not already installed.
  362. */
  363. handler_obj = acpi_ev_find_region_handler(space_id,
  364. obj_desc->
  365. common_notify.
  366. handler);
  367. if (handler_obj) {
  368. if (handler_obj->address_space.handler == handler) {
  369. /*
  370. * It is (relatively) OK to attempt to install the SAME
  371. * handler twice. This can easily happen with the
  372. * PCI_Config space.
  373. */
  374. status = AE_SAME_HANDLER;
  375. goto unlock_and_exit;
  376. } else {
  377. /* A handler is already installed */
  378. status = AE_ALREADY_EXISTS;
  379. }
  380. goto unlock_and_exit;
  381. }
  382. } else {
  383. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  384. "Creating object on Device %p while installing handler\n",
  385. node));
  386. /* obj_desc does not exist, create one */
  387. if (node->type == ACPI_TYPE_ANY) {
  388. type = ACPI_TYPE_DEVICE;
  389. } else {
  390. type = node->type;
  391. }
  392. obj_desc = acpi_ut_create_internal_object(type);
  393. if (!obj_desc) {
  394. status = AE_NO_MEMORY;
  395. goto unlock_and_exit;
  396. }
  397. /* Init new descriptor */
  398. obj_desc->common.type = (u8)type;
  399. /* Attach the new object to the Node */
  400. status = acpi_ns_attach_object(node, obj_desc, type);
  401. /* Remove local reference to the object */
  402. acpi_ut_remove_reference(obj_desc);
  403. if (ACPI_FAILURE(status)) {
  404. goto unlock_and_exit;
  405. }
  406. }
  407. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  408. "Installing address handler for region %s(%X) "
  409. "on Device %4.4s %p(%p)\n",
  410. acpi_ut_get_region_name(space_id), space_id,
  411. acpi_ut_get_node_name(node), node, obj_desc));
  412. /*
  413. * Install the handler
  414. *
  415. * At this point there is no existing handler. Just allocate the object
  416. * for the handler and link it into the list.
  417. */
  418. handler_obj =
  419. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
  420. if (!handler_obj) {
  421. status = AE_NO_MEMORY;
  422. goto unlock_and_exit;
  423. }
  424. /* Init handler obj */
  425. handler_obj->address_space.space_id = (u8)space_id;
  426. handler_obj->address_space.handler_flags = flags;
  427. handler_obj->address_space.region_list = NULL;
  428. handler_obj->address_space.node = node;
  429. handler_obj->address_space.handler = handler;
  430. handler_obj->address_space.context = context;
  431. handler_obj->address_space.setup = setup;
  432. /* Install at head of Device.address_space list */
  433. handler_obj->address_space.next = obj_desc->common_notify.handler;
  434. /*
  435. * The Device object is the first reference on the handler_obj.
  436. * Each region that uses the handler adds a reference.
  437. */
  438. obj_desc->common_notify.handler = handler_obj;
  439. /*
  440. * Walk the namespace finding all of the regions this handler will
  441. * manage.
  442. *
  443. * Start at the device and search the branch toward the leaf nodes
  444. * until either the leaf is encountered or a device is detected that
  445. * has an address handler of the same type.
  446. *
  447. * In either case, back up and search down the remainder of the branch
  448. */
  449. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node,
  450. ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
  451. acpi_ev_install_handler, NULL,
  452. handler_obj, NULL);
  453. unlock_and_exit:
  454. return_ACPI_STATUS(status);
  455. }