scan.c 71 KB

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