scan.c 74 KB

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