dmar.c 8.2 KB

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