iort.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * Copyright (C) 2016, Semihalf
  3. * Author: Tomasz Nowicki <tn@semihalf.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * This file implements early detection/parsing of I/O mapping
  15. * reported to OS through firmware via I/O Remapping Table (IORT)
  16. * IORT document number: ARM DEN 0049A
  17. */
  18. #define pr_fmt(fmt) "ACPI: IORT: " fmt
  19. #include <linux/acpi_iort.h>
  20. #include <linux/iommu.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #define IORT_TYPE_MASK(type) (1 << (type))
  27. #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
  28. #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
  29. (1 << ACPI_IORT_NODE_SMMU_V3))
  30. struct iort_its_msi_chip {
  31. struct list_head list;
  32. struct fwnode_handle *fw_node;
  33. phys_addr_t base_addr;
  34. u32 translation_id;
  35. };
  36. struct iort_fwnode {
  37. struct list_head list;
  38. struct acpi_iort_node *iort_node;
  39. struct fwnode_handle *fwnode;
  40. };
  41. static LIST_HEAD(iort_fwnode_list);
  42. static DEFINE_SPINLOCK(iort_fwnode_lock);
  43. /**
  44. * iort_set_fwnode() - Create iort_fwnode and use it to register
  45. * iommu data in the iort_fwnode_list
  46. *
  47. * @node: IORT table node associated with the IOMMU
  48. * @fwnode: fwnode associated with the IORT node
  49. *
  50. * Returns: 0 on success
  51. * <0 on failure
  52. */
  53. static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
  54. struct fwnode_handle *fwnode)
  55. {
  56. struct iort_fwnode *np;
  57. np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
  58. if (WARN_ON(!np))
  59. return -ENOMEM;
  60. INIT_LIST_HEAD(&np->list);
  61. np->iort_node = iort_node;
  62. np->fwnode = fwnode;
  63. spin_lock(&iort_fwnode_lock);
  64. list_add_tail(&np->list, &iort_fwnode_list);
  65. spin_unlock(&iort_fwnode_lock);
  66. return 0;
  67. }
  68. /**
  69. * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
  70. *
  71. * @node: IORT table node to be looked-up
  72. *
  73. * Returns: fwnode_handle pointer on success, NULL on failure
  74. */
  75. static inline struct fwnode_handle *iort_get_fwnode(
  76. struct acpi_iort_node *node)
  77. {
  78. struct iort_fwnode *curr;
  79. struct fwnode_handle *fwnode = NULL;
  80. spin_lock(&iort_fwnode_lock);
  81. list_for_each_entry(curr, &iort_fwnode_list, list) {
  82. if (curr->iort_node == node) {
  83. fwnode = curr->fwnode;
  84. break;
  85. }
  86. }
  87. spin_unlock(&iort_fwnode_lock);
  88. return fwnode;
  89. }
  90. /**
  91. * iort_delete_fwnode() - Delete fwnode associated with an IORT node
  92. *
  93. * @node: IORT table node associated with fwnode to delete
  94. */
  95. static inline void iort_delete_fwnode(struct acpi_iort_node *node)
  96. {
  97. struct iort_fwnode *curr, *tmp;
  98. spin_lock(&iort_fwnode_lock);
  99. list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
  100. if (curr->iort_node == node) {
  101. list_del(&curr->list);
  102. kfree(curr);
  103. break;
  104. }
  105. }
  106. spin_unlock(&iort_fwnode_lock);
  107. }
  108. /**
  109. * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
  110. *
  111. * @fwnode: fwnode associated with device to be looked-up
  112. *
  113. * Returns: iort_node pointer on success, NULL on failure
  114. */
  115. static inline struct acpi_iort_node *iort_get_iort_node(
  116. struct fwnode_handle *fwnode)
  117. {
  118. struct iort_fwnode *curr;
  119. struct acpi_iort_node *iort_node = NULL;
  120. spin_lock(&iort_fwnode_lock);
  121. list_for_each_entry(curr, &iort_fwnode_list, list) {
  122. if (curr->fwnode == fwnode) {
  123. iort_node = curr->iort_node;
  124. break;
  125. }
  126. }
  127. spin_unlock(&iort_fwnode_lock);
  128. return iort_node;
  129. }
  130. typedef acpi_status (*iort_find_node_callback)
  131. (struct acpi_iort_node *node, void *context);
  132. /* Root pointer to the mapped IORT table */
  133. static struct acpi_table_header *iort_table;
  134. static LIST_HEAD(iort_msi_chip_list);
  135. static DEFINE_SPINLOCK(iort_msi_chip_lock);
  136. /**
  137. * iort_register_domain_token() - register domain token along with related
  138. * ITS ID and base address to the list from where we can get it back later on.
  139. * @trans_id: ITS ID.
  140. * @base: ITS base address.
  141. * @fw_node: Domain token.
  142. *
  143. * Returns: 0 on success, -ENOMEM if no memory when allocating list element
  144. */
  145. int iort_register_domain_token(int trans_id, phys_addr_t base,
  146. struct fwnode_handle *fw_node)
  147. {
  148. struct iort_its_msi_chip *its_msi_chip;
  149. its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
  150. if (!its_msi_chip)
  151. return -ENOMEM;
  152. its_msi_chip->fw_node = fw_node;
  153. its_msi_chip->translation_id = trans_id;
  154. its_msi_chip->base_addr = base;
  155. spin_lock(&iort_msi_chip_lock);
  156. list_add(&its_msi_chip->list, &iort_msi_chip_list);
  157. spin_unlock(&iort_msi_chip_lock);
  158. return 0;
  159. }
  160. /**
  161. * iort_deregister_domain_token() - Deregister domain token based on ITS ID
  162. * @trans_id: ITS ID.
  163. *
  164. * Returns: none.
  165. */
  166. void iort_deregister_domain_token(int trans_id)
  167. {
  168. struct iort_its_msi_chip *its_msi_chip, *t;
  169. spin_lock(&iort_msi_chip_lock);
  170. list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
  171. if (its_msi_chip->translation_id == trans_id) {
  172. list_del(&its_msi_chip->list);
  173. kfree(its_msi_chip);
  174. break;
  175. }
  176. }
  177. spin_unlock(&iort_msi_chip_lock);
  178. }
  179. /**
  180. * iort_find_domain_token() - Find domain token based on given ITS ID
  181. * @trans_id: ITS ID.
  182. *
  183. * Returns: domain token when find on the list, NULL otherwise
  184. */
  185. struct fwnode_handle *iort_find_domain_token(int trans_id)
  186. {
  187. struct fwnode_handle *fw_node = NULL;
  188. struct iort_its_msi_chip *its_msi_chip;
  189. spin_lock(&iort_msi_chip_lock);
  190. list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
  191. if (its_msi_chip->translation_id == trans_id) {
  192. fw_node = its_msi_chip->fw_node;
  193. break;
  194. }
  195. }
  196. spin_unlock(&iort_msi_chip_lock);
  197. return fw_node;
  198. }
  199. static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
  200. iort_find_node_callback callback,
  201. void *context)
  202. {
  203. struct acpi_iort_node *iort_node, *iort_end;
  204. struct acpi_table_iort *iort;
  205. int i;
  206. if (!iort_table)
  207. return NULL;
  208. /* Get the first IORT node */
  209. iort = (struct acpi_table_iort *)iort_table;
  210. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  211. iort->node_offset);
  212. iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  213. iort_table->length);
  214. for (i = 0; i < iort->node_count; i++) {
  215. if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
  216. "IORT node pointer overflows, bad table!\n"))
  217. return NULL;
  218. if (iort_node->type == type &&
  219. ACPI_SUCCESS(callback(iort_node, context)))
  220. return iort_node;
  221. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
  222. iort_node->length);
  223. }
  224. return NULL;
  225. }
  226. static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
  227. void *context)
  228. {
  229. struct device *dev = context;
  230. acpi_status status = AE_NOT_FOUND;
  231. if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
  232. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  233. struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
  234. struct acpi_iort_named_component *ncomp;
  235. if (!adev)
  236. goto out;
  237. status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
  238. if (ACPI_FAILURE(status)) {
  239. dev_warn(dev, "Can't get device full path name\n");
  240. goto out;
  241. }
  242. ncomp = (struct acpi_iort_named_component *)node->node_data;
  243. status = !strcmp(ncomp->device_name, buf.pointer) ?
  244. AE_OK : AE_NOT_FOUND;
  245. acpi_os_free(buf.pointer);
  246. } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  247. struct acpi_iort_root_complex *pci_rc;
  248. struct pci_bus *bus;
  249. bus = to_pci_bus(dev);
  250. pci_rc = (struct acpi_iort_root_complex *)node->node_data;
  251. /*
  252. * It is assumed that PCI segment numbers maps one-to-one
  253. * with root complexes. Each segment number can represent only
  254. * one root complex.
  255. */
  256. status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
  257. AE_OK : AE_NOT_FOUND;
  258. }
  259. out:
  260. return status;
  261. }
  262. static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
  263. u32 *rid_out)
  264. {
  265. /* Single mapping does not care for input id */
  266. if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
  267. if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
  268. type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  269. *rid_out = map->output_base;
  270. return 0;
  271. }
  272. pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
  273. map, type);
  274. return -ENXIO;
  275. }
  276. if (rid_in < map->input_base ||
  277. (rid_in >= map->input_base + map->id_count))
  278. return -ENXIO;
  279. *rid_out = map->output_base + (rid_in - map->input_base);
  280. return 0;
  281. }
  282. static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
  283. u32 *id_out, int index)
  284. {
  285. struct acpi_iort_node *parent;
  286. struct acpi_iort_id_mapping *map;
  287. if (!node->mapping_offset || !node->mapping_count ||
  288. index >= node->mapping_count)
  289. return NULL;
  290. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  291. node->mapping_offset + index * sizeof(*map));
  292. /* Firmware bug! */
  293. if (!map->output_reference) {
  294. pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
  295. node, node->type);
  296. return NULL;
  297. }
  298. parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  299. map->output_reference);
  300. if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
  301. if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
  302. node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
  303. node->type == ACPI_IORT_NODE_SMMU_V3) {
  304. *id_out = map->output_base;
  305. return parent;
  306. }
  307. }
  308. return NULL;
  309. }
  310. static int iort_get_id_mapping_index(struct acpi_iort_node *node)
  311. {
  312. struct acpi_iort_smmu_v3 *smmu;
  313. switch (node->type) {
  314. case ACPI_IORT_NODE_SMMU_V3:
  315. /*
  316. * SMMUv3 dev ID mapping index was introduced in revision 1
  317. * table, not available in revision 0
  318. */
  319. if (node->revision < 1)
  320. return -EINVAL;
  321. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  322. /*
  323. * ID mapping index is only ignored if all interrupts are
  324. * GSIV based
  325. */
  326. if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
  327. && smmu->sync_gsiv)
  328. return -EINVAL;
  329. if (smmu->id_mapping_index >= node->mapping_count) {
  330. pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
  331. node, node->type);
  332. return -EINVAL;
  333. }
  334. return smmu->id_mapping_index;
  335. default:
  336. return -EINVAL;
  337. }
  338. }
  339. static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
  340. u32 id_in, u32 *id_out,
  341. u8 type_mask)
  342. {
  343. u32 id = id_in;
  344. /* Parse the ID mapping tree to find specified node type */
  345. while (node) {
  346. struct acpi_iort_id_mapping *map;
  347. int i, index;
  348. if (IORT_TYPE_MASK(node->type) & type_mask) {
  349. if (id_out)
  350. *id_out = id;
  351. return node;
  352. }
  353. if (!node->mapping_offset || !node->mapping_count)
  354. goto fail_map;
  355. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  356. node->mapping_offset);
  357. /* Firmware bug! */
  358. if (!map->output_reference) {
  359. pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
  360. node, node->type);
  361. goto fail_map;
  362. }
  363. /*
  364. * Get the special ID mapping index (if any) and skip its
  365. * associated ID map to prevent erroneous multi-stage
  366. * IORT ID translations.
  367. */
  368. index = iort_get_id_mapping_index(node);
  369. /* Do the ID translation */
  370. for (i = 0; i < node->mapping_count; i++, map++) {
  371. /* if it is special mapping index, skip it */
  372. if (i == index)
  373. continue;
  374. if (!iort_id_map(map, node->type, id, &id))
  375. break;
  376. }
  377. if (i == node->mapping_count)
  378. goto fail_map;
  379. node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  380. map->output_reference);
  381. }
  382. fail_map:
  383. /* Map input ID to output ID unchanged on mapping failure */
  384. if (id_out)
  385. *id_out = id_in;
  386. return NULL;
  387. }
  388. static struct acpi_iort_node *iort_node_map_platform_id(
  389. struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
  390. int index)
  391. {
  392. struct acpi_iort_node *parent;
  393. u32 id;
  394. /* step 1: retrieve the initial dev id */
  395. parent = iort_node_get_id(node, &id, index);
  396. if (!parent)
  397. return NULL;
  398. /*
  399. * optional step 2: map the initial dev id if its parent is not
  400. * the target type we want, map it again for the use cases such
  401. * as NC (named component) -> SMMU -> ITS. If the type is matched,
  402. * return the initial dev id and its parent pointer directly.
  403. */
  404. if (!(IORT_TYPE_MASK(parent->type) & type_mask))
  405. parent = iort_node_map_id(parent, id, id_out, type_mask);
  406. else
  407. if (id_out)
  408. *id_out = id;
  409. return parent;
  410. }
  411. static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
  412. {
  413. struct pci_bus *pbus;
  414. if (!dev_is_pci(dev)) {
  415. struct acpi_iort_node *node;
  416. /*
  417. * scan iort_fwnode_list to see if it's an iort platform
  418. * device (such as SMMU, PMCG),its iort node already cached
  419. * and associated with fwnode when iort platform devices
  420. * were initialized.
  421. */
  422. node = iort_get_iort_node(dev->fwnode);
  423. if (node)
  424. return node;
  425. /*
  426. * if not, then it should be a platform device defined in
  427. * DSDT/SSDT (with Named Component node in IORT)
  428. */
  429. return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  430. iort_match_node_callback, dev);
  431. }
  432. /* Find a PCI root bus */
  433. pbus = to_pci_dev(dev)->bus;
  434. while (!pci_is_root_bus(pbus))
  435. pbus = pbus->parent;
  436. return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  437. iort_match_node_callback, &pbus->dev);
  438. }
  439. /**
  440. * iort_msi_map_rid() - Map a MSI requester ID for a device
  441. * @dev: The device for which the mapping is to be done.
  442. * @req_id: The device requester ID.
  443. *
  444. * Returns: mapped MSI RID on success, input requester ID otherwise
  445. */
  446. u32 iort_msi_map_rid(struct device *dev, u32 req_id)
  447. {
  448. struct acpi_iort_node *node;
  449. u32 dev_id;
  450. node = iort_find_dev_node(dev);
  451. if (!node)
  452. return req_id;
  453. iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
  454. return dev_id;
  455. }
  456. /**
  457. * iort_pmsi_get_dev_id() - Get the device id for a device
  458. * @dev: The device for which the mapping is to be done.
  459. * @dev_id: The device ID found.
  460. *
  461. * Returns: 0 for successful find a dev id, -ENODEV on error
  462. */
  463. int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
  464. {
  465. int i, index;
  466. struct acpi_iort_node *node;
  467. node = iort_find_dev_node(dev);
  468. if (!node)
  469. return -ENODEV;
  470. index = iort_get_id_mapping_index(node);
  471. /* if there is a valid index, go get the dev_id directly */
  472. if (index >= 0) {
  473. if (iort_node_get_id(node, dev_id, index))
  474. return 0;
  475. } else {
  476. for (i = 0; i < node->mapping_count; i++) {
  477. if (iort_node_map_platform_id(node, dev_id,
  478. IORT_MSI_TYPE, i))
  479. return 0;
  480. }
  481. }
  482. return -ENODEV;
  483. }
  484. static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
  485. {
  486. struct iort_its_msi_chip *its_msi_chip;
  487. int ret = -ENODEV;
  488. spin_lock(&iort_msi_chip_lock);
  489. list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
  490. if (its_msi_chip->translation_id == its_id) {
  491. *base = its_msi_chip->base_addr;
  492. ret = 0;
  493. break;
  494. }
  495. }
  496. spin_unlock(&iort_msi_chip_lock);
  497. return ret;
  498. }
  499. /**
  500. * iort_dev_find_its_id() - Find the ITS identifier for a device
  501. * @dev: The device.
  502. * @req_id: Device's requester ID
  503. * @idx: Index of the ITS identifier list.
  504. * @its_id: ITS identifier.
  505. *
  506. * Returns: 0 on success, appropriate error value otherwise
  507. */
  508. static int iort_dev_find_its_id(struct device *dev, u32 req_id,
  509. unsigned int idx, int *its_id)
  510. {
  511. struct acpi_iort_its_group *its;
  512. struct acpi_iort_node *node;
  513. node = iort_find_dev_node(dev);
  514. if (!node)
  515. return -ENXIO;
  516. node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
  517. if (!node)
  518. return -ENXIO;
  519. /* Move to ITS specific data */
  520. its = (struct acpi_iort_its_group *)node->node_data;
  521. if (idx > its->its_count) {
  522. dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
  523. idx, its->its_count);
  524. return -ENXIO;
  525. }
  526. *its_id = its->identifiers[idx];
  527. return 0;
  528. }
  529. /**
  530. * iort_get_device_domain() - Find MSI domain related to a device
  531. * @dev: The device.
  532. * @req_id: Requester ID for the device.
  533. *
  534. * Returns: the MSI domain for this device, NULL otherwise
  535. */
  536. struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
  537. {
  538. struct fwnode_handle *handle;
  539. int its_id;
  540. if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
  541. return NULL;
  542. handle = iort_find_domain_token(its_id);
  543. if (!handle)
  544. return NULL;
  545. return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
  546. }
  547. static void iort_set_device_domain(struct device *dev,
  548. struct acpi_iort_node *node)
  549. {
  550. struct acpi_iort_its_group *its;
  551. struct acpi_iort_node *msi_parent;
  552. struct acpi_iort_id_mapping *map;
  553. struct fwnode_handle *iort_fwnode;
  554. struct irq_domain *domain;
  555. int index;
  556. index = iort_get_id_mapping_index(node);
  557. if (index < 0)
  558. return;
  559. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  560. node->mapping_offset + index * sizeof(*map));
  561. /* Firmware bug! */
  562. if (!map->output_reference ||
  563. !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
  564. pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
  565. node, node->type);
  566. return;
  567. }
  568. msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  569. map->output_reference);
  570. if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
  571. return;
  572. /* Move to ITS specific data */
  573. its = (struct acpi_iort_its_group *)msi_parent->node_data;
  574. iort_fwnode = iort_find_domain_token(its->identifiers[0]);
  575. if (!iort_fwnode)
  576. return;
  577. domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
  578. if (domain)
  579. dev_set_msi_domain(dev, domain);
  580. }
  581. /**
  582. * iort_get_platform_device_domain() - Find MSI domain related to a
  583. * platform device
  584. * @dev: the dev pointer associated with the platform device
  585. *
  586. * Returns: the MSI domain for this device, NULL otherwise
  587. */
  588. static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
  589. {
  590. struct acpi_iort_node *node, *msi_parent;
  591. struct fwnode_handle *iort_fwnode;
  592. struct acpi_iort_its_group *its;
  593. int i;
  594. /* find its associated iort node */
  595. node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  596. iort_match_node_callback, dev);
  597. if (!node)
  598. return NULL;
  599. /* then find its msi parent node */
  600. for (i = 0; i < node->mapping_count; i++) {
  601. msi_parent = iort_node_map_platform_id(node, NULL,
  602. IORT_MSI_TYPE, i);
  603. if (msi_parent)
  604. break;
  605. }
  606. if (!msi_parent)
  607. return NULL;
  608. /* Move to ITS specific data */
  609. its = (struct acpi_iort_its_group *)msi_parent->node_data;
  610. iort_fwnode = iort_find_domain_token(its->identifiers[0]);
  611. if (!iort_fwnode)
  612. return NULL;
  613. return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
  614. }
  615. void acpi_configure_pmsi_domain(struct device *dev)
  616. {
  617. struct irq_domain *msi_domain;
  618. msi_domain = iort_get_platform_device_domain(dev);
  619. if (msi_domain)
  620. dev_set_msi_domain(dev, msi_domain);
  621. }
  622. static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
  623. void *data)
  624. {
  625. u32 *rid = data;
  626. *rid = alias;
  627. return 0;
  628. }
  629. static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
  630. struct fwnode_handle *fwnode,
  631. const struct iommu_ops *ops)
  632. {
  633. int ret = iommu_fwspec_init(dev, fwnode, ops);
  634. if (!ret)
  635. ret = iommu_fwspec_add_ids(dev, &streamid, 1);
  636. return ret;
  637. }
  638. static inline bool iort_iommu_driver_enabled(u8 type)
  639. {
  640. switch (type) {
  641. case ACPI_IORT_NODE_SMMU_V3:
  642. return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
  643. case ACPI_IORT_NODE_SMMU:
  644. return IS_BUILTIN(CONFIG_ARM_SMMU);
  645. default:
  646. pr_warn("IORT node type %u does not describe an SMMU\n", type);
  647. return false;
  648. }
  649. }
  650. #ifdef CONFIG_IOMMU_API
  651. static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
  652. {
  653. struct acpi_iort_node *iommu;
  654. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  655. iommu = iort_get_iort_node(fwspec->iommu_fwnode);
  656. if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
  657. struct acpi_iort_smmu_v3 *smmu;
  658. smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
  659. if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
  660. return iommu;
  661. }
  662. return NULL;
  663. }
  664. static inline const struct iommu_ops *iort_fwspec_iommu_ops(
  665. struct iommu_fwspec *fwspec)
  666. {
  667. return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
  668. }
  669. static inline int iort_add_device_replay(const struct iommu_ops *ops,
  670. struct device *dev)
  671. {
  672. int err = 0;
  673. if (ops->add_device && dev->bus && !dev->iommu_group)
  674. err = ops->add_device(dev);
  675. return err;
  676. }
  677. /**
  678. * iort_iommu_msi_get_resv_regions - Reserved region driver helper
  679. * @dev: Device from iommu_get_resv_regions()
  680. * @head: Reserved region list from iommu_get_resv_regions()
  681. *
  682. * Returns: Number of msi reserved regions on success (0 if platform
  683. * doesn't require the reservation or no associated msi regions),
  684. * appropriate error value otherwise. The ITS interrupt translation
  685. * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
  686. * are the msi reserved regions.
  687. */
  688. int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
  689. {
  690. struct acpi_iort_its_group *its;
  691. struct acpi_iort_node *iommu_node, *its_node = NULL;
  692. int i, resv = 0;
  693. iommu_node = iort_get_msi_resv_iommu(dev);
  694. if (!iommu_node)
  695. return 0;
  696. /*
  697. * Current logic to reserve ITS regions relies on HW topologies
  698. * where a given PCI or named component maps its IDs to only one
  699. * ITS group; if a PCI or named component can map its IDs to
  700. * different ITS groups through IORT mappings this function has
  701. * to be reworked to ensure we reserve regions for all ITS groups
  702. * a given PCI or named component may map IDs to.
  703. */
  704. for (i = 0; i < dev->iommu_fwspec->num_ids; i++) {
  705. its_node = iort_node_map_id(iommu_node,
  706. dev->iommu_fwspec->ids[i],
  707. NULL, IORT_MSI_TYPE);
  708. if (its_node)
  709. break;
  710. }
  711. if (!its_node)
  712. return 0;
  713. /* Move to ITS specific data */
  714. its = (struct acpi_iort_its_group *)its_node->node_data;
  715. for (i = 0; i < its->its_count; i++) {
  716. phys_addr_t base;
  717. if (!iort_find_its_base(its->identifiers[i], &base)) {
  718. int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  719. struct iommu_resv_region *region;
  720. region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
  721. prot, IOMMU_RESV_MSI);
  722. if (region) {
  723. list_add_tail(&region->list, head);
  724. resv++;
  725. }
  726. }
  727. }
  728. return (resv == its->its_count) ? resv : -ENODEV;
  729. }
  730. #else
  731. static inline const struct iommu_ops *iort_fwspec_iommu_ops(
  732. struct iommu_fwspec *fwspec)
  733. { return NULL; }
  734. static inline int iort_add_device_replay(const struct iommu_ops *ops,
  735. struct device *dev)
  736. { return 0; }
  737. int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
  738. { return 0; }
  739. #endif
  740. static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
  741. u32 streamid)
  742. {
  743. const struct iommu_ops *ops;
  744. struct fwnode_handle *iort_fwnode;
  745. if (!node)
  746. return -ENODEV;
  747. iort_fwnode = iort_get_fwnode(node);
  748. if (!iort_fwnode)
  749. return -ENODEV;
  750. /*
  751. * If the ops look-up fails, this means that either
  752. * the SMMU drivers have not been probed yet or that
  753. * the SMMU drivers are not built in the kernel;
  754. * Depending on whether the SMMU drivers are built-in
  755. * in the kernel or not, defer the IOMMU configuration
  756. * or just abort it.
  757. */
  758. ops = iommu_ops_from_fwnode(iort_fwnode);
  759. if (!ops)
  760. return iort_iommu_driver_enabled(node->type) ?
  761. -EPROBE_DEFER : -ENODEV;
  762. return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
  763. }
  764. struct iort_pci_alias_info {
  765. struct device *dev;
  766. struct acpi_iort_node *node;
  767. };
  768. static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
  769. {
  770. struct iort_pci_alias_info *info = data;
  771. struct acpi_iort_node *parent;
  772. u32 streamid;
  773. parent = iort_node_map_id(info->node, alias, &streamid,
  774. IORT_IOMMU_TYPE);
  775. return iort_iommu_xlate(info->dev, parent, streamid);
  776. }
  777. static int nc_dma_get_range(struct device *dev, u64 *size)
  778. {
  779. struct acpi_iort_node *node;
  780. struct acpi_iort_named_component *ncomp;
  781. node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  782. iort_match_node_callback, dev);
  783. if (!node)
  784. return -ENODEV;
  785. ncomp = (struct acpi_iort_named_component *)node->node_data;
  786. *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
  787. 1ULL<<ncomp->memory_address_limit;
  788. return 0;
  789. }
  790. /**
  791. * iort_dma_setup() - Set-up device DMA parameters.
  792. *
  793. * @dev: device to configure
  794. * @dma_addr: device DMA address result pointer
  795. * @size: DMA range size result pointer
  796. */
  797. void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
  798. {
  799. u64 mask, dmaaddr = 0, size = 0, offset = 0;
  800. int ret, msb;
  801. /*
  802. * Set default coherent_dma_mask to 32 bit. Drivers are expected to
  803. * setup the correct supported mask.
  804. */
  805. if (!dev->coherent_dma_mask)
  806. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  807. /*
  808. * Set it to coherent_dma_mask by default if the architecture
  809. * code has not set it.
  810. */
  811. if (!dev->dma_mask)
  812. dev->dma_mask = &dev->coherent_dma_mask;
  813. size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
  814. if (dev_is_pci(dev))
  815. ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
  816. else
  817. ret = nc_dma_get_range(dev, &size);
  818. if (!ret) {
  819. msb = fls64(dmaaddr + size - 1);
  820. /*
  821. * Round-up to the power-of-two mask or set
  822. * the mask to the whole 64-bit address space
  823. * in case the DMA region covers the full
  824. * memory window.
  825. */
  826. mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
  827. /*
  828. * Limit coherent and dma mask based on size
  829. * retrieved from firmware.
  830. */
  831. dev->coherent_dma_mask = mask;
  832. *dev->dma_mask = mask;
  833. }
  834. *dma_addr = dmaaddr;
  835. *dma_size = size;
  836. dev->dma_pfn_offset = PFN_DOWN(offset);
  837. dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
  838. }
  839. /**
  840. * iort_iommu_configure - Set-up IOMMU configuration for a device.
  841. *
  842. * @dev: device to configure
  843. *
  844. * Returns: iommu_ops pointer on configuration success
  845. * NULL on configuration failure
  846. */
  847. const struct iommu_ops *iort_iommu_configure(struct device *dev)
  848. {
  849. struct acpi_iort_node *node, *parent;
  850. const struct iommu_ops *ops;
  851. u32 streamid = 0;
  852. int err = -ENODEV;
  853. /*
  854. * If we already translated the fwspec there
  855. * is nothing left to do, return the iommu_ops.
  856. */
  857. ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
  858. if (ops)
  859. return ops;
  860. if (dev_is_pci(dev)) {
  861. struct pci_bus *bus = to_pci_dev(dev)->bus;
  862. struct iort_pci_alias_info info = { .dev = dev };
  863. node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  864. iort_match_node_callback, &bus->dev);
  865. if (!node)
  866. return NULL;
  867. info.node = node;
  868. err = pci_for_each_dma_alias(to_pci_dev(dev),
  869. iort_pci_iommu_init, &info);
  870. } else {
  871. int i = 0;
  872. node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  873. iort_match_node_callback, dev);
  874. if (!node)
  875. return NULL;
  876. do {
  877. parent = iort_node_map_platform_id(node, &streamid,
  878. IORT_IOMMU_TYPE,
  879. i++);
  880. if (parent)
  881. err = iort_iommu_xlate(dev, parent, streamid);
  882. } while (parent && !err);
  883. }
  884. /*
  885. * If we have reason to believe the IOMMU driver missed the initial
  886. * add_device callback for dev, replay it to get things in order.
  887. */
  888. if (!err) {
  889. ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
  890. err = iort_add_device_replay(ops, dev);
  891. }
  892. /* Ignore all other errors apart from EPROBE_DEFER */
  893. if (err == -EPROBE_DEFER) {
  894. ops = ERR_PTR(err);
  895. } else if (err) {
  896. dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
  897. ops = NULL;
  898. }
  899. return ops;
  900. }
  901. static void __init acpi_iort_register_irq(int hwirq, const char *name,
  902. int trigger,
  903. struct resource *res)
  904. {
  905. int irq = acpi_register_gsi(NULL, hwirq, trigger,
  906. ACPI_ACTIVE_HIGH);
  907. if (irq <= 0) {
  908. pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
  909. name);
  910. return;
  911. }
  912. res->start = irq;
  913. res->end = irq;
  914. res->flags = IORESOURCE_IRQ;
  915. res->name = name;
  916. }
  917. static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
  918. {
  919. struct acpi_iort_smmu_v3 *smmu;
  920. /* Always present mem resource */
  921. int num_res = 1;
  922. /* Retrieve SMMUv3 specific data */
  923. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  924. if (smmu->event_gsiv)
  925. num_res++;
  926. if (smmu->pri_gsiv)
  927. num_res++;
  928. if (smmu->gerr_gsiv)
  929. num_res++;
  930. if (smmu->sync_gsiv)
  931. num_res++;
  932. return num_res;
  933. }
  934. static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
  935. {
  936. /*
  937. * Cavium ThunderX2 implementation doesn't not support unique
  938. * irq line. Use single irq line for all the SMMUv3 interrupts.
  939. */
  940. if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
  941. return false;
  942. /*
  943. * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
  944. * SPI numbers here.
  945. */
  946. return smmu->event_gsiv == smmu->pri_gsiv &&
  947. smmu->event_gsiv == smmu->gerr_gsiv &&
  948. smmu->event_gsiv == smmu->sync_gsiv;
  949. }
  950. static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
  951. {
  952. /*
  953. * Override the size, for Cavium ThunderX2 implementation
  954. * which doesn't support the page 1 SMMU register space.
  955. */
  956. if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
  957. return SZ_64K;
  958. return SZ_128K;
  959. }
  960. static void __init arm_smmu_v3_init_resources(struct resource *res,
  961. struct acpi_iort_node *node)
  962. {
  963. struct acpi_iort_smmu_v3 *smmu;
  964. int num_res = 0;
  965. /* Retrieve SMMUv3 specific data */
  966. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  967. res[num_res].start = smmu->base_address;
  968. res[num_res].end = smmu->base_address +
  969. arm_smmu_v3_resource_size(smmu) - 1;
  970. res[num_res].flags = IORESOURCE_MEM;
  971. num_res++;
  972. if (arm_smmu_v3_is_combined_irq(smmu)) {
  973. if (smmu->event_gsiv)
  974. acpi_iort_register_irq(smmu->event_gsiv, "combined",
  975. ACPI_EDGE_SENSITIVE,
  976. &res[num_res++]);
  977. } else {
  978. if (smmu->event_gsiv)
  979. acpi_iort_register_irq(smmu->event_gsiv, "eventq",
  980. ACPI_EDGE_SENSITIVE,
  981. &res[num_res++]);
  982. if (smmu->pri_gsiv)
  983. acpi_iort_register_irq(smmu->pri_gsiv, "priq",
  984. ACPI_EDGE_SENSITIVE,
  985. &res[num_res++]);
  986. if (smmu->gerr_gsiv)
  987. acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
  988. ACPI_EDGE_SENSITIVE,
  989. &res[num_res++]);
  990. if (smmu->sync_gsiv)
  991. acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
  992. ACPI_EDGE_SENSITIVE,
  993. &res[num_res++]);
  994. }
  995. }
  996. static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
  997. {
  998. struct acpi_iort_smmu_v3 *smmu;
  999. /* Retrieve SMMUv3 specific data */
  1000. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1001. return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
  1002. }
  1003. #if defined(CONFIG_ACPI_NUMA)
  1004. /*
  1005. * set numa proximity domain for smmuv3 device
  1006. */
  1007. static void __init arm_smmu_v3_set_proximity(struct device *dev,
  1008. struct acpi_iort_node *node)
  1009. {
  1010. struct acpi_iort_smmu_v3 *smmu;
  1011. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1012. if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
  1013. set_dev_node(dev, acpi_map_pxm_to_node(smmu->pxm));
  1014. pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
  1015. smmu->base_address,
  1016. smmu->pxm);
  1017. }
  1018. }
  1019. #else
  1020. #define arm_smmu_v3_set_proximity NULL
  1021. #endif
  1022. static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
  1023. {
  1024. struct acpi_iort_smmu *smmu;
  1025. /* Retrieve SMMU specific data */
  1026. smmu = (struct acpi_iort_smmu *)node->node_data;
  1027. /*
  1028. * Only consider the global fault interrupt and ignore the
  1029. * configuration access interrupt.
  1030. *
  1031. * MMIO address and global fault interrupt resources are always
  1032. * present so add them to the context interrupt count as a static
  1033. * value.
  1034. */
  1035. return smmu->context_interrupt_count + 2;
  1036. }
  1037. static void __init arm_smmu_init_resources(struct resource *res,
  1038. struct acpi_iort_node *node)
  1039. {
  1040. struct acpi_iort_smmu *smmu;
  1041. int i, hw_irq, trigger, num_res = 0;
  1042. u64 *ctx_irq, *glb_irq;
  1043. /* Retrieve SMMU specific data */
  1044. smmu = (struct acpi_iort_smmu *)node->node_data;
  1045. res[num_res].start = smmu->base_address;
  1046. res[num_res].end = smmu->base_address + smmu->span - 1;
  1047. res[num_res].flags = IORESOURCE_MEM;
  1048. num_res++;
  1049. glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
  1050. /* Global IRQs */
  1051. hw_irq = IORT_IRQ_MASK(glb_irq[0]);
  1052. trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
  1053. acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
  1054. &res[num_res++]);
  1055. /* Context IRQs */
  1056. ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
  1057. for (i = 0; i < smmu->context_interrupt_count; i++) {
  1058. hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
  1059. trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
  1060. acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
  1061. &res[num_res++]);
  1062. }
  1063. }
  1064. static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
  1065. {
  1066. struct acpi_iort_smmu *smmu;
  1067. /* Retrieve SMMU specific data */
  1068. smmu = (struct acpi_iort_smmu *)node->node_data;
  1069. return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
  1070. }
  1071. struct iort_dev_config {
  1072. const char *name;
  1073. int (*dev_init)(struct acpi_iort_node *node);
  1074. bool (*dev_is_coherent)(struct acpi_iort_node *node);
  1075. int (*dev_count_resources)(struct acpi_iort_node *node);
  1076. void (*dev_init_resources)(struct resource *res,
  1077. struct acpi_iort_node *node);
  1078. void (*dev_set_proximity)(struct device *dev,
  1079. struct acpi_iort_node *node);
  1080. };
  1081. static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
  1082. .name = "arm-smmu-v3",
  1083. .dev_is_coherent = arm_smmu_v3_is_coherent,
  1084. .dev_count_resources = arm_smmu_v3_count_resources,
  1085. .dev_init_resources = arm_smmu_v3_init_resources,
  1086. .dev_set_proximity = arm_smmu_v3_set_proximity,
  1087. };
  1088. static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
  1089. .name = "arm-smmu",
  1090. .dev_is_coherent = arm_smmu_is_coherent,
  1091. .dev_count_resources = arm_smmu_count_resources,
  1092. .dev_init_resources = arm_smmu_init_resources
  1093. };
  1094. static __init const struct iort_dev_config *iort_get_dev_cfg(
  1095. struct acpi_iort_node *node)
  1096. {
  1097. switch (node->type) {
  1098. case ACPI_IORT_NODE_SMMU_V3:
  1099. return &iort_arm_smmu_v3_cfg;
  1100. case ACPI_IORT_NODE_SMMU:
  1101. return &iort_arm_smmu_cfg;
  1102. default:
  1103. return NULL;
  1104. }
  1105. }
  1106. /**
  1107. * iort_add_platform_device() - Allocate a platform device for IORT node
  1108. * @node: Pointer to device ACPI IORT node
  1109. *
  1110. * Returns: 0 on success, <0 failure
  1111. */
  1112. static int __init iort_add_platform_device(struct acpi_iort_node *node,
  1113. const struct iort_dev_config *ops)
  1114. {
  1115. struct fwnode_handle *fwnode;
  1116. struct platform_device *pdev;
  1117. struct resource *r;
  1118. enum dev_dma_attr attr;
  1119. int ret, count;
  1120. pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
  1121. if (!pdev)
  1122. return -ENOMEM;
  1123. if (ops->dev_set_proximity)
  1124. ops->dev_set_proximity(&pdev->dev, node);
  1125. count = ops->dev_count_resources(node);
  1126. r = kcalloc(count, sizeof(*r), GFP_KERNEL);
  1127. if (!r) {
  1128. ret = -ENOMEM;
  1129. goto dev_put;
  1130. }
  1131. ops->dev_init_resources(r, node);
  1132. ret = platform_device_add_resources(pdev, r, count);
  1133. /*
  1134. * Resources are duplicated in platform_device_add_resources,
  1135. * free their allocated memory
  1136. */
  1137. kfree(r);
  1138. if (ret)
  1139. goto dev_put;
  1140. /*
  1141. * Add a copy of IORT node pointer to platform_data to
  1142. * be used to retrieve IORT data information.
  1143. */
  1144. ret = platform_device_add_data(pdev, &node, sizeof(node));
  1145. if (ret)
  1146. goto dev_put;
  1147. /*
  1148. * We expect the dma masks to be equivalent for
  1149. * all SMMUs set-ups
  1150. */
  1151. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  1152. fwnode = iort_get_fwnode(node);
  1153. if (!fwnode) {
  1154. ret = -ENODEV;
  1155. goto dev_put;
  1156. }
  1157. pdev->dev.fwnode = fwnode;
  1158. attr = ops->dev_is_coherent && ops->dev_is_coherent(node) ?
  1159. DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
  1160. /* Configure DMA for the page table walker */
  1161. acpi_dma_configure(&pdev->dev, attr);
  1162. iort_set_device_domain(&pdev->dev, node);
  1163. ret = platform_device_add(pdev);
  1164. if (ret)
  1165. goto dma_deconfigure;
  1166. return 0;
  1167. dma_deconfigure:
  1168. acpi_dma_deconfigure(&pdev->dev);
  1169. dev_put:
  1170. platform_device_put(pdev);
  1171. return ret;
  1172. }
  1173. static bool __init iort_enable_acs(struct acpi_iort_node *iort_node)
  1174. {
  1175. if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  1176. struct acpi_iort_node *parent;
  1177. struct acpi_iort_id_mapping *map;
  1178. int i;
  1179. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
  1180. iort_node->mapping_offset);
  1181. for (i = 0; i < iort_node->mapping_count; i++, map++) {
  1182. if (!map->output_reference)
  1183. continue;
  1184. parent = ACPI_ADD_PTR(struct acpi_iort_node,
  1185. iort_table, map->output_reference);
  1186. /*
  1187. * If we detect a RC->SMMU mapping, make sure
  1188. * we enable ACS on the system.
  1189. */
  1190. if ((parent->type == ACPI_IORT_NODE_SMMU) ||
  1191. (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
  1192. pci_request_acs();
  1193. return true;
  1194. }
  1195. }
  1196. }
  1197. return false;
  1198. }
  1199. static void __init iort_init_platform_devices(void)
  1200. {
  1201. struct acpi_iort_node *iort_node, *iort_end;
  1202. struct acpi_table_iort *iort;
  1203. struct fwnode_handle *fwnode;
  1204. int i, ret;
  1205. bool acs_enabled = false;
  1206. const struct iort_dev_config *ops;
  1207. /*
  1208. * iort_table and iort both point to the start of IORT table, but
  1209. * have different struct types
  1210. */
  1211. iort = (struct acpi_table_iort *)iort_table;
  1212. /* Get the first IORT node */
  1213. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  1214. iort->node_offset);
  1215. iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  1216. iort_table->length);
  1217. for (i = 0; i < iort->node_count; i++) {
  1218. if (iort_node >= iort_end) {
  1219. pr_err("iort node pointer overflows, bad table\n");
  1220. return;
  1221. }
  1222. if (!acs_enabled)
  1223. acs_enabled = iort_enable_acs(iort_node);
  1224. ops = iort_get_dev_cfg(iort_node);
  1225. if (ops) {
  1226. fwnode = acpi_alloc_fwnode_static();
  1227. if (!fwnode)
  1228. return;
  1229. iort_set_fwnode(iort_node, fwnode);
  1230. ret = iort_add_platform_device(iort_node, ops);
  1231. if (ret) {
  1232. iort_delete_fwnode(iort_node);
  1233. acpi_free_fwnode_static(fwnode);
  1234. return;
  1235. }
  1236. }
  1237. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
  1238. iort_node->length);
  1239. }
  1240. }
  1241. void __init acpi_iort_init(void)
  1242. {
  1243. acpi_status status;
  1244. status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
  1245. if (ACPI_FAILURE(status)) {
  1246. if (status != AE_NOT_FOUND) {
  1247. const char *msg = acpi_format_exception(status);
  1248. pr_err("Failed to get table, %s\n", msg);
  1249. }
  1250. return;
  1251. }
  1252. iort_init_platform_devices();
  1253. }