property.c 42 KB

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