dmar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2006, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Copyright (C) 2006-2008 Intel Corporation
  18. * Author: Ashok Raj <ashok.raj@intel.com>
  19. * Author: Shaohua Li <shaohua.li@intel.com>
  20. * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  21. *
  22. * This file implements early detection/parsing of DMA Remapping Devices
  23. * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI
  24. * tables.
  25. */
  26. #include <linux/pci.h>
  27. #include <linux/dmar.h>
  28. #include "iova.h"
  29. #include "intel-iommu.h"
  30. #undef PREFIX
  31. #define PREFIX "DMAR:"
  32. /* No locks are needed as DMA remapping hardware unit
  33. * list is constructed at boot time and hotplug of
  34. * these units are not supported by the architecture.
  35. */
  36. LIST_HEAD(dmar_drhd_units);
  37. LIST_HEAD(dmar_rmrr_units);
  38. static struct acpi_table_header * __initdata dmar_tbl;
  39. static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
  40. {
  41. /*
  42. * add INCLUDE_ALL at the tail, so scan the list will find it at
  43. * the very end.
  44. */
  45. if (drhd->include_all)
  46. list_add_tail(&drhd->list, &dmar_drhd_units);
  47. else
  48. list_add(&drhd->list, &dmar_drhd_units);
  49. }
  50. static void __init dmar_register_rmrr_unit(struct dmar_rmrr_unit *rmrr)
  51. {
  52. list_add(&rmrr->list, &dmar_rmrr_units);
  53. }
  54. static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope,
  55. struct pci_dev **dev, u16 segment)
  56. {
  57. struct pci_bus *bus;
  58. struct pci_dev *pdev = NULL;
  59. struct acpi_dmar_pci_path *path;
  60. int count;
  61. bus = pci_find_bus(segment, scope->bus);
  62. path = (struct acpi_dmar_pci_path *)(scope + 1);
  63. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  64. / sizeof(struct acpi_dmar_pci_path);
  65. while (count) {
  66. if (pdev)
  67. pci_dev_put(pdev);
  68. /*
  69. * Some BIOSes list non-exist devices in DMAR table, just
  70. * ignore it
  71. */
  72. if (!bus) {
  73. printk(KERN_WARNING
  74. PREFIX "Device scope bus [%d] not found\n",
  75. scope->bus);
  76. break;
  77. }
  78. pdev = pci_get_slot(bus, PCI_DEVFN(path->dev, path->fn));
  79. if (!pdev) {
  80. printk(KERN_WARNING PREFIX
  81. "Device scope device [%04x:%02x:%02x.%02x] not found\n",
  82. segment, bus->number, path->dev, path->fn);
  83. break;
  84. }
  85. path ++;
  86. count --;
  87. bus = pdev->subordinate;
  88. }
  89. if (!pdev) {
  90. printk(KERN_WARNING PREFIX
  91. "Device scope device [%04x:%02x:%02x.%02x] not found\n",
  92. segment, scope->bus, path->dev, path->fn);
  93. *dev = NULL;
  94. return 0;
  95. }
  96. if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \
  97. pdev->subordinate) || (scope->entry_type == \
  98. ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) {
  99. pci_dev_put(pdev);
  100. printk(KERN_WARNING PREFIX
  101. "Device scope type does not match for %s\n",
  102. pci_name(pdev));
  103. return -EINVAL;
  104. }
  105. *dev = pdev;
  106. return 0;
  107. }
  108. static int __init dmar_parse_dev_scope(void *start, void *end, int *cnt,
  109. struct pci_dev ***devices, u16 segment)
  110. {
  111. struct acpi_dmar_device_scope *scope;
  112. void * tmp = start;
  113. int index;
  114. int ret;
  115. *cnt = 0;
  116. while (start < end) {
  117. scope = start;
  118. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
  119. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
  120. (*cnt)++;
  121. else
  122. printk(KERN_WARNING PREFIX
  123. "Unsupported device scope\n");
  124. start += scope->length;
  125. }
  126. if (*cnt == 0)
  127. return 0;
  128. *devices = kcalloc(*cnt, sizeof(struct pci_dev *), GFP_KERNEL);
  129. if (!*devices)
  130. return -ENOMEM;
  131. start = tmp;
  132. index = 0;
  133. while (start < end) {
  134. scope = start;
  135. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
  136. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) {
  137. ret = dmar_parse_one_dev_scope(scope,
  138. &(*devices)[index], segment);
  139. if (ret) {
  140. kfree(*devices);
  141. return ret;
  142. }
  143. index ++;
  144. }
  145. start += scope->length;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
  151. * structure which uniquely represent one DMA remapping hardware unit
  152. * present in the platform
  153. */
  154. static int __init
  155. dmar_parse_one_drhd(struct acpi_dmar_header *header)
  156. {
  157. struct acpi_dmar_hardware_unit *drhd;
  158. struct dmar_drhd_unit *dmaru;
  159. int ret = 0;
  160. static int include_all;
  161. dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
  162. if (!dmaru)
  163. return -ENOMEM;
  164. drhd = (struct acpi_dmar_hardware_unit *)header;
  165. dmaru->reg_base_addr = drhd->address;
  166. dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
  167. if (!dmaru->include_all)
  168. ret = dmar_parse_dev_scope((void *)(drhd + 1),
  169. ((void *)drhd) + header->length,
  170. &dmaru->devices_cnt, &dmaru->devices,
  171. drhd->segment);
  172. else {
  173. /* Only allow one INCLUDE_ALL */
  174. if (include_all) {
  175. printk(KERN_WARNING PREFIX "Only one INCLUDE_ALL "
  176. "device scope is allowed\n");
  177. ret = -EINVAL;
  178. }
  179. include_all = 1;
  180. }
  181. if (ret || (dmaru->devices_cnt == 0 && !dmaru->include_all))
  182. kfree(dmaru);
  183. else
  184. dmar_register_drhd_unit(dmaru);
  185. return ret;
  186. }
  187. static int __init
  188. dmar_parse_one_rmrr(struct acpi_dmar_header *header)
  189. {
  190. struct acpi_dmar_reserved_memory *rmrr;
  191. struct dmar_rmrr_unit *rmrru;
  192. int ret = 0;
  193. rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
  194. if (!rmrru)
  195. return -ENOMEM;
  196. rmrr = (struct acpi_dmar_reserved_memory *)header;
  197. rmrru->base_address = rmrr->base_address;
  198. rmrru->end_address = rmrr->end_address;
  199. ret = dmar_parse_dev_scope((void *)(rmrr + 1),
  200. ((void *)rmrr) + header->length,
  201. &rmrru->devices_cnt, &rmrru->devices, rmrr->segment);
  202. if (ret || (rmrru->devices_cnt == 0))
  203. kfree(rmrru);
  204. else
  205. dmar_register_rmrr_unit(rmrru);
  206. return ret;
  207. }
  208. static void __init
  209. dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
  210. {
  211. struct acpi_dmar_hardware_unit *drhd;
  212. struct acpi_dmar_reserved_memory *rmrr;
  213. switch (header->type) {
  214. case ACPI_DMAR_TYPE_HARDWARE_UNIT:
  215. drhd = (struct acpi_dmar_hardware_unit *)header;
  216. printk (KERN_INFO PREFIX
  217. "DRHD (flags: 0x%08x)base: 0x%016Lx\n",
  218. drhd->flags, drhd->address);
  219. break;
  220. case ACPI_DMAR_TYPE_RESERVED_MEMORY:
  221. rmrr = (struct acpi_dmar_reserved_memory *)header;
  222. printk (KERN_INFO PREFIX
  223. "RMRR base: 0x%016Lx end: 0x%016Lx\n",
  224. rmrr->base_address, rmrr->end_address);
  225. break;
  226. }
  227. }
  228. /**
  229. * parse_dmar_table - parses the DMA reporting table
  230. */
  231. static int __init
  232. parse_dmar_table(void)
  233. {
  234. struct acpi_table_dmar *dmar;
  235. struct acpi_dmar_header *entry_header;
  236. int ret = 0;
  237. dmar = (struct acpi_table_dmar *)dmar_tbl;
  238. if (!dmar)
  239. return -ENODEV;
  240. if (dmar->width < PAGE_SHIFT_4K - 1) {
  241. printk(KERN_WARNING PREFIX "Invalid DMAR haw\n");
  242. return -EINVAL;
  243. }
  244. printk (KERN_INFO PREFIX "Host address width %d\n",
  245. dmar->width + 1);
  246. entry_header = (struct acpi_dmar_header *)(dmar + 1);
  247. while (((unsigned long)entry_header) <
  248. (((unsigned long)dmar) + dmar_tbl->length)) {
  249. dmar_table_print_dmar_entry(entry_header);
  250. switch (entry_header->type) {
  251. case ACPI_DMAR_TYPE_HARDWARE_UNIT:
  252. ret = dmar_parse_one_drhd(entry_header);
  253. break;
  254. case ACPI_DMAR_TYPE_RESERVED_MEMORY:
  255. ret = dmar_parse_one_rmrr(entry_header);
  256. break;
  257. default:
  258. printk(KERN_WARNING PREFIX
  259. "Unknown DMAR structure type\n");
  260. ret = 0; /* for forward compatibility */
  261. break;
  262. }
  263. if (ret)
  264. break;
  265. entry_header = ((void *)entry_header + entry_header->length);
  266. }
  267. return ret;
  268. }
  269. int __init dmar_table_init(void)
  270. {
  271. int ret;
  272. ret = parse_dmar_table();
  273. if (ret) {
  274. printk(KERN_INFO PREFIX "parse DMAR table failure.\n");
  275. return ret;
  276. }
  277. if (list_empty(&dmar_drhd_units)) {
  278. printk(KERN_INFO PREFIX "No DMAR devices found\n");
  279. return -ENODEV;
  280. }
  281. if (list_empty(&dmar_rmrr_units))
  282. printk(KERN_INFO PREFIX "No RMRR found\n");
  283. return 0;
  284. }
  285. /**
  286. * early_dmar_detect - checks to see if the platform supports DMAR devices
  287. */
  288. int __init early_dmar_detect(void)
  289. {
  290. acpi_status status = AE_OK;
  291. /* if we could find DMAR table, then there are DMAR devices */
  292. status = acpi_get_table(ACPI_SIG_DMAR, 0,
  293. (struct acpi_table_header **)&dmar_tbl);
  294. if (ACPI_SUCCESS(status) && !dmar_tbl) {
  295. printk (KERN_WARNING PREFIX "Unable to map DMAR\n");
  296. status = AE_NOT_FOUND;
  297. }
  298. return (ACPI_SUCCESS(status) ? 1 : 0);
  299. }