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