iort.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/kernel.h>
  21. #include <linux/pci.h>
  22. struct iort_its_msi_chip {
  23. struct list_head list;
  24. struct fwnode_handle *fw_node;
  25. u32 translation_id;
  26. };
  27. typedef acpi_status (*iort_find_node_callback)
  28. (struct acpi_iort_node *node, void *context);
  29. /* Root pointer to the mapped IORT table */
  30. static struct acpi_table_header *iort_table;
  31. static LIST_HEAD(iort_msi_chip_list);
  32. static DEFINE_SPINLOCK(iort_msi_chip_lock);
  33. /**
  34. * iort_register_domain_token() - register domain token and related ITS ID
  35. * to the list from where we can get it back later on.
  36. * @trans_id: ITS ID.
  37. * @fw_node: Domain token.
  38. *
  39. * Returns: 0 on success, -ENOMEM if no memory when allocating list element
  40. */
  41. int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node)
  42. {
  43. struct iort_its_msi_chip *its_msi_chip;
  44. its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
  45. if (!its_msi_chip)
  46. return -ENOMEM;
  47. its_msi_chip->fw_node = fw_node;
  48. its_msi_chip->translation_id = trans_id;
  49. spin_lock(&iort_msi_chip_lock);
  50. list_add(&its_msi_chip->list, &iort_msi_chip_list);
  51. spin_unlock(&iort_msi_chip_lock);
  52. return 0;
  53. }
  54. /**
  55. * iort_deregister_domain_token() - Deregister domain token based on ITS ID
  56. * @trans_id: ITS ID.
  57. *
  58. * Returns: none.
  59. */
  60. void iort_deregister_domain_token(int trans_id)
  61. {
  62. struct iort_its_msi_chip *its_msi_chip, *t;
  63. spin_lock(&iort_msi_chip_lock);
  64. list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
  65. if (its_msi_chip->translation_id == trans_id) {
  66. list_del(&its_msi_chip->list);
  67. kfree(its_msi_chip);
  68. break;
  69. }
  70. }
  71. spin_unlock(&iort_msi_chip_lock);
  72. }
  73. /**
  74. * iort_find_domain_token() - Find domain token based on given ITS ID
  75. * @trans_id: ITS ID.
  76. *
  77. * Returns: domain token when find on the list, NULL otherwise
  78. */
  79. struct fwnode_handle *iort_find_domain_token(int trans_id)
  80. {
  81. struct fwnode_handle *fw_node = NULL;
  82. struct iort_its_msi_chip *its_msi_chip;
  83. spin_lock(&iort_msi_chip_lock);
  84. list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
  85. if (its_msi_chip->translation_id == trans_id) {
  86. fw_node = its_msi_chip->fw_node;
  87. break;
  88. }
  89. }
  90. spin_unlock(&iort_msi_chip_lock);
  91. return fw_node;
  92. }
  93. static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
  94. iort_find_node_callback callback,
  95. void *context)
  96. {
  97. struct acpi_iort_node *iort_node, *iort_end;
  98. struct acpi_table_iort *iort;
  99. int i;
  100. if (!iort_table)
  101. return NULL;
  102. /* Get the first IORT node */
  103. iort = (struct acpi_table_iort *)iort_table;
  104. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
  105. iort->node_offset);
  106. iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  107. iort_table->length);
  108. for (i = 0; i < iort->node_count; i++) {
  109. if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
  110. "IORT node pointer overflows, bad table!\n"))
  111. return NULL;
  112. if (iort_node->type == type &&
  113. ACPI_SUCCESS(callback(iort_node, context)))
  114. return iort_node;
  115. iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
  116. iort_node->length);
  117. }
  118. return NULL;
  119. }
  120. static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
  121. void *context)
  122. {
  123. struct device *dev = context;
  124. acpi_status status;
  125. if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
  126. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  127. struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
  128. struct acpi_iort_named_component *ncomp;
  129. if (!adev) {
  130. status = AE_NOT_FOUND;
  131. goto out;
  132. }
  133. status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
  134. if (ACPI_FAILURE(status)) {
  135. dev_warn(dev, "Can't get device full path name\n");
  136. goto out;
  137. }
  138. ncomp = (struct acpi_iort_named_component *)node->node_data;
  139. status = !strcmp(ncomp->device_name, buf.pointer) ?
  140. AE_OK : AE_NOT_FOUND;
  141. acpi_os_free(buf.pointer);
  142. } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  143. struct acpi_iort_root_complex *pci_rc;
  144. struct pci_bus *bus;
  145. bus = to_pci_bus(dev);
  146. pci_rc = (struct acpi_iort_root_complex *)node->node_data;
  147. /*
  148. * It is assumed that PCI segment numbers maps one-to-one
  149. * with root complexes. Each segment number can represent only
  150. * one root complex.
  151. */
  152. status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
  153. AE_OK : AE_NOT_FOUND;
  154. } else {
  155. status = AE_NOT_FOUND;
  156. }
  157. out:
  158. return status;
  159. }
  160. static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
  161. u32 *rid_out)
  162. {
  163. /* Single mapping does not care for input id */
  164. if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
  165. if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
  166. type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
  167. *rid_out = map->output_base;
  168. return 0;
  169. }
  170. pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
  171. map, type);
  172. return -ENXIO;
  173. }
  174. if (rid_in < map->input_base ||
  175. (rid_in >= map->input_base + map->id_count))
  176. return -ENXIO;
  177. *rid_out = map->output_base + (rid_in - map->input_base);
  178. return 0;
  179. }
  180. static struct acpi_iort_node *iort_node_map_rid(struct acpi_iort_node *node,
  181. u32 rid_in, u32 *rid_out,
  182. u8 type)
  183. {
  184. u32 rid = rid_in;
  185. /* Parse the ID mapping tree to find specified node type */
  186. while (node) {
  187. struct acpi_iort_id_mapping *map;
  188. int i;
  189. if (node->type == type) {
  190. if (rid_out)
  191. *rid_out = rid;
  192. return node;
  193. }
  194. if (!node->mapping_offset || !node->mapping_count)
  195. goto fail_map;
  196. map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
  197. node->mapping_offset);
  198. /* Firmware bug! */
  199. if (!map->output_reference) {
  200. pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
  201. node, node->type);
  202. goto fail_map;
  203. }
  204. /* Do the RID translation */
  205. for (i = 0; i < node->mapping_count; i++, map++) {
  206. if (!iort_id_map(map, node->type, rid, &rid))
  207. break;
  208. }
  209. if (i == node->mapping_count)
  210. goto fail_map;
  211. node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
  212. map->output_reference);
  213. }
  214. fail_map:
  215. /* Map input RID to output RID unchanged on mapping failure*/
  216. if (rid_out)
  217. *rid_out = rid_in;
  218. return NULL;
  219. }
  220. static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
  221. {
  222. struct pci_bus *pbus;
  223. if (!dev_is_pci(dev))
  224. return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
  225. iort_match_node_callback, dev);
  226. /* Find a PCI root bus */
  227. pbus = to_pci_dev(dev)->bus;
  228. while (!pci_is_root_bus(pbus))
  229. pbus = pbus->parent;
  230. return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
  231. iort_match_node_callback, &pbus->dev);
  232. }
  233. /**
  234. * iort_msi_map_rid() - Map a MSI requester ID for a device
  235. * @dev: The device for which the mapping is to be done.
  236. * @req_id: The device requester ID.
  237. *
  238. * Returns: mapped MSI RID on success, input requester ID otherwise
  239. */
  240. u32 iort_msi_map_rid(struct device *dev, u32 req_id)
  241. {
  242. struct acpi_iort_node *node;
  243. u32 dev_id;
  244. node = iort_find_dev_node(dev);
  245. if (!node)
  246. return req_id;
  247. iort_node_map_rid(node, req_id, &dev_id, ACPI_IORT_NODE_ITS_GROUP);
  248. return dev_id;
  249. }
  250. /**
  251. * iort_dev_find_its_id() - Find the ITS identifier for a device
  252. * @dev: The device.
  253. * @idx: Index of the ITS identifier list.
  254. * @its_id: ITS identifier.
  255. *
  256. * Returns: 0 on success, appropriate error value otherwise
  257. */
  258. static int iort_dev_find_its_id(struct device *dev, u32 req_id,
  259. unsigned int idx, int *its_id)
  260. {
  261. struct acpi_iort_its_group *its;
  262. struct acpi_iort_node *node;
  263. node = iort_find_dev_node(dev);
  264. if (!node)
  265. return -ENXIO;
  266. node = iort_node_map_rid(node, req_id, NULL, ACPI_IORT_NODE_ITS_GROUP);
  267. if (!node)
  268. return -ENXIO;
  269. /* Move to ITS specific data */
  270. its = (struct acpi_iort_its_group *)node->node_data;
  271. if (idx > its->its_count) {
  272. dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
  273. idx, its->its_count);
  274. return -ENXIO;
  275. }
  276. *its_id = its->identifiers[idx];
  277. return 0;
  278. }
  279. /**
  280. * iort_get_device_domain() - Find MSI domain related to a device
  281. * @dev: The device.
  282. * @req_id: Requester ID for the device.
  283. *
  284. * Returns: the MSI domain for this device, NULL otherwise
  285. */
  286. struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
  287. {
  288. struct fwnode_handle *handle;
  289. int its_id;
  290. if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
  291. return NULL;
  292. handle = iort_find_domain_token(its_id);
  293. if (!handle)
  294. return NULL;
  295. return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
  296. }
  297. void __init acpi_iort_init(void)
  298. {
  299. acpi_status status;
  300. status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
  301. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  302. const char *msg = acpi_format_exception(status);
  303. pr_err("Failed to get table, %s\n", msg);
  304. }
  305. }