iort.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016, Semihalf
  4. * Author: Tomasz Nowicki <[email protected]>
  5. *
  6. * This file implements early detection/parsing of I/O mapping
  7. * reported to OS through firmware via I/O Remapping Table (IORT)
  8. * IORT document number: ARM DEN 0049A
  9. */
  10. #define pr_fmt(fmt) "ACPI: IORT: " fmt
  11. #include <linux/acpi_iort.h>
  12. #include <linux/bitfield.h>
  13. #include <linux/iommu.h>
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/dma-map-ops.h>
  20. #define IORT_TYPE_MASK(type) (1 << (type))
  21. #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
  22. #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
  23. (1 << ACPI_IORT_NODE_SMMU_V3))
  24. struct iort_its_msi_chip {
  25. struct list_head list;
  26. struct fwnode_handle *fw_node;
  27. phys_addr_t base_addr;
  28. u32 translation_id;
  29. };
  30. struct iort_fwnode {
  31. struct list_head list;
  32. struct acpi_iort_node *iort_node;
  33. struct fwnode_handle *fwnode;
  34. };
  35. static LIST_HEAD(iort_fwnode_list);
  36. static DEFINE_SPINLOCK(iort_fwnode_lock);
  37. /**
  38. * iort_set_fwnode() - Create iort_fwnode and use it to register
  39. * iommu data in the iort_fwnode_list
  40. *
  41. * @iort_node: IORT table node associated with the IOMMU
  42. * @fwnode: fwnode associated with the IORT node
  43. *
  44. * Returns: 0 on success
  45. * <0 on failure
  46. */
  47. static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
  48. struct fwnode_handle *fwnode)
  49. {
  50. struct iort_fwnode *np;
  51. np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
  52. if (WARN_ON(!np))
  53. return -ENOMEM;
  54. INIT_LIST_HEAD(&np->list);
  55. np->iort_node = iort_node;
  56. np->fwnode = fwnode;
  57. spin_lock(&iort_fwnode_lock);
  58. list_add_tail(&np->list, &iort_fwnode_list);
  59. spin_unlock(&iort_fwnode_lock);
  60. return 0;
  61. }
  62. /**
  63. * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
  64. *
  65. * @node: IORT table node to be looked-up
  66. *
  67. * Returns: fwnode_handle pointer on success, NULL on failure
  68. */
  69. static inline struct fwnode_handle *iort_get_fwnode(
  70. struct acpi_iort_node *node)
  71. {
  72. struct iort_fwnode *curr;
  73. struct fwnode_handle *fwnode = NULL;
  74. spin_lock(&iort_fwnode_lock);
  75. list_for_each_entry(curr, &iort_fwnode_list, list) {
  76. if (curr->iort_node == node) {
  77. fwnode = curr->fwnode;
  78. break;
  79. }
  80. }
  81. spin_unlock(&iort_fwnode_lock);
  82. return fwnode;
  83. }
  84. /**
  85. * iort_delete_fwnode() - Delete fwnode associated with an IORT node
  86. *
  87. * @node: IORT table node associated with fwnode to delete
  88. */
  89. static inline void iort_delete_fwnode(struct acpi_iort_node *node)
  90. {
  91. struct iort_fwnode *curr, *tmp;
  92. spin_lock(&iort_fwnode_lock);
  93. list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
  94. if (curr->iort_node == node) {
  95. list_del(&curr->list);
  96. kfree(curr);
  97. break;
  98. }
  99. }
  100. spin_unlock(&iort_fwnode_lock);
  101. }
  102. /**
  103. * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
  104. *
  105. * @fwnode: fwnode associated with device to be looked-up
  106. *
  107. * Returns: iort_node pointer on success, NULL on failure
  108. */
  109. static inline struct acpi_iort_node *iort_get_iort_node(
  110. struct fwnode_handle *fwnode)
  111. {
  112. struct iort_fwnode *curr;
  113. struct acpi_iort_node *iort_node = NULL;
  114. spin_lock(&iort_fwnode_lock);
  115. list_for_each_entry(curr, &iort_fwnode_list, list) {
  116. if (curr->fwnode == fwnode) {
  117. iort_node = curr->iort_node;
  118. break;
  119. }
  120. }
  121. spin_unlock(&iort_fwnode_lock);
  122. return iort_node;
  123. }
  124. typedef acpi_status (*iort_find_node_callback)
  125. (struct acpi_iort_node *node, void *context);
  126. /* Root pointer to the mapped IORT table */
  127. static struct acpi_table_header *iort_table;
  128. static LIST_HEAD(iort_msi_chip_list);
  129. static DEFINE_SPINLOCK(iort_msi_chip_lock);
  130. /**
  131. * iort_register_domain_token() - register domain token along with related
  132. * ITS ID and base address to the list from where we can get it back later on.
  133. * @trans_id: ITS ID.
  134. * @base: ITS base address.
  135. * @fw_node: Domain token.
  136. *
  137. * Returns: 0 on success, -ENOMEM if no memory when allocating list element
  138. */
  139. int iort_register_domain_token(int trans_id, phys_addr_t base,
  140. struct fwnode_handle *fw_node)
  141. {
  142. struct iort_its_msi_chip *its_msi_chip;
  143. its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
  144. if (!its_msi_chip)
  145. return -ENOMEM;
  146. its_msi_chip->fw_node = fw_node;
  147. its_msi_chip->translation_id = trans_id;
  148. its_msi_chip->base_addr = base;
  149. spin_lock(&iort_msi_chip_lock);
  150. list_add(&its_msi_chip->list, &iort_msi_chip_list);
  151. spin_unlock(&iort_msi_chip_lock);
  152. return 0;
  153. }
  154. /**
  155. * iort_deregister_domain_token() - Deregister domain token based on ITS ID
  156. * @trans_id: ITS ID.
  157. *
  158. * Returns: none.
  159. */
  160. void iort_deregister_domain_token(int trans_id)
  161. {
  162. struct iort_its_msi_chip *its_msi_chip, *t;
  163. spin_lock(&iort_msi_chip_lock);
  164. list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
  165. if (its_msi_chip->translation_id == trans_id) {
  166. list_del(&its_msi_chip->list);
  167. kfree(its_msi_chip);
  168. break;
  169. }
  170. }
  171. spin_unlock(&iort_msi_chip_lock);
  172. }
  173. /**
  174. * iort_find_domain_token() - Find domain token based on given ITS ID
  175. * @trans_id: ITS ID.
  176. *
  177. * Returns: domain token when find on the list, NULL otherwise
  178. */
  179. struct fwnode_handle *iort_find_domain_token(int trans_id)
  180. {
  181. struct fwnode_handle *fw_node = NULL;
  182. struct iort_its_msi_chip *its_msi_chip;
  183. spin_lock(&iort_msi_chip_lock);
  184. list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
  185. if (its_msi_chip->translation_id == trans_id) {
  186. fw_node = its_msi_chip->fw_node;
  187. break;
  188. }
  189. }
  190. spin_unlock(&iort_msi_chip_lock);
  191. return fw_node;
  192. }
  193. static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
  194. iort_find_node_callback callback,
  195. void *context)
  196. {
  197. struct acpi_iort_node *iort_node, *iort_end;
  198. struct acpi_table_iort *iort;
  199. int i;
  200. if (!iort_table)
  201. return NULL;
  202. /* Get the first IORT node */
  203. iort = (struct acpi_table_iort *)iort_table;
  204. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  205. iort->node_offset);
  206. iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  207. iort_table->length);
  208. for (i = 0; i < iort->node_count; i++) {
  209. if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
  210. "IORT node pointer overflows, bad table!\n"))
  211. return NULL;
  212. if (iort_node->type == type &&
  213. ACPI_SUCCESS(callback(iort_node, context)))
  214. return iort_node;
  215. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
  216. iort_node->length);
  217. }
  218. return NULL;
  219. }
  220. static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
  221. void *context)
  222. {
  223. struct device *dev = context;
  224. acpi_status status = AE_NOT_FOUND;
  225. if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
  226. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  227. struct acpi_device *adev;
  228. struct acpi_iort_named_component *ncomp;
  229. struct device *nc_dev = dev;
  230. /*
  231. * Walk the device tree to find a device with an
  232. * ACPI companion; there is no point in scanning
  233. * IORT for a device matching a named component if
  234. * the device does not have an ACPI companion to
  235. * start with.
  236. */
  237. do {
  238. adev = ACPI_COMPANION(nc_dev);
  239. if (adev)
  240. break;
  241. nc_dev = nc_dev->parent;
  242. } while (nc_dev);
  243. if (!adev)
  244. goto out;
  245. status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
  246. if (ACPI_FAILURE(status)) {
  247. dev_warn(nc_dev, "Can't get device full path name\n");
  248. goto out;
  249. }
  250. ncomp = (struct acpi_iort_named_component *)node->node_data;
  251. status = !strcmp(ncomp->device_name, buf.pointer) ?
  252. AE_OK : AE_NOT_FOUND;
  253. acpi_os_free(buf.pointer);
  254. } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  255. struct acpi_iort_root_complex *pci_rc;
  256. struct pci_bus *bus;
  257. bus = to_pci_bus(dev);
  258. pci_rc = (struct acpi_iort_root_complex *)node->node_data;
  259. /*
  260. * It is assumed that PCI segment numbers maps one-to-one
  261. * with root complexes. Each segment number can represent only
  262. * one root complex.
  263. */
  264. status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
  265. AE_OK : AE_NOT_FOUND;
  266. }
  267. out:
  268. return status;
  269. }
  270. static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
  271. u32 *rid_out, bool check_overlap)
  272. {
  273. /* Single mapping does not care for input id */
  274. if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
  275. if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
  276. type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  277. *rid_out = map->output_base;
  278. return 0;
  279. }
  280. pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
  281. map, type);
  282. return -ENXIO;
  283. }
  284. if (rid_in < map->input_base ||
  285. (rid_in > map->input_base + map->id_count))
  286. return -ENXIO;
  287. if (check_overlap) {
  288. /*
  289. * We already found a mapping for this input ID at the end of
  290. * another region. If it coincides with the start of this
  291. * region, we assume the prior match was due to the off-by-1
  292. * issue mentioned below, and allow it to be superseded.
  293. * Otherwise, things are *really* broken, and we just disregard
  294. * duplicate matches entirely to retain compatibility.
  295. */
  296. pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
  297. map, rid_in);
  298. if (rid_in != map->input_base)
  299. return -ENXIO;
  300. pr_err(FW_BUG "applying workaround.\n");
  301. }
  302. *rid_out = map->output_base + (rid_in - map->input_base);
  303. /*
  304. * Due to confusion regarding the meaning of the id_count field (which
  305. * carries the number of IDs *minus 1*), we may have to disregard this
  306. * match if it is at the end of the range, and overlaps with the start
  307. * of another one.
  308. */
  309. if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
  310. return -EAGAIN;
  311. return 0;
  312. }
  313. static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
  314. u32 *id_out, int index)
  315. {
  316. struct acpi_iort_node *parent;
  317. struct acpi_iort_id_mapping *map;
  318. if (!node->mapping_offset || !node->mapping_count ||
  319. index >= node->mapping_count)
  320. return NULL;
  321. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  322. node->mapping_offset + index * sizeof(*map));
  323. /* Firmware bug! */
  324. if (!map->output_reference) {
  325. pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
  326. node, node->type);
  327. return NULL;
  328. }
  329. parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  330. map->output_reference);
  331. if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
  332. if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
  333. node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
  334. node->type == ACPI_IORT_NODE_SMMU_V3 ||
  335. node->type == ACPI_IORT_NODE_PMCG) {
  336. *id_out = map->output_base;
  337. return parent;
  338. }
  339. }
  340. return NULL;
  341. }
  342. static int iort_get_id_mapping_index(struct acpi_iort_node *node)
  343. {
  344. struct acpi_iort_smmu_v3 *smmu;
  345. struct acpi_iort_pmcg *pmcg;
  346. switch (node->type) {
  347. case ACPI_IORT_NODE_SMMU_V3:
  348. /*
  349. * SMMUv3 dev ID mapping index was introduced in revision 1
  350. * table, not available in revision 0
  351. */
  352. if (node->revision < 1)
  353. return -EINVAL;
  354. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  355. /*
  356. * ID mapping index is only ignored if all interrupts are
  357. * GSIV based
  358. */
  359. if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
  360. && smmu->sync_gsiv)
  361. return -EINVAL;
  362. if (smmu->id_mapping_index >= node->mapping_count) {
  363. pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
  364. node, node->type);
  365. return -EINVAL;
  366. }
  367. return smmu->id_mapping_index;
  368. case ACPI_IORT_NODE_PMCG:
  369. pmcg = (struct acpi_iort_pmcg *)node->node_data;
  370. if (pmcg->overflow_gsiv || node->mapping_count == 0)
  371. return -EINVAL;
  372. return 0;
  373. default:
  374. return -EINVAL;
  375. }
  376. }
  377. static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
  378. u32 id_in, u32 *id_out,
  379. u8 type_mask)
  380. {
  381. u32 id = id_in;
  382. /* Parse the ID mapping tree to find specified node type */
  383. while (node) {
  384. struct acpi_iort_id_mapping *map;
  385. int i, index, rc = 0;
  386. u32 out_ref = 0, map_id = id;
  387. if (IORT_TYPE_MASK(node->type) & type_mask) {
  388. if (id_out)
  389. *id_out = id;
  390. return node;
  391. }
  392. if (!node->mapping_offset || !node->mapping_count)
  393. goto fail_map;
  394. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  395. node->mapping_offset);
  396. /* Firmware bug! */
  397. if (!map->output_reference) {
  398. pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
  399. node, node->type);
  400. goto fail_map;
  401. }
  402. /*
  403. * Get the special ID mapping index (if any) and skip its
  404. * associated ID map to prevent erroneous multi-stage
  405. * IORT ID translations.
  406. */
  407. index = iort_get_id_mapping_index(node);
  408. /* Do the ID translation */
  409. for (i = 0; i < node->mapping_count; i++, map++) {
  410. /* if it is special mapping index, skip it */
  411. if (i == index)
  412. continue;
  413. rc = iort_id_map(map, node->type, map_id, &id, out_ref);
  414. if (!rc)
  415. break;
  416. if (rc == -EAGAIN)
  417. out_ref = map->output_reference;
  418. }
  419. if (i == node->mapping_count && !out_ref)
  420. goto fail_map;
  421. node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  422. rc ? out_ref : map->output_reference);
  423. }
  424. fail_map:
  425. /* Map input ID to output ID unchanged on mapping failure */
  426. if (id_out)
  427. *id_out = id_in;
  428. return NULL;
  429. }
  430. static struct acpi_iort_node *iort_node_map_platform_id(
  431. struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
  432. int index)
  433. {
  434. struct acpi_iort_node *parent;
  435. u32 id;
  436. /* step 1: retrieve the initial dev id */
  437. parent = iort_node_get_id(node, &id, index);
  438. if (!parent)
  439. return NULL;
  440. /*
  441. * optional step 2: map the initial dev id if its parent is not
  442. * the target type we want, map it again for the use cases such
  443. * as NC (named component) -> SMMU -> ITS. If the type is matched,
  444. * return the initial dev id and its parent pointer directly.
  445. */
  446. if (!(IORT_TYPE_MASK(parent->type) & type_mask))
  447. parent = iort_node_map_id(parent, id, id_out, type_mask);
  448. else
  449. if (id_out)
  450. *id_out = id;
  451. return parent;
  452. }
  453. static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
  454. {
  455. struct pci_bus *pbus;
  456. if (!dev_is_pci(dev)) {
  457. struct acpi_iort_node *node;
  458. /*
  459. * scan iort_fwnode_list to see if it's an iort platform
  460. * device (such as SMMU, PMCG),its iort node already cached
  461. * and associated with fwnode when iort platform devices
  462. * were initialized.
  463. */
  464. node = iort_get_iort_node(dev->fwnode);
  465. if (node)
  466. return node;
  467. /*
  468. * if not, then it should be a platform device defined in
  469. * DSDT/SSDT (with Named Component node in IORT)
  470. */
  471. return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  472. iort_match_node_callback, dev);
  473. }
  474. pbus = to_pci_dev(dev)->bus;
  475. return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  476. iort_match_node_callback, &pbus->dev);
  477. }
  478. /**
  479. * iort_msi_map_id() - Map a MSI input ID for a device
  480. * @dev: The device for which the mapping is to be done.
  481. * @input_id: The device input ID.
  482. *
  483. * Returns: mapped MSI ID on success, input ID otherwise
  484. */
  485. u32 iort_msi_map_id(struct device *dev, u32 input_id)
  486. {
  487. struct acpi_iort_node *node;
  488. u32 dev_id;
  489. node = iort_find_dev_node(dev);
  490. if (!node)
  491. return input_id;
  492. iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE);
  493. return dev_id;
  494. }
  495. /**
  496. * iort_pmsi_get_dev_id() - Get the device id for a device
  497. * @dev: The device for which the mapping is to be done.
  498. * @dev_id: The device ID found.
  499. *
  500. * Returns: 0 for successful find a dev id, -ENODEV on error
  501. */
  502. int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
  503. {
  504. int i, index;
  505. struct acpi_iort_node *node;
  506. node = iort_find_dev_node(dev);
  507. if (!node)
  508. return -ENODEV;
  509. index = iort_get_id_mapping_index(node);
  510. /* if there is a valid index, go get the dev_id directly */
  511. if (index >= 0) {
  512. if (iort_node_get_id(node, dev_id, index))
  513. return 0;
  514. } else {
  515. for (i = 0; i < node->mapping_count; i++) {
  516. if (iort_node_map_platform_id(node, dev_id,
  517. IORT_MSI_TYPE, i))
  518. return 0;
  519. }
  520. }
  521. return -ENODEV;
  522. }
  523. static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
  524. {
  525. struct iort_its_msi_chip *its_msi_chip;
  526. int ret = -ENODEV;
  527. spin_lock(&iort_msi_chip_lock);
  528. list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
  529. if (its_msi_chip->translation_id == its_id) {
  530. *base = its_msi_chip->base_addr;
  531. ret = 0;
  532. break;
  533. }
  534. }
  535. spin_unlock(&iort_msi_chip_lock);
  536. return ret;
  537. }
  538. /**
  539. * iort_dev_find_its_id() - Find the ITS identifier for a device
  540. * @dev: The device.
  541. * @id: Device's ID
  542. * @idx: Index of the ITS identifier list.
  543. * @its_id: ITS identifier.
  544. *
  545. * Returns: 0 on success, appropriate error value otherwise
  546. */
  547. static int iort_dev_find_its_id(struct device *dev, u32 id,
  548. unsigned int idx, int *its_id)
  549. {
  550. struct acpi_iort_its_group *its;
  551. struct acpi_iort_node *node;
  552. node = iort_find_dev_node(dev);
  553. if (!node)
  554. return -ENXIO;
  555. node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE);
  556. if (!node)
  557. return -ENXIO;
  558. /* Move to ITS specific data */
  559. its = (struct acpi_iort_its_group *)node->node_data;
  560. if (idx >= its->its_count) {
  561. dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
  562. idx, its->its_count);
  563. return -ENXIO;
  564. }
  565. *its_id = its->identifiers[idx];
  566. return 0;
  567. }
  568. /**
  569. * iort_get_device_domain() - Find MSI domain related to a device
  570. * @dev: The device.
  571. * @id: Requester ID for the device.
  572. * @bus_token: irq domain bus token.
  573. *
  574. * Returns: the MSI domain for this device, NULL otherwise
  575. */
  576. struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,
  577. enum irq_domain_bus_token bus_token)
  578. {
  579. struct fwnode_handle *handle;
  580. int its_id;
  581. if (iort_dev_find_its_id(dev, id, 0, &its_id))
  582. return NULL;
  583. handle = iort_find_domain_token(its_id);
  584. if (!handle)
  585. return NULL;
  586. return irq_find_matching_fwnode(handle, bus_token);
  587. }
  588. static void iort_set_device_domain(struct device *dev,
  589. struct acpi_iort_node *node)
  590. {
  591. struct acpi_iort_its_group *its;
  592. struct acpi_iort_node *msi_parent;
  593. struct acpi_iort_id_mapping *map;
  594. struct fwnode_handle *iort_fwnode;
  595. struct irq_domain *domain;
  596. int index;
  597. index = iort_get_id_mapping_index(node);
  598. if (index < 0)
  599. return;
  600. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  601. node->mapping_offset + index * sizeof(*map));
  602. /* Firmware bug! */
  603. if (!map->output_reference ||
  604. !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
  605. pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
  606. node, node->type);
  607. return;
  608. }
  609. msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  610. map->output_reference);
  611. if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
  612. return;
  613. /* Move to ITS specific data */
  614. its = (struct acpi_iort_its_group *)msi_parent->node_data;
  615. iort_fwnode = iort_find_domain_token(its->identifiers[0]);
  616. if (!iort_fwnode)
  617. return;
  618. domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
  619. if (domain)
  620. dev_set_msi_domain(dev, domain);
  621. }
  622. /**
  623. * iort_get_platform_device_domain() - Find MSI domain related to a
  624. * platform device
  625. * @dev: the dev pointer associated with the platform device
  626. *
  627. * Returns: the MSI domain for this device, NULL otherwise
  628. */
  629. static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
  630. {
  631. struct acpi_iort_node *node, *msi_parent = NULL;
  632. struct fwnode_handle *iort_fwnode;
  633. struct acpi_iort_its_group *its;
  634. int i;
  635. /* find its associated iort node */
  636. node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  637. iort_match_node_callback, dev);
  638. if (!node)
  639. return NULL;
  640. /* then find its msi parent node */
  641. for (i = 0; i < node->mapping_count; i++) {
  642. msi_parent = iort_node_map_platform_id(node, NULL,
  643. IORT_MSI_TYPE, i);
  644. if (msi_parent)
  645. break;
  646. }
  647. if (!msi_parent)
  648. return NULL;
  649. /* Move to ITS specific data */
  650. its = (struct acpi_iort_its_group *)msi_parent->node_data;
  651. iort_fwnode = iort_find_domain_token(its->identifiers[0]);
  652. if (!iort_fwnode)
  653. return NULL;
  654. return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
  655. }
  656. void acpi_configure_pmsi_domain(struct device *dev)
  657. {
  658. struct irq_domain *msi_domain;
  659. msi_domain = iort_get_platform_device_domain(dev);
  660. if (msi_domain)
  661. dev_set_msi_domain(dev, msi_domain);
  662. }
  663. #ifdef CONFIG_IOMMU_API
  664. static void iort_rmr_free(struct device *dev,
  665. struct iommu_resv_region *region)
  666. {
  667. struct iommu_iort_rmr_data *rmr_data;
  668. rmr_data = container_of(region, struct iommu_iort_rmr_data, rr);
  669. kfree(rmr_data->sids);
  670. kfree(rmr_data);
  671. }
  672. static struct iommu_iort_rmr_data *iort_rmr_alloc(
  673. struct acpi_iort_rmr_desc *rmr_desc,
  674. int prot, enum iommu_resv_type type,
  675. u32 *sids, u32 num_sids)
  676. {
  677. struct iommu_iort_rmr_data *rmr_data;
  678. struct iommu_resv_region *region;
  679. u32 *sids_copy;
  680. u64 addr = rmr_desc->base_address, size = rmr_desc->length;
  681. rmr_data = kmalloc(sizeof(*rmr_data), GFP_KERNEL);
  682. if (!rmr_data)
  683. return NULL;
  684. /* Create a copy of SIDs array to associate with this rmr_data */
  685. sids_copy = kmemdup(sids, num_sids * sizeof(*sids), GFP_KERNEL);
  686. if (!sids_copy) {
  687. kfree(rmr_data);
  688. return NULL;
  689. }
  690. rmr_data->sids = sids_copy;
  691. rmr_data->num_sids = num_sids;
  692. if (!IS_ALIGNED(addr, SZ_64K) || !IS_ALIGNED(size, SZ_64K)) {
  693. /* PAGE align base addr and size */
  694. addr &= PAGE_MASK;
  695. size = PAGE_ALIGN(size + offset_in_page(rmr_desc->base_address));
  696. pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] not aligned to 64K, continue with [0x%llx - 0x%llx]\n",
  697. rmr_desc->base_address,
  698. rmr_desc->base_address + rmr_desc->length - 1,
  699. addr, addr + size - 1);
  700. }
  701. region = &rmr_data->rr;
  702. INIT_LIST_HEAD(&region->list);
  703. region->start = addr;
  704. region->length = size;
  705. region->prot = prot;
  706. region->type = type;
  707. region->free = iort_rmr_free;
  708. return rmr_data;
  709. }
  710. static void iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc *desc,
  711. u32 count)
  712. {
  713. int i, j;
  714. for (i = 0; i < count; i++) {
  715. u64 end, start = desc[i].base_address, length = desc[i].length;
  716. if (!length) {
  717. pr_err(FW_BUG "RMR descriptor[0x%llx] with zero length, continue anyway\n",
  718. start);
  719. continue;
  720. }
  721. end = start + length - 1;
  722. /* Check for address overlap */
  723. for (j = i + 1; j < count; j++) {
  724. u64 e_start = desc[j].base_address;
  725. u64 e_end = e_start + desc[j].length - 1;
  726. if (start <= e_end && end >= e_start)
  727. pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] overlaps, continue anyway\n",
  728. start, end);
  729. }
  730. }
  731. }
  732. /*
  733. * Please note, we will keep the already allocated RMR reserve
  734. * regions in case of a memory allocation failure.
  735. */
  736. static void iort_get_rmrs(struct acpi_iort_node *node,
  737. struct acpi_iort_node *smmu,
  738. u32 *sids, u32 num_sids,
  739. struct list_head *head)
  740. {
  741. struct acpi_iort_rmr *rmr = (struct acpi_iort_rmr *)node->node_data;
  742. struct acpi_iort_rmr_desc *rmr_desc;
  743. int i;
  744. rmr_desc = ACPI_ADD_PTR(struct acpi_iort_rmr_desc, node,
  745. rmr->rmr_offset);
  746. iort_rmr_desc_check_overlap(rmr_desc, rmr->rmr_count);
  747. for (i = 0; i < rmr->rmr_count; i++, rmr_desc++) {
  748. struct iommu_iort_rmr_data *rmr_data;
  749. enum iommu_resv_type type;
  750. int prot = IOMMU_READ | IOMMU_WRITE;
  751. if (rmr->flags & ACPI_IORT_RMR_REMAP_PERMITTED)
  752. type = IOMMU_RESV_DIRECT_RELAXABLE;
  753. else
  754. type = IOMMU_RESV_DIRECT;
  755. if (rmr->flags & ACPI_IORT_RMR_ACCESS_PRIVILEGE)
  756. prot |= IOMMU_PRIV;
  757. /* Attributes 0x00 - 0x03 represents device memory */
  758. if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) <=
  759. ACPI_IORT_RMR_ATTR_DEVICE_GRE)
  760. prot |= IOMMU_MMIO;
  761. else if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) ==
  762. ACPI_IORT_RMR_ATTR_NORMAL_IWB_OWB)
  763. prot |= IOMMU_CACHE;
  764. rmr_data = iort_rmr_alloc(rmr_desc, prot, type,
  765. sids, num_sids);
  766. if (!rmr_data)
  767. return;
  768. list_add_tail(&rmr_data->rr.list, head);
  769. }
  770. }
  771. static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start,
  772. u32 new_count)
  773. {
  774. u32 *new_sids;
  775. u32 total_count = count + new_count;
  776. int i;
  777. new_sids = krealloc_array(sids, count + new_count,
  778. sizeof(*new_sids), GFP_KERNEL);
  779. if (!new_sids)
  780. return NULL;
  781. for (i = count; i < total_count; i++)
  782. new_sids[i] = id_start++;
  783. return new_sids;
  784. }
  785. static bool iort_rmr_has_dev(struct device *dev, u32 id_start,
  786. u32 id_count)
  787. {
  788. int i;
  789. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  790. /*
  791. * Make sure the kernel has preserved the boot firmware PCIe
  792. * configuration. This is required to ensure that the RMR PCIe
  793. * StreamIDs are still valid (Refer: ARM DEN 0049E.d Section 3.1.1.5).
  794. */
  795. if (dev_is_pci(dev)) {
  796. struct pci_dev *pdev = to_pci_dev(dev);
  797. struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
  798. if (!host->preserve_config)
  799. return false;
  800. }
  801. for (i = 0; i < fwspec->num_ids; i++) {
  802. if (fwspec->ids[i] >= id_start &&
  803. fwspec->ids[i] <= id_start + id_count)
  804. return true;
  805. }
  806. return false;
  807. }
  808. static void iort_node_get_rmr_info(struct acpi_iort_node *node,
  809. struct acpi_iort_node *iommu,
  810. struct device *dev, struct list_head *head)
  811. {
  812. struct acpi_iort_node *smmu = NULL;
  813. struct acpi_iort_rmr *rmr;
  814. struct acpi_iort_id_mapping *map;
  815. u32 *sids = NULL;
  816. u32 num_sids = 0;
  817. int i;
  818. if (!node->mapping_offset || !node->mapping_count) {
  819. pr_err(FW_BUG "Invalid ID mapping, skipping RMR node %p\n",
  820. node);
  821. return;
  822. }
  823. rmr = (struct acpi_iort_rmr *)node->node_data;
  824. if (!rmr->rmr_offset || !rmr->rmr_count)
  825. return;
  826. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  827. node->mapping_offset);
  828. /*
  829. * Go through the ID mappings and see if we have a match for SMMU
  830. * and dev(if !NULL). If found, get the sids for the Node.
  831. * Please note, id_count is equal to the number of IDs in the
  832. * range minus one.
  833. */
  834. for (i = 0; i < node->mapping_count; i++, map++) {
  835. struct acpi_iort_node *parent;
  836. parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  837. map->output_reference);
  838. if (parent != iommu)
  839. continue;
  840. /* If dev is valid, check RMR node corresponds to the dev SID */
  841. if (dev && !iort_rmr_has_dev(dev, map->output_base,
  842. map->id_count))
  843. continue;
  844. /* Retrieve SIDs associated with the Node. */
  845. sids = iort_rmr_alloc_sids(sids, num_sids, map->output_base,
  846. map->id_count + 1);
  847. if (!sids)
  848. return;
  849. num_sids += map->id_count + 1;
  850. }
  851. if (!sids)
  852. return;
  853. iort_get_rmrs(node, smmu, sids, num_sids, head);
  854. kfree(sids);
  855. }
  856. static void iort_find_rmrs(struct acpi_iort_node *iommu, struct device *dev,
  857. struct list_head *head)
  858. {
  859. struct acpi_table_iort *iort;
  860. struct acpi_iort_node *iort_node, *iort_end;
  861. int i;
  862. /* Only supports ARM DEN 0049E.d onwards */
  863. if (iort_table->revision < 5)
  864. return;
  865. iort = (struct acpi_table_iort *)iort_table;
  866. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  867. iort->node_offset);
  868. iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  869. iort_table->length);
  870. for (i = 0; i < iort->node_count; i++) {
  871. if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
  872. "IORT node pointer overflows, bad table!\n"))
  873. return;
  874. if (iort_node->type == ACPI_IORT_NODE_RMR)
  875. iort_node_get_rmr_info(iort_node, iommu, dev, head);
  876. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
  877. iort_node->length);
  878. }
  879. }
  880. /*
  881. * Populate the RMR list associated with a given IOMMU and dev(if provided).
  882. * If dev is NULL, the function populates all the RMRs associated with the
  883. * given IOMMU.
  884. */
  885. static void iort_iommu_rmr_get_resv_regions(struct fwnode_handle *iommu_fwnode,
  886. struct device *dev,
  887. struct list_head *head)
  888. {
  889. struct acpi_iort_node *iommu;
  890. iommu = iort_get_iort_node(iommu_fwnode);
  891. if (!iommu)
  892. return;
  893. iort_find_rmrs(iommu, dev, head);
  894. }
  895. static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
  896. {
  897. struct acpi_iort_node *iommu;
  898. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  899. iommu = iort_get_iort_node(fwspec->iommu_fwnode);
  900. if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
  901. struct acpi_iort_smmu_v3 *smmu;
  902. smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
  903. if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
  904. return iommu;
  905. }
  906. return NULL;
  907. }
  908. /*
  909. * Retrieve platform specific HW MSI reserve regions.
  910. * The ITS interrupt translation spaces (ITS_base + SZ_64K, SZ_64K)
  911. * associated with the device are the HW MSI reserved regions.
  912. */
  913. static void iort_iommu_msi_get_resv_regions(struct device *dev,
  914. struct list_head *head)
  915. {
  916. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  917. struct acpi_iort_its_group *its;
  918. struct acpi_iort_node *iommu_node, *its_node = NULL;
  919. int i;
  920. iommu_node = iort_get_msi_resv_iommu(dev);
  921. if (!iommu_node)
  922. return;
  923. /*
  924. * Current logic to reserve ITS regions relies on HW topologies
  925. * where a given PCI or named component maps its IDs to only one
  926. * ITS group; if a PCI or named component can map its IDs to
  927. * different ITS groups through IORT mappings this function has
  928. * to be reworked to ensure we reserve regions for all ITS groups
  929. * a given PCI or named component may map IDs to.
  930. */
  931. for (i = 0; i < fwspec->num_ids; i++) {
  932. its_node = iort_node_map_id(iommu_node,
  933. fwspec->ids[i],
  934. NULL, IORT_MSI_TYPE);
  935. if (its_node)
  936. break;
  937. }
  938. if (!its_node)
  939. return;
  940. /* Move to ITS specific data */
  941. its = (struct acpi_iort_its_group *)its_node->node_data;
  942. for (i = 0; i < its->its_count; i++) {
  943. phys_addr_t base;
  944. if (!iort_find_its_base(its->identifiers[i], &base)) {
  945. int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  946. struct iommu_resv_region *region;
  947. region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
  948. prot, IOMMU_RESV_MSI,
  949. GFP_KERNEL);
  950. if (region)
  951. list_add_tail(&region->list, head);
  952. }
  953. }
  954. }
  955. /**
  956. * iort_iommu_get_resv_regions - Generic helper to retrieve reserved regions.
  957. * @dev: Device from iommu_get_resv_regions()
  958. * @head: Reserved region list from iommu_get_resv_regions()
  959. */
  960. void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)
  961. {
  962. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  963. iort_iommu_msi_get_resv_regions(dev, head);
  964. iort_iommu_rmr_get_resv_regions(fwspec->iommu_fwnode, dev, head);
  965. }
  966. /**
  967. * iort_get_rmr_sids - Retrieve IORT RMR node reserved regions with
  968. * associated StreamIDs information.
  969. * @iommu_fwnode: fwnode associated with IOMMU
  970. * @head: Resereved region list
  971. */
  972. void iort_get_rmr_sids(struct fwnode_handle *iommu_fwnode,
  973. struct list_head *head)
  974. {
  975. iort_iommu_rmr_get_resv_regions(iommu_fwnode, NULL, head);
  976. }
  977. EXPORT_SYMBOL_GPL(iort_get_rmr_sids);
  978. /**
  979. * iort_put_rmr_sids - Free memory allocated for RMR reserved regions.
  980. * @iommu_fwnode: fwnode associated with IOMMU
  981. * @head: Resereved region list
  982. */
  983. void iort_put_rmr_sids(struct fwnode_handle *iommu_fwnode,
  984. struct list_head *head)
  985. {
  986. struct iommu_resv_region *entry, *next;
  987. list_for_each_entry_safe(entry, next, head, list)
  988. entry->free(NULL, entry);
  989. }
  990. EXPORT_SYMBOL_GPL(iort_put_rmr_sids);
  991. static inline bool iort_iommu_driver_enabled(u8 type)
  992. {
  993. switch (type) {
  994. case ACPI_IORT_NODE_SMMU_V3:
  995. return IS_ENABLED(CONFIG_ARM_SMMU_V3);
  996. case ACPI_IORT_NODE_SMMU:
  997. return IS_ENABLED(CONFIG_ARM_SMMU);
  998. default:
  999. pr_warn("IORT node type %u does not describe an SMMU\n", type);
  1000. return false;
  1001. }
  1002. }
  1003. static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
  1004. {
  1005. struct acpi_iort_root_complex *pci_rc;
  1006. pci_rc = (struct acpi_iort_root_complex *)node->node_data;
  1007. return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
  1008. }
  1009. static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
  1010. u32 streamid)
  1011. {
  1012. const struct iommu_ops *ops;
  1013. struct fwnode_handle *iort_fwnode;
  1014. if (!node)
  1015. return -ENODEV;
  1016. iort_fwnode = iort_get_fwnode(node);
  1017. if (!iort_fwnode)
  1018. return -ENODEV;
  1019. /*
  1020. * If the ops look-up fails, this means that either
  1021. * the SMMU drivers have not been probed yet or that
  1022. * the SMMU drivers are not built in the kernel;
  1023. * Depending on whether the SMMU drivers are built-in
  1024. * in the kernel or not, defer the IOMMU configuration
  1025. * or just abort it.
  1026. */
  1027. ops = iommu_ops_from_fwnode(iort_fwnode);
  1028. if (!ops)
  1029. return iort_iommu_driver_enabled(node->type) ?
  1030. -EPROBE_DEFER : -ENODEV;
  1031. return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode, ops);
  1032. }
  1033. struct iort_pci_alias_info {
  1034. struct device *dev;
  1035. struct acpi_iort_node *node;
  1036. };
  1037. static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
  1038. {
  1039. struct iort_pci_alias_info *info = data;
  1040. struct acpi_iort_node *parent;
  1041. u32 streamid;
  1042. parent = iort_node_map_id(info->node, alias, &streamid,
  1043. IORT_IOMMU_TYPE);
  1044. return iort_iommu_xlate(info->dev, parent, streamid);
  1045. }
  1046. static void iort_named_component_init(struct device *dev,
  1047. struct acpi_iort_node *node)
  1048. {
  1049. struct property_entry props[3] = {};
  1050. struct acpi_iort_named_component *nc;
  1051. nc = (struct acpi_iort_named_component *)node->node_data;
  1052. props[0] = PROPERTY_ENTRY_U32("pasid-num-bits",
  1053. FIELD_GET(ACPI_IORT_NC_PASID_BITS,
  1054. nc->node_flags));
  1055. if (nc->node_flags & ACPI_IORT_NC_STALL_SUPPORTED)
  1056. props[1] = PROPERTY_ENTRY_BOOL("dma-can-stall");
  1057. if (device_create_managed_software_node(dev, props, NULL))
  1058. dev_warn(dev, "Could not add device properties\n");
  1059. }
  1060. static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node)
  1061. {
  1062. struct acpi_iort_node *parent;
  1063. int err = -ENODEV, i = 0;
  1064. u32 streamid = 0;
  1065. do {
  1066. parent = iort_node_map_platform_id(node, &streamid,
  1067. IORT_IOMMU_TYPE,
  1068. i++);
  1069. if (parent)
  1070. err = iort_iommu_xlate(dev, parent, streamid);
  1071. } while (parent && !err);
  1072. return err;
  1073. }
  1074. static int iort_nc_iommu_map_id(struct device *dev,
  1075. struct acpi_iort_node *node,
  1076. const u32 *in_id)
  1077. {
  1078. struct acpi_iort_node *parent;
  1079. u32 streamid;
  1080. parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE);
  1081. if (parent)
  1082. return iort_iommu_xlate(dev, parent, streamid);
  1083. return -ENODEV;
  1084. }
  1085. /**
  1086. * iort_iommu_configure_id - Set-up IOMMU configuration for a device.
  1087. *
  1088. * @dev: device to configure
  1089. * @id_in: optional input id const value pointer
  1090. *
  1091. * Returns: 0 on success, <0 on failure
  1092. */
  1093. int iort_iommu_configure_id(struct device *dev, const u32 *id_in)
  1094. {
  1095. struct acpi_iort_node *node;
  1096. int err = -ENODEV;
  1097. if (dev_is_pci(dev)) {
  1098. struct iommu_fwspec *fwspec;
  1099. struct pci_bus *bus = to_pci_dev(dev)->bus;
  1100. struct iort_pci_alias_info info = { .dev = dev };
  1101. node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  1102. iort_match_node_callback, &bus->dev);
  1103. if (!node)
  1104. return -ENODEV;
  1105. info.node = node;
  1106. err = pci_for_each_dma_alias(to_pci_dev(dev),
  1107. iort_pci_iommu_init, &info);
  1108. fwspec = dev_iommu_fwspec_get(dev);
  1109. if (fwspec && iort_pci_rc_supports_ats(node))
  1110. fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
  1111. } else {
  1112. node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  1113. iort_match_node_callback, dev);
  1114. if (!node)
  1115. return -ENODEV;
  1116. err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) :
  1117. iort_nc_iommu_map(dev, node);
  1118. if (!err)
  1119. iort_named_component_init(dev, node);
  1120. }
  1121. return err;
  1122. }
  1123. #else
  1124. void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)
  1125. { }
  1126. int iort_iommu_configure_id(struct device *dev, const u32 *input_id)
  1127. { return -ENODEV; }
  1128. #endif
  1129. static int nc_dma_get_range(struct device *dev, u64 *size)
  1130. {
  1131. struct acpi_iort_node *node;
  1132. struct acpi_iort_named_component *ncomp;
  1133. node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  1134. iort_match_node_callback, dev);
  1135. if (!node)
  1136. return -ENODEV;
  1137. ncomp = (struct acpi_iort_named_component *)node->node_data;
  1138. if (!ncomp->memory_address_limit) {
  1139. pr_warn(FW_BUG "Named component missing memory address limit\n");
  1140. return -EINVAL;
  1141. }
  1142. *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
  1143. 1ULL<<ncomp->memory_address_limit;
  1144. return 0;
  1145. }
  1146. static int rc_dma_get_range(struct device *dev, u64 *size)
  1147. {
  1148. struct acpi_iort_node *node;
  1149. struct acpi_iort_root_complex *rc;
  1150. struct pci_bus *pbus = to_pci_dev(dev)->bus;
  1151. node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  1152. iort_match_node_callback, &pbus->dev);
  1153. if (!node || node->revision < 1)
  1154. return -ENODEV;
  1155. rc = (struct acpi_iort_root_complex *)node->node_data;
  1156. if (!rc->memory_address_limit) {
  1157. pr_warn(FW_BUG "Root complex missing memory address limit\n");
  1158. return -EINVAL;
  1159. }
  1160. *size = rc->memory_address_limit >= 64 ? U64_MAX :
  1161. 1ULL<<rc->memory_address_limit;
  1162. return 0;
  1163. }
  1164. /**
  1165. * iort_dma_get_ranges() - Look up DMA addressing limit for the device
  1166. * @dev: device to lookup
  1167. * @size: DMA range size result pointer
  1168. *
  1169. * Return: 0 on success, an error otherwise.
  1170. */
  1171. int iort_dma_get_ranges(struct device *dev, u64 *size)
  1172. {
  1173. if (dev_is_pci(dev))
  1174. return rc_dma_get_range(dev, size);
  1175. else
  1176. return nc_dma_get_range(dev, size);
  1177. }
  1178. static void __init acpi_iort_register_irq(int hwirq, const char *name,
  1179. int trigger,
  1180. struct resource *res)
  1181. {
  1182. int irq = acpi_register_gsi(NULL, hwirq, trigger,
  1183. ACPI_ACTIVE_HIGH);
  1184. if (irq <= 0) {
  1185. pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
  1186. name);
  1187. return;
  1188. }
  1189. res->start = irq;
  1190. res->end = irq;
  1191. res->flags = IORESOURCE_IRQ;
  1192. res->name = name;
  1193. }
  1194. static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
  1195. {
  1196. struct acpi_iort_smmu_v3 *smmu;
  1197. /* Always present mem resource */
  1198. int num_res = 1;
  1199. /* Retrieve SMMUv3 specific data */
  1200. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1201. if (smmu->event_gsiv)
  1202. num_res++;
  1203. if (smmu->pri_gsiv)
  1204. num_res++;
  1205. if (smmu->gerr_gsiv)
  1206. num_res++;
  1207. if (smmu->sync_gsiv)
  1208. num_res++;
  1209. return num_res;
  1210. }
  1211. static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
  1212. {
  1213. /*
  1214. * Cavium ThunderX2 implementation doesn't not support unique
  1215. * irq line. Use single irq line for all the SMMUv3 interrupts.
  1216. */
  1217. if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
  1218. return false;
  1219. /*
  1220. * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
  1221. * SPI numbers here.
  1222. */
  1223. return smmu->event_gsiv == smmu->pri_gsiv &&
  1224. smmu->event_gsiv == smmu->gerr_gsiv &&
  1225. smmu->event_gsiv == smmu->sync_gsiv;
  1226. }
  1227. static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
  1228. {
  1229. /*
  1230. * Override the size, for Cavium ThunderX2 implementation
  1231. * which doesn't support the page 1 SMMU register space.
  1232. */
  1233. if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
  1234. return SZ_64K;
  1235. return SZ_128K;
  1236. }
  1237. static void __init arm_smmu_v3_init_resources(struct resource *res,
  1238. struct acpi_iort_node *node)
  1239. {
  1240. struct acpi_iort_smmu_v3 *smmu;
  1241. int num_res = 0;
  1242. /* Retrieve SMMUv3 specific data */
  1243. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1244. res[num_res].start = smmu->base_address;
  1245. res[num_res].end = smmu->base_address +
  1246. arm_smmu_v3_resource_size(smmu) - 1;
  1247. res[num_res].flags = IORESOURCE_MEM;
  1248. num_res++;
  1249. if (arm_smmu_v3_is_combined_irq(smmu)) {
  1250. if (smmu->event_gsiv)
  1251. acpi_iort_register_irq(smmu->event_gsiv, "combined",
  1252. ACPI_EDGE_SENSITIVE,
  1253. &res[num_res++]);
  1254. } else {
  1255. if (smmu->event_gsiv)
  1256. acpi_iort_register_irq(smmu->event_gsiv, "eventq",
  1257. ACPI_EDGE_SENSITIVE,
  1258. &res[num_res++]);
  1259. if (smmu->pri_gsiv)
  1260. acpi_iort_register_irq(smmu->pri_gsiv, "priq",
  1261. ACPI_EDGE_SENSITIVE,
  1262. &res[num_res++]);
  1263. if (smmu->gerr_gsiv)
  1264. acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
  1265. ACPI_EDGE_SENSITIVE,
  1266. &res[num_res++]);
  1267. if (smmu->sync_gsiv)
  1268. acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
  1269. ACPI_EDGE_SENSITIVE,
  1270. &res[num_res++]);
  1271. }
  1272. }
  1273. static void __init arm_smmu_v3_dma_configure(struct device *dev,
  1274. struct acpi_iort_node *node)
  1275. {
  1276. struct acpi_iort_smmu_v3 *smmu;
  1277. enum dev_dma_attr attr;
  1278. /* Retrieve SMMUv3 specific data */
  1279. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1280. attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
  1281. DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
  1282. /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
  1283. dev->dma_mask = &dev->coherent_dma_mask;
  1284. /* Configure DMA for the page table walker */
  1285. acpi_dma_configure(dev, attr);
  1286. }
  1287. #if defined(CONFIG_ACPI_NUMA)
  1288. /*
  1289. * set numa proximity domain for smmuv3 device
  1290. */
  1291. static int __init arm_smmu_v3_set_proximity(struct device *dev,
  1292. struct acpi_iort_node *node)
  1293. {
  1294. struct acpi_iort_smmu_v3 *smmu;
  1295. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1296. if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
  1297. int dev_node = pxm_to_node(smmu->pxm);
  1298. if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
  1299. return -EINVAL;
  1300. set_dev_node(dev, dev_node);
  1301. pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
  1302. smmu->base_address,
  1303. smmu->pxm);
  1304. }
  1305. return 0;
  1306. }
  1307. #else
  1308. #define arm_smmu_v3_set_proximity NULL
  1309. #endif
  1310. static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
  1311. {
  1312. struct acpi_iort_smmu *smmu;
  1313. /* Retrieve SMMU specific data */
  1314. smmu = (struct acpi_iort_smmu *)node->node_data;
  1315. /*
  1316. * Only consider the global fault interrupt and ignore the
  1317. * configuration access interrupt.
  1318. *
  1319. * MMIO address and global fault interrupt resources are always
  1320. * present so add them to the context interrupt count as a static
  1321. * value.
  1322. */
  1323. return smmu->context_interrupt_count + 2;
  1324. }
  1325. static void __init arm_smmu_init_resources(struct resource *res,
  1326. struct acpi_iort_node *node)
  1327. {
  1328. struct acpi_iort_smmu *smmu;
  1329. int i, hw_irq, trigger, num_res = 0;
  1330. u64 *ctx_irq, *glb_irq;
  1331. /* Retrieve SMMU specific data */
  1332. smmu = (struct acpi_iort_smmu *)node->node_data;
  1333. res[num_res].start = smmu->base_address;
  1334. res[num_res].end = smmu->base_address + smmu->span - 1;
  1335. res[num_res].flags = IORESOURCE_MEM;
  1336. num_res++;
  1337. glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
  1338. /* Global IRQs */
  1339. hw_irq = IORT_IRQ_MASK(glb_irq[0]);
  1340. trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
  1341. acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
  1342. &res[num_res++]);
  1343. /* Context IRQs */
  1344. ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
  1345. for (i = 0; i < smmu->context_interrupt_count; i++) {
  1346. hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
  1347. trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
  1348. acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
  1349. &res[num_res++]);
  1350. }
  1351. }
  1352. static void __init arm_smmu_dma_configure(struct device *dev,
  1353. struct acpi_iort_node *node)
  1354. {
  1355. struct acpi_iort_smmu *smmu;
  1356. enum dev_dma_attr attr;
  1357. /* Retrieve SMMU specific data */
  1358. smmu = (struct acpi_iort_smmu *)node->node_data;
  1359. attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
  1360. DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
  1361. /* We expect the dma masks to be equivalent for SMMU set-ups */
  1362. dev->dma_mask = &dev->coherent_dma_mask;
  1363. /* Configure DMA for the page table walker */
  1364. acpi_dma_configure(dev, attr);
  1365. }
  1366. static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
  1367. {
  1368. struct acpi_iort_pmcg *pmcg;
  1369. /* Retrieve PMCG specific data */
  1370. pmcg = (struct acpi_iort_pmcg *)node->node_data;
  1371. /*
  1372. * There are always 2 memory resources.
  1373. * If the overflow_gsiv is present then add that for a total of 3.
  1374. */
  1375. return pmcg->overflow_gsiv ? 3 : 2;
  1376. }
  1377. static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
  1378. struct acpi_iort_node *node)
  1379. {
  1380. struct acpi_iort_pmcg *pmcg;
  1381. /* Retrieve PMCG specific data */
  1382. pmcg = (struct acpi_iort_pmcg *)node->node_data;
  1383. res[0].start = pmcg->page0_base_address;
  1384. res[0].end = pmcg->page0_base_address + SZ_4K - 1;
  1385. res[0].flags = IORESOURCE_MEM;
  1386. /*
  1387. * The initial version in DEN0049C lacked a way to describe register
  1388. * page 1, which makes it broken for most PMCG implementations; in
  1389. * that case, just let the driver fail gracefully if it expects to
  1390. * find a second memory resource.
  1391. */
  1392. if (node->revision > 0) {
  1393. res[1].start = pmcg->page1_base_address;
  1394. res[1].end = pmcg->page1_base_address + SZ_4K - 1;
  1395. res[1].flags = IORESOURCE_MEM;
  1396. }
  1397. if (pmcg->overflow_gsiv)
  1398. acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
  1399. ACPI_EDGE_SENSITIVE, &res[2]);
  1400. }
  1401. static struct acpi_platform_list pmcg_plat_info[] __initdata = {
  1402. /* HiSilicon Hip08 Platform */
  1403. {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
  1404. "Erratum #162001800, Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP08},
  1405. /* HiSilicon Hip09 Platform */
  1406. {"HISI ", "HIP09 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
  1407. "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09},
  1408. { }
  1409. };
  1410. static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
  1411. {
  1412. u32 model;
  1413. int idx;
  1414. idx = acpi_match_platform_list(pmcg_plat_info);
  1415. if (idx >= 0)
  1416. model = pmcg_plat_info[idx].data;
  1417. else
  1418. model = IORT_SMMU_V3_PMCG_GENERIC;
  1419. return platform_device_add_data(pdev, &model, sizeof(model));
  1420. }
  1421. struct iort_dev_config {
  1422. const char *name;
  1423. int (*dev_init)(struct acpi_iort_node *node);
  1424. void (*dev_dma_configure)(struct device *dev,
  1425. struct acpi_iort_node *node);
  1426. int (*dev_count_resources)(struct acpi_iort_node *node);
  1427. void (*dev_init_resources)(struct resource *res,
  1428. struct acpi_iort_node *node);
  1429. int (*dev_set_proximity)(struct device *dev,
  1430. struct acpi_iort_node *node);
  1431. int (*dev_add_platdata)(struct platform_device *pdev);
  1432. };
  1433. static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
  1434. .name = "arm-smmu-v3",
  1435. .dev_dma_configure = arm_smmu_v3_dma_configure,
  1436. .dev_count_resources = arm_smmu_v3_count_resources,
  1437. .dev_init_resources = arm_smmu_v3_init_resources,
  1438. .dev_set_proximity = arm_smmu_v3_set_proximity,
  1439. };
  1440. static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
  1441. .name = "arm-smmu",
  1442. .dev_dma_configure = arm_smmu_dma_configure,
  1443. .dev_count_resources = arm_smmu_count_resources,
  1444. .dev_init_resources = arm_smmu_init_resources,
  1445. };
  1446. static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
  1447. .name = "arm-smmu-v3-pmcg",
  1448. .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
  1449. .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
  1450. .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
  1451. };
  1452. static __init const struct iort_dev_config *iort_get_dev_cfg(
  1453. struct acpi_iort_node *node)
  1454. {
  1455. switch (node->type) {
  1456. case ACPI_IORT_NODE_SMMU_V3:
  1457. return &iort_arm_smmu_v3_cfg;
  1458. case ACPI_IORT_NODE_SMMU:
  1459. return &iort_arm_smmu_cfg;
  1460. case ACPI_IORT_NODE_PMCG:
  1461. return &iort_arm_smmu_v3_pmcg_cfg;
  1462. default:
  1463. return NULL;
  1464. }
  1465. }
  1466. /**
  1467. * iort_add_platform_device() - Allocate a platform device for IORT node
  1468. * @node: Pointer to device ACPI IORT node
  1469. * @ops: Pointer to IORT device config struct
  1470. *
  1471. * Returns: 0 on success, <0 failure
  1472. */
  1473. static int __init iort_add_platform_device(struct acpi_iort_node *node,
  1474. const struct iort_dev_config *ops)
  1475. {
  1476. struct fwnode_handle *fwnode;
  1477. struct platform_device *pdev;
  1478. struct resource *r;
  1479. int ret, count;
  1480. pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
  1481. if (!pdev)
  1482. return -ENOMEM;
  1483. if (ops->dev_set_proximity) {
  1484. ret = ops->dev_set_proximity(&pdev->dev, node);
  1485. if (ret)
  1486. goto dev_put;
  1487. }
  1488. count = ops->dev_count_resources(node);
  1489. r = kcalloc(count, sizeof(*r), GFP_KERNEL);
  1490. if (!r) {
  1491. ret = -ENOMEM;
  1492. goto dev_put;
  1493. }
  1494. ops->dev_init_resources(r, node);
  1495. ret = platform_device_add_resources(pdev, r, count);
  1496. /*
  1497. * Resources are duplicated in platform_device_add_resources,
  1498. * free their allocated memory
  1499. */
  1500. kfree(r);
  1501. if (ret)
  1502. goto dev_put;
  1503. /*
  1504. * Platform devices based on PMCG nodes uses platform_data to
  1505. * pass the hardware model info to the driver. For others, add
  1506. * a copy of IORT node pointer to platform_data to be used to
  1507. * retrieve IORT data information.
  1508. */
  1509. if (ops->dev_add_platdata)
  1510. ret = ops->dev_add_platdata(pdev);
  1511. else
  1512. ret = platform_device_add_data(pdev, &node, sizeof(node));
  1513. if (ret)
  1514. goto dev_put;
  1515. fwnode = iort_get_fwnode(node);
  1516. if (!fwnode) {
  1517. ret = -ENODEV;
  1518. goto dev_put;
  1519. }
  1520. pdev->dev.fwnode = fwnode;
  1521. if (ops->dev_dma_configure)
  1522. ops->dev_dma_configure(&pdev->dev, node);
  1523. iort_set_device_domain(&pdev->dev, node);
  1524. ret = platform_device_add(pdev);
  1525. if (ret)
  1526. goto dma_deconfigure;
  1527. return 0;
  1528. dma_deconfigure:
  1529. arch_teardown_dma_ops(&pdev->dev);
  1530. dev_put:
  1531. platform_device_put(pdev);
  1532. return ret;
  1533. }
  1534. #ifdef CONFIG_PCI
  1535. static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
  1536. {
  1537. static bool acs_enabled __initdata;
  1538. if (acs_enabled)
  1539. return;
  1540. if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  1541. struct acpi_iort_node *parent;
  1542. struct acpi_iort_id_mapping *map;
  1543. int i;
  1544. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
  1545. iort_node->mapping_offset);
  1546. for (i = 0; i < iort_node->mapping_count; i++, map++) {
  1547. if (!map->output_reference)
  1548. continue;
  1549. parent = ACPI_ADD_PTR(struct acpi_iort_node,
  1550. iort_table, map->output_reference);
  1551. /*
  1552. * If we detect a RC->SMMU mapping, make sure
  1553. * we enable ACS on the system.
  1554. */
  1555. if ((parent->type == ACPI_IORT_NODE_SMMU) ||
  1556. (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
  1557. pci_request_acs();
  1558. acs_enabled = true;
  1559. return;
  1560. }
  1561. }
  1562. }
  1563. }
  1564. #else
  1565. static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
  1566. #endif
  1567. static void __init iort_init_platform_devices(void)
  1568. {
  1569. struct acpi_iort_node *iort_node, *iort_end;
  1570. struct acpi_table_iort *iort;
  1571. struct fwnode_handle *fwnode;
  1572. int i, ret;
  1573. const struct iort_dev_config *ops;
  1574. /*
  1575. * iort_table and iort both point to the start of IORT table, but
  1576. * have different struct types
  1577. */
  1578. iort = (struct acpi_table_iort *)iort_table;
  1579. /* Get the first IORT node */
  1580. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  1581. iort->node_offset);
  1582. iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  1583. iort_table->length);
  1584. for (i = 0; i < iort->node_count; i++) {
  1585. if (iort_node >= iort_end) {
  1586. pr_err("iort node pointer overflows, bad table\n");
  1587. return;
  1588. }
  1589. iort_enable_acs(iort_node);
  1590. ops = iort_get_dev_cfg(iort_node);
  1591. if (ops) {
  1592. fwnode = acpi_alloc_fwnode_static();
  1593. if (!fwnode)
  1594. return;
  1595. iort_set_fwnode(iort_node, fwnode);
  1596. ret = iort_add_platform_device(iort_node, ops);
  1597. if (ret) {
  1598. iort_delete_fwnode(iort_node);
  1599. acpi_free_fwnode_static(fwnode);
  1600. return;
  1601. }
  1602. }
  1603. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
  1604. iort_node->length);
  1605. }
  1606. }
  1607. void __init acpi_iort_init(void)
  1608. {
  1609. acpi_status status;
  1610. /* iort_table will be used at runtime after the iort init,
  1611. * so we don't need to call acpi_put_table() to release
  1612. * the IORT table mapping.
  1613. */
  1614. status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
  1615. if (ACPI_FAILURE(status)) {
  1616. if (status != AE_NOT_FOUND) {
  1617. const char *msg = acpi_format_exception(status);
  1618. pr_err("Failed to get table, %s\n", msg);
  1619. }
  1620. return;
  1621. }
  1622. iort_init_platform_devices();
  1623. }
  1624. #ifdef CONFIG_ZONE_DMA
  1625. /*
  1626. * Extract the highest CPU physical address accessible to all DMA masters in
  1627. * the system. PHYS_ADDR_MAX is returned when no constrained device is found.
  1628. */
  1629. phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
  1630. {
  1631. phys_addr_t limit = PHYS_ADDR_MAX;
  1632. struct acpi_iort_node *node, *end;
  1633. struct acpi_table_iort *iort;
  1634. acpi_status status;
  1635. int i;
  1636. if (acpi_disabled)
  1637. return limit;
  1638. status = acpi_get_table(ACPI_SIG_IORT, 0,
  1639. (struct acpi_table_header **)&iort);
  1640. if (ACPI_FAILURE(status))
  1641. return limit;
  1642. node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset);
  1643. end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length);
  1644. for (i = 0; i < iort->node_count; i++) {
  1645. if (node >= end)
  1646. break;
  1647. switch (node->type) {
  1648. struct acpi_iort_named_component *ncomp;
  1649. struct acpi_iort_root_complex *rc;
  1650. phys_addr_t local_limit;
  1651. case ACPI_IORT_NODE_NAMED_COMPONENT:
  1652. ncomp = (struct acpi_iort_named_component *)node->node_data;
  1653. local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
  1654. limit = min_not_zero(limit, local_limit);
  1655. break;
  1656. case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
  1657. if (node->revision < 1)
  1658. break;
  1659. rc = (struct acpi_iort_root_complex *)node->node_data;
  1660. local_limit = DMA_BIT_MASK(rc->memory_address_limit);
  1661. limit = min_not_zero(limit, local_limit);
  1662. break;
  1663. }
  1664. node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length);
  1665. }
  1666. acpi_put_table(&iort->header);
  1667. return limit;
  1668. }
  1669. #endif