property.c 44 KB

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