scan.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * scan.c - support for transforming the ACPI namespace into individual objects
  4. */
  5. #define pr_fmt(fmt) "ACPI: " fmt
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/kernel.h>
  10. #include <linux/acpi.h>
  11. #include <linux/acpi_iort.h>
  12. #include <linux/acpi_viot.h>
  13. #include <linux/iommu.h>
  14. #include <linux/signal.h>
  15. #include <linux/kthread.h>
  16. #include <linux/dmi.h>
  17. #include <linux/dma-map-ops.h>
  18. #include <linux/platform_data/x86/apple.h>
  19. #include <linux/pgtable.h>
  20. #include <linux/crc32.h>
  21. #include <linux/dma-direct.h>
  22. #include "internal.h"
  23. extern struct acpi_device *acpi_root;
  24. #define ACPI_BUS_CLASS "system_bus"
  25. #define ACPI_BUS_HID "LNXSYBUS"
  26. #define ACPI_BUS_DEVICE_NAME "System Bus"
  27. #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
  28. static const char *dummy_hid = "device";
  29. static LIST_HEAD(acpi_dep_list);
  30. static DEFINE_MUTEX(acpi_dep_list_lock);
  31. LIST_HEAD(acpi_bus_id_list);
  32. static DEFINE_MUTEX(acpi_scan_lock);
  33. static LIST_HEAD(acpi_scan_handlers_list);
  34. DEFINE_MUTEX(acpi_device_lock);
  35. LIST_HEAD(acpi_wakeup_device_list);
  36. static DEFINE_MUTEX(acpi_hp_context_lock);
  37. /*
  38. * The UART device described by the SPCR table is the only object which needs
  39. * special-casing. Everything else is covered by ACPI namespace paths in STAO
  40. * table.
  41. */
  42. static u64 spcr_uart_addr;
  43. void acpi_scan_lock_acquire(void)
  44. {
  45. mutex_lock(&acpi_scan_lock);
  46. }
  47. EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
  48. void acpi_scan_lock_release(void)
  49. {
  50. mutex_unlock(&acpi_scan_lock);
  51. }
  52. EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
  53. void acpi_lock_hp_context(void)
  54. {
  55. mutex_lock(&acpi_hp_context_lock);
  56. }
  57. void acpi_unlock_hp_context(void)
  58. {
  59. mutex_unlock(&acpi_hp_context_lock);
  60. }
  61. void acpi_initialize_hp_context(struct acpi_device *adev,
  62. struct acpi_hotplug_context *hp,
  63. int (*notify)(struct acpi_device *, u32),
  64. void (*uevent)(struct acpi_device *, u32))
  65. {
  66. acpi_lock_hp_context();
  67. hp->notify = notify;
  68. hp->uevent = uevent;
  69. acpi_set_hp_context(adev, hp);
  70. acpi_unlock_hp_context();
  71. }
  72. EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
  73. int acpi_scan_add_handler(struct acpi_scan_handler *handler)
  74. {
  75. if (!handler)
  76. return -EINVAL;
  77. list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
  78. return 0;
  79. }
  80. int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
  81. const char *hotplug_profile_name)
  82. {
  83. int error;
  84. error = acpi_scan_add_handler(handler);
  85. if (error)
  86. return error;
  87. acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
  88. return 0;
  89. }
  90. bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
  91. {
  92. struct acpi_device_physical_node *pn;
  93. bool offline = true;
  94. char *envp[] = { "EVENT=offline", NULL };
  95. /*
  96. * acpi_container_offline() calls this for all of the container's
  97. * children under the container's physical_node_lock lock.
  98. */
  99. mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
  100. list_for_each_entry(pn, &adev->physical_node_list, node)
  101. if (device_supports_offline(pn->dev) && !pn->dev->offline) {
  102. if (uevent)
  103. kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
  104. offline = false;
  105. break;
  106. }
  107. mutex_unlock(&adev->physical_node_lock);
  108. return offline;
  109. }
  110. static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
  111. void **ret_p)
  112. {
  113. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  114. struct acpi_device_physical_node *pn;
  115. bool second_pass = (bool)data;
  116. acpi_status status = AE_OK;
  117. if (!device)
  118. return AE_OK;
  119. if (device->handler && !device->handler->hotplug.enabled) {
  120. *ret_p = &device->dev;
  121. return AE_SUPPORT;
  122. }
  123. mutex_lock(&device->physical_node_lock);
  124. list_for_each_entry(pn, &device->physical_node_list, node) {
  125. int ret;
  126. if (second_pass) {
  127. /* Skip devices offlined by the first pass. */
  128. if (pn->put_online)
  129. continue;
  130. } else {
  131. pn->put_online = false;
  132. }
  133. ret = device_offline(pn->dev);
  134. if (ret >= 0) {
  135. pn->put_online = !ret;
  136. } else {
  137. *ret_p = pn->dev;
  138. if (second_pass) {
  139. status = AE_ERROR;
  140. break;
  141. }
  142. }
  143. }
  144. mutex_unlock(&device->physical_node_lock);
  145. return status;
  146. }
  147. static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
  148. void **ret_p)
  149. {
  150. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  151. struct acpi_device_physical_node *pn;
  152. if (!device)
  153. return AE_OK;
  154. mutex_lock(&device->physical_node_lock);
  155. list_for_each_entry(pn, &device->physical_node_list, node)
  156. if (pn->put_online) {
  157. device_online(pn->dev);
  158. pn->put_online = false;
  159. }
  160. mutex_unlock(&device->physical_node_lock);
  161. return AE_OK;
  162. }
  163. static int acpi_scan_try_to_offline(struct acpi_device *device)
  164. {
  165. acpi_handle handle = device->handle;
  166. struct device *errdev = NULL;
  167. acpi_status status;
  168. /*
  169. * Carry out two passes here and ignore errors in the first pass,
  170. * because if the devices in question are memory blocks and
  171. * CONFIG_MEMCG is set, one of the blocks may hold data structures
  172. * that the other blocks depend on, but it is not known in advance which
  173. * block holds them.
  174. *
  175. * If the first pass is successful, the second one isn't needed, though.
  176. */
  177. status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  178. NULL, acpi_bus_offline, (void *)false,
  179. (void **)&errdev);
  180. if (status == AE_SUPPORT) {
  181. dev_warn(errdev, "Offline disabled.\n");
  182. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  183. acpi_bus_online, NULL, NULL, NULL);
  184. return -EPERM;
  185. }
  186. acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
  187. if (errdev) {
  188. errdev = NULL;
  189. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  190. NULL, acpi_bus_offline, (void *)true,
  191. (void **)&errdev);
  192. if (!errdev)
  193. acpi_bus_offline(handle, 0, (void *)true,
  194. (void **)&errdev);
  195. if (errdev) {
  196. dev_warn(errdev, "Offline failed.\n");
  197. acpi_bus_online(handle, 0, NULL, NULL);
  198. acpi_walk_namespace(ACPI_TYPE_ANY, handle,
  199. ACPI_UINT32_MAX, acpi_bus_online,
  200. NULL, NULL, NULL);
  201. return -EBUSY;
  202. }
  203. }
  204. return 0;
  205. }
  206. static int acpi_scan_hot_remove(struct acpi_device *device)
  207. {
  208. acpi_handle handle = device->handle;
  209. unsigned long long sta;
  210. acpi_status status;
  211. if (device->handler && device->handler->hotplug.demand_offline) {
  212. if (!acpi_scan_is_offline(device, true))
  213. return -EBUSY;
  214. } else {
  215. int error = acpi_scan_try_to_offline(device);
  216. if (error)
  217. return error;
  218. }
  219. acpi_handle_debug(handle, "Ejecting\n");
  220. acpi_bus_trim(device);
  221. acpi_evaluate_lck(handle, 0);
  222. /*
  223. * TBD: _EJD support.
  224. */
  225. status = acpi_evaluate_ej0(handle);
  226. if (status == AE_NOT_FOUND)
  227. return -ENODEV;
  228. else if (ACPI_FAILURE(status))
  229. return -EIO;
  230. /*
  231. * Verify if eject was indeed successful. If not, log an error
  232. * message. No need to call _OST since _EJ0 call was made OK.
  233. */
  234. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  235. if (ACPI_FAILURE(status)) {
  236. acpi_handle_warn(handle,
  237. "Status check after eject failed (0x%x)\n", status);
  238. } else if (sta & ACPI_STA_DEVICE_ENABLED) {
  239. acpi_handle_warn(handle,
  240. "Eject incomplete - status 0x%llx\n", sta);
  241. }
  242. return 0;
  243. }
  244. static int acpi_scan_device_not_present(struct acpi_device *adev)
  245. {
  246. if (!acpi_device_enumerated(adev)) {
  247. dev_warn(&adev->dev, "Still not present\n");
  248. return -EALREADY;
  249. }
  250. acpi_bus_trim(adev);
  251. return 0;
  252. }
  253. static int acpi_scan_device_check(struct acpi_device *adev)
  254. {
  255. int error;
  256. acpi_bus_get_status(adev);
  257. if (adev->status.present || adev->status.functional) {
  258. /*
  259. * This function is only called for device objects for which
  260. * matching scan handlers exist. The only situation in which
  261. * the scan handler is not attached to this device object yet
  262. * is when the device has just appeared (either it wasn't
  263. * present at all before or it was removed and then added
  264. * again).
  265. */
  266. if (adev->handler) {
  267. dev_warn(&adev->dev, "Already enumerated\n");
  268. return -EALREADY;
  269. }
  270. error = acpi_bus_scan(adev->handle);
  271. if (error) {
  272. dev_warn(&adev->dev, "Namespace scan failure\n");
  273. return error;
  274. }
  275. if (!adev->handler) {
  276. dev_warn(&adev->dev, "Enumeration failure\n");
  277. error = -ENODEV;
  278. }
  279. } else {
  280. error = acpi_scan_device_not_present(adev);
  281. }
  282. return error;
  283. }
  284. static int acpi_scan_bus_check(struct acpi_device *adev, void *not_used)
  285. {
  286. struct acpi_scan_handler *handler = adev->handler;
  287. int error;
  288. acpi_bus_get_status(adev);
  289. if (!(adev->status.present || adev->status.functional)) {
  290. acpi_scan_device_not_present(adev);
  291. return 0;
  292. }
  293. if (handler && handler->hotplug.scan_dependent)
  294. return handler->hotplug.scan_dependent(adev);
  295. error = acpi_bus_scan(adev->handle);
  296. if (error) {
  297. dev_warn(&adev->dev, "Namespace scan failure\n");
  298. return error;
  299. }
  300. return acpi_dev_for_each_child(adev, acpi_scan_bus_check, NULL);
  301. }
  302. static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
  303. {
  304. switch (type) {
  305. case ACPI_NOTIFY_BUS_CHECK:
  306. return acpi_scan_bus_check(adev, NULL);
  307. case ACPI_NOTIFY_DEVICE_CHECK:
  308. return acpi_scan_device_check(adev);
  309. case ACPI_NOTIFY_EJECT_REQUEST:
  310. case ACPI_OST_EC_OSPM_EJECT:
  311. if (adev->handler && !adev->handler->hotplug.enabled) {
  312. dev_info(&adev->dev, "Eject disabled\n");
  313. return -EPERM;
  314. }
  315. acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
  316. ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
  317. return acpi_scan_hot_remove(adev);
  318. }
  319. return -EINVAL;
  320. }
  321. void acpi_device_hotplug(struct acpi_device *adev, u32 src)
  322. {
  323. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  324. int error = -ENODEV;
  325. lock_device_hotplug();
  326. mutex_lock(&acpi_scan_lock);
  327. /*
  328. * The device object's ACPI handle cannot become invalid as long as we
  329. * are holding acpi_scan_lock, but it might have become invalid before
  330. * that lock was acquired.
  331. */
  332. if (adev->handle == INVALID_ACPI_HANDLE)
  333. goto err_out;
  334. if (adev->flags.is_dock_station) {
  335. error = dock_notify(adev, src);
  336. } else if (adev->flags.hotplug_notify) {
  337. error = acpi_generic_hotplug_event(adev, src);
  338. } else {
  339. int (*notify)(struct acpi_device *, u32);
  340. acpi_lock_hp_context();
  341. notify = adev->hp ? adev->hp->notify : NULL;
  342. acpi_unlock_hp_context();
  343. /*
  344. * There may be additional notify handlers for device objects
  345. * without the .event() callback, so ignore them here.
  346. */
  347. if (notify)
  348. error = notify(adev, src);
  349. else
  350. goto out;
  351. }
  352. switch (error) {
  353. case 0:
  354. ost_code = ACPI_OST_SC_SUCCESS;
  355. break;
  356. case -EPERM:
  357. ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
  358. break;
  359. case -EBUSY:
  360. ost_code = ACPI_OST_SC_DEVICE_BUSY;
  361. break;
  362. default:
  363. ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  364. break;
  365. }
  366. err_out:
  367. acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
  368. out:
  369. acpi_put_acpi_dev(adev);
  370. mutex_unlock(&acpi_scan_lock);
  371. unlock_device_hotplug();
  372. }
  373. static void acpi_free_power_resources_lists(struct acpi_device *device)
  374. {
  375. int i;
  376. if (device->wakeup.flags.valid)
  377. acpi_power_resources_list_free(&device->wakeup.resources);
  378. if (!device->power.flags.power_resources)
  379. return;
  380. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  381. struct acpi_device_power_state *ps = &device->power.states[i];
  382. acpi_power_resources_list_free(&ps->resources);
  383. }
  384. }
  385. static void acpi_device_release(struct device *dev)
  386. {
  387. struct acpi_device *acpi_dev = to_acpi_device(dev);
  388. acpi_free_properties(acpi_dev);
  389. acpi_free_pnp_ids(&acpi_dev->pnp);
  390. acpi_free_power_resources_lists(acpi_dev);
  391. kfree(acpi_dev);
  392. }
  393. static void acpi_device_del(struct acpi_device *device)
  394. {
  395. struct acpi_device_bus_id *acpi_device_bus_id;
  396. mutex_lock(&acpi_device_lock);
  397. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
  398. if (!strcmp(acpi_device_bus_id->bus_id,
  399. acpi_device_hid(device))) {
  400. ida_free(&acpi_device_bus_id->instance_ida,
  401. device->pnp.instance_no);
  402. if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
  403. list_del(&acpi_device_bus_id->node);
  404. kfree_const(acpi_device_bus_id->bus_id);
  405. kfree(acpi_device_bus_id);
  406. }
  407. break;
  408. }
  409. list_del(&device->wakeup_list);
  410. mutex_unlock(&acpi_device_lock);
  411. acpi_power_add_remove_device(device, false);
  412. acpi_device_remove_files(device);
  413. if (device->remove)
  414. device->remove(device);
  415. device_del(&device->dev);
  416. }
  417. static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
  418. static LIST_HEAD(acpi_device_del_list);
  419. static DEFINE_MUTEX(acpi_device_del_lock);
  420. static void acpi_device_del_work_fn(struct work_struct *work_not_used)
  421. {
  422. for (;;) {
  423. struct acpi_device *adev;
  424. mutex_lock(&acpi_device_del_lock);
  425. if (list_empty(&acpi_device_del_list)) {
  426. mutex_unlock(&acpi_device_del_lock);
  427. break;
  428. }
  429. adev = list_first_entry(&acpi_device_del_list,
  430. struct acpi_device, del_list);
  431. list_del(&adev->del_list);
  432. mutex_unlock(&acpi_device_del_lock);
  433. blocking_notifier_call_chain(&acpi_reconfig_chain,
  434. ACPI_RECONFIG_DEVICE_REMOVE, adev);
  435. acpi_device_del(adev);
  436. /*
  437. * Drop references to all power resources that might have been
  438. * used by the device.
  439. */
  440. acpi_power_transition(adev, ACPI_STATE_D3_COLD);
  441. acpi_dev_put(adev);
  442. }
  443. }
  444. /**
  445. * acpi_scan_drop_device - Drop an ACPI device object.
  446. * @handle: Handle of an ACPI namespace node, not used.
  447. * @context: Address of the ACPI device object to drop.
  448. *
  449. * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
  450. * namespace node the device object pointed to by @context is attached to.
  451. *
  452. * The unregistration is carried out asynchronously to avoid running
  453. * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
  454. * ensure the correct ordering (the device objects must be unregistered in the
  455. * same order in which the corresponding namespace nodes are deleted).
  456. */
  457. static void acpi_scan_drop_device(acpi_handle handle, void *context)
  458. {
  459. static DECLARE_WORK(work, acpi_device_del_work_fn);
  460. struct acpi_device *adev = context;
  461. mutex_lock(&acpi_device_del_lock);
  462. /*
  463. * Use the ACPI hotplug workqueue which is ordered, so this work item
  464. * won't run after any hotplug work items submitted subsequently. That
  465. * prevents attempts to register device objects identical to those being
  466. * deleted from happening concurrently (such attempts result from
  467. * hotplug events handled via the ACPI hotplug workqueue). It also will
  468. * run after all of the work items submitted previously, which helps
  469. * those work items to ensure that they are not accessing stale device
  470. * objects.
  471. */
  472. if (list_empty(&acpi_device_del_list))
  473. acpi_queue_hotplug_work(&work);
  474. list_add_tail(&adev->del_list, &acpi_device_del_list);
  475. /* Make acpi_ns_validate_handle() return NULL for this handle. */
  476. adev->handle = INVALID_ACPI_HANDLE;
  477. mutex_unlock(&acpi_device_del_lock);
  478. }
  479. static struct acpi_device *handle_to_device(acpi_handle handle,
  480. void (*callback)(void *))
  481. {
  482. struct acpi_device *adev = NULL;
  483. acpi_status status;
  484. status = acpi_get_data_full(handle, acpi_scan_drop_device,
  485. (void **)&adev, callback);
  486. if (ACPI_FAILURE(status) || !adev) {
  487. acpi_handle_debug(handle, "No context!\n");
  488. return NULL;
  489. }
  490. return adev;
  491. }
  492. /**
  493. * acpi_fetch_acpi_dev - Retrieve ACPI device object.
  494. * @handle: ACPI handle associated with the requested ACPI device object.
  495. *
  496. * Return a pointer to the ACPI device object associated with @handle, if
  497. * present, or NULL otherwise.
  498. */
  499. struct acpi_device *acpi_fetch_acpi_dev(acpi_handle handle)
  500. {
  501. return handle_to_device(handle, NULL);
  502. }
  503. EXPORT_SYMBOL_GPL(acpi_fetch_acpi_dev);
  504. static void get_acpi_device(void *dev)
  505. {
  506. acpi_dev_get(dev);
  507. }
  508. /**
  509. * acpi_get_acpi_dev - Retrieve ACPI device object and reference count it.
  510. * @handle: ACPI handle associated with the requested ACPI device object.
  511. *
  512. * Return a pointer to the ACPI device object associated with @handle and bump
  513. * up that object's reference counter (under the ACPI Namespace lock), if
  514. * present, or return NULL otherwise.
  515. *
  516. * The ACPI device object reference acquired by this function needs to be
  517. * dropped via acpi_dev_put().
  518. */
  519. struct acpi_device *acpi_get_acpi_dev(acpi_handle handle)
  520. {
  521. return handle_to_device(handle, get_acpi_device);
  522. }
  523. EXPORT_SYMBOL_GPL(acpi_get_acpi_dev);
  524. static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
  525. {
  526. struct acpi_device_bus_id *acpi_device_bus_id;
  527. /* Find suitable bus_id and instance number in acpi_bus_id_list. */
  528. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  529. if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
  530. return acpi_device_bus_id;
  531. }
  532. return NULL;
  533. }
  534. static int acpi_device_set_name(struct acpi_device *device,
  535. struct acpi_device_bus_id *acpi_device_bus_id)
  536. {
  537. struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
  538. int result;
  539. result = ida_alloc(instance_ida, GFP_KERNEL);
  540. if (result < 0)
  541. return result;
  542. device->pnp.instance_no = result;
  543. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
  544. return 0;
  545. }
  546. int acpi_tie_acpi_dev(struct acpi_device *adev)
  547. {
  548. acpi_handle handle = adev->handle;
  549. acpi_status status;
  550. if (!handle)
  551. return 0;
  552. status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
  553. if (ACPI_FAILURE(status)) {
  554. acpi_handle_err(handle, "Unable to attach device data\n");
  555. return -ENODEV;
  556. }
  557. return 0;
  558. }
  559. static void acpi_store_pld_crc(struct acpi_device *adev)
  560. {
  561. struct acpi_pld_info *pld;
  562. acpi_status status;
  563. status = acpi_get_physical_device_location(adev->handle, &pld);
  564. if (ACPI_FAILURE(status))
  565. return;
  566. adev->pld_crc = crc32(~0, pld, sizeof(*pld));
  567. ACPI_FREE(pld);
  568. }
  569. int acpi_device_add(struct acpi_device *device)
  570. {
  571. struct acpi_device_bus_id *acpi_device_bus_id;
  572. int result;
  573. /*
  574. * Linkage
  575. * -------
  576. * Link this device to its parent and siblings.
  577. */
  578. INIT_LIST_HEAD(&device->wakeup_list);
  579. INIT_LIST_HEAD(&device->physical_node_list);
  580. INIT_LIST_HEAD(&device->del_list);
  581. mutex_init(&device->physical_node_lock);
  582. mutex_lock(&acpi_device_lock);
  583. acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
  584. if (acpi_device_bus_id) {
  585. result = acpi_device_set_name(device, acpi_device_bus_id);
  586. if (result)
  587. goto err_unlock;
  588. } else {
  589. acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
  590. GFP_KERNEL);
  591. if (!acpi_device_bus_id) {
  592. result = -ENOMEM;
  593. goto err_unlock;
  594. }
  595. acpi_device_bus_id->bus_id =
  596. kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
  597. if (!acpi_device_bus_id->bus_id) {
  598. kfree(acpi_device_bus_id);
  599. result = -ENOMEM;
  600. goto err_unlock;
  601. }
  602. ida_init(&acpi_device_bus_id->instance_ida);
  603. result = acpi_device_set_name(device, acpi_device_bus_id);
  604. if (result) {
  605. kfree_const(acpi_device_bus_id->bus_id);
  606. kfree(acpi_device_bus_id);
  607. goto err_unlock;
  608. }
  609. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  610. }
  611. if (device->wakeup.flags.valid)
  612. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  613. acpi_store_pld_crc(device);
  614. mutex_unlock(&acpi_device_lock);
  615. result = device_add(&device->dev);
  616. if (result) {
  617. dev_err(&device->dev, "Error registering device\n");
  618. goto err;
  619. }
  620. result = acpi_device_setup_files(device);
  621. if (result)
  622. pr_err("Error creating sysfs interface for device %s\n",
  623. dev_name(&device->dev));
  624. return 0;
  625. err:
  626. mutex_lock(&acpi_device_lock);
  627. list_del(&device->wakeup_list);
  628. err_unlock:
  629. mutex_unlock(&acpi_device_lock);
  630. acpi_detach_data(device->handle, acpi_scan_drop_device);
  631. return result;
  632. }
  633. /* --------------------------------------------------------------------------
  634. Device Enumeration
  635. -------------------------------------------------------------------------- */
  636. static bool acpi_info_matches_ids(struct acpi_device_info *info,
  637. const char * const ids[])
  638. {
  639. struct acpi_pnp_device_id_list *cid_list = NULL;
  640. int i, index;
  641. if (!(info->valid & ACPI_VALID_HID))
  642. return false;
  643. index = match_string(ids, -1, info->hardware_id.string);
  644. if (index >= 0)
  645. return true;
  646. if (info->valid & ACPI_VALID_CID)
  647. cid_list = &info->compatible_id_list;
  648. if (!cid_list)
  649. return false;
  650. for (i = 0; i < cid_list->count; i++) {
  651. index = match_string(ids, -1, cid_list->ids[i].string);
  652. if (index >= 0)
  653. return true;
  654. }
  655. return false;
  656. }
  657. /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
  658. static const char * const acpi_ignore_dep_ids[] = {
  659. "PNP0D80", /* Windows-compatible System Power Management Controller */
  660. "INT33BD", /* Intel Baytrail Mailbox Device */
  661. "LATT2021", /* Lattice FW Update Client Driver */
  662. NULL
  663. };
  664. /* List of HIDs for which we honor deps of matching ACPI devs, when checking _DEP lists. */
  665. static const char * const acpi_honor_dep_ids[] = {
  666. "INT3472", /* Camera sensor PMIC / clk and regulator info */
  667. NULL
  668. };
  669. static struct acpi_device *acpi_find_parent_acpi_dev(acpi_handle handle)
  670. {
  671. struct acpi_device *adev;
  672. /*
  673. * Fixed hardware devices do not appear in the namespace and do not
  674. * have handles, but we fabricate acpi_devices for them, so we have
  675. * to deal with them specially.
  676. */
  677. if (!handle)
  678. return acpi_root;
  679. do {
  680. acpi_status status;
  681. status = acpi_get_parent(handle, &handle);
  682. if (ACPI_FAILURE(status)) {
  683. if (status != AE_NULL_ENTRY)
  684. return acpi_root;
  685. return NULL;
  686. }
  687. adev = acpi_fetch_acpi_dev(handle);
  688. } while (!adev);
  689. return adev;
  690. }
  691. acpi_status
  692. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  693. {
  694. acpi_status status;
  695. acpi_handle tmp;
  696. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  697. union acpi_object *obj;
  698. status = acpi_get_handle(handle, "_EJD", &tmp);
  699. if (ACPI_FAILURE(status))
  700. return status;
  701. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  702. if (ACPI_SUCCESS(status)) {
  703. obj = buffer.pointer;
  704. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  705. ejd);
  706. kfree(buffer.pointer);
  707. }
  708. return status;
  709. }
  710. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  711. static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
  712. {
  713. acpi_handle handle = dev->handle;
  714. struct acpi_device_wakeup *wakeup = &dev->wakeup;
  715. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  716. union acpi_object *package = NULL;
  717. union acpi_object *element = NULL;
  718. acpi_status status;
  719. int err = -ENODATA;
  720. INIT_LIST_HEAD(&wakeup->resources);
  721. /* _PRW */
  722. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  723. if (ACPI_FAILURE(status)) {
  724. acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
  725. acpi_format_exception(status));
  726. return err;
  727. }
  728. package = (union acpi_object *)buffer.pointer;
  729. if (!package || package->package.count < 2)
  730. goto out;
  731. element = &(package->package.elements[0]);
  732. if (!element)
  733. goto out;
  734. if (element->type == ACPI_TYPE_PACKAGE) {
  735. if ((element->package.count < 2) ||
  736. (element->package.elements[0].type !=
  737. ACPI_TYPE_LOCAL_REFERENCE)
  738. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  739. goto out;
  740. wakeup->gpe_device =
  741. element->package.elements[0].reference.handle;
  742. wakeup->gpe_number =
  743. (u32) element->package.elements[1].integer.value;
  744. } else if (element->type == ACPI_TYPE_INTEGER) {
  745. wakeup->gpe_device = NULL;
  746. wakeup->gpe_number = element->integer.value;
  747. } else {
  748. goto out;
  749. }
  750. element = &(package->package.elements[1]);
  751. if (element->type != ACPI_TYPE_INTEGER)
  752. goto out;
  753. wakeup->sleep_state = element->integer.value;
  754. err = acpi_extract_power_resources(package, 2, &wakeup->resources);
  755. if (err)
  756. goto out;
  757. if (!list_empty(&wakeup->resources)) {
  758. int sleep_state;
  759. err = acpi_power_wakeup_list_init(&wakeup->resources,
  760. &sleep_state);
  761. if (err) {
  762. acpi_handle_warn(handle, "Retrieving current states "
  763. "of wakeup power resources failed\n");
  764. acpi_power_resources_list_free(&wakeup->resources);
  765. goto out;
  766. }
  767. if (sleep_state < wakeup->sleep_state) {
  768. acpi_handle_warn(handle, "Overriding _PRW sleep state "
  769. "(S%d) by S%d from power resources\n",
  770. (int)wakeup->sleep_state, sleep_state);
  771. wakeup->sleep_state = sleep_state;
  772. }
  773. }
  774. out:
  775. kfree(buffer.pointer);
  776. return err;
  777. }
  778. static bool acpi_wakeup_gpe_init(struct acpi_device *device)
  779. {
  780. static const struct acpi_device_id button_device_ids[] = {
  781. {"PNP0C0C", 0}, /* Power button */
  782. {"PNP0C0D", 0}, /* Lid */
  783. {"PNP0C0E", 0}, /* Sleep button */
  784. {"", 0},
  785. };
  786. struct acpi_device_wakeup *wakeup = &device->wakeup;
  787. acpi_status status;
  788. wakeup->flags.notifier_present = 0;
  789. /* Power button, Lid switch always enable wakeup */
  790. if (!acpi_match_device_ids(device, button_device_ids)) {
  791. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  792. /* Do not use Lid/sleep button for S5 wakeup */
  793. if (wakeup->sleep_state == ACPI_STATE_S5)
  794. wakeup->sleep_state = ACPI_STATE_S4;
  795. }
  796. acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
  797. device_set_wakeup_capable(&device->dev, true);
  798. return true;
  799. }
  800. status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
  801. wakeup->gpe_number);
  802. return ACPI_SUCCESS(status);
  803. }
  804. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  805. {
  806. int err;
  807. /* Presence of _PRW indicates wake capable */
  808. if (!acpi_has_method(device->handle, "_PRW"))
  809. return;
  810. err = acpi_bus_extract_wakeup_device_power_package(device);
  811. if (err) {
  812. dev_err(&device->dev, "Unable to extract wakeup power resources");
  813. return;
  814. }
  815. device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
  816. device->wakeup.prepare_count = 0;
  817. /*
  818. * Call _PSW/_DSW object to disable its ability to wake the sleeping
  819. * system for the ACPI device with the _PRW object.
  820. * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
  821. * So it is necessary to call _DSW object first. Only when it is not
  822. * present will the _PSW object used.
  823. */
  824. err = acpi_device_sleep_wake(device, 0, 0, 0);
  825. if (err)
  826. pr_debug("error in _DSW or _PSW evaluation\n");
  827. }
  828. static void acpi_bus_init_power_state(struct acpi_device *device, int state)
  829. {
  830. struct acpi_device_power_state *ps = &device->power.states[state];
  831. char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
  832. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  833. acpi_status status;
  834. INIT_LIST_HEAD(&ps->resources);
  835. /* Evaluate "_PRx" to get referenced power resources */
  836. status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
  837. if (ACPI_SUCCESS(status)) {
  838. union acpi_object *package = buffer.pointer;
  839. if (buffer.length && package
  840. && package->type == ACPI_TYPE_PACKAGE
  841. && package->package.count)
  842. acpi_extract_power_resources(package, 0, &ps->resources);
  843. ACPI_FREE(buffer.pointer);
  844. }
  845. /* Evaluate "_PSx" to see if we can do explicit sets */
  846. pathname[2] = 'S';
  847. if (acpi_has_method(device->handle, pathname))
  848. ps->flags.explicit_set = 1;
  849. /* State is valid if there are means to put the device into it. */
  850. if (!list_empty(&ps->resources) || ps->flags.explicit_set)
  851. ps->flags.valid = 1;
  852. ps->power = -1; /* Unknown - driver assigned */
  853. ps->latency = -1; /* Unknown - driver assigned */
  854. }
  855. static void acpi_bus_get_power_flags(struct acpi_device *device)
  856. {
  857. unsigned long long dsc = ACPI_STATE_D0;
  858. u32 i;
  859. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  860. if (!acpi_has_method(device->handle, "_PS0") &&
  861. !acpi_has_method(device->handle, "_PR0"))
  862. return;
  863. device->flags.power_manageable = 1;
  864. /*
  865. * Power Management Flags
  866. */
  867. if (acpi_has_method(device->handle, "_PSC"))
  868. device->power.flags.explicit_get = 1;
  869. if (acpi_has_method(device->handle, "_IRC"))
  870. device->power.flags.inrush_current = 1;
  871. if (acpi_has_method(device->handle, "_DSW"))
  872. device->power.flags.dsw_present = 1;
  873. acpi_evaluate_integer(device->handle, "_DSC", NULL, &dsc);
  874. device->power.state_for_enumeration = dsc;
  875. /*
  876. * Enumerate supported power management states
  877. */
  878. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
  879. acpi_bus_init_power_state(device, i);
  880. INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
  881. /* Set the defaults for D0 and D3hot (always supported). */
  882. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  883. device->power.states[ACPI_STATE_D0].power = 100;
  884. device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
  885. /*
  886. * Use power resources only if the D0 list of them is populated, because
  887. * some platforms may provide _PR3 only to indicate D3cold support and
  888. * in those cases the power resources list returned by it may be bogus.
  889. */
  890. if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
  891. device->power.flags.power_resources = 1;
  892. /*
  893. * D3cold is supported if the D3hot list of power resources is
  894. * not empty.
  895. */
  896. if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
  897. device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
  898. }
  899. if (acpi_bus_init_power(device))
  900. device->flags.power_manageable = 0;
  901. }
  902. static void acpi_bus_get_flags(struct acpi_device *device)
  903. {
  904. /* Presence of _STA indicates 'dynamic_status' */
  905. if (acpi_has_method(device->handle, "_STA"))
  906. device->flags.dynamic_status = 1;
  907. /* Presence of _RMV indicates 'removable' */
  908. if (acpi_has_method(device->handle, "_RMV"))
  909. device->flags.removable = 1;
  910. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  911. if (acpi_has_method(device->handle, "_EJD") ||
  912. acpi_has_method(device->handle, "_EJ0"))
  913. device->flags.ejectable = 1;
  914. }
  915. static void acpi_device_get_busid(struct acpi_device *device)
  916. {
  917. char bus_id[5] = { '?', 0 };
  918. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  919. int i = 0;
  920. /*
  921. * Bus ID
  922. * ------
  923. * The device's Bus ID is simply the object name.
  924. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  925. */
  926. if (!acpi_dev_parent(device)) {
  927. strcpy(device->pnp.bus_id, "ACPI");
  928. return;
  929. }
  930. switch (device->device_type) {
  931. case ACPI_BUS_TYPE_POWER_BUTTON:
  932. strcpy(device->pnp.bus_id, "PWRF");
  933. break;
  934. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  935. strcpy(device->pnp.bus_id, "SLPF");
  936. break;
  937. case ACPI_BUS_TYPE_ECDT_EC:
  938. strcpy(device->pnp.bus_id, "ECDT");
  939. break;
  940. default:
  941. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  942. /* Clean up trailing underscores (if any) */
  943. for (i = 3; i > 1; i--) {
  944. if (bus_id[i] == '_')
  945. bus_id[i] = '\0';
  946. else
  947. break;
  948. }
  949. strcpy(device->pnp.bus_id, bus_id);
  950. break;
  951. }
  952. }
  953. /*
  954. * acpi_ata_match - see if an acpi object is an ATA device
  955. *
  956. * If an acpi object has one of the ACPI ATA methods defined,
  957. * then we can safely call it an ATA device.
  958. */
  959. bool acpi_ata_match(acpi_handle handle)
  960. {
  961. return acpi_has_method(handle, "_GTF") ||
  962. acpi_has_method(handle, "_GTM") ||
  963. acpi_has_method(handle, "_STM") ||
  964. acpi_has_method(handle, "_SDD");
  965. }
  966. /*
  967. * acpi_bay_match - see if an acpi object is an ejectable driver bay
  968. *
  969. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  970. * then we can safely call it an ejectable drive bay
  971. */
  972. bool acpi_bay_match(acpi_handle handle)
  973. {
  974. acpi_handle phandle;
  975. if (!acpi_has_method(handle, "_EJ0"))
  976. return false;
  977. if (acpi_ata_match(handle))
  978. return true;
  979. if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
  980. return false;
  981. return acpi_ata_match(phandle);
  982. }
  983. bool acpi_device_is_battery(struct acpi_device *adev)
  984. {
  985. struct acpi_hardware_id *hwid;
  986. list_for_each_entry(hwid, &adev->pnp.ids, list)
  987. if (!strcmp("PNP0C0A", hwid->id))
  988. return true;
  989. return false;
  990. }
  991. static bool is_ejectable_bay(struct acpi_device *adev)
  992. {
  993. acpi_handle handle = adev->handle;
  994. if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
  995. return true;
  996. return acpi_bay_match(handle);
  997. }
  998. /*
  999. * acpi_dock_match - see if an acpi object has a _DCK method
  1000. */
  1001. bool acpi_dock_match(acpi_handle handle)
  1002. {
  1003. return acpi_has_method(handle, "_DCK");
  1004. }
  1005. static acpi_status
  1006. acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
  1007. void **return_value)
  1008. {
  1009. long *cap = context;
  1010. if (acpi_has_method(handle, "_BCM") &&
  1011. acpi_has_method(handle, "_BCL")) {
  1012. acpi_handle_debug(handle, "Found generic backlight support\n");
  1013. *cap |= ACPI_VIDEO_BACKLIGHT;
  1014. /* We have backlight support, no need to scan further */
  1015. return AE_CTRL_TERMINATE;
  1016. }
  1017. return 0;
  1018. }
  1019. /* Returns true if the ACPI object is a video device which can be
  1020. * handled by video.ko.
  1021. * The device will get a Linux specific CID added in scan.c to
  1022. * identify the device as an ACPI graphics device
  1023. * Be aware that the graphics device may not be physically present
  1024. * Use acpi_video_get_capabilities() to detect general ACPI video
  1025. * capabilities of present cards
  1026. */
  1027. long acpi_is_video_device(acpi_handle handle)
  1028. {
  1029. long video_caps = 0;
  1030. /* Is this device able to support video switching ? */
  1031. if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
  1032. video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
  1033. /* Is this device able to retrieve a video ROM ? */
  1034. if (acpi_has_method(handle, "_ROM"))
  1035. video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
  1036. /* Is this device able to configure which video head to be POSTed ? */
  1037. if (acpi_has_method(handle, "_VPO") &&
  1038. acpi_has_method(handle, "_GPD") &&
  1039. acpi_has_method(handle, "_SPD"))
  1040. video_caps |= ACPI_VIDEO_DEVICE_POSTING;
  1041. /* Only check for backlight functionality if one of the above hit. */
  1042. if (video_caps)
  1043. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1044. ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
  1045. &video_caps, NULL);
  1046. return video_caps;
  1047. }
  1048. EXPORT_SYMBOL(acpi_is_video_device);
  1049. const char *acpi_device_hid(struct acpi_device *device)
  1050. {
  1051. struct acpi_hardware_id *hid;
  1052. if (list_empty(&device->pnp.ids))
  1053. return dummy_hid;
  1054. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  1055. return hid->id;
  1056. }
  1057. EXPORT_SYMBOL(acpi_device_hid);
  1058. static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
  1059. {
  1060. struct acpi_hardware_id *id;
  1061. id = kmalloc(sizeof(*id), GFP_KERNEL);
  1062. if (!id)
  1063. return;
  1064. id->id = kstrdup_const(dev_id, GFP_KERNEL);
  1065. if (!id->id) {
  1066. kfree(id);
  1067. return;
  1068. }
  1069. list_add_tail(&id->list, &pnp->ids);
  1070. pnp->type.hardware_id = 1;
  1071. }
  1072. /*
  1073. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1074. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1075. * prefix. Work around this.
  1076. */
  1077. static bool acpi_ibm_smbus_match(acpi_handle handle)
  1078. {
  1079. char node_name[ACPI_PATH_SEGMENT_LENGTH];
  1080. struct acpi_buffer path = { sizeof(node_name), node_name };
  1081. if (!dmi_name_in_vendors("IBM"))
  1082. return false;
  1083. /* Look for SMBS object */
  1084. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
  1085. strcmp("SMBS", path.pointer))
  1086. return false;
  1087. /* Does it have the necessary (but misnamed) methods? */
  1088. if (acpi_has_method(handle, "SBI") &&
  1089. acpi_has_method(handle, "SBR") &&
  1090. acpi_has_method(handle, "SBW"))
  1091. return true;
  1092. return false;
  1093. }
  1094. static bool acpi_object_is_system_bus(acpi_handle handle)
  1095. {
  1096. acpi_handle tmp;
  1097. if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
  1098. tmp == handle)
  1099. return true;
  1100. if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
  1101. tmp == handle)
  1102. return true;
  1103. return false;
  1104. }
  1105. static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
  1106. int device_type)
  1107. {
  1108. struct acpi_device_info *info = NULL;
  1109. struct acpi_pnp_device_id_list *cid_list;
  1110. int i;
  1111. switch (device_type) {
  1112. case ACPI_BUS_TYPE_DEVICE:
  1113. if (handle == ACPI_ROOT_OBJECT) {
  1114. acpi_add_id(pnp, ACPI_SYSTEM_HID);
  1115. break;
  1116. }
  1117. acpi_get_object_info(handle, &info);
  1118. if (!info) {
  1119. pr_err("%s: Error reading device info\n", __func__);
  1120. return;
  1121. }
  1122. if (info->valid & ACPI_VALID_HID) {
  1123. acpi_add_id(pnp, info->hardware_id.string);
  1124. pnp->type.platform_id = 1;
  1125. }
  1126. if (info->valid & ACPI_VALID_CID) {
  1127. cid_list = &info->compatible_id_list;
  1128. for (i = 0; i < cid_list->count; i++)
  1129. acpi_add_id(pnp, cid_list->ids[i].string);
  1130. }
  1131. if (info->valid & ACPI_VALID_ADR) {
  1132. pnp->bus_address = info->address;
  1133. pnp->type.bus_address = 1;
  1134. }
  1135. if (info->valid & ACPI_VALID_UID)
  1136. pnp->unique_id = kstrdup(info->unique_id.string,
  1137. GFP_KERNEL);
  1138. if (info->valid & ACPI_VALID_CLS)
  1139. acpi_add_id(pnp, info->class_code.string);
  1140. kfree(info);
  1141. /*
  1142. * Some devices don't reliably have _HIDs & _CIDs, so add
  1143. * synthetic HIDs to make sure drivers can find them.
  1144. */
  1145. if (acpi_is_video_device(handle)) {
  1146. acpi_add_id(pnp, ACPI_VIDEO_HID);
  1147. pnp->type.backlight = 1;
  1148. break;
  1149. }
  1150. if (acpi_bay_match(handle))
  1151. acpi_add_id(pnp, ACPI_BAY_HID);
  1152. else if (acpi_dock_match(handle))
  1153. acpi_add_id(pnp, ACPI_DOCK_HID);
  1154. else if (acpi_ibm_smbus_match(handle))
  1155. acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
  1156. else if (list_empty(&pnp->ids) &&
  1157. acpi_object_is_system_bus(handle)) {
  1158. /* \_SB, \_TZ, LNXSYBUS */
  1159. acpi_add_id(pnp, ACPI_BUS_HID);
  1160. strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
  1161. strcpy(pnp->device_class, ACPI_BUS_CLASS);
  1162. }
  1163. break;
  1164. case ACPI_BUS_TYPE_POWER:
  1165. acpi_add_id(pnp, ACPI_POWER_HID);
  1166. break;
  1167. case ACPI_BUS_TYPE_PROCESSOR:
  1168. acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
  1169. break;
  1170. case ACPI_BUS_TYPE_THERMAL:
  1171. acpi_add_id(pnp, ACPI_THERMAL_HID);
  1172. break;
  1173. case ACPI_BUS_TYPE_POWER_BUTTON:
  1174. acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
  1175. break;
  1176. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1177. acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
  1178. break;
  1179. case ACPI_BUS_TYPE_ECDT_EC:
  1180. acpi_add_id(pnp, ACPI_ECDT_HID);
  1181. break;
  1182. }
  1183. }
  1184. void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
  1185. {
  1186. struct acpi_hardware_id *id, *tmp;
  1187. list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
  1188. kfree_const(id->id);
  1189. kfree(id);
  1190. }
  1191. kfree(pnp->unique_id);
  1192. }
  1193. /**
  1194. * acpi_dma_supported - Check DMA support for the specified device.
  1195. * @adev: The pointer to acpi device
  1196. *
  1197. * Return false if DMA is not supported. Otherwise, return true
  1198. */
  1199. bool acpi_dma_supported(const struct acpi_device *adev)
  1200. {
  1201. if (!adev)
  1202. return false;
  1203. if (adev->flags.cca_seen)
  1204. return true;
  1205. /*
  1206. * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
  1207. * DMA on "Intel platforms". Presumably that includes all x86 and
  1208. * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
  1209. */
  1210. if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
  1211. return true;
  1212. return false;
  1213. }
  1214. /**
  1215. * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
  1216. * @adev: The pointer to acpi device
  1217. *
  1218. * Return enum dev_dma_attr.
  1219. */
  1220. enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
  1221. {
  1222. if (!acpi_dma_supported(adev))
  1223. return DEV_DMA_NOT_SUPPORTED;
  1224. if (adev->flags.coherent_dma)
  1225. return DEV_DMA_COHERENT;
  1226. else
  1227. return DEV_DMA_NON_COHERENT;
  1228. }
  1229. /**
  1230. * acpi_dma_get_range() - Get device DMA parameters.
  1231. *
  1232. * @dev: device to configure
  1233. * @map: pointer to DMA ranges result
  1234. *
  1235. * Evaluate DMA regions and return pointer to DMA regions on
  1236. * parsing success; it does not update the passed in values on failure.
  1237. *
  1238. * Return 0 on success, < 0 on failure.
  1239. */
  1240. int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map)
  1241. {
  1242. struct acpi_device *adev;
  1243. LIST_HEAD(list);
  1244. struct resource_entry *rentry;
  1245. int ret;
  1246. struct device *dma_dev = dev;
  1247. struct bus_dma_region *r;
  1248. /*
  1249. * Walk the device tree chasing an ACPI companion with a _DMA
  1250. * object while we go. Stop if we find a device with an ACPI
  1251. * companion containing a _DMA method.
  1252. */
  1253. do {
  1254. adev = ACPI_COMPANION(dma_dev);
  1255. if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
  1256. break;
  1257. dma_dev = dma_dev->parent;
  1258. } while (dma_dev);
  1259. if (!dma_dev)
  1260. return -ENODEV;
  1261. if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
  1262. acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
  1263. return -EINVAL;
  1264. }
  1265. ret = acpi_dev_get_dma_resources(adev, &list);
  1266. if (ret > 0) {
  1267. r = kcalloc(ret + 1, sizeof(*r), GFP_KERNEL);
  1268. if (!r) {
  1269. ret = -ENOMEM;
  1270. goto out;
  1271. }
  1272. *map = r;
  1273. list_for_each_entry(rentry, &list, node) {
  1274. if (rentry->res->start >= rentry->res->end) {
  1275. kfree(*map);
  1276. *map = NULL;
  1277. ret = -EINVAL;
  1278. dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
  1279. goto out;
  1280. }
  1281. r->cpu_start = rentry->res->start;
  1282. r->dma_start = rentry->res->start - rentry->offset;
  1283. r->size = resource_size(rentry->res);
  1284. r->offset = rentry->offset;
  1285. r++;
  1286. }
  1287. }
  1288. out:
  1289. acpi_dev_free_resource_list(&list);
  1290. return ret >= 0 ? 0 : ret;
  1291. }
  1292. #ifdef CONFIG_IOMMU_API
  1293. int acpi_iommu_fwspec_init(struct device *dev, u32 id,
  1294. struct fwnode_handle *fwnode,
  1295. const struct iommu_ops *ops)
  1296. {
  1297. int ret = iommu_fwspec_init(dev, fwnode, ops);
  1298. if (!ret)
  1299. ret = iommu_fwspec_add_ids(dev, &id, 1);
  1300. return ret;
  1301. }
  1302. static inline const struct iommu_ops *acpi_iommu_fwspec_ops(struct device *dev)
  1303. {
  1304. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  1305. return fwspec ? fwspec->ops : NULL;
  1306. }
  1307. static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
  1308. const u32 *id_in)
  1309. {
  1310. int err;
  1311. const struct iommu_ops *ops;
  1312. /* Serialise to make dev->iommu stable under our potential fwspec */
  1313. mutex_lock(&iommu_probe_device_lock);
  1314. /*
  1315. * If we already translated the fwspec there is nothing left to do,
  1316. * return the iommu_ops.
  1317. */
  1318. ops = acpi_iommu_fwspec_ops(dev);
  1319. if (ops) {
  1320. mutex_unlock(&iommu_probe_device_lock);
  1321. return ops;
  1322. }
  1323. err = iort_iommu_configure_id(dev, id_in);
  1324. if (err && err != -EPROBE_DEFER)
  1325. err = viot_iommu_configure(dev);
  1326. mutex_unlock(&iommu_probe_device_lock);
  1327. /*
  1328. * If we have reason to believe the IOMMU driver missed the initial
  1329. * iommu_probe_device() call for dev, replay it to get things in order.
  1330. */
  1331. if (!err && dev->bus && !device_iommu_mapped(dev))
  1332. err = iommu_probe_device(dev);
  1333. /* Ignore all other errors apart from EPROBE_DEFER */
  1334. if (err == -EPROBE_DEFER) {
  1335. return ERR_PTR(err);
  1336. } else if (err) {
  1337. dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
  1338. return NULL;
  1339. }
  1340. return acpi_iommu_fwspec_ops(dev);
  1341. }
  1342. #else /* !CONFIG_IOMMU_API */
  1343. int acpi_iommu_fwspec_init(struct device *dev, u32 id,
  1344. struct fwnode_handle *fwnode,
  1345. const struct iommu_ops *ops)
  1346. {
  1347. return -ENODEV;
  1348. }
  1349. static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
  1350. const u32 *id_in)
  1351. {
  1352. return NULL;
  1353. }
  1354. #endif /* !CONFIG_IOMMU_API */
  1355. /**
  1356. * acpi_dma_configure_id - Set-up DMA configuration for the device.
  1357. * @dev: The pointer to the device
  1358. * @attr: device dma attributes
  1359. * @input_id: input device id const value pointer
  1360. */
  1361. int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
  1362. const u32 *input_id)
  1363. {
  1364. const struct iommu_ops *iommu;
  1365. if (attr == DEV_DMA_NOT_SUPPORTED) {
  1366. set_dma_ops(dev, &dma_dummy_ops);
  1367. return 0;
  1368. }
  1369. acpi_arch_dma_setup(dev);
  1370. iommu = acpi_iommu_configure_id(dev, input_id);
  1371. if (PTR_ERR(iommu) == -EPROBE_DEFER)
  1372. return -EPROBE_DEFER;
  1373. arch_setup_dma_ops(dev, 0, U64_MAX,
  1374. iommu, attr == DEV_DMA_COHERENT);
  1375. return 0;
  1376. }
  1377. EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
  1378. static void acpi_init_coherency(struct acpi_device *adev)
  1379. {
  1380. unsigned long long cca = 0;
  1381. acpi_status status;
  1382. struct acpi_device *parent = acpi_dev_parent(adev);
  1383. if (parent && parent->flags.cca_seen) {
  1384. /*
  1385. * From ACPI spec, OSPM will ignore _CCA if an ancestor
  1386. * already saw one.
  1387. */
  1388. adev->flags.cca_seen = 1;
  1389. cca = parent->flags.coherent_dma;
  1390. } else {
  1391. status = acpi_evaluate_integer(adev->handle, "_CCA",
  1392. NULL, &cca);
  1393. if (ACPI_SUCCESS(status))
  1394. adev->flags.cca_seen = 1;
  1395. else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
  1396. /*
  1397. * If architecture does not specify that _CCA is
  1398. * required for DMA-able devices (e.g. x86),
  1399. * we default to _CCA=1.
  1400. */
  1401. cca = 1;
  1402. else
  1403. acpi_handle_debug(adev->handle,
  1404. "ACPI device is missing _CCA.\n");
  1405. }
  1406. adev->flags.coherent_dma = cca;
  1407. }
  1408. static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
  1409. {
  1410. bool *is_serial_bus_slave_p = data;
  1411. if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  1412. return 1;
  1413. *is_serial_bus_slave_p = true;
  1414. /* no need to do more checking */
  1415. return -1;
  1416. }
  1417. static bool acpi_is_indirect_io_slave(struct acpi_device *device)
  1418. {
  1419. struct acpi_device *parent = acpi_dev_parent(device);
  1420. static const struct acpi_device_id indirect_io_hosts[] = {
  1421. {"HISI0191", 0},
  1422. {}
  1423. };
  1424. return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
  1425. }
  1426. static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
  1427. {
  1428. struct list_head resource_list;
  1429. bool is_serial_bus_slave = false;
  1430. static const struct acpi_device_id ignore_serial_bus_ids[] = {
  1431. /*
  1432. * These devices have multiple SerialBus resources and a client
  1433. * device must be instantiated for each of them, each with
  1434. * its own device id.
  1435. * Normally we only instantiate one client device for the first
  1436. * resource, using the ACPI HID as id. These special cases are handled
  1437. * by the drivers/platform/x86/serial-multi-instantiate.c driver, which
  1438. * knows which client device id to use for each resource.
  1439. */
  1440. {"BSG1160", },
  1441. {"BSG2150", },
  1442. {"CSC3551", },
  1443. {"CSC3556", },
  1444. {"INT33FE", },
  1445. {"INT3515", },
  1446. /* Non-conforming _HID for Cirrus Logic already released */
  1447. {"CLSA0100", },
  1448. {"CLSA0101", },
  1449. /*
  1450. * Some ACPI devs contain SerialBus resources even though they are not
  1451. * attached to a serial bus at all.
  1452. */
  1453. {"MSHW0028", },
  1454. /*
  1455. * HIDs of device with an UartSerialBusV2 resource for which userspace
  1456. * expects a regular tty cdev to be created (instead of the in kernel
  1457. * serdev) and which have a kernel driver which expects a platform_dev
  1458. * such as the rfkill-gpio driver.
  1459. */
  1460. {"BCM4752", },
  1461. {"LNV4752", },
  1462. {}
  1463. };
  1464. if (acpi_is_indirect_io_slave(device))
  1465. return true;
  1466. /* Macs use device properties in lieu of _CRS resources */
  1467. if (x86_apple_machine &&
  1468. (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
  1469. fwnode_property_present(&device->fwnode, "i2cAddress") ||
  1470. fwnode_property_present(&device->fwnode, "baud")))
  1471. return true;
  1472. if (!acpi_match_device_ids(device, ignore_serial_bus_ids))
  1473. return false;
  1474. INIT_LIST_HEAD(&resource_list);
  1475. acpi_dev_get_resources(device, &resource_list,
  1476. acpi_check_serial_bus_slave,
  1477. &is_serial_bus_slave);
  1478. acpi_dev_free_resource_list(&resource_list);
  1479. return is_serial_bus_slave;
  1480. }
  1481. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1482. int type, void (*release)(struct device *))
  1483. {
  1484. struct acpi_device *parent = acpi_find_parent_acpi_dev(handle);
  1485. INIT_LIST_HEAD(&device->pnp.ids);
  1486. device->device_type = type;
  1487. device->handle = handle;
  1488. device->dev.parent = parent ? &parent->dev : NULL;
  1489. device->dev.release = release;
  1490. device->dev.bus = &acpi_bus_type;
  1491. fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
  1492. acpi_set_device_status(device, ACPI_STA_DEFAULT);
  1493. acpi_device_get_busid(device);
  1494. acpi_set_pnp_ids(handle, &device->pnp, type);
  1495. acpi_init_properties(device);
  1496. acpi_bus_get_flags(device);
  1497. device->flags.match_driver = false;
  1498. device->flags.initialized = true;
  1499. device->flags.enumeration_by_parent =
  1500. acpi_device_enumeration_by_parent(device);
  1501. acpi_device_clear_enumerated(device);
  1502. device_initialize(&device->dev);
  1503. dev_set_uevent_suppress(&device->dev, true);
  1504. acpi_init_coherency(device);
  1505. }
  1506. static void acpi_scan_dep_init(struct acpi_device *adev)
  1507. {
  1508. struct acpi_dep_data *dep;
  1509. list_for_each_entry(dep, &acpi_dep_list, node) {
  1510. if (dep->consumer == adev->handle) {
  1511. if (dep->honor_dep)
  1512. adev->flags.honor_deps = 1;
  1513. adev->dep_unmet++;
  1514. }
  1515. }
  1516. }
  1517. void acpi_device_add_finalize(struct acpi_device *device)
  1518. {
  1519. dev_set_uevent_suppress(&device->dev, false);
  1520. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1521. }
  1522. static void acpi_scan_init_status(struct acpi_device *adev)
  1523. {
  1524. if (acpi_bus_get_status(adev))
  1525. acpi_set_device_status(adev, 0);
  1526. }
  1527. static int acpi_add_single_object(struct acpi_device **child,
  1528. acpi_handle handle, int type, bool dep_init)
  1529. {
  1530. struct acpi_device *device;
  1531. bool release_dep_lock = false;
  1532. int result;
  1533. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1534. if (!device)
  1535. return -ENOMEM;
  1536. acpi_init_device_object(device, handle, type, acpi_device_release);
  1537. /*
  1538. * Getting the status is delayed till here so that we can call
  1539. * acpi_bus_get_status() and use its quirk handling. Note that
  1540. * this must be done before the get power-/wakeup_dev-flags calls.
  1541. */
  1542. if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
  1543. if (dep_init) {
  1544. mutex_lock(&acpi_dep_list_lock);
  1545. /*
  1546. * Hold the lock until the acpi_tie_acpi_dev() call
  1547. * below to prevent concurrent acpi_scan_clear_dep()
  1548. * from deleting a dependency list entry without
  1549. * updating dep_unmet for the device.
  1550. */
  1551. release_dep_lock = true;
  1552. acpi_scan_dep_init(device);
  1553. }
  1554. acpi_scan_init_status(device);
  1555. }
  1556. acpi_bus_get_power_flags(device);
  1557. acpi_bus_get_wakeup_device_flags(device);
  1558. result = acpi_tie_acpi_dev(device);
  1559. if (release_dep_lock)
  1560. mutex_unlock(&acpi_dep_list_lock);
  1561. if (!result)
  1562. result = acpi_device_add(device);
  1563. if (result) {
  1564. acpi_device_release(&device->dev);
  1565. return result;
  1566. }
  1567. acpi_power_add_remove_device(device, true);
  1568. acpi_device_add_finalize(device);
  1569. acpi_handle_debug(handle, "Added as %s, parent %s\n",
  1570. dev_name(&device->dev), device->dev.parent ?
  1571. dev_name(device->dev.parent) : "(null)");
  1572. *child = device;
  1573. return 0;
  1574. }
  1575. static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
  1576. void *context)
  1577. {
  1578. struct resource *res = context;
  1579. if (acpi_dev_resource_memory(ares, res))
  1580. return AE_CTRL_TERMINATE;
  1581. return AE_OK;
  1582. }
  1583. static bool acpi_device_should_be_hidden(acpi_handle handle)
  1584. {
  1585. acpi_status status;
  1586. struct resource res;
  1587. /* Check if it should ignore the UART device */
  1588. if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
  1589. return false;
  1590. /*
  1591. * The UART device described in SPCR table is assumed to have only one
  1592. * memory resource present. So we only look for the first one here.
  1593. */
  1594. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1595. acpi_get_resource_memory, &res);
  1596. if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
  1597. return false;
  1598. acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
  1599. &res.start);
  1600. return true;
  1601. }
  1602. bool acpi_device_is_present(const struct acpi_device *adev)
  1603. {
  1604. return adev->status.present || adev->status.functional;
  1605. }
  1606. static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
  1607. const char *idstr,
  1608. const struct acpi_device_id **matchid)
  1609. {
  1610. const struct acpi_device_id *devid;
  1611. if (handler->match)
  1612. return handler->match(idstr, matchid);
  1613. for (devid = handler->ids; devid->id[0]; devid++)
  1614. if (!strcmp((char *)devid->id, idstr)) {
  1615. if (matchid)
  1616. *matchid = devid;
  1617. return true;
  1618. }
  1619. return false;
  1620. }
  1621. static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
  1622. const struct acpi_device_id **matchid)
  1623. {
  1624. struct acpi_scan_handler *handler;
  1625. list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
  1626. if (acpi_scan_handler_matching(handler, idstr, matchid))
  1627. return handler;
  1628. return NULL;
  1629. }
  1630. void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
  1631. {
  1632. if (!!hotplug->enabled == !!val)
  1633. return;
  1634. mutex_lock(&acpi_scan_lock);
  1635. hotplug->enabled = val;
  1636. mutex_unlock(&acpi_scan_lock);
  1637. }
  1638. static void acpi_scan_init_hotplug(struct acpi_device *adev)
  1639. {
  1640. struct acpi_hardware_id *hwid;
  1641. if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
  1642. acpi_dock_add(adev);
  1643. return;
  1644. }
  1645. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  1646. struct acpi_scan_handler *handler;
  1647. handler = acpi_scan_match_handler(hwid->id, NULL);
  1648. if (handler) {
  1649. adev->flags.hotplug_notify = true;
  1650. break;
  1651. }
  1652. }
  1653. }
  1654. static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
  1655. {
  1656. struct acpi_handle_list dep_devices;
  1657. acpi_status status;
  1658. u32 count;
  1659. int i;
  1660. /*
  1661. * Check for _HID here to avoid deferring the enumeration of:
  1662. * 1. PCI devices.
  1663. * 2. ACPI nodes describing USB ports.
  1664. * Still, checking for _HID catches more then just these cases ...
  1665. */
  1666. if (!check_dep || !acpi_has_method(handle, "_DEP") ||
  1667. !acpi_has_method(handle, "_HID"))
  1668. return 0;
  1669. status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
  1670. if (ACPI_FAILURE(status)) {
  1671. acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
  1672. return 0;
  1673. }
  1674. for (count = 0, i = 0; i < dep_devices.count; i++) {
  1675. struct acpi_device_info *info;
  1676. struct acpi_dep_data *dep;
  1677. bool skip, honor_dep;
  1678. status = acpi_get_object_info(dep_devices.handles[i], &info);
  1679. if (ACPI_FAILURE(status)) {
  1680. acpi_handle_debug(handle, "Error reading _DEP device info\n");
  1681. continue;
  1682. }
  1683. skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
  1684. honor_dep = acpi_info_matches_ids(info, acpi_honor_dep_ids);
  1685. kfree(info);
  1686. if (skip)
  1687. continue;
  1688. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1689. if (!dep)
  1690. continue;
  1691. count++;
  1692. dep->supplier = dep_devices.handles[i];
  1693. dep->consumer = handle;
  1694. dep->honor_dep = honor_dep;
  1695. mutex_lock(&acpi_dep_list_lock);
  1696. list_add_tail(&dep->node , &acpi_dep_list);
  1697. mutex_unlock(&acpi_dep_list_lock);
  1698. }
  1699. return count;
  1700. }
  1701. static bool acpi_bus_scan_second_pass;
  1702. static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
  1703. struct acpi_device **adev_p)
  1704. {
  1705. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  1706. acpi_object_type acpi_type;
  1707. int type;
  1708. if (device)
  1709. goto out;
  1710. if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
  1711. return AE_OK;
  1712. switch (acpi_type) {
  1713. case ACPI_TYPE_DEVICE:
  1714. if (acpi_device_should_be_hidden(handle))
  1715. return AE_OK;
  1716. /* Bail out if there are dependencies. */
  1717. if (acpi_scan_check_dep(handle, check_dep) > 0) {
  1718. acpi_bus_scan_second_pass = true;
  1719. return AE_CTRL_DEPTH;
  1720. }
  1721. fallthrough;
  1722. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1723. type = ACPI_BUS_TYPE_DEVICE;
  1724. break;
  1725. case ACPI_TYPE_PROCESSOR:
  1726. type = ACPI_BUS_TYPE_PROCESSOR;
  1727. break;
  1728. case ACPI_TYPE_THERMAL:
  1729. type = ACPI_BUS_TYPE_THERMAL;
  1730. break;
  1731. case ACPI_TYPE_POWER:
  1732. acpi_add_power_resource(handle);
  1733. fallthrough;
  1734. default:
  1735. return AE_OK;
  1736. }
  1737. /*
  1738. * If check_dep is true at this point, the device has no dependencies,
  1739. * or the creation of the device object would have been postponed above.
  1740. */
  1741. acpi_add_single_object(&device, handle, type, !check_dep);
  1742. if (!device)
  1743. return AE_CTRL_DEPTH;
  1744. acpi_scan_init_hotplug(device);
  1745. out:
  1746. if (!*adev_p)
  1747. *adev_p = device;
  1748. return AE_OK;
  1749. }
  1750. static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
  1751. void *not_used, void **ret_p)
  1752. {
  1753. return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
  1754. }
  1755. static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
  1756. void *not_used, void **ret_p)
  1757. {
  1758. return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
  1759. }
  1760. static void acpi_default_enumeration(struct acpi_device *device)
  1761. {
  1762. /*
  1763. * Do not enumerate devices with enumeration_by_parent flag set as
  1764. * they will be enumerated by their respective parents.
  1765. */
  1766. if (!device->flags.enumeration_by_parent) {
  1767. acpi_create_platform_device(device, NULL);
  1768. acpi_device_set_enumerated(device);
  1769. } else {
  1770. blocking_notifier_call_chain(&acpi_reconfig_chain,
  1771. ACPI_RECONFIG_DEVICE_ADD, device);
  1772. }
  1773. }
  1774. static const struct acpi_device_id generic_device_ids[] = {
  1775. {ACPI_DT_NAMESPACE_HID, },
  1776. {"", },
  1777. };
  1778. static int acpi_generic_device_attach(struct acpi_device *adev,
  1779. const struct acpi_device_id *not_used)
  1780. {
  1781. /*
  1782. * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
  1783. * below can be unconditional.
  1784. */
  1785. if (adev->data.of_compatible)
  1786. acpi_default_enumeration(adev);
  1787. return 1;
  1788. }
  1789. static struct acpi_scan_handler generic_device_handler = {
  1790. .ids = generic_device_ids,
  1791. .attach = acpi_generic_device_attach,
  1792. };
  1793. static int acpi_scan_attach_handler(struct acpi_device *device)
  1794. {
  1795. struct acpi_hardware_id *hwid;
  1796. int ret = 0;
  1797. list_for_each_entry(hwid, &device->pnp.ids, list) {
  1798. const struct acpi_device_id *devid;
  1799. struct acpi_scan_handler *handler;
  1800. handler = acpi_scan_match_handler(hwid->id, &devid);
  1801. if (handler) {
  1802. if (!handler->attach) {
  1803. device->pnp.type.platform_id = 0;
  1804. continue;
  1805. }
  1806. device->handler = handler;
  1807. ret = handler->attach(device, devid);
  1808. if (ret > 0)
  1809. break;
  1810. device->handler = NULL;
  1811. if (ret < 0)
  1812. break;
  1813. }
  1814. }
  1815. return ret;
  1816. }
  1817. static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
  1818. {
  1819. bool skip = !first_pass && device->flags.visited;
  1820. acpi_handle ejd;
  1821. int ret;
  1822. if (skip)
  1823. goto ok;
  1824. if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
  1825. register_dock_dependent_device(device, ejd);
  1826. acpi_bus_get_status(device);
  1827. /* Skip devices that are not ready for enumeration (e.g. not present) */
  1828. if (!acpi_dev_ready_for_enumeration(device)) {
  1829. device->flags.initialized = false;
  1830. acpi_device_clear_enumerated(device);
  1831. device->flags.power_manageable = 0;
  1832. return 0;
  1833. }
  1834. if (device->handler)
  1835. goto ok;
  1836. if (!device->flags.initialized) {
  1837. device->flags.power_manageable =
  1838. device->power.states[ACPI_STATE_D0].flags.valid;
  1839. if (acpi_bus_init_power(device))
  1840. device->flags.power_manageable = 0;
  1841. device->flags.initialized = true;
  1842. } else if (device->flags.visited) {
  1843. goto ok;
  1844. }
  1845. ret = acpi_scan_attach_handler(device);
  1846. if (ret < 0)
  1847. return 0;
  1848. device->flags.match_driver = true;
  1849. if (ret > 0 && !device->flags.enumeration_by_parent) {
  1850. acpi_device_set_enumerated(device);
  1851. goto ok;
  1852. }
  1853. ret = device_attach(&device->dev);
  1854. if (ret < 0)
  1855. return 0;
  1856. if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
  1857. acpi_default_enumeration(device);
  1858. else
  1859. acpi_device_set_enumerated(device);
  1860. ok:
  1861. acpi_dev_for_each_child(device, acpi_bus_attach, first_pass);
  1862. if (!skip && device->handler && device->handler->hotplug.notify_online)
  1863. device->handler->hotplug.notify_online(device);
  1864. return 0;
  1865. }
  1866. static int acpi_dev_get_next_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
  1867. {
  1868. struct acpi_device **adev_p = data;
  1869. struct acpi_device *adev = *adev_p;
  1870. /*
  1871. * If we're passed a 'previous' consumer device then we need to skip
  1872. * any consumers until we meet the previous one, and then NULL @data
  1873. * so the next one can be returned.
  1874. */
  1875. if (adev) {
  1876. if (dep->consumer == adev->handle)
  1877. *adev_p = NULL;
  1878. return 0;
  1879. }
  1880. adev = acpi_get_acpi_dev(dep->consumer);
  1881. if (adev) {
  1882. *(struct acpi_device **)data = adev;
  1883. return 1;
  1884. }
  1885. /* Continue parsing if the device object is not present. */
  1886. return 0;
  1887. }
  1888. struct acpi_scan_clear_dep_work {
  1889. struct work_struct work;
  1890. struct acpi_device *adev;
  1891. };
  1892. static void acpi_scan_clear_dep_fn(struct work_struct *work)
  1893. {
  1894. struct acpi_scan_clear_dep_work *cdw;
  1895. cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
  1896. acpi_scan_lock_acquire();
  1897. acpi_bus_attach(cdw->adev, (void *)true);
  1898. acpi_scan_lock_release();
  1899. acpi_dev_put(cdw->adev);
  1900. kfree(cdw);
  1901. }
  1902. static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
  1903. {
  1904. struct acpi_scan_clear_dep_work *cdw;
  1905. if (adev->dep_unmet)
  1906. return false;
  1907. cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
  1908. if (!cdw)
  1909. return false;
  1910. cdw->adev = adev;
  1911. INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
  1912. /*
  1913. * Since the work function may block on the lock until the entire
  1914. * initial enumeration of devices is complete, put it into the unbound
  1915. * workqueue.
  1916. */
  1917. queue_work(system_unbound_wq, &cdw->work);
  1918. return true;
  1919. }
  1920. static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
  1921. {
  1922. struct acpi_device *adev = acpi_get_acpi_dev(dep->consumer);
  1923. if (adev) {
  1924. adev->dep_unmet--;
  1925. if (!acpi_scan_clear_dep_queue(adev))
  1926. acpi_dev_put(adev);
  1927. }
  1928. list_del(&dep->node);
  1929. kfree(dep);
  1930. return 0;
  1931. }
  1932. /**
  1933. * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
  1934. * @handle: The ACPI handle of the supplier device
  1935. * @callback: Pointer to the callback function to apply
  1936. * @data: Pointer to some data to pass to the callback
  1937. *
  1938. * The return value of the callback determines this function's behaviour. If 0
  1939. * is returned we continue to iterate over acpi_dep_list. If a positive value
  1940. * is returned then the loop is broken but this function returns 0. If a
  1941. * negative value is returned by the callback then the loop is broken and that
  1942. * value is returned as the final error.
  1943. */
  1944. static int acpi_walk_dep_device_list(acpi_handle handle,
  1945. int (*callback)(struct acpi_dep_data *, void *),
  1946. void *data)
  1947. {
  1948. struct acpi_dep_data *dep, *tmp;
  1949. int ret = 0;
  1950. mutex_lock(&acpi_dep_list_lock);
  1951. list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
  1952. if (dep->supplier == handle) {
  1953. ret = callback(dep, data);
  1954. if (ret)
  1955. break;
  1956. }
  1957. }
  1958. mutex_unlock(&acpi_dep_list_lock);
  1959. return ret > 0 ? 0 : ret;
  1960. }
  1961. /**
  1962. * acpi_dev_clear_dependencies - Inform consumers that the device is now active
  1963. * @supplier: Pointer to the supplier &struct acpi_device
  1964. *
  1965. * Clear dependencies on the given device.
  1966. */
  1967. void acpi_dev_clear_dependencies(struct acpi_device *supplier)
  1968. {
  1969. acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
  1970. }
  1971. EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
  1972. /**
  1973. * acpi_dev_ready_for_enumeration - Check if the ACPI device is ready for enumeration
  1974. * @device: Pointer to the &struct acpi_device to check
  1975. *
  1976. * Check if the device is present and has no unmet dependencies.
  1977. *
  1978. * Return true if the device is ready for enumeratino. Otherwise, return false.
  1979. */
  1980. bool acpi_dev_ready_for_enumeration(const struct acpi_device *device)
  1981. {
  1982. if (device->flags.honor_deps && device->dep_unmet)
  1983. return false;
  1984. return acpi_device_is_present(device);
  1985. }
  1986. EXPORT_SYMBOL_GPL(acpi_dev_ready_for_enumeration);
  1987. /**
  1988. * acpi_dev_get_next_consumer_dev - Return the next adev dependent on @supplier
  1989. * @supplier: Pointer to the dependee device
  1990. * @start: Pointer to the current dependent device
  1991. *
  1992. * Returns the next &struct acpi_device which declares itself dependent on
  1993. * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
  1994. *
  1995. * If the returned adev is not passed as @start to this function, the caller is
  1996. * responsible for putting the reference to adev when it is no longer needed.
  1997. */
  1998. struct acpi_device *acpi_dev_get_next_consumer_dev(struct acpi_device *supplier,
  1999. struct acpi_device *start)
  2000. {
  2001. struct acpi_device *adev = start;
  2002. acpi_walk_dep_device_list(supplier->handle,
  2003. acpi_dev_get_next_consumer_dev_cb, &adev);
  2004. acpi_dev_put(start);
  2005. if (adev == start)
  2006. return NULL;
  2007. return adev;
  2008. }
  2009. EXPORT_SYMBOL_GPL(acpi_dev_get_next_consumer_dev);
  2010. /**
  2011. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  2012. * @handle: Root of the namespace scope to scan.
  2013. *
  2014. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  2015. * found devices.
  2016. *
  2017. * If no devices were found, -ENODEV is returned, but it does not mean that
  2018. * there has been a real error. There just have been no suitable ACPI objects
  2019. * in the table trunk from which the kernel could create a device and add an
  2020. * appropriate driver.
  2021. *
  2022. * Must be called under acpi_scan_lock.
  2023. */
  2024. int acpi_bus_scan(acpi_handle handle)
  2025. {
  2026. struct acpi_device *device = NULL;
  2027. acpi_bus_scan_second_pass = false;
  2028. /* Pass 1: Avoid enumerating devices with missing dependencies. */
  2029. if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
  2030. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  2031. acpi_bus_check_add_1, NULL, NULL,
  2032. (void **)&device);
  2033. if (!device)
  2034. return -ENODEV;
  2035. acpi_bus_attach(device, (void *)true);
  2036. if (!acpi_bus_scan_second_pass)
  2037. return 0;
  2038. /* Pass 2: Enumerate all of the remaining devices. */
  2039. device = NULL;
  2040. if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
  2041. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  2042. acpi_bus_check_add_2, NULL, NULL,
  2043. (void **)&device);
  2044. acpi_bus_attach(device, NULL);
  2045. return 0;
  2046. }
  2047. EXPORT_SYMBOL(acpi_bus_scan);
  2048. static int acpi_bus_trim_one(struct acpi_device *adev, void *not_used)
  2049. {
  2050. struct acpi_scan_handler *handler = adev->handler;
  2051. acpi_dev_for_each_child_reverse(adev, acpi_bus_trim_one, NULL);
  2052. adev->flags.match_driver = false;
  2053. if (handler) {
  2054. if (handler->detach)
  2055. handler->detach(adev);
  2056. adev->handler = NULL;
  2057. } else {
  2058. device_release_driver(&adev->dev);
  2059. }
  2060. /*
  2061. * Most likely, the device is going away, so put it into D3cold before
  2062. * that.
  2063. */
  2064. acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
  2065. adev->flags.initialized = false;
  2066. acpi_device_clear_enumerated(adev);
  2067. return 0;
  2068. }
  2069. /**
  2070. * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
  2071. * @adev: Root of the ACPI namespace scope to walk.
  2072. *
  2073. * Must be called under acpi_scan_lock.
  2074. */
  2075. void acpi_bus_trim(struct acpi_device *adev)
  2076. {
  2077. acpi_bus_trim_one(adev, NULL);
  2078. }
  2079. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  2080. int acpi_bus_register_early_device(int type)
  2081. {
  2082. struct acpi_device *device = NULL;
  2083. int result;
  2084. result = acpi_add_single_object(&device, NULL, type, false);
  2085. if (result)
  2086. return result;
  2087. device->flags.match_driver = true;
  2088. return device_attach(&device->dev);
  2089. }
  2090. EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
  2091. static void acpi_bus_scan_fixed(void)
  2092. {
  2093. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  2094. struct acpi_device *adev = NULL;
  2095. acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_POWER_BUTTON,
  2096. false);
  2097. if (adev) {
  2098. adev->flags.match_driver = true;
  2099. if (device_attach(&adev->dev) >= 0)
  2100. device_init_wakeup(&adev->dev, true);
  2101. else
  2102. dev_dbg(&adev->dev, "No driver\n");
  2103. }
  2104. }
  2105. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  2106. struct acpi_device *adev = NULL;
  2107. acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_SLEEP_BUTTON,
  2108. false);
  2109. if (adev) {
  2110. adev->flags.match_driver = true;
  2111. if (device_attach(&adev->dev) < 0)
  2112. dev_dbg(&adev->dev, "No driver\n");
  2113. }
  2114. }
  2115. }
  2116. static void __init acpi_get_spcr_uart_addr(void)
  2117. {
  2118. acpi_status status;
  2119. struct acpi_table_spcr *spcr_ptr;
  2120. status = acpi_get_table(ACPI_SIG_SPCR, 0,
  2121. (struct acpi_table_header **)&spcr_ptr);
  2122. if (ACPI_FAILURE(status)) {
  2123. pr_warn("STAO table present, but SPCR is missing\n");
  2124. return;
  2125. }
  2126. spcr_uart_addr = spcr_ptr->serial_port.address;
  2127. acpi_put_table((struct acpi_table_header *)spcr_ptr);
  2128. }
  2129. static bool acpi_scan_initialized;
  2130. void __init acpi_scan_init(void)
  2131. {
  2132. acpi_status status;
  2133. struct acpi_table_stao *stao_ptr;
  2134. acpi_pci_root_init();
  2135. acpi_pci_link_init();
  2136. acpi_processor_init();
  2137. acpi_platform_init();
  2138. acpi_lpss_init();
  2139. acpi_apd_init();
  2140. acpi_cmos_rtc_init();
  2141. acpi_container_init();
  2142. acpi_memory_hotplug_init();
  2143. acpi_watchdog_init();
  2144. acpi_pnp_init();
  2145. acpi_int340x_thermal_init();
  2146. acpi_amba_init();
  2147. acpi_init_lpit();
  2148. acpi_scan_add_handler(&generic_device_handler);
  2149. /*
  2150. * If there is STAO table, check whether it needs to ignore the UART
  2151. * device in SPCR table.
  2152. */
  2153. status = acpi_get_table(ACPI_SIG_STAO, 0,
  2154. (struct acpi_table_header **)&stao_ptr);
  2155. if (ACPI_SUCCESS(status)) {
  2156. if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
  2157. pr_info("STAO Name List not yet supported.\n");
  2158. if (stao_ptr->ignore_uart)
  2159. acpi_get_spcr_uart_addr();
  2160. acpi_put_table((struct acpi_table_header *)stao_ptr);
  2161. }
  2162. acpi_gpe_apply_masked_gpes();
  2163. acpi_update_all_gpes();
  2164. /*
  2165. * Although we call __add_memory() that is documented to require the
  2166. * device_hotplug_lock, it is not necessary here because this is an
  2167. * early code when userspace or any other code path cannot trigger
  2168. * hotplug/hotunplug operations.
  2169. */
  2170. mutex_lock(&acpi_scan_lock);
  2171. /*
  2172. * Enumerate devices in the ACPI namespace.
  2173. */
  2174. if (acpi_bus_scan(ACPI_ROOT_OBJECT))
  2175. goto unlock;
  2176. acpi_root = acpi_fetch_acpi_dev(ACPI_ROOT_OBJECT);
  2177. if (!acpi_root)
  2178. goto unlock;
  2179. /* Fixed feature devices do not exist on HW-reduced platform */
  2180. if (!acpi_gbl_reduced_hardware)
  2181. acpi_bus_scan_fixed();
  2182. acpi_turn_off_unused_power_resources();
  2183. acpi_scan_initialized = true;
  2184. unlock:
  2185. mutex_unlock(&acpi_scan_lock);
  2186. }
  2187. static struct acpi_probe_entry *ape;
  2188. static int acpi_probe_count;
  2189. static DEFINE_MUTEX(acpi_probe_mutex);
  2190. static int __init acpi_match_madt(union acpi_subtable_headers *header,
  2191. const unsigned long end)
  2192. {
  2193. if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
  2194. if (!ape->probe_subtbl(header, end))
  2195. acpi_probe_count++;
  2196. return 0;
  2197. }
  2198. int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
  2199. {
  2200. int count = 0;
  2201. if (acpi_disabled)
  2202. return 0;
  2203. mutex_lock(&acpi_probe_mutex);
  2204. for (ape = ap_head; nr; ape++, nr--) {
  2205. if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
  2206. acpi_probe_count = 0;
  2207. acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
  2208. count += acpi_probe_count;
  2209. } else {
  2210. int res;
  2211. res = acpi_table_parse(ape->id, ape->probe_table);
  2212. if (!res)
  2213. count++;
  2214. }
  2215. }
  2216. mutex_unlock(&acpi_probe_mutex);
  2217. return count;
  2218. }
  2219. static void acpi_table_events_fn(struct work_struct *work)
  2220. {
  2221. acpi_scan_lock_acquire();
  2222. acpi_bus_scan(ACPI_ROOT_OBJECT);
  2223. acpi_scan_lock_release();
  2224. kfree(work);
  2225. }
  2226. void acpi_scan_table_notify(void)
  2227. {
  2228. struct work_struct *work;
  2229. if (!acpi_scan_initialized)
  2230. return;
  2231. work = kmalloc(sizeof(*work), GFP_KERNEL);
  2232. if (!work)
  2233. return;
  2234. INIT_WORK(work, acpi_table_events_fn);
  2235. schedule_work(work);
  2236. }
  2237. int acpi_reconfig_notifier_register(struct notifier_block *nb)
  2238. {
  2239. return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
  2240. }
  2241. EXPORT_SYMBOL(acpi_reconfig_notifier_register);
  2242. int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
  2243. {
  2244. return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
  2245. }
  2246. EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);