property.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ACPI device specific properties support.
  4. *
  5. * Copyright (C) 2014 - 2023, Intel Corporation
  6. * All rights reserved.
  7. *
  8. * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
  9. * Darren Hart <dvhart@linux.intel.com>
  10. * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  11. * Sakari Ailus <sakari.ailus@linux.intel.com>
  12. */
  13. #define pr_fmt(fmt) "ACPI: " fmt
  14. #include <linux/acpi.h>
  15. #include <linux/device.h>
  16. #include <linux/export.h>
  17. #include "internal.h"
  18. static int acpi_data_get_property_array(const struct acpi_device_data *data,
  19. const char *name,
  20. acpi_object_type type,
  21. const union acpi_object **obj);
  22. /*
  23. * The GUIDs here are made equivalent to each other in order to avoid extra
  24. * complexity in the properties handling code, with the caveat that the
  25. * kernel will accept certain combinations of GUID and properties that are
  26. * not defined without a warning. For instance if any of the properties
  27. * from different GUID appear in a property list of another, it will be
  28. * accepted by the kernel. Firmware validation tools should catch these.
  29. *
  30. * References:
  31. *
  32. * [1] UEFI DSD Guide.
  33. * https://github.com/UEFI/DSD-Guide/blob/main/src/dsd-guide.adoc
  34. */
  35. static const guid_t prp_guids[] = {
  36. /* ACPI _DSD device properties GUID [1]: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
  37. GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c,
  38. 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01),
  39. /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */
  40. GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3,
  41. 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4),
  42. /* External facing port GUID: efcc06cc-73ac-4bc3-bff0-76143807c389 */
  43. GUID_INIT(0xefcc06cc, 0x73ac, 0x4bc3,
  44. 0xbf, 0xf0, 0x76, 0x14, 0x38, 0x07, 0xc3, 0x89),
  45. /* Thunderbolt GUID for IMR_VALID: c44d002f-69f9-4e7d-a904-a7baabdf43f7 */
  46. GUID_INIT(0xc44d002f, 0x69f9, 0x4e7d,
  47. 0xa9, 0x04, 0xa7, 0xba, 0xab, 0xdf, 0x43, 0xf7),
  48. /* Thunderbolt GUID for WAKE_SUPPORTED: 6c501103-c189-4296-ba72-9bf5a26ebe5d */
  49. GUID_INIT(0x6c501103, 0xc189, 0x4296,
  50. 0xba, 0x72, 0x9b, 0xf5, 0xa2, 0x6e, 0xbe, 0x5d),
  51. /* Storage device needs D3 GUID: 5025030f-842f-4ab4-a561-99a5189762d0 */
  52. GUID_INIT(0x5025030f, 0x842f, 0x4ab4,
  53. 0xa5, 0x61, 0x99, 0xa5, 0x18, 0x97, 0x62, 0xd0),
  54. };
  55. /* ACPI _DSD data subnodes GUID [1]: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
  56. static const guid_t ads_guid =
  57. GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
  58. 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
  59. /* ACPI _DSD data buffer GUID [1]: edb12dd0-363d-4085-a3d2-49522ca160c4 */
  60. static const guid_t buffer_prop_guid =
  61. GUID_INIT(0xedb12dd0, 0x363d, 0x4085,
  62. 0xa3, 0xd2, 0x49, 0x52, 0x2c, 0xa1, 0x60, 0xc4);
  63. static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
  64. union acpi_object *desc,
  65. struct acpi_device_data *data,
  66. struct fwnode_handle *parent);
  67. static bool acpi_extract_properties(acpi_handle handle,
  68. union acpi_object *desc,
  69. struct acpi_device_data *data);
  70. static bool acpi_nondev_subnode_extract(union acpi_object *desc,
  71. acpi_handle handle,
  72. const union acpi_object *link,
  73. struct list_head *list,
  74. struct fwnode_handle *parent)
  75. {
  76. struct acpi_data_node *dn;
  77. acpi_handle scope = NULL;
  78. bool result;
  79. if (acpi_graph_ignore_port(handle))
  80. return false;
  81. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  82. if (!dn)
  83. return false;
  84. dn->name = link->package.elements[0].string.pointer;
  85. fwnode_init(&dn->fwnode, &acpi_data_fwnode_ops);
  86. dn->parent = parent;
  87. INIT_LIST_HEAD(&dn->data.properties);
  88. INIT_LIST_HEAD(&dn->data.subnodes);
  89. /*
  90. * The scope for the completion of relative pathname segments and
  91. * subnode object lookup is the one of the namespace node (device)
  92. * containing the object that has returned the package. That is, it's
  93. * the scope of that object's parent device.
  94. */
  95. if (handle)
  96. acpi_get_parent(handle, &scope);
  97. /*
  98. * Extract properties from the _DSD-equivalent package pointed to by
  99. * desc and use scope (if not NULL) for the completion of relative
  100. * pathname segments.
  101. *
  102. * The extracted properties will be held in the new data node dn.
  103. */
  104. result = acpi_extract_properties(scope, desc, &dn->data);
  105. /*
  106. * Look for subnodes in the _DSD-equivalent package pointed to by desc
  107. * and create child nodes of dn if there are any.
  108. */
  109. if (acpi_enumerate_nondev_subnodes(scope, desc, &dn->data, &dn->fwnode))
  110. result = true;
  111. if (result) {
  112. /*
  113. * This will be NULL if the desc package is embedded in an outer
  114. * _DSD-equivalent package and its scope cannot be determined.
  115. */
  116. dn->handle = handle;
  117. dn->data.pointer = desc;
  118. list_add_tail(&dn->sibling, list);
  119. return true;
  120. }
  121. kfree(dn);
  122. acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
  123. return false;
  124. }
  125. static bool acpi_nondev_subnode_ok(acpi_handle scope,
  126. const union acpi_object *link,
  127. struct list_head *list,
  128. struct fwnode_handle *parent)
  129. {
  130. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  131. acpi_handle handle;
  132. acpi_status status;
  133. /*
  134. * If the scope is unknown, the _DSD-equivalent package being parsed
  135. * was embedded in an outer _DSD-equivalent package as a result of
  136. * direct evaluation of an object pointed to by a reference. In that
  137. * case, using a pathname as the target object pointer is invalid.
  138. */
  139. if (!scope)
  140. return false;
  141. status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
  142. &handle);
  143. if (ACPI_FAILURE(status))
  144. return false;
  145. status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
  146. ACPI_TYPE_PACKAGE);
  147. if (ACPI_FAILURE(status))
  148. return false;
  149. if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
  150. parent))
  151. return true;
  152. ACPI_FREE(buf.pointer);
  153. return false;
  154. }
  155. static bool acpi_add_nondev_subnodes(acpi_handle scope,
  156. union acpi_object *links,
  157. struct list_head *list,
  158. struct fwnode_handle *parent)
  159. {
  160. bool ret = false;
  161. int i;
  162. /*
  163. * Every element in the links package is expected to represent a link
  164. * to a non-device node in a tree containing device-specific data.
  165. */
  166. for (i = 0; i < links->package.count; i++) {
  167. union acpi_object *link, *desc;
  168. bool result;
  169. link = &links->package.elements[i];
  170. /* Only two elements allowed. */
  171. if (link->package.count != 2)
  172. continue;
  173. /* The first one (the key) must be a string. */
  174. if (link->package.elements[0].type != ACPI_TYPE_STRING)
  175. continue;
  176. /* The second one (the target) may be a string or a package. */
  177. switch (link->package.elements[1].type) {
  178. case ACPI_TYPE_STRING:
  179. /*
  180. * The string is expected to be a full pathname or a
  181. * pathname segment relative to the given scope. That
  182. * pathname is expected to point to an object returning
  183. * a package that contains _DSD-equivalent information.
  184. */
  185. result = acpi_nondev_subnode_ok(scope, link, list,
  186. parent);
  187. break;
  188. case ACPI_TYPE_PACKAGE:
  189. /*
  190. * This happens when a reference is used in AML to
  191. * point to the target. Since the target is expected
  192. * to be a named object, a reference to it will cause it
  193. * to be avaluated in place and its return package will
  194. * be embedded in the links package at the location of
  195. * the reference.
  196. *
  197. * The target package is expected to contain _DSD-
  198. * equivalent information, but the scope in which it
  199. * is located in the original AML is unknown. Thus
  200. * it cannot contain pathname segments represented as
  201. * strings because there is no way to build full
  202. * pathnames out of them.
  203. */
  204. acpi_handle_debug(scope, "subnode %s: Unknown scope\n",
  205. link->package.elements[0].string.pointer);
  206. desc = &link->package.elements[1];
  207. result = acpi_nondev_subnode_extract(desc, NULL, link,
  208. list, parent);
  209. break;
  210. case ACPI_TYPE_LOCAL_REFERENCE:
  211. /*
  212. * It is not expected to see any local references in
  213. * the links package because referencing a named object
  214. * should cause it to be evaluated in place.
  215. */
  216. acpi_handle_info(scope, "subnode %s: Unexpected reference\n",
  217. link->package.elements[0].string.pointer);
  218. fallthrough;
  219. default:
  220. result = false;
  221. break;
  222. }
  223. ret = ret || result;
  224. }
  225. return ret;
  226. }
  227. static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
  228. union acpi_object *desc,
  229. struct acpi_device_data *data,
  230. struct fwnode_handle *parent)
  231. {
  232. int i;
  233. /* Look for the ACPI data subnodes GUID. */
  234. for (i = 0; i < desc->package.count; i += 2) {
  235. const union acpi_object *guid;
  236. union acpi_object *links;
  237. guid = &desc->package.elements[i];
  238. links = &desc->package.elements[i + 1];
  239. /*
  240. * The first element must be a GUID and the second one must be
  241. * a package.
  242. */
  243. if (guid->type != ACPI_TYPE_BUFFER ||
  244. guid->buffer.length != 16 ||
  245. links->type != ACPI_TYPE_PACKAGE)
  246. break;
  247. if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
  248. continue;
  249. return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
  250. parent);
  251. }
  252. return false;
  253. }
  254. static bool acpi_property_value_ok(const union acpi_object *value)
  255. {
  256. int j;
  257. /*
  258. * The value must be an integer, a string, a reference, or a package
  259. * whose every element must be an integer, a string, or a reference.
  260. */
  261. switch (value->type) {
  262. case ACPI_TYPE_INTEGER:
  263. case ACPI_TYPE_STRING:
  264. case ACPI_TYPE_LOCAL_REFERENCE:
  265. return true;
  266. case ACPI_TYPE_PACKAGE:
  267. for (j = 0; j < value->package.count; j++)
  268. switch (value->package.elements[j].type) {
  269. case ACPI_TYPE_INTEGER:
  270. case ACPI_TYPE_STRING:
  271. case ACPI_TYPE_LOCAL_REFERENCE:
  272. continue;
  273. default:
  274. return false;
  275. }
  276. return true;
  277. }
  278. return false;
  279. }
  280. static bool acpi_properties_format_valid(const union acpi_object *properties)
  281. {
  282. int i;
  283. for (i = 0; i < properties->package.count; i++) {
  284. const union acpi_object *property;
  285. property = &properties->package.elements[i];
  286. /*
  287. * Only two elements allowed, the first one must be a string and
  288. * the second one has to satisfy certain conditions.
  289. */
  290. if (property->package.count != 2
  291. || property->package.elements[0].type != ACPI_TYPE_STRING
  292. || !acpi_property_value_ok(&property->package.elements[1]))
  293. return false;
  294. }
  295. return true;
  296. }
  297. static void acpi_init_of_compatible(struct acpi_device *adev)
  298. {
  299. const union acpi_object *of_compatible;
  300. int ret;
  301. ret = acpi_data_get_property_array(&adev->data, "compatible",
  302. ACPI_TYPE_STRING, &of_compatible);
  303. if (ret) {
  304. ret = acpi_dev_get_property(adev, "compatible",
  305. ACPI_TYPE_STRING, &of_compatible);
  306. if (ret) {
  307. struct acpi_device *parent;
  308. parent = acpi_dev_parent(adev);
  309. if (parent && parent->flags.of_compatible_ok)
  310. goto out;
  311. return;
  312. }
  313. }
  314. adev->data.of_compatible = of_compatible;
  315. out:
  316. adev->flags.of_compatible_ok = 1;
  317. }
  318. static bool acpi_is_property_guid(const guid_t *guid)
  319. {
  320. int i;
  321. for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
  322. if (guid_equal(guid, &prp_guids[i]))
  323. return true;
  324. }
  325. return false;
  326. }
  327. struct acpi_device_properties *
  328. acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid,
  329. union acpi_object *properties)
  330. {
  331. struct acpi_device_properties *props;
  332. props = kzalloc(sizeof(*props), GFP_KERNEL);
  333. if (props) {
  334. INIT_LIST_HEAD(&props->list);
  335. props->guid = guid;
  336. props->properties = properties;
  337. list_add_tail(&props->list, &data->properties);
  338. }
  339. return props;
  340. }
  341. static void acpi_nondev_subnode_tag(acpi_handle handle, void *context)
  342. {
  343. }
  344. static void acpi_untie_nondev_subnodes(struct acpi_device_data *data)
  345. {
  346. struct acpi_data_node *dn;
  347. list_for_each_entry(dn, &data->subnodes, sibling) {
  348. if (!dn->handle)
  349. continue;
  350. acpi_detach_data(dn->handle, acpi_nondev_subnode_tag);
  351. acpi_untie_nondev_subnodes(&dn->data);
  352. }
  353. }
  354. static bool acpi_tie_nondev_subnodes(struct acpi_device_data *data)
  355. {
  356. struct acpi_data_node *dn;
  357. list_for_each_entry(dn, &data->subnodes, sibling) {
  358. acpi_status status;
  359. bool ret;
  360. if (!dn->handle)
  361. continue;
  362. status = acpi_attach_data(dn->handle, acpi_nondev_subnode_tag, dn);
  363. if (ACPI_FAILURE(status) && status != AE_ALREADY_EXISTS) {
  364. acpi_handle_err(dn->handle, "Can't tag data node\n");
  365. return false;
  366. }
  367. ret = acpi_tie_nondev_subnodes(&dn->data);
  368. if (!ret)
  369. return ret;
  370. }
  371. return true;
  372. }
  373. static void acpi_data_add_buffer_props(acpi_handle handle,
  374. struct acpi_device_data *data,
  375. union acpi_object *properties)
  376. {
  377. struct acpi_device_properties *props;
  378. union acpi_object *package;
  379. size_t alloc_size;
  380. unsigned int i;
  381. u32 *count;
  382. if (check_mul_overflow((size_t)properties->package.count,
  383. sizeof(*package) + sizeof(void *),
  384. &alloc_size) ||
  385. check_add_overflow(sizeof(*props) + sizeof(*package), alloc_size,
  386. &alloc_size)) {
  387. acpi_handle_warn(handle,
  388. "can't allocate memory for %u buffer props",
  389. properties->package.count);
  390. return;
  391. }
  392. props = kvzalloc(alloc_size, GFP_KERNEL);
  393. if (!props)
  394. return;
  395. props->guid = &buffer_prop_guid;
  396. props->bufs = (void *)(props + 1);
  397. props->properties = (void *)(props->bufs + properties->package.count);
  398. /* Outer package */
  399. package = props->properties;
  400. package->type = ACPI_TYPE_PACKAGE;
  401. package->package.elements = package + 1;
  402. count = &package->package.count;
  403. *count = 0;
  404. /* Inner packages */
  405. package++;
  406. for (i = 0; i < properties->package.count; i++) {
  407. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  408. union acpi_object *property = &properties->package.elements[i];
  409. union acpi_object *prop, *obj, *buf_obj;
  410. acpi_status status;
  411. if (property->type != ACPI_TYPE_PACKAGE ||
  412. property->package.count != 2) {
  413. acpi_handle_warn(handle,
  414. "buffer property %u has %u entries\n",
  415. i, property->package.count);
  416. continue;
  417. }
  418. prop = &property->package.elements[0];
  419. obj = &property->package.elements[1];
  420. if (prop->type != ACPI_TYPE_STRING ||
  421. obj->type != ACPI_TYPE_STRING) {
  422. acpi_handle_warn(handle,
  423. "wrong object types %u and %u\n",
  424. prop->type, obj->type);
  425. continue;
  426. }
  427. status = acpi_evaluate_object_typed(handle, obj->string.pointer,
  428. NULL, &buf,
  429. ACPI_TYPE_BUFFER);
  430. if (ACPI_FAILURE(status)) {
  431. acpi_handle_warn(handle,
  432. "can't evaluate \"%*pE\" as buffer\n",
  433. obj->string.length,
  434. obj->string.pointer);
  435. continue;
  436. }
  437. package->type = ACPI_TYPE_PACKAGE;
  438. package->package.elements = prop;
  439. package->package.count = 2;
  440. buf_obj = buf.pointer;
  441. /* Replace the string object with a buffer object */
  442. obj->type = ACPI_TYPE_BUFFER;
  443. obj->buffer.length = buf_obj->buffer.length;
  444. obj->buffer.pointer = buf_obj->buffer.pointer;
  445. props->bufs[i] = buf.pointer;
  446. package++;
  447. (*count)++;
  448. }
  449. if (*count)
  450. list_add(&props->list, &data->properties);
  451. else
  452. kvfree(props);
  453. }
  454. static bool acpi_extract_properties(acpi_handle scope, union acpi_object *desc,
  455. struct acpi_device_data *data)
  456. {
  457. int i;
  458. if (desc->package.count % 2)
  459. return false;
  460. /* Look for the device properties GUID. */
  461. for (i = 0; i < desc->package.count; i += 2) {
  462. const union acpi_object *guid;
  463. union acpi_object *properties;
  464. guid = &desc->package.elements[i];
  465. properties = &desc->package.elements[i + 1];
  466. /*
  467. * The first element must be a GUID and the second one must be
  468. * a package.
  469. */
  470. if (guid->type != ACPI_TYPE_BUFFER ||
  471. guid->buffer.length != 16 ||
  472. properties->type != ACPI_TYPE_PACKAGE)
  473. break;
  474. if (guid_equal((guid_t *)guid->buffer.pointer,
  475. &buffer_prop_guid)) {
  476. acpi_data_add_buffer_props(scope, data, properties);
  477. continue;
  478. }
  479. if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
  480. continue;
  481. /*
  482. * We found the matching GUID. Now validate the format of the
  483. * package immediately following it.
  484. */
  485. if (!acpi_properties_format_valid(properties))
  486. continue;
  487. acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
  488. properties);
  489. }
  490. return !list_empty(&data->properties);
  491. }
  492. void acpi_init_properties(struct acpi_device *adev)
  493. {
  494. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  495. struct acpi_hardware_id *hwid;
  496. acpi_status status;
  497. bool acpi_of = false;
  498. INIT_LIST_HEAD(&adev->data.properties);
  499. INIT_LIST_HEAD(&adev->data.subnodes);
  500. if (!adev->handle)
  501. return;
  502. /*
  503. * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
  504. * Device Tree compatible properties for this device.
  505. */
  506. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  507. if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
  508. acpi_of = true;
  509. break;
  510. }
  511. }
  512. status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
  513. ACPI_TYPE_PACKAGE);
  514. if (ACPI_FAILURE(status))
  515. goto out;
  516. if (acpi_extract_properties(adev->handle, buf.pointer, &adev->data)) {
  517. adev->data.pointer = buf.pointer;
  518. if (acpi_of)
  519. acpi_init_of_compatible(adev);
  520. }
  521. if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
  522. &adev->data, acpi_fwnode_handle(adev)))
  523. adev->data.pointer = buf.pointer;
  524. if (!adev->data.pointer) {
  525. acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
  526. ACPI_FREE(buf.pointer);
  527. } else {
  528. if (!acpi_tie_nondev_subnodes(&adev->data))
  529. acpi_untie_nondev_subnodes(&adev->data);
  530. }
  531. out:
  532. if (acpi_of && !adev->flags.of_compatible_ok)
  533. acpi_handle_info(adev->handle,
  534. ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
  535. if (!adev->data.pointer)
  536. acpi_extract_apple_properties(adev);
  537. }
  538. static void acpi_free_device_properties(struct list_head *list)
  539. {
  540. struct acpi_device_properties *props, *tmp;
  541. list_for_each_entry_safe(props, tmp, list, list) {
  542. u32 i;
  543. list_del(&props->list);
  544. /* Buffer data properties were separately allocated */
  545. if (props->bufs)
  546. for (i = 0; i < props->properties->package.count; i++)
  547. ACPI_FREE(props->bufs[i]);
  548. kvfree(props);
  549. }
  550. }
  551. static void acpi_destroy_nondev_subnodes(struct list_head *list)
  552. {
  553. struct acpi_data_node *dn, *next;
  554. if (list_empty(list))
  555. return;
  556. list_for_each_entry_safe_reverse(dn, next, list, sibling) {
  557. acpi_destroy_nondev_subnodes(&dn->data.subnodes);
  558. wait_for_completion(&dn->kobj_done);
  559. list_del(&dn->sibling);
  560. ACPI_FREE((void *)dn->data.pointer);
  561. acpi_free_device_properties(&dn->data.properties);
  562. kfree(dn);
  563. }
  564. }
  565. void acpi_free_properties(struct acpi_device *adev)
  566. {
  567. acpi_untie_nondev_subnodes(&adev->data);
  568. acpi_destroy_nondev_subnodes(&adev->data.subnodes);
  569. ACPI_FREE((void *)adev->data.pointer);
  570. adev->data.of_compatible = NULL;
  571. adev->data.pointer = NULL;
  572. acpi_free_device_properties(&adev->data.properties);
  573. }
  574. /**
  575. * acpi_data_get_property - return an ACPI property with given name
  576. * @data: ACPI device deta object to get the property from
  577. * @name: Name of the property
  578. * @type: Expected property type
  579. * @obj: Location to store the property value (if not %NULL)
  580. *
  581. * Look up a property with @name and store a pointer to the resulting ACPI
  582. * object at the location pointed to by @obj if found.
  583. *
  584. * Callers must not attempt to free the returned objects. These objects will be
  585. * freed by the ACPI core automatically during the removal of @data.
  586. *
  587. * Return: %0 if property with @name has been found (success),
  588. * %-EINVAL if the arguments are invalid,
  589. * %-EINVAL if the property doesn't exist,
  590. * %-EPROTO if the property value type doesn't match @type.
  591. */
  592. static int acpi_data_get_property(const struct acpi_device_data *data,
  593. const char *name, acpi_object_type type,
  594. const union acpi_object **obj)
  595. {
  596. const struct acpi_device_properties *props;
  597. if (!data || !name)
  598. return -EINVAL;
  599. if (!data->pointer || list_empty(&data->properties))
  600. return -EINVAL;
  601. list_for_each_entry(props, &data->properties, list) {
  602. const union acpi_object *properties;
  603. unsigned int i;
  604. properties = props->properties;
  605. for (i = 0; i < properties->package.count; i++) {
  606. const union acpi_object *propname, *propvalue;
  607. const union acpi_object *property;
  608. property = &properties->package.elements[i];
  609. propname = &property->package.elements[0];
  610. propvalue = &property->package.elements[1];
  611. if (!strcmp(name, propname->string.pointer)) {
  612. if (type != ACPI_TYPE_ANY &&
  613. propvalue->type != type)
  614. return -EPROTO;
  615. if (obj)
  616. *obj = propvalue;
  617. return 0;
  618. }
  619. }
  620. }
  621. return -EINVAL;
  622. }
  623. /**
  624. * acpi_dev_get_property - return an ACPI property with given name.
  625. * @adev: ACPI device to get the property from.
  626. * @name: Name of the property.
  627. * @type: Expected property type.
  628. * @obj: Location to store the property value (if not %NULL).
  629. */
  630. int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
  631. acpi_object_type type, const union acpi_object **obj)
  632. {
  633. return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
  634. }
  635. EXPORT_SYMBOL_GPL(acpi_dev_get_property);
  636. static const struct acpi_device_data *
  637. acpi_device_data_of_node(const struct fwnode_handle *fwnode)
  638. {
  639. if (is_acpi_device_node(fwnode)) {
  640. const struct acpi_device *adev = to_acpi_device_node(fwnode);
  641. return &adev->data;
  642. }
  643. if (is_acpi_data_node(fwnode)) {
  644. const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
  645. return &dn->data;
  646. }
  647. return NULL;
  648. }
  649. /**
  650. * acpi_node_prop_get - return an ACPI property with given name.
  651. * @fwnode: Firmware node to get the property from.
  652. * @propname: Name of the property.
  653. * @valptr: Location to store a pointer to the property value (if not %NULL).
  654. */
  655. int acpi_node_prop_get(const struct fwnode_handle *fwnode,
  656. const char *propname, void **valptr)
  657. {
  658. return acpi_data_get_property(acpi_device_data_of_node(fwnode),
  659. propname, ACPI_TYPE_ANY,
  660. (const union acpi_object **)valptr);
  661. }
  662. /**
  663. * acpi_data_get_property_array - return an ACPI array property with given name
  664. * @data: ACPI data object to get the property from
  665. * @name: Name of the property
  666. * @type: Expected type of array elements
  667. * @obj: Location to store a pointer to the property value (if not NULL)
  668. *
  669. * Look up an array property with @name and store a pointer to the resulting
  670. * ACPI object at the location pointed to by @obj if found.
  671. *
  672. * Callers must not attempt to free the returned objects. Those objects will be
  673. * freed by the ACPI core automatically during the removal of @data.
  674. *
  675. * Return: %0 if array property (package) with @name has been found (success),
  676. * %-EINVAL if the arguments are invalid,
  677. * %-EINVAL if the property doesn't exist,
  678. * %-EPROTO if the property is not a package or the type of its elements
  679. * doesn't match @type.
  680. */
  681. static int acpi_data_get_property_array(const struct acpi_device_data *data,
  682. const char *name,
  683. acpi_object_type type,
  684. const union acpi_object **obj)
  685. {
  686. const union acpi_object *prop;
  687. int ret, i;
  688. ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
  689. if (ret)
  690. return ret;
  691. if (type != ACPI_TYPE_ANY) {
  692. /* Check that all elements are of correct type. */
  693. for (i = 0; i < prop->package.count; i++)
  694. if (prop->package.elements[i].type != type)
  695. return -EPROTO;
  696. }
  697. if (obj)
  698. *obj = prop;
  699. return 0;
  700. }
  701. static struct fwnode_handle *
  702. acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  703. const char *childname)
  704. {
  705. struct fwnode_handle *child;
  706. fwnode_for_each_child_node(fwnode, child) {
  707. if (is_acpi_data_node(child)) {
  708. if (acpi_data_node_match(child, childname))
  709. return child;
  710. continue;
  711. }
  712. if (!strncmp(acpi_device_bid(to_acpi_device_node(child)),
  713. childname, ACPI_NAMESEG_SIZE))
  714. return child;
  715. }
  716. return NULL;
  717. }
  718. static int acpi_get_ref_args(struct fwnode_reference_args *args,
  719. struct fwnode_handle *ref_fwnode,
  720. const union acpi_object **element,
  721. const union acpi_object *end, size_t num_args)
  722. {
  723. u32 nargs = 0, i;
  724. /*
  725. * Assume the following integer elements are all args. Stop counting on
  726. * the first reference (possibly represented as a string) or end of the
  727. * package arguments. In case of neither reference, nor integer, return
  728. * an error, we can't parse it.
  729. */
  730. for (i = 0; (*element) + i < end && i < num_args; i++) {
  731. acpi_object_type type = (*element)[i].type;
  732. if (type == ACPI_TYPE_LOCAL_REFERENCE || type == ACPI_TYPE_STRING)
  733. break;
  734. if (type == ACPI_TYPE_INTEGER)
  735. nargs++;
  736. else
  737. return -EINVAL;
  738. }
  739. if (nargs > NR_FWNODE_REFERENCE_ARGS)
  740. return -EINVAL;
  741. if (args) {
  742. args->fwnode = ref_fwnode;
  743. args->nargs = nargs;
  744. for (i = 0; i < nargs; i++)
  745. args->args[i] = (*element)[i].integer.value;
  746. }
  747. (*element) += nargs;
  748. return 0;
  749. }
  750. static struct fwnode_handle *acpi_parse_string_ref(const struct fwnode_handle *fwnode,
  751. const char *refstring)
  752. {
  753. acpi_handle scope, handle;
  754. struct acpi_data_node *dn;
  755. struct acpi_device *device;
  756. acpi_status status;
  757. if (is_acpi_device_node(fwnode)) {
  758. scope = to_acpi_device_node(fwnode)->handle;
  759. } else if (is_acpi_data_node(fwnode)) {
  760. scope = to_acpi_data_node(fwnode)->handle;
  761. } else {
  762. pr_debug("Bad node type for node %pfw\n", fwnode);
  763. return NULL;
  764. }
  765. status = acpi_get_handle(scope, refstring, &handle);
  766. if (ACPI_FAILURE(status)) {
  767. acpi_handle_debug(scope, "Unable to get an ACPI handle for %s\n",
  768. refstring);
  769. return NULL;
  770. }
  771. device = acpi_fetch_acpi_dev(handle);
  772. if (device)
  773. return acpi_fwnode_handle(device);
  774. status = acpi_get_data_full(handle, acpi_nondev_subnode_tag,
  775. (void **)&dn, NULL);
  776. if (ACPI_FAILURE(status) || !dn) {
  777. acpi_handle_debug(handle, "Subnode not found\n");
  778. return NULL;
  779. }
  780. return &dn->fwnode;
  781. }
  782. /**
  783. * __acpi_node_get_property_reference - returns handle to the referenced object
  784. * @fwnode: Firmware node to get the property from
  785. * @propname: Name of the property
  786. * @index: Index of the reference to return
  787. * @num_args: Maximum number of arguments after each reference
  788. * @args: Location to store the returned reference with optional arguments
  789. * (may be NULL)
  790. *
  791. * Find property with @name, verifify that it is a package containing at least
  792. * one object reference and if so, store the ACPI device object pointer to the
  793. * target object in @args->adev. If the reference includes arguments, store
  794. * them in the @args->args[] array.
  795. *
  796. * If there's more than one reference in the property value package, @index is
  797. * used to select the one to return.
  798. *
  799. * It is possible to leave holes in the property value set like in the
  800. * example below:
  801. *
  802. * Package () {
  803. * "cs-gpios",
  804. * Package () {
  805. * ^GPIO, 19, 0, 0,
  806. * ^GPIO, 20, 0, 0,
  807. * 0,
  808. * ^GPIO, 21, 0, 0,
  809. * }
  810. * }
  811. *
  812. * Calling this function with index %2 or index %3 return %-ENOENT. If the
  813. * property does not contain any more values %-ENOENT is returned. The NULL
  814. * entry must be single integer and preferably contain value %0.
  815. *
  816. * Return: %0 on success, negative error code on failure.
  817. */
  818. int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
  819. const char *propname, size_t index, size_t num_args,
  820. struct fwnode_reference_args *args)
  821. {
  822. const union acpi_object *element, *end;
  823. const union acpi_object *obj;
  824. const struct acpi_device_data *data;
  825. struct fwnode_handle *ref_fwnode;
  826. struct acpi_device *device;
  827. int ret, idx = 0;
  828. data = acpi_device_data_of_node(fwnode);
  829. if (!data)
  830. return -ENOENT;
  831. ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
  832. if (ret)
  833. return ret == -EINVAL ? -ENOENT : -EINVAL;
  834. switch (obj->type) {
  835. case ACPI_TYPE_LOCAL_REFERENCE:
  836. /* Plain single reference without arguments. */
  837. if (index)
  838. return -ENOENT;
  839. device = acpi_fetch_acpi_dev(obj->reference.handle);
  840. if (!device)
  841. return -EINVAL;
  842. if (!args)
  843. return 0;
  844. args->fwnode = acpi_fwnode_handle(device);
  845. args->nargs = 0;
  846. return 0;
  847. case ACPI_TYPE_STRING:
  848. if (index)
  849. return -ENOENT;
  850. ref_fwnode = acpi_parse_string_ref(fwnode, obj->string.pointer);
  851. if (!ref_fwnode)
  852. return -EINVAL;
  853. args->fwnode = ref_fwnode;
  854. args->nargs = 0;
  855. return 0;
  856. case ACPI_TYPE_PACKAGE:
  857. /*
  858. * If it is not a single reference, then it is a package of
  859. * references, followed by number of ints as follows:
  860. *
  861. * Package () { REF, INT, REF, INT, INT }
  862. *
  863. * Here, REF may be either a local reference or a string. The
  864. * index argument is then used to determine which reference the
  865. * caller wants (along with the arguments).
  866. */
  867. break;
  868. default:
  869. return -EINVAL;
  870. }
  871. if (index >= obj->package.count)
  872. return -ENOENT;
  873. element = obj->package.elements;
  874. end = element + obj->package.count;
  875. while (element < end) {
  876. switch (element->type) {
  877. case ACPI_TYPE_LOCAL_REFERENCE:
  878. device = acpi_fetch_acpi_dev(element->reference.handle);
  879. if (!device)
  880. return -EINVAL;
  881. element++;
  882. ret = acpi_get_ref_args(idx == index ? args : NULL,
  883. acpi_fwnode_handle(device),
  884. &element, end, num_args);
  885. if (ret < 0)
  886. return ret;
  887. if (idx == index)
  888. return 0;
  889. break;
  890. case ACPI_TYPE_STRING:
  891. ref_fwnode = acpi_parse_string_ref(fwnode,
  892. element->string.pointer);
  893. if (!ref_fwnode)
  894. return -EINVAL;
  895. element++;
  896. ret = acpi_get_ref_args(idx == index ? args : NULL,
  897. ref_fwnode, &element, end,
  898. num_args);
  899. if (ret < 0)
  900. return ret;
  901. if (idx == index)
  902. return 0;
  903. break;
  904. case ACPI_TYPE_INTEGER:
  905. if (idx == index)
  906. return -ENOENT;
  907. element++;
  908. break;
  909. default:
  910. return -EINVAL;
  911. }
  912. idx++;
  913. }
  914. return -ENOENT;
  915. }
  916. EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
  917. static int acpi_data_prop_read_single(const struct acpi_device_data *data,
  918. const char *propname,
  919. enum dev_prop_type proptype, void *val)
  920. {
  921. const union acpi_object *obj;
  922. int ret = 0;
  923. if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
  924. ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
  925. else if (proptype == DEV_PROP_STRING)
  926. ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
  927. if (ret)
  928. return ret;
  929. switch (proptype) {
  930. case DEV_PROP_U8:
  931. if (obj->integer.value > U8_MAX)
  932. return -EOVERFLOW;
  933. if (val)
  934. *(u8 *)val = obj->integer.value;
  935. break;
  936. case DEV_PROP_U16:
  937. if (obj->integer.value > U16_MAX)
  938. return -EOVERFLOW;
  939. if (val)
  940. *(u16 *)val = obj->integer.value;
  941. break;
  942. case DEV_PROP_U32:
  943. if (obj->integer.value > U32_MAX)
  944. return -EOVERFLOW;
  945. if (val)
  946. *(u32 *)val = obj->integer.value;
  947. break;
  948. case DEV_PROP_U64:
  949. if (val)
  950. *(u64 *)val = obj->integer.value;
  951. break;
  952. case DEV_PROP_STRING:
  953. if (val)
  954. *(char **)val = obj->string.pointer;
  955. return 1;
  956. default:
  957. return -EINVAL;
  958. }
  959. /* When no storage provided return number of available values */
  960. return val ? 0 : 1;
  961. }
  962. #define acpi_copy_property_array_uint(items, val, nval) \
  963. ({ \
  964. typeof(items) __items = items; \
  965. typeof(val) __val = val; \
  966. typeof(nval) __nval = nval; \
  967. size_t i; \
  968. int ret = 0; \
  969. \
  970. for (i = 0; i < __nval; i++) { \
  971. if (__items->type == ACPI_TYPE_BUFFER) { \
  972. __val[i] = __items->buffer.pointer[i]; \
  973. continue; \
  974. } \
  975. if (__items[i].type != ACPI_TYPE_INTEGER) { \
  976. ret = -EPROTO; \
  977. break; \
  978. } \
  979. if (__items[i].integer.value > _Generic(__val, \
  980. u8 *: U8_MAX, \
  981. u16 *: U16_MAX, \
  982. u32 *: U32_MAX, \
  983. u64 *: U64_MAX)) { \
  984. ret = -EOVERFLOW; \
  985. break; \
  986. } \
  987. \
  988. __val[i] = __items[i].integer.value; \
  989. } \
  990. ret; \
  991. })
  992. static int acpi_copy_property_array_string(const union acpi_object *items,
  993. char **val, size_t nval)
  994. {
  995. int i;
  996. for (i = 0; i < nval; i++) {
  997. if (items[i].type != ACPI_TYPE_STRING)
  998. return -EPROTO;
  999. val[i] = items[i].string.pointer;
  1000. }
  1001. return nval;
  1002. }
  1003. static int acpi_data_prop_read(const struct acpi_device_data *data,
  1004. const char *propname,
  1005. enum dev_prop_type proptype,
  1006. void *val, size_t nval)
  1007. {
  1008. const union acpi_object *obj;
  1009. const union acpi_object *items;
  1010. int ret;
  1011. if (nval == 1 || !val) {
  1012. ret = acpi_data_prop_read_single(data, propname, proptype, val);
  1013. /*
  1014. * The overflow error means that the property is there and it is
  1015. * single-value, but its type does not match, so return.
  1016. */
  1017. if (ret >= 0 || ret == -EOVERFLOW)
  1018. return ret;
  1019. /*
  1020. * Reading this property as a single-value one failed, but its
  1021. * value may still be represented as one-element array, so
  1022. * continue.
  1023. */
  1024. }
  1025. ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
  1026. if (ret && proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
  1027. ret = acpi_data_get_property(data, propname, ACPI_TYPE_BUFFER,
  1028. &obj);
  1029. if (ret)
  1030. return ret;
  1031. if (!val) {
  1032. if (obj->type == ACPI_TYPE_BUFFER)
  1033. return obj->buffer.length;
  1034. return obj->package.count;
  1035. }
  1036. switch (proptype) {
  1037. case DEV_PROP_STRING:
  1038. break;
  1039. default:
  1040. if (obj->type == ACPI_TYPE_BUFFER) {
  1041. if (nval > obj->buffer.length)
  1042. return -EOVERFLOW;
  1043. } else {
  1044. if (nval > obj->package.count)
  1045. return -EOVERFLOW;
  1046. }
  1047. break;
  1048. }
  1049. if (obj->type == ACPI_TYPE_BUFFER) {
  1050. if (proptype != DEV_PROP_U8)
  1051. return -EPROTO;
  1052. items = obj;
  1053. } else {
  1054. items = obj->package.elements;
  1055. }
  1056. switch (proptype) {
  1057. case DEV_PROP_U8:
  1058. ret = acpi_copy_property_array_uint(items, (u8 *)val, nval);
  1059. break;
  1060. case DEV_PROP_U16:
  1061. ret = acpi_copy_property_array_uint(items, (u16 *)val, nval);
  1062. break;
  1063. case DEV_PROP_U32:
  1064. ret = acpi_copy_property_array_uint(items, (u32 *)val, nval);
  1065. break;
  1066. case DEV_PROP_U64:
  1067. ret = acpi_copy_property_array_uint(items, (u64 *)val, nval);
  1068. break;
  1069. case DEV_PROP_STRING:
  1070. nval = min_t(u32, nval, obj->package.count);
  1071. if (nval == 0)
  1072. return -ENODATA;
  1073. ret = acpi_copy_property_array_string(items, (char **)val, nval);
  1074. break;
  1075. default:
  1076. ret = -EINVAL;
  1077. break;
  1078. }
  1079. return ret;
  1080. }
  1081. /**
  1082. * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
  1083. * @fwnode: Firmware node to get the property from.
  1084. * @propname: Name of the property.
  1085. * @proptype: Expected property type.
  1086. * @val: Location to store the property value (if not %NULL).
  1087. * @nval: Size of the array pointed to by @val.
  1088. *
  1089. * If @val is %NULL, return the number of array elements comprising the value
  1090. * of the property. Otherwise, read at most @nval values to the array at the
  1091. * location pointed to by @val.
  1092. */
  1093. static int acpi_node_prop_read(const struct fwnode_handle *fwnode,
  1094. const char *propname, enum dev_prop_type proptype,
  1095. void *val, size_t nval)
  1096. {
  1097. return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
  1098. propname, proptype, val, nval);
  1099. }
  1100. static int stop_on_next(struct acpi_device *adev, void *data)
  1101. {
  1102. struct acpi_device **ret_p = data;
  1103. if (!*ret_p) {
  1104. *ret_p = adev;
  1105. return 1;
  1106. }
  1107. /* Skip until the "previous" object is found. */
  1108. if (*ret_p == adev)
  1109. *ret_p = NULL;
  1110. return 0;
  1111. }
  1112. /**
  1113. * acpi_get_next_subnode - Return the next child node handle for a fwnode
  1114. * @fwnode: Firmware node to find the next child node for.
  1115. * @child: Handle to one of the device's child nodes or a null handle.
  1116. */
  1117. struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
  1118. struct fwnode_handle *child)
  1119. {
  1120. struct acpi_device *adev = to_acpi_device_node(fwnode);
  1121. if ((!child || is_acpi_device_node(child)) && adev) {
  1122. struct acpi_device *child_adev = to_acpi_device_node(child);
  1123. acpi_dev_for_each_child(adev, stop_on_next, &child_adev);
  1124. if (child_adev)
  1125. return acpi_fwnode_handle(child_adev);
  1126. child = NULL;
  1127. }
  1128. if (!child || is_acpi_data_node(child)) {
  1129. const struct acpi_data_node *data = to_acpi_data_node(fwnode);
  1130. const struct list_head *head;
  1131. struct list_head *next;
  1132. struct acpi_data_node *dn;
  1133. /*
  1134. * We can have a combination of device and data nodes, e.g. with
  1135. * hierarchical _DSD properties. Make sure the adev pointer is
  1136. * restored before going through data nodes, otherwise we will
  1137. * be looking for data_nodes below the last device found instead
  1138. * of the common fwnode shared by device_nodes and data_nodes.
  1139. */
  1140. adev = to_acpi_device_node(fwnode);
  1141. if (adev)
  1142. head = &adev->data.subnodes;
  1143. else if (data)
  1144. head = &data->data.subnodes;
  1145. else
  1146. return NULL;
  1147. if (list_empty(head))
  1148. return NULL;
  1149. if (child) {
  1150. dn = to_acpi_data_node(child);
  1151. next = dn->sibling.next;
  1152. if (next == head)
  1153. return NULL;
  1154. dn = list_entry(next, struct acpi_data_node, sibling);
  1155. } else {
  1156. dn = list_first_entry(head, struct acpi_data_node, sibling);
  1157. }
  1158. return &dn->fwnode;
  1159. }
  1160. return NULL;
  1161. }
  1162. /**
  1163. * acpi_node_get_parent - Return parent fwnode of this fwnode
  1164. * @fwnode: Firmware node whose parent to get
  1165. *
  1166. * Returns parent node of an ACPI device or data firmware node or %NULL if
  1167. * not available.
  1168. */
  1169. static struct fwnode_handle *
  1170. acpi_node_get_parent(const struct fwnode_handle *fwnode)
  1171. {
  1172. if (is_acpi_data_node(fwnode)) {
  1173. /* All data nodes have parent pointer so just return that */
  1174. return to_acpi_data_node(fwnode)->parent;
  1175. }
  1176. if (is_acpi_device_node(fwnode)) {
  1177. struct acpi_device *parent;
  1178. parent = acpi_dev_parent(to_acpi_device_node(fwnode));
  1179. if (parent)
  1180. return acpi_fwnode_handle(parent);
  1181. }
  1182. return NULL;
  1183. }
  1184. /*
  1185. * Return true if the node is an ACPI graph node. Called on either ports
  1186. * or endpoints.
  1187. */
  1188. static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
  1189. const char *str)
  1190. {
  1191. unsigned int len = strlen(str);
  1192. const char *name;
  1193. if (!len || !is_acpi_data_node(fwnode))
  1194. return false;
  1195. name = to_acpi_data_node(fwnode)->name;
  1196. return (fwnode_property_present(fwnode, "reg") &&
  1197. !strncmp(name, str, len) && name[len] == '@') ||
  1198. fwnode_property_present(fwnode, str);
  1199. }
  1200. /**
  1201. * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
  1202. * @fwnode: Pointer to the parent firmware node
  1203. * @prev: Previous endpoint node or %NULL to get the first
  1204. *
  1205. * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
  1206. * %NULL if there is no next endpoint or in case of error. In case of success
  1207. * the next endpoint is returned.
  1208. */
  1209. static struct fwnode_handle *acpi_graph_get_next_endpoint(
  1210. const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
  1211. {
  1212. struct fwnode_handle *port = NULL;
  1213. struct fwnode_handle *endpoint;
  1214. if (!prev) {
  1215. do {
  1216. port = fwnode_get_next_child_node(fwnode, port);
  1217. /*
  1218. * The names of the port nodes begin with "port@"
  1219. * followed by the number of the port node and they also
  1220. * have a "reg" property that also has the number of the
  1221. * port node. For compatibility reasons a node is also
  1222. * recognised as a port node from the "port" property.
  1223. */
  1224. if (is_acpi_graph_node(port, "port"))
  1225. break;
  1226. } while (port);
  1227. } else {
  1228. port = fwnode_get_parent(prev);
  1229. }
  1230. if (!port)
  1231. return NULL;
  1232. endpoint = fwnode_get_next_child_node(port, prev);
  1233. while (!endpoint) {
  1234. port = fwnode_get_next_child_node(fwnode, port);
  1235. if (!port)
  1236. break;
  1237. if (is_acpi_graph_node(port, "port"))
  1238. endpoint = fwnode_get_next_child_node(port, NULL);
  1239. }
  1240. /*
  1241. * The names of the endpoint nodes begin with "endpoint@" followed by
  1242. * the number of the endpoint node and they also have a "reg" property
  1243. * that also has the number of the endpoint node. For compatibility
  1244. * reasons a node is also recognised as an endpoint node from the
  1245. * "endpoint" property.
  1246. */
  1247. if (!is_acpi_graph_node(endpoint, "endpoint"))
  1248. return NULL;
  1249. return endpoint;
  1250. }
  1251. /**
  1252. * acpi_graph_get_child_prop_value - Return a child with a given property value
  1253. * @fwnode: device fwnode
  1254. * @prop_name: The name of the property to look for
  1255. * @val: the desired property value
  1256. *
  1257. * Return the port node corresponding to a given port number. Returns
  1258. * the child node on success, NULL otherwise.
  1259. */
  1260. static struct fwnode_handle *acpi_graph_get_child_prop_value(
  1261. const struct fwnode_handle *fwnode, const char *prop_name,
  1262. unsigned int val)
  1263. {
  1264. struct fwnode_handle *child;
  1265. fwnode_for_each_child_node(fwnode, child) {
  1266. u32 nr;
  1267. if (fwnode_property_read_u32(child, prop_name, &nr))
  1268. continue;
  1269. if (val == nr)
  1270. return child;
  1271. }
  1272. return NULL;
  1273. }
  1274. /**
  1275. * acpi_graph_get_remote_endpoint - Parses and returns remote end of an endpoint
  1276. * @__fwnode: Endpoint firmware node pointing to a remote device
  1277. *
  1278. * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
  1279. */
  1280. static struct fwnode_handle *
  1281. acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
  1282. {
  1283. struct fwnode_handle *fwnode;
  1284. unsigned int port_nr, endpoint_nr;
  1285. struct fwnode_reference_args args;
  1286. int ret;
  1287. memset(&args, 0, sizeof(args));
  1288. ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
  1289. &args);
  1290. if (ret)
  1291. return NULL;
  1292. /* Direct endpoint reference? */
  1293. if (!is_acpi_device_node(args.fwnode))
  1294. return args.nargs ? NULL : args.fwnode;
  1295. /*
  1296. * Always require two arguments with the reference: port and
  1297. * endpoint indices.
  1298. */
  1299. if (args.nargs != 2)
  1300. return NULL;
  1301. fwnode = args.fwnode;
  1302. port_nr = args.args[0];
  1303. endpoint_nr = args.args[1];
  1304. fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
  1305. return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
  1306. }
  1307. static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
  1308. {
  1309. if (!is_acpi_device_node(fwnode))
  1310. return false;
  1311. return acpi_device_is_present(to_acpi_device_node(fwnode));
  1312. }
  1313. static const void *
  1314. acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
  1315. const struct device *dev)
  1316. {
  1317. return acpi_device_get_match_data(dev);
  1318. }
  1319. static bool acpi_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
  1320. {
  1321. return acpi_dma_supported(to_acpi_device_node(fwnode));
  1322. }
  1323. static enum dev_dma_attr
  1324. acpi_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
  1325. {
  1326. return acpi_get_dma_attr(to_acpi_device_node(fwnode));
  1327. }
  1328. static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
  1329. const char *propname)
  1330. {
  1331. return !acpi_node_prop_get(fwnode, propname, NULL);
  1332. }
  1333. static int
  1334. acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  1335. const char *propname,
  1336. unsigned int elem_size, void *val,
  1337. size_t nval)
  1338. {
  1339. enum dev_prop_type type;
  1340. switch (elem_size) {
  1341. case sizeof(u8):
  1342. type = DEV_PROP_U8;
  1343. break;
  1344. case sizeof(u16):
  1345. type = DEV_PROP_U16;
  1346. break;
  1347. case sizeof(u32):
  1348. type = DEV_PROP_U32;
  1349. break;
  1350. case sizeof(u64):
  1351. type = DEV_PROP_U64;
  1352. break;
  1353. default:
  1354. return -ENXIO;
  1355. }
  1356. return acpi_node_prop_read(fwnode, propname, type, val, nval);
  1357. }
  1358. static int
  1359. acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  1360. const char *propname, const char **val,
  1361. size_t nval)
  1362. {
  1363. return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
  1364. val, nval);
  1365. }
  1366. static int
  1367. acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
  1368. const char *prop, const char *nargs_prop,
  1369. unsigned int args_count, unsigned int index,
  1370. struct fwnode_reference_args *args)
  1371. {
  1372. return __acpi_node_get_property_reference(fwnode, prop, index,
  1373. args_count, args);
  1374. }
  1375. static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode)
  1376. {
  1377. const struct acpi_device *adev;
  1378. struct fwnode_handle *parent;
  1379. /* Is this the root node? */
  1380. parent = fwnode_get_parent(fwnode);
  1381. if (!parent)
  1382. return "\\";
  1383. fwnode_handle_put(parent);
  1384. if (is_acpi_data_node(fwnode)) {
  1385. const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
  1386. return dn->name;
  1387. }
  1388. adev = to_acpi_device_node(fwnode);
  1389. if (WARN_ON(!adev))
  1390. return NULL;
  1391. return acpi_device_bid(adev);
  1392. }
  1393. static const char *
  1394. acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
  1395. {
  1396. struct fwnode_handle *parent;
  1397. /* Is this the root node? */
  1398. parent = fwnode_get_parent(fwnode);
  1399. if (!parent)
  1400. return "";
  1401. /* Is this 2nd node from the root? */
  1402. parent = fwnode_get_next_parent(parent);
  1403. if (!parent)
  1404. return "";
  1405. fwnode_handle_put(parent);
  1406. /* ACPI device or data node. */
  1407. return ".";
  1408. }
  1409. static struct fwnode_handle *
  1410. acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
  1411. {
  1412. return acpi_node_get_parent(fwnode);
  1413. }
  1414. static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  1415. struct fwnode_endpoint *endpoint)
  1416. {
  1417. struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
  1418. endpoint->local_fwnode = fwnode;
  1419. if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port))
  1420. fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
  1421. if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id))
  1422. fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
  1423. return 0;
  1424. }
  1425. static int acpi_fwnode_irq_get(const struct fwnode_handle *fwnode,
  1426. unsigned int index)
  1427. {
  1428. struct resource res;
  1429. int ret;
  1430. ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
  1431. if (ret)
  1432. return ret;
  1433. return res.start;
  1434. }
  1435. #define DECLARE_ACPI_FWNODE_OPS(ops) \
  1436. const struct fwnode_operations ops = { \
  1437. .device_is_available = acpi_fwnode_device_is_available, \
  1438. .device_get_match_data = acpi_fwnode_device_get_match_data, \
  1439. .device_dma_supported = \
  1440. acpi_fwnode_device_dma_supported, \
  1441. .device_get_dma_attr = acpi_fwnode_device_get_dma_attr, \
  1442. .property_present = acpi_fwnode_property_present, \
  1443. .property_read_int_array = \
  1444. acpi_fwnode_property_read_int_array, \
  1445. .property_read_string_array = \
  1446. acpi_fwnode_property_read_string_array, \
  1447. .get_parent = acpi_node_get_parent, \
  1448. .get_next_child_node = acpi_get_next_subnode, \
  1449. .get_named_child_node = acpi_fwnode_get_named_child_node, \
  1450. .get_name = acpi_fwnode_get_name, \
  1451. .get_name_prefix = acpi_fwnode_get_name_prefix, \
  1452. .get_reference_args = acpi_fwnode_get_reference_args, \
  1453. .graph_get_next_endpoint = \
  1454. acpi_graph_get_next_endpoint, \
  1455. .graph_get_remote_endpoint = \
  1456. acpi_graph_get_remote_endpoint, \
  1457. .graph_get_port_parent = acpi_fwnode_get_parent, \
  1458. .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
  1459. .irq_get = acpi_fwnode_irq_get, \
  1460. }; \
  1461. EXPORT_SYMBOL_GPL(ops)
  1462. DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
  1463. DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
  1464. const struct fwnode_operations acpi_static_fwnode_ops;
  1465. bool is_acpi_device_node(const struct fwnode_handle *fwnode)
  1466. {
  1467. return !IS_ERR_OR_NULL(fwnode) &&
  1468. fwnode->ops == &acpi_device_fwnode_ops;
  1469. }
  1470. EXPORT_SYMBOL(is_acpi_device_node);
  1471. bool is_acpi_data_node(const struct fwnode_handle *fwnode)
  1472. {
  1473. return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
  1474. }
  1475. EXPORT_SYMBOL(is_acpi_data_node);