dmar.c 8.4 KB

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