iort.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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] overruns ITS entries [%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 = NULL;
  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. static int rc_dma_get_range(struct device *dev, u64 *size)
  791. {
  792. struct acpi_iort_node *node;
  793. struct acpi_iort_root_complex *rc;
  794. struct pci_bus *pbus = to_pci_dev(dev)->bus;
  795. node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  796. iort_match_node_callback, &pbus->dev);
  797. if (!node || node->revision < 1)
  798. return -ENODEV;
  799. rc = (struct acpi_iort_root_complex *)node->node_data;
  800. *size = rc->memory_address_limit >= 64 ? U64_MAX :
  801. 1ULL<<rc->memory_address_limit;
  802. return 0;
  803. }
  804. /**
  805. * iort_dma_setup() - Set-up device DMA parameters.
  806. *
  807. * @dev: device to configure
  808. * @dma_addr: device DMA address result pointer
  809. * @size: DMA range size result pointer
  810. */
  811. void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
  812. {
  813. u64 mask, dmaaddr = 0, size = 0, offset = 0;
  814. int ret, msb;
  815. /*
  816. * If @dev is expected to be DMA-capable then the bus code that created
  817. * it should have initialised its dma_mask pointer by this point. For
  818. * now, we'll continue the legacy behaviour of coercing it to the
  819. * coherent mask if not, but we'll no longer do so quietly.
  820. */
  821. if (!dev->dma_mask) {
  822. dev_warn(dev, "DMA mask not set\n");
  823. dev->dma_mask = &dev->coherent_dma_mask;
  824. }
  825. if (dev->coherent_dma_mask)
  826. size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
  827. else
  828. size = 1ULL << 32;
  829. if (dev_is_pci(dev)) {
  830. ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
  831. if (ret == -ENODEV)
  832. ret = rc_dma_get_range(dev, &size);
  833. } else {
  834. ret = nc_dma_get_range(dev, &size);
  835. }
  836. if (!ret) {
  837. msb = fls64(dmaaddr + size - 1);
  838. /*
  839. * Round-up to the power-of-two mask or set
  840. * the mask to the whole 64-bit address space
  841. * in case the DMA region covers the full
  842. * memory window.
  843. */
  844. mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
  845. /*
  846. * Limit coherent and dma mask based on size
  847. * retrieved from firmware.
  848. */
  849. dev->bus_dma_mask = mask;
  850. dev->coherent_dma_mask = mask;
  851. *dev->dma_mask = mask;
  852. }
  853. *dma_addr = dmaaddr;
  854. *dma_size = size;
  855. dev->dma_pfn_offset = PFN_DOWN(offset);
  856. dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
  857. }
  858. /**
  859. * iort_iommu_configure - Set-up IOMMU configuration for a device.
  860. *
  861. * @dev: device to configure
  862. *
  863. * Returns: iommu_ops pointer on configuration success
  864. * NULL on configuration failure
  865. */
  866. const struct iommu_ops *iort_iommu_configure(struct device *dev)
  867. {
  868. struct acpi_iort_node *node, *parent;
  869. const struct iommu_ops *ops;
  870. u32 streamid = 0;
  871. int err = -ENODEV;
  872. /*
  873. * If we already translated the fwspec there
  874. * is nothing left to do, return the iommu_ops.
  875. */
  876. ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
  877. if (ops)
  878. return ops;
  879. if (dev_is_pci(dev)) {
  880. struct pci_bus *bus = to_pci_dev(dev)->bus;
  881. struct iort_pci_alias_info info = { .dev = dev };
  882. node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  883. iort_match_node_callback, &bus->dev);
  884. if (!node)
  885. return NULL;
  886. info.node = node;
  887. err = pci_for_each_dma_alias(to_pci_dev(dev),
  888. iort_pci_iommu_init, &info);
  889. } else {
  890. int i = 0;
  891. node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  892. iort_match_node_callback, dev);
  893. if (!node)
  894. return NULL;
  895. do {
  896. parent = iort_node_map_platform_id(node, &streamid,
  897. IORT_IOMMU_TYPE,
  898. i++);
  899. if (parent)
  900. err = iort_iommu_xlate(dev, parent, streamid);
  901. } while (parent && !err);
  902. }
  903. /*
  904. * If we have reason to believe the IOMMU driver missed the initial
  905. * add_device callback for dev, replay it to get things in order.
  906. */
  907. if (!err) {
  908. ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
  909. err = iort_add_device_replay(ops, dev);
  910. }
  911. /* Ignore all other errors apart from EPROBE_DEFER */
  912. if (err == -EPROBE_DEFER) {
  913. ops = ERR_PTR(err);
  914. } else if (err) {
  915. dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
  916. ops = NULL;
  917. }
  918. return ops;
  919. }
  920. static void __init acpi_iort_register_irq(int hwirq, const char *name,
  921. int trigger,
  922. struct resource *res)
  923. {
  924. int irq = acpi_register_gsi(NULL, hwirq, trigger,
  925. ACPI_ACTIVE_HIGH);
  926. if (irq <= 0) {
  927. pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
  928. name);
  929. return;
  930. }
  931. res->start = irq;
  932. res->end = irq;
  933. res->flags = IORESOURCE_IRQ;
  934. res->name = name;
  935. }
  936. static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
  937. {
  938. struct acpi_iort_smmu_v3 *smmu;
  939. /* Always present mem resource */
  940. int num_res = 1;
  941. /* Retrieve SMMUv3 specific data */
  942. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  943. if (smmu->event_gsiv)
  944. num_res++;
  945. if (smmu->pri_gsiv)
  946. num_res++;
  947. if (smmu->gerr_gsiv)
  948. num_res++;
  949. if (smmu->sync_gsiv)
  950. num_res++;
  951. return num_res;
  952. }
  953. static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
  954. {
  955. /*
  956. * Cavium ThunderX2 implementation doesn't not support unique
  957. * irq line. Use single irq line for all the SMMUv3 interrupts.
  958. */
  959. if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
  960. return false;
  961. /*
  962. * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
  963. * SPI numbers here.
  964. */
  965. return smmu->event_gsiv == smmu->pri_gsiv &&
  966. smmu->event_gsiv == smmu->gerr_gsiv &&
  967. smmu->event_gsiv == smmu->sync_gsiv;
  968. }
  969. static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
  970. {
  971. /*
  972. * Override the size, for Cavium ThunderX2 implementation
  973. * which doesn't support the page 1 SMMU register space.
  974. */
  975. if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
  976. return SZ_64K;
  977. return SZ_128K;
  978. }
  979. static void __init arm_smmu_v3_init_resources(struct resource *res,
  980. struct acpi_iort_node *node)
  981. {
  982. struct acpi_iort_smmu_v3 *smmu;
  983. int num_res = 0;
  984. /* Retrieve SMMUv3 specific data */
  985. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  986. res[num_res].start = smmu->base_address;
  987. res[num_res].end = smmu->base_address +
  988. arm_smmu_v3_resource_size(smmu) - 1;
  989. res[num_res].flags = IORESOURCE_MEM;
  990. num_res++;
  991. if (arm_smmu_v3_is_combined_irq(smmu)) {
  992. if (smmu->event_gsiv)
  993. acpi_iort_register_irq(smmu->event_gsiv, "combined",
  994. ACPI_EDGE_SENSITIVE,
  995. &res[num_res++]);
  996. } else {
  997. if (smmu->event_gsiv)
  998. acpi_iort_register_irq(smmu->event_gsiv, "eventq",
  999. ACPI_EDGE_SENSITIVE,
  1000. &res[num_res++]);
  1001. if (smmu->pri_gsiv)
  1002. acpi_iort_register_irq(smmu->pri_gsiv, "priq",
  1003. ACPI_EDGE_SENSITIVE,
  1004. &res[num_res++]);
  1005. if (smmu->gerr_gsiv)
  1006. acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
  1007. ACPI_EDGE_SENSITIVE,
  1008. &res[num_res++]);
  1009. if (smmu->sync_gsiv)
  1010. acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
  1011. ACPI_EDGE_SENSITIVE,
  1012. &res[num_res++]);
  1013. }
  1014. }
  1015. static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
  1016. {
  1017. struct acpi_iort_smmu_v3 *smmu;
  1018. /* Retrieve SMMUv3 specific data */
  1019. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1020. return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
  1021. }
  1022. #if defined(CONFIG_ACPI_NUMA)
  1023. /*
  1024. * set numa proximity domain for smmuv3 device
  1025. */
  1026. static int __init arm_smmu_v3_set_proximity(struct device *dev,
  1027. struct acpi_iort_node *node)
  1028. {
  1029. struct acpi_iort_smmu_v3 *smmu;
  1030. smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
  1031. if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
  1032. int node = acpi_map_pxm_to_node(smmu->pxm);
  1033. if (node != NUMA_NO_NODE && !node_online(node))
  1034. return -EINVAL;
  1035. set_dev_node(dev, node);
  1036. pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
  1037. smmu->base_address,
  1038. smmu->pxm);
  1039. }
  1040. return 0;
  1041. }
  1042. #else
  1043. #define arm_smmu_v3_set_proximity NULL
  1044. #endif
  1045. static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
  1046. {
  1047. struct acpi_iort_smmu *smmu;
  1048. /* Retrieve SMMU specific data */
  1049. smmu = (struct acpi_iort_smmu *)node->node_data;
  1050. /*
  1051. * Only consider the global fault interrupt and ignore the
  1052. * configuration access interrupt.
  1053. *
  1054. * MMIO address and global fault interrupt resources are always
  1055. * present so add them to the context interrupt count as a static
  1056. * value.
  1057. */
  1058. return smmu->context_interrupt_count + 2;
  1059. }
  1060. static void __init arm_smmu_init_resources(struct resource *res,
  1061. struct acpi_iort_node *node)
  1062. {
  1063. struct acpi_iort_smmu *smmu;
  1064. int i, hw_irq, trigger, num_res = 0;
  1065. u64 *ctx_irq, *glb_irq;
  1066. /* Retrieve SMMU specific data */
  1067. smmu = (struct acpi_iort_smmu *)node->node_data;
  1068. res[num_res].start = smmu->base_address;
  1069. res[num_res].end = smmu->base_address + smmu->span - 1;
  1070. res[num_res].flags = IORESOURCE_MEM;
  1071. num_res++;
  1072. glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
  1073. /* Global IRQs */
  1074. hw_irq = IORT_IRQ_MASK(glb_irq[0]);
  1075. trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
  1076. acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
  1077. &res[num_res++]);
  1078. /* Context IRQs */
  1079. ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
  1080. for (i = 0; i < smmu->context_interrupt_count; i++) {
  1081. hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
  1082. trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
  1083. acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
  1084. &res[num_res++]);
  1085. }
  1086. }
  1087. static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
  1088. {
  1089. struct acpi_iort_smmu *smmu;
  1090. /* Retrieve SMMU specific data */
  1091. smmu = (struct acpi_iort_smmu *)node->node_data;
  1092. return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
  1093. }
  1094. struct iort_dev_config {
  1095. const char *name;
  1096. int (*dev_init)(struct acpi_iort_node *node);
  1097. bool (*dev_is_coherent)(struct acpi_iort_node *node);
  1098. int (*dev_count_resources)(struct acpi_iort_node *node);
  1099. void (*dev_init_resources)(struct resource *res,
  1100. struct acpi_iort_node *node);
  1101. int (*dev_set_proximity)(struct device *dev,
  1102. struct acpi_iort_node *node);
  1103. };
  1104. static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
  1105. .name = "arm-smmu-v3",
  1106. .dev_is_coherent = arm_smmu_v3_is_coherent,
  1107. .dev_count_resources = arm_smmu_v3_count_resources,
  1108. .dev_init_resources = arm_smmu_v3_init_resources,
  1109. .dev_set_proximity = arm_smmu_v3_set_proximity,
  1110. };
  1111. static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
  1112. .name = "arm-smmu",
  1113. .dev_is_coherent = arm_smmu_is_coherent,
  1114. .dev_count_resources = arm_smmu_count_resources,
  1115. .dev_init_resources = arm_smmu_init_resources
  1116. };
  1117. static __init const struct iort_dev_config *iort_get_dev_cfg(
  1118. struct acpi_iort_node *node)
  1119. {
  1120. switch (node->type) {
  1121. case ACPI_IORT_NODE_SMMU_V3:
  1122. return &iort_arm_smmu_v3_cfg;
  1123. case ACPI_IORT_NODE_SMMU:
  1124. return &iort_arm_smmu_cfg;
  1125. default:
  1126. return NULL;
  1127. }
  1128. }
  1129. /**
  1130. * iort_add_platform_device() - Allocate a platform device for IORT node
  1131. * @node: Pointer to device ACPI IORT node
  1132. *
  1133. * Returns: 0 on success, <0 failure
  1134. */
  1135. static int __init iort_add_platform_device(struct acpi_iort_node *node,
  1136. const struct iort_dev_config *ops)
  1137. {
  1138. struct fwnode_handle *fwnode;
  1139. struct platform_device *pdev;
  1140. struct resource *r;
  1141. enum dev_dma_attr attr;
  1142. int ret, count;
  1143. pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
  1144. if (!pdev)
  1145. return -ENOMEM;
  1146. if (ops->dev_set_proximity) {
  1147. ret = ops->dev_set_proximity(&pdev->dev, node);
  1148. if (ret)
  1149. goto dev_put;
  1150. }
  1151. count = ops->dev_count_resources(node);
  1152. r = kcalloc(count, sizeof(*r), GFP_KERNEL);
  1153. if (!r) {
  1154. ret = -ENOMEM;
  1155. goto dev_put;
  1156. }
  1157. ops->dev_init_resources(r, node);
  1158. ret = platform_device_add_resources(pdev, r, count);
  1159. /*
  1160. * Resources are duplicated in platform_device_add_resources,
  1161. * free their allocated memory
  1162. */
  1163. kfree(r);
  1164. if (ret)
  1165. goto dev_put;
  1166. /*
  1167. * Add a copy of IORT node pointer to platform_data to
  1168. * be used to retrieve IORT data information.
  1169. */
  1170. ret = platform_device_add_data(pdev, &node, sizeof(node));
  1171. if (ret)
  1172. goto dev_put;
  1173. /*
  1174. * We expect the dma masks to be equivalent for
  1175. * all SMMUs set-ups
  1176. */
  1177. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  1178. fwnode = iort_get_fwnode(node);
  1179. if (!fwnode) {
  1180. ret = -ENODEV;
  1181. goto dev_put;
  1182. }
  1183. pdev->dev.fwnode = fwnode;
  1184. attr = ops->dev_is_coherent && ops->dev_is_coherent(node) ?
  1185. DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
  1186. /* Configure DMA for the page table walker */
  1187. acpi_dma_configure(&pdev->dev, attr);
  1188. iort_set_device_domain(&pdev->dev, node);
  1189. ret = platform_device_add(pdev);
  1190. if (ret)
  1191. goto dma_deconfigure;
  1192. return 0;
  1193. dma_deconfigure:
  1194. acpi_dma_deconfigure(&pdev->dev);
  1195. dev_put:
  1196. platform_device_put(pdev);
  1197. return ret;
  1198. }
  1199. static bool __init iort_enable_acs(struct acpi_iort_node *iort_node)
  1200. {
  1201. if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  1202. struct acpi_iort_node *parent;
  1203. struct acpi_iort_id_mapping *map;
  1204. int i;
  1205. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
  1206. iort_node->mapping_offset);
  1207. for (i = 0; i < iort_node->mapping_count; i++, map++) {
  1208. if (!map->output_reference)
  1209. continue;
  1210. parent = ACPI_ADD_PTR(struct acpi_iort_node,
  1211. iort_table, map->output_reference);
  1212. /*
  1213. * If we detect a RC->SMMU mapping, make sure
  1214. * we enable ACS on the system.
  1215. */
  1216. if ((parent->type == ACPI_IORT_NODE_SMMU) ||
  1217. (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
  1218. pci_request_acs();
  1219. return true;
  1220. }
  1221. }
  1222. }
  1223. return false;
  1224. }
  1225. static void __init iort_init_platform_devices(void)
  1226. {
  1227. struct acpi_iort_node *iort_node, *iort_end;
  1228. struct acpi_table_iort *iort;
  1229. struct fwnode_handle *fwnode;
  1230. int i, ret;
  1231. bool acs_enabled = false;
  1232. const struct iort_dev_config *ops;
  1233. /*
  1234. * iort_table and iort both point to the start of IORT table, but
  1235. * have different struct types
  1236. */
  1237. iort = (struct acpi_table_iort *)iort_table;
  1238. /* Get the first IORT node */
  1239. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  1240. iort->node_offset);
  1241. iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  1242. iort_table->length);
  1243. for (i = 0; i < iort->node_count; i++) {
  1244. if (iort_node >= iort_end) {
  1245. pr_err("iort node pointer overflows, bad table\n");
  1246. return;
  1247. }
  1248. if (!acs_enabled)
  1249. acs_enabled = iort_enable_acs(iort_node);
  1250. ops = iort_get_dev_cfg(iort_node);
  1251. if (ops) {
  1252. fwnode = acpi_alloc_fwnode_static();
  1253. if (!fwnode)
  1254. return;
  1255. iort_set_fwnode(iort_node, fwnode);
  1256. ret = iort_add_platform_device(iort_node, ops);
  1257. if (ret) {
  1258. iort_delete_fwnode(iort_node);
  1259. acpi_free_fwnode_static(fwnode);
  1260. return;
  1261. }
  1262. }
  1263. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
  1264. iort_node->length);
  1265. }
  1266. }
  1267. void __init acpi_iort_init(void)
  1268. {
  1269. acpi_status status;
  1270. status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
  1271. if (ACPI_FAILURE(status)) {
  1272. if (status != AE_NOT_FOUND) {
  1273. const char *msg = acpi_format_exception(status);
  1274. pr_err("Failed to get table, %s\n", msg);
  1275. }
  1276. return;
  1277. }
  1278. iort_init_platform_devices();
  1279. }