iort.c 55 KB

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