evhandler.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /******************************************************************************
  2. *
  3. * Module Name: evhandler - Support for Address Space handlers
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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->device.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 = obj_desc->device.handler;
  210. while (next_handler_obj) {
  211. /* Found a handler, is it for the same address space? */
  212. if (next_handler_obj->address_space.space_id ==
  213. handler_obj->address_space.space_id) {
  214. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  215. "Found handler for region [%s] in device %p(%p) "
  216. "handler %p\n",
  217. acpi_ut_get_region_name
  218. (handler_obj->address_space.
  219. space_id), obj_desc,
  220. next_handler_obj,
  221. handler_obj));
  222. /*
  223. * Since the object we found it on was a device, then it
  224. * means that someone has already installed a handler for
  225. * the branch of the namespace from this device on. Just
  226. * bail out telling the walk routine to not traverse this
  227. * branch. This preserves the scoping rule for handlers.
  228. */
  229. return (AE_CTRL_DEPTH);
  230. }
  231. /* Walk the linked list of handlers attached to this device */
  232. next_handler_obj = next_handler_obj->address_space.next;
  233. }
  234. /*
  235. * As long as the device didn't have a handler for this space we
  236. * don't care about it. We just ignore it and proceed.
  237. */
  238. return (AE_OK);
  239. }
  240. /* Object is a Region */
  241. if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
  242. /* This region is for a different address space, just ignore it */
  243. return (AE_OK);
  244. }
  245. /*
  246. * Now we have a region and it is for the handler's address space type.
  247. *
  248. * First disconnect region for any previous handler (if any)
  249. */
  250. acpi_ev_detach_region(obj_desc, FALSE);
  251. /* Connect the region to the new handler */
  252. status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
  253. return (status);
  254. }
  255. /*******************************************************************************
  256. *
  257. * FUNCTION: acpi_ev_install_space_handler
  258. *
  259. * PARAMETERS: node - Namespace node for the device
  260. * space_id - The address space ID
  261. * handler - Address of the handler
  262. * setup - Address of the setup function
  263. * context - Value passed to the handler on each access
  264. *
  265. * RETURN: Status
  266. *
  267. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  268. * Assumes namespace is locked
  269. *
  270. ******************************************************************************/
  271. acpi_status
  272. acpi_ev_install_space_handler(struct acpi_namespace_node * node,
  273. acpi_adr_space_type space_id,
  274. acpi_adr_space_handler handler,
  275. acpi_adr_space_setup setup, void *context)
  276. {
  277. union acpi_operand_object *obj_desc;
  278. union acpi_operand_object *handler_obj;
  279. acpi_status status;
  280. acpi_object_type type;
  281. u8 flags = 0;
  282. ACPI_FUNCTION_TRACE(ev_install_space_handler);
  283. /*
  284. * This registration is valid for only the types below and the root. This
  285. * is where the default handlers get placed.
  286. */
  287. if ((node->type != ACPI_TYPE_DEVICE) &&
  288. (node->type != ACPI_TYPE_PROCESSOR) &&
  289. (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
  290. status = AE_BAD_PARAMETER;
  291. goto unlock_and_exit;
  292. }
  293. if (handler == ACPI_DEFAULT_HANDLER) {
  294. flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
  295. switch (space_id) {
  296. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  297. handler = acpi_ex_system_memory_space_handler;
  298. setup = acpi_ev_system_memory_region_setup;
  299. break;
  300. case ACPI_ADR_SPACE_SYSTEM_IO:
  301. handler = acpi_ex_system_io_space_handler;
  302. setup = acpi_ev_io_space_region_setup;
  303. break;
  304. case ACPI_ADR_SPACE_PCI_CONFIG:
  305. handler = acpi_ex_pci_config_space_handler;
  306. setup = acpi_ev_pci_config_region_setup;
  307. break;
  308. case ACPI_ADR_SPACE_CMOS:
  309. handler = acpi_ex_cmos_space_handler;
  310. setup = acpi_ev_cmos_region_setup;
  311. break;
  312. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  313. handler = acpi_ex_pci_bar_space_handler;
  314. setup = acpi_ev_pci_bar_region_setup;
  315. break;
  316. case ACPI_ADR_SPACE_DATA_TABLE:
  317. handler = acpi_ex_data_table_space_handler;
  318. setup = NULL;
  319. break;
  320. default:
  321. status = AE_BAD_PARAMETER;
  322. goto unlock_and_exit;
  323. }
  324. }
  325. /* If the caller hasn't specified a setup routine, use the default */
  326. if (!setup) {
  327. setup = acpi_ev_default_region_setup;
  328. }
  329. /* Check for an existing internal object */
  330. obj_desc = acpi_ns_get_attached_object(node);
  331. if (obj_desc) {
  332. /*
  333. * The attached device object already exists. Make sure the handler
  334. * is not already installed.
  335. */
  336. handler_obj = obj_desc->device.handler;
  337. /* Walk the handler list for this device */
  338. while (handler_obj) {
  339. /* Same space_id indicates a handler already installed */
  340. if (handler_obj->address_space.space_id == space_id) {
  341. if (handler_obj->address_space.handler ==
  342. handler) {
  343. /*
  344. * It is (relatively) OK to attempt to install the SAME
  345. * handler twice. This can easily happen with the
  346. * PCI_Config space.
  347. */
  348. status = AE_SAME_HANDLER;
  349. goto unlock_and_exit;
  350. } else {
  351. /* A handler is already installed */
  352. status = AE_ALREADY_EXISTS;
  353. }
  354. goto unlock_and_exit;
  355. }
  356. /* Walk the linked list of handlers */
  357. handler_obj = handler_obj->address_space.next;
  358. }
  359. } else {
  360. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  361. "Creating object on Device %p while installing handler\n",
  362. node));
  363. /* obj_desc does not exist, create one */
  364. if (node->type == ACPI_TYPE_ANY) {
  365. type = ACPI_TYPE_DEVICE;
  366. } else {
  367. type = node->type;
  368. }
  369. obj_desc = acpi_ut_create_internal_object(type);
  370. if (!obj_desc) {
  371. status = AE_NO_MEMORY;
  372. goto unlock_and_exit;
  373. }
  374. /* Init new descriptor */
  375. obj_desc->common.type = (u8)type;
  376. /* Attach the new object to the Node */
  377. status = acpi_ns_attach_object(node, obj_desc, type);
  378. /* Remove local reference to the object */
  379. acpi_ut_remove_reference(obj_desc);
  380. if (ACPI_FAILURE(status)) {
  381. goto unlock_and_exit;
  382. }
  383. }
  384. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  385. "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
  386. acpi_ut_get_region_name(space_id), space_id,
  387. acpi_ut_get_node_name(node), node, obj_desc));
  388. /*
  389. * Install the handler
  390. *
  391. * At this point there is no existing handler. Just allocate the object
  392. * for the handler and link it into the list.
  393. */
  394. handler_obj =
  395. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
  396. if (!handler_obj) {
  397. status = AE_NO_MEMORY;
  398. goto unlock_and_exit;
  399. }
  400. /* Init handler obj */
  401. handler_obj->address_space.space_id = (u8)space_id;
  402. handler_obj->address_space.handler_flags = flags;
  403. handler_obj->address_space.region_list = NULL;
  404. handler_obj->address_space.node = node;
  405. handler_obj->address_space.handler = handler;
  406. handler_obj->address_space.context = context;
  407. handler_obj->address_space.setup = setup;
  408. /* Install at head of Device.address_space list */
  409. handler_obj->address_space.next = obj_desc->device.handler;
  410. /*
  411. * The Device object is the first reference on the handler_obj.
  412. * Each region that uses the handler adds a reference.
  413. */
  414. obj_desc->device.handler = handler_obj;
  415. /*
  416. * Walk the namespace finding all of the regions this
  417. * handler will manage.
  418. *
  419. * Start at the device and search the branch toward
  420. * the leaf nodes until either the leaf is encountered or
  421. * a device is detected that has an address handler of the
  422. * same type.
  423. *
  424. * In either case, back up and search down the remainder
  425. * of the branch
  426. */
  427. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  428. ACPI_NS_WALK_UNLOCK,
  429. acpi_ev_install_handler, NULL,
  430. handler_obj, NULL);
  431. unlock_and_exit:
  432. return_ACPI_STATUS(status);
  433. }