dmar.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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 Remapping Devices
  23. * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI
  24. * tables.
  25. *
  26. * These routines are used by both DMA-remapping and Interrupt-remapping
  27. */
  28. #include <linux/pci.h>
  29. #include <linux/dmar.h>
  30. #include <linux/iova.h>
  31. #include <linux/intel-iommu.h>
  32. #include <linux/timer.h>
  33. #include <linux/irq.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/tboot.h>
  36. #include <linux/dmi.h>
  37. #define PREFIX "DMAR: "
  38. /* No locks are needed as DMA remapping hardware unit
  39. * list is constructed at boot time and hotplug of
  40. * these units are not supported by the architecture.
  41. */
  42. LIST_HEAD(dmar_drhd_units);
  43. static struct acpi_table_header * __initdata dmar_tbl;
  44. static acpi_size dmar_tbl_size;
  45. static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
  46. {
  47. /*
  48. * add INCLUDE_ALL at the tail, so scan the list will find it at
  49. * the very end.
  50. */
  51. if (drhd->include_all)
  52. list_add_tail(&drhd->list, &dmar_drhd_units);
  53. else
  54. list_add(&drhd->list, &dmar_drhd_units);
  55. }
  56. static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope,
  57. struct pci_dev **dev, u16 segment)
  58. {
  59. struct pci_bus *bus;
  60. struct pci_dev *pdev = NULL;
  61. struct acpi_dmar_pci_path *path;
  62. int count;
  63. bus = pci_find_bus(segment, scope->bus);
  64. path = (struct acpi_dmar_pci_path *)(scope + 1);
  65. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  66. / sizeof(struct acpi_dmar_pci_path);
  67. while (count) {
  68. if (pdev)
  69. pci_dev_put(pdev);
  70. /*
  71. * Some BIOSes list non-exist devices in DMAR table, just
  72. * ignore it
  73. */
  74. if (!bus) {
  75. printk(KERN_WARNING
  76. PREFIX "Device scope bus [%d] not found\n",
  77. scope->bus);
  78. break;
  79. }
  80. pdev = pci_get_slot(bus, PCI_DEVFN(path->dev, path->fn));
  81. if (!pdev) {
  82. printk(KERN_WARNING PREFIX
  83. "Device scope device [%04x:%02x:%02x.%02x] not found\n",
  84. segment, bus->number, path->dev, path->fn);
  85. break;
  86. }
  87. path ++;
  88. count --;
  89. bus = pdev->subordinate;
  90. }
  91. if (!pdev) {
  92. printk(KERN_WARNING PREFIX
  93. "Device scope device [%04x:%02x:%02x.%02x] not found\n",
  94. segment, scope->bus, path->dev, path->fn);
  95. *dev = NULL;
  96. return 0;
  97. }
  98. if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \
  99. pdev->subordinate) || (scope->entry_type == \
  100. ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) {
  101. pci_dev_put(pdev);
  102. printk(KERN_WARNING PREFIX
  103. "Device scope type does not match for %s\n",
  104. pci_name(pdev));
  105. return -EINVAL;
  106. }
  107. *dev = pdev;
  108. return 0;
  109. }
  110. static int __init dmar_parse_dev_scope(void *start, void *end, int *cnt,
  111. struct pci_dev ***devices, u16 segment)
  112. {
  113. struct acpi_dmar_device_scope *scope;
  114. void * tmp = start;
  115. int index;
  116. int ret;
  117. *cnt = 0;
  118. while (start < end) {
  119. scope = start;
  120. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
  121. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
  122. (*cnt)++;
  123. else
  124. printk(KERN_WARNING PREFIX
  125. "Unsupported device scope\n");
  126. start += scope->length;
  127. }
  128. if (*cnt == 0)
  129. return 0;
  130. *devices = kcalloc(*cnt, sizeof(struct pci_dev *), GFP_KERNEL);
  131. if (!*devices)
  132. return -ENOMEM;
  133. start = tmp;
  134. index = 0;
  135. while (start < end) {
  136. scope = start;
  137. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
  138. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) {
  139. ret = dmar_parse_one_dev_scope(scope,
  140. &(*devices)[index], segment);
  141. if (ret) {
  142. kfree(*devices);
  143. return ret;
  144. }
  145. index ++;
  146. }
  147. start += scope->length;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
  153. * structure which uniquely represent one DMA remapping hardware unit
  154. * present in the platform
  155. */
  156. static int __init
  157. dmar_parse_one_drhd(struct acpi_dmar_header *header)
  158. {
  159. struct acpi_dmar_hardware_unit *drhd;
  160. struct dmar_drhd_unit *dmaru;
  161. int ret = 0;
  162. drhd = (struct acpi_dmar_hardware_unit *)header;
  163. dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
  164. if (!dmaru)
  165. return -ENOMEM;
  166. dmaru->hdr = header;
  167. dmaru->reg_base_addr = drhd->address;
  168. dmaru->segment = drhd->segment;
  169. dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
  170. ret = alloc_iommu(dmaru);
  171. if (ret) {
  172. kfree(dmaru);
  173. return ret;
  174. }
  175. dmar_register_drhd_unit(dmaru);
  176. return 0;
  177. }
  178. static int __init dmar_parse_dev(struct dmar_drhd_unit *dmaru)
  179. {
  180. struct acpi_dmar_hardware_unit *drhd;
  181. int ret = 0;
  182. drhd = (struct acpi_dmar_hardware_unit *) dmaru->hdr;
  183. if (dmaru->include_all)
  184. return 0;
  185. ret = dmar_parse_dev_scope((void *)(drhd + 1),
  186. ((void *)drhd) + drhd->header.length,
  187. &dmaru->devices_cnt, &dmaru->devices,
  188. drhd->segment);
  189. if (ret) {
  190. list_del(&dmaru->list);
  191. kfree(dmaru);
  192. }
  193. return ret;
  194. }
  195. #ifdef CONFIG_DMAR
  196. LIST_HEAD(dmar_rmrr_units);
  197. static void __init dmar_register_rmrr_unit(struct dmar_rmrr_unit *rmrr)
  198. {
  199. list_add(&rmrr->list, &dmar_rmrr_units);
  200. }
  201. static int __init
  202. dmar_parse_one_rmrr(struct acpi_dmar_header *header)
  203. {
  204. struct acpi_dmar_reserved_memory *rmrr;
  205. struct dmar_rmrr_unit *rmrru;
  206. rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
  207. if (!rmrru)
  208. return -ENOMEM;
  209. rmrru->hdr = header;
  210. rmrr = (struct acpi_dmar_reserved_memory *)header;
  211. rmrru->base_address = rmrr->base_address;
  212. rmrru->end_address = rmrr->end_address;
  213. dmar_register_rmrr_unit(rmrru);
  214. return 0;
  215. }
  216. static int __init
  217. rmrr_parse_dev(struct dmar_rmrr_unit *rmrru)
  218. {
  219. struct acpi_dmar_reserved_memory *rmrr;
  220. int ret;
  221. rmrr = (struct acpi_dmar_reserved_memory *) rmrru->hdr;
  222. ret = dmar_parse_dev_scope((void *)(rmrr + 1),
  223. ((void *)rmrr) + rmrr->header.length,
  224. &rmrru->devices_cnt, &rmrru->devices, rmrr->segment);
  225. if (ret || (rmrru->devices_cnt == 0)) {
  226. list_del(&rmrru->list);
  227. kfree(rmrru);
  228. }
  229. return ret;
  230. }
  231. static LIST_HEAD(dmar_atsr_units);
  232. static int __init dmar_parse_one_atsr(struct acpi_dmar_header *hdr)
  233. {
  234. struct acpi_dmar_atsr *atsr;
  235. struct dmar_atsr_unit *atsru;
  236. atsr = container_of(hdr, struct acpi_dmar_atsr, header);
  237. atsru = kzalloc(sizeof(*atsru), GFP_KERNEL);
  238. if (!atsru)
  239. return -ENOMEM;
  240. atsru->hdr = hdr;
  241. atsru->include_all = atsr->flags & 0x1;
  242. list_add(&atsru->list, &dmar_atsr_units);
  243. return 0;
  244. }
  245. static int __init atsr_parse_dev(struct dmar_atsr_unit *atsru)
  246. {
  247. int rc;
  248. struct acpi_dmar_atsr *atsr;
  249. if (atsru->include_all)
  250. return 0;
  251. atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
  252. rc = dmar_parse_dev_scope((void *)(atsr + 1),
  253. (void *)atsr + atsr->header.length,
  254. &atsru->devices_cnt, &atsru->devices,
  255. atsr->segment);
  256. if (rc || !atsru->devices_cnt) {
  257. list_del(&atsru->list);
  258. kfree(atsru);
  259. }
  260. return rc;
  261. }
  262. int dmar_find_matched_atsr_unit(struct pci_dev *dev)
  263. {
  264. int i;
  265. struct pci_bus *bus;
  266. struct acpi_dmar_atsr *atsr;
  267. struct dmar_atsr_unit *atsru;
  268. list_for_each_entry(atsru, &dmar_atsr_units, list) {
  269. atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
  270. if (atsr->segment == pci_domain_nr(dev->bus))
  271. goto found;
  272. }
  273. return 0;
  274. found:
  275. for (bus = dev->bus; bus; bus = bus->parent) {
  276. struct pci_dev *bridge = bus->self;
  277. if (!bridge || !pci_is_pcie(bridge) ||
  278. bridge->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
  279. return 0;
  280. if (bridge->pcie_type == PCI_EXP_TYPE_ROOT_PORT) {
  281. for (i = 0; i < atsru->devices_cnt; i++)
  282. if (atsru->devices[i] == bridge)
  283. return 1;
  284. break;
  285. }
  286. }
  287. if (atsru->include_all)
  288. return 1;
  289. return 0;
  290. }
  291. #endif
  292. static void __init
  293. dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
  294. {
  295. struct acpi_dmar_hardware_unit *drhd;
  296. struct acpi_dmar_reserved_memory *rmrr;
  297. struct acpi_dmar_atsr *atsr;
  298. struct acpi_dmar_rhsa *rhsa;
  299. switch (header->type) {
  300. case ACPI_DMAR_TYPE_HARDWARE_UNIT:
  301. drhd = container_of(header, struct acpi_dmar_hardware_unit,
  302. header);
  303. printk (KERN_INFO PREFIX
  304. "DRHD base: %#016Lx flags: %#x\n",
  305. (unsigned long long)drhd->address, drhd->flags);
  306. break;
  307. case ACPI_DMAR_TYPE_RESERVED_MEMORY:
  308. rmrr = container_of(header, struct acpi_dmar_reserved_memory,
  309. header);
  310. printk (KERN_INFO PREFIX
  311. "RMRR base: %#016Lx end: %#016Lx\n",
  312. (unsigned long long)rmrr->base_address,
  313. (unsigned long long)rmrr->end_address);
  314. break;
  315. case ACPI_DMAR_TYPE_ATSR:
  316. atsr = container_of(header, struct acpi_dmar_atsr, header);
  317. printk(KERN_INFO PREFIX "ATSR flags: %#x\n", atsr->flags);
  318. break;
  319. case ACPI_DMAR_HARDWARE_AFFINITY:
  320. rhsa = container_of(header, struct acpi_dmar_rhsa, header);
  321. printk(KERN_INFO PREFIX "RHSA base: %#016Lx proximity domain: %#x\n",
  322. (unsigned long long)rhsa->base_address,
  323. rhsa->proximity_domain);
  324. break;
  325. }
  326. }
  327. /**
  328. * dmar_table_detect - checks to see if the platform supports DMAR devices
  329. */
  330. static int __init dmar_table_detect(void)
  331. {
  332. acpi_status status = AE_OK;
  333. /* if we could find DMAR table, then there are DMAR devices */
  334. status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0,
  335. (struct acpi_table_header **)&dmar_tbl,
  336. &dmar_tbl_size);
  337. if (ACPI_SUCCESS(status) && !dmar_tbl) {
  338. printk (KERN_WARNING PREFIX "Unable to map DMAR\n");
  339. status = AE_NOT_FOUND;
  340. }
  341. return (ACPI_SUCCESS(status) ? 1 : 0);
  342. }
  343. /**
  344. * parse_dmar_table - parses the DMA reporting table
  345. */
  346. static int __init
  347. parse_dmar_table(void)
  348. {
  349. struct acpi_table_dmar *dmar;
  350. struct acpi_dmar_header *entry_header;
  351. int ret = 0;
  352. /*
  353. * Do it again, earlier dmar_tbl mapping could be mapped with
  354. * fixed map.
  355. */
  356. dmar_table_detect();
  357. /*
  358. * ACPI tables may not be DMA protected by tboot, so use DMAR copy
  359. * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
  360. */
  361. dmar_tbl = tboot_get_dmar_table(dmar_tbl);
  362. dmar = (struct acpi_table_dmar *)dmar_tbl;
  363. if (!dmar)
  364. return -ENODEV;
  365. if (dmar->width < PAGE_SHIFT - 1) {
  366. printk(KERN_WARNING PREFIX "Invalid DMAR haw\n");
  367. return -EINVAL;
  368. }
  369. printk (KERN_INFO PREFIX "Host address width %d\n",
  370. dmar->width + 1);
  371. entry_header = (struct acpi_dmar_header *)(dmar + 1);
  372. while (((unsigned long)entry_header) <
  373. (((unsigned long)dmar) + dmar_tbl->length)) {
  374. /* Avoid looping forever on bad ACPI tables */
  375. if (entry_header->length == 0) {
  376. printk(KERN_WARNING PREFIX
  377. "Invalid 0-length structure\n");
  378. ret = -EINVAL;
  379. break;
  380. }
  381. dmar_table_print_dmar_entry(entry_header);
  382. switch (entry_header->type) {
  383. case ACPI_DMAR_TYPE_HARDWARE_UNIT:
  384. ret = dmar_parse_one_drhd(entry_header);
  385. break;
  386. case ACPI_DMAR_TYPE_RESERVED_MEMORY:
  387. #ifdef CONFIG_DMAR
  388. ret = dmar_parse_one_rmrr(entry_header);
  389. #endif
  390. break;
  391. case ACPI_DMAR_TYPE_ATSR:
  392. #ifdef CONFIG_DMAR
  393. ret = dmar_parse_one_atsr(entry_header);
  394. #endif
  395. break;
  396. case ACPI_DMAR_HARDWARE_AFFINITY:
  397. /* We don't do anything with RHSA (yet?) */
  398. break;
  399. default:
  400. printk(KERN_WARNING PREFIX
  401. "Unknown DMAR structure type %d\n",
  402. entry_header->type);
  403. ret = 0; /* for forward compatibility */
  404. break;
  405. }
  406. if (ret)
  407. break;
  408. entry_header = ((void *)entry_header + entry_header->length);
  409. }
  410. return ret;
  411. }
  412. int dmar_pci_device_match(struct pci_dev *devices[], int cnt,
  413. struct pci_dev *dev)
  414. {
  415. int index;
  416. while (dev) {
  417. for (index = 0; index < cnt; index++)
  418. if (dev == devices[index])
  419. return 1;
  420. /* Check our parent */
  421. dev = dev->bus->self;
  422. }
  423. return 0;
  424. }
  425. struct dmar_drhd_unit *
  426. dmar_find_matched_drhd_unit(struct pci_dev *dev)
  427. {
  428. struct dmar_drhd_unit *dmaru = NULL;
  429. struct acpi_dmar_hardware_unit *drhd;
  430. list_for_each_entry(dmaru, &dmar_drhd_units, list) {
  431. drhd = container_of(dmaru->hdr,
  432. struct acpi_dmar_hardware_unit,
  433. header);
  434. if (dmaru->include_all &&
  435. drhd->segment == pci_domain_nr(dev->bus))
  436. return dmaru;
  437. if (dmar_pci_device_match(dmaru->devices,
  438. dmaru->devices_cnt, dev))
  439. return dmaru;
  440. }
  441. return NULL;
  442. }
  443. int __init dmar_dev_scope_init(void)
  444. {
  445. struct dmar_drhd_unit *drhd, *drhd_n;
  446. int ret = -ENODEV;
  447. list_for_each_entry_safe(drhd, drhd_n, &dmar_drhd_units, list) {
  448. ret = dmar_parse_dev(drhd);
  449. if (ret)
  450. return ret;
  451. }
  452. #ifdef CONFIG_DMAR
  453. {
  454. struct dmar_rmrr_unit *rmrr, *rmrr_n;
  455. struct dmar_atsr_unit *atsr, *atsr_n;
  456. list_for_each_entry_safe(rmrr, rmrr_n, &dmar_rmrr_units, list) {
  457. ret = rmrr_parse_dev(rmrr);
  458. if (ret)
  459. return ret;
  460. }
  461. list_for_each_entry_safe(atsr, atsr_n, &dmar_atsr_units, list) {
  462. ret = atsr_parse_dev(atsr);
  463. if (ret)
  464. return ret;
  465. }
  466. }
  467. #endif
  468. return ret;
  469. }
  470. int __init dmar_table_init(void)
  471. {
  472. static int dmar_table_initialized;
  473. int ret;
  474. if (dmar_table_initialized)
  475. return 0;
  476. dmar_table_initialized = 1;
  477. ret = parse_dmar_table();
  478. if (ret) {
  479. if (ret != -ENODEV)
  480. printk(KERN_INFO PREFIX "parse DMAR table failure.\n");
  481. return ret;
  482. }
  483. if (list_empty(&dmar_drhd_units)) {
  484. printk(KERN_INFO PREFIX "No DMAR devices found\n");
  485. return -ENODEV;
  486. }
  487. #ifdef CONFIG_DMAR
  488. if (list_empty(&dmar_rmrr_units))
  489. printk(KERN_INFO PREFIX "No RMRR found\n");
  490. if (list_empty(&dmar_atsr_units))
  491. printk(KERN_INFO PREFIX "No ATSR found\n");
  492. #endif
  493. return 0;
  494. }
  495. int __init check_zero_address(void)
  496. {
  497. struct acpi_table_dmar *dmar;
  498. struct acpi_dmar_header *entry_header;
  499. struct acpi_dmar_hardware_unit *drhd;
  500. dmar = (struct acpi_table_dmar *)dmar_tbl;
  501. entry_header = (struct acpi_dmar_header *)(dmar + 1);
  502. while (((unsigned long)entry_header) <
  503. (((unsigned long)dmar) + dmar_tbl->length)) {
  504. /* Avoid looping forever on bad ACPI tables */
  505. if (entry_header->length == 0) {
  506. printk(KERN_WARNING PREFIX
  507. "Invalid 0-length structure\n");
  508. return 0;
  509. }
  510. if (entry_header->type == ACPI_DMAR_TYPE_HARDWARE_UNIT) {
  511. drhd = (void *)entry_header;
  512. if (!drhd->address) {
  513. /* Promote an attitude of violence to a BIOS engineer today */
  514. WARN(1, "Your BIOS is broken; DMAR reported at address zero!\n"
  515. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  516. dmi_get_system_info(DMI_BIOS_VENDOR),
  517. dmi_get_system_info(DMI_BIOS_VERSION),
  518. dmi_get_system_info(DMI_PRODUCT_VERSION));
  519. #ifdef CONFIG_DMAR
  520. dmar_disabled = 1;
  521. #endif
  522. return 0;
  523. }
  524. break;
  525. }
  526. entry_header = ((void *)entry_header + entry_header->length);
  527. }
  528. return 1;
  529. }
  530. void __init detect_intel_iommu(void)
  531. {
  532. int ret;
  533. ret = dmar_table_detect();
  534. if (ret)
  535. ret = check_zero_address();
  536. {
  537. #ifdef CONFIG_INTR_REMAP
  538. struct acpi_table_dmar *dmar;
  539. /*
  540. * for now we will disable dma-remapping when interrupt
  541. * remapping is enabled.
  542. * When support for queued invalidation for IOTLB invalidation
  543. * is added, we will not need this any more.
  544. */
  545. dmar = (struct acpi_table_dmar *) dmar_tbl;
  546. if (ret && cpu_has_x2apic && dmar->flags & 0x1)
  547. printk(KERN_INFO
  548. "Queued invalidation will be enabled to support "
  549. "x2apic and Intr-remapping.\n");
  550. #endif
  551. #ifdef CONFIG_DMAR
  552. if (ret && !no_iommu && !iommu_detected && !dmar_disabled) {
  553. iommu_detected = 1;
  554. /* Make sure ACS will be enabled */
  555. pci_request_acs();
  556. }
  557. #endif
  558. #ifdef CONFIG_X86
  559. if (ret)
  560. x86_init.iommu.iommu_init = intel_iommu_init;
  561. #endif
  562. }
  563. early_acpi_os_unmap_memory(dmar_tbl, dmar_tbl_size);
  564. dmar_tbl = NULL;
  565. }
  566. int alloc_iommu(struct dmar_drhd_unit *drhd)
  567. {
  568. struct intel_iommu *iommu;
  569. int map_size;
  570. u32 ver;
  571. static int iommu_allocated = 0;
  572. int agaw = 0;
  573. int msagaw = 0;
  574. iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
  575. if (!iommu)
  576. return -ENOMEM;
  577. iommu->seq_id = iommu_allocated++;
  578. sprintf (iommu->name, "dmar%d", iommu->seq_id);
  579. iommu->reg = ioremap(drhd->reg_base_addr, VTD_PAGE_SIZE);
  580. if (!iommu->reg) {
  581. printk(KERN_ERR "IOMMU: can't map the region\n");
  582. goto error;
  583. }
  584. iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
  585. iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
  586. if (iommu->cap == (uint64_t)-1 && iommu->ecap == (uint64_t)-1) {
  587. /* Promote an attitude of violence to a BIOS engineer today */
  588. WARN(1, "Your BIOS is broken; DMAR reported at address %llx returns all ones!\n"
  589. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  590. drhd->reg_base_addr,
  591. dmi_get_system_info(DMI_BIOS_VENDOR),
  592. dmi_get_system_info(DMI_BIOS_VERSION),
  593. dmi_get_system_info(DMI_PRODUCT_VERSION));
  594. goto err_unmap;
  595. }
  596. #ifdef CONFIG_DMAR
  597. agaw = iommu_calculate_agaw(iommu);
  598. if (agaw < 0) {
  599. printk(KERN_ERR
  600. "Cannot get a valid agaw for iommu (seq_id = %d)\n",
  601. iommu->seq_id);
  602. goto err_unmap;
  603. }
  604. msagaw = iommu_calculate_max_sagaw(iommu);
  605. if (msagaw < 0) {
  606. printk(KERN_ERR
  607. "Cannot get a valid max agaw for iommu (seq_id = %d)\n",
  608. iommu->seq_id);
  609. goto err_unmap;
  610. }
  611. #endif
  612. iommu->agaw = agaw;
  613. iommu->msagaw = msagaw;
  614. /* the registers might be more than one page */
  615. map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
  616. cap_max_fault_reg_offset(iommu->cap));
  617. map_size = VTD_PAGE_ALIGN(map_size);
  618. if (map_size > VTD_PAGE_SIZE) {
  619. iounmap(iommu->reg);
  620. iommu->reg = ioremap(drhd->reg_base_addr, map_size);
  621. if (!iommu->reg) {
  622. printk(KERN_ERR "IOMMU: can't map the region\n");
  623. goto error;
  624. }
  625. }
  626. ver = readl(iommu->reg + DMAR_VER_REG);
  627. pr_info("IOMMU %llx: ver %d:%d cap %llx ecap %llx\n",
  628. (unsigned long long)drhd->reg_base_addr,
  629. DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
  630. (unsigned long long)iommu->cap,
  631. (unsigned long long)iommu->ecap);
  632. spin_lock_init(&iommu->register_lock);
  633. drhd->iommu = iommu;
  634. return 0;
  635. err_unmap:
  636. iounmap(iommu->reg);
  637. error:
  638. kfree(iommu);
  639. return -1;
  640. }
  641. void free_iommu(struct intel_iommu *iommu)
  642. {
  643. if (!iommu)
  644. return;
  645. #ifdef CONFIG_DMAR
  646. free_dmar_iommu(iommu);
  647. #endif
  648. if (iommu->reg)
  649. iounmap(iommu->reg);
  650. kfree(iommu);
  651. }
  652. /*
  653. * Reclaim all the submitted descriptors which have completed its work.
  654. */
  655. static inline void reclaim_free_desc(struct q_inval *qi)
  656. {
  657. while (qi->desc_status[qi->free_tail] == QI_DONE ||
  658. qi->desc_status[qi->free_tail] == QI_ABORT) {
  659. qi->desc_status[qi->free_tail] = QI_FREE;
  660. qi->free_tail = (qi->free_tail + 1) % QI_LENGTH;
  661. qi->free_cnt++;
  662. }
  663. }
  664. static int qi_check_fault(struct intel_iommu *iommu, int index)
  665. {
  666. u32 fault;
  667. int head, tail;
  668. struct q_inval *qi = iommu->qi;
  669. int wait_index = (index + 1) % QI_LENGTH;
  670. if (qi->desc_status[wait_index] == QI_ABORT)
  671. return -EAGAIN;
  672. fault = readl(iommu->reg + DMAR_FSTS_REG);
  673. /*
  674. * If IQE happens, the head points to the descriptor associated
  675. * with the error. No new descriptors are fetched until the IQE
  676. * is cleared.
  677. */
  678. if (fault & DMA_FSTS_IQE) {
  679. head = readl(iommu->reg + DMAR_IQH_REG);
  680. if ((head >> DMAR_IQ_SHIFT) == index) {
  681. printk(KERN_ERR "VT-d detected invalid descriptor: "
  682. "low=%llx, high=%llx\n",
  683. (unsigned long long)qi->desc[index].low,
  684. (unsigned long long)qi->desc[index].high);
  685. memcpy(&qi->desc[index], &qi->desc[wait_index],
  686. sizeof(struct qi_desc));
  687. __iommu_flush_cache(iommu, &qi->desc[index],
  688. sizeof(struct qi_desc));
  689. writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG);
  690. return -EINVAL;
  691. }
  692. }
  693. /*
  694. * If ITE happens, all pending wait_desc commands are aborted.
  695. * No new descriptors are fetched until the ITE is cleared.
  696. */
  697. if (fault & DMA_FSTS_ITE) {
  698. head = readl(iommu->reg + DMAR_IQH_REG);
  699. head = ((head >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
  700. head |= 1;
  701. tail = readl(iommu->reg + DMAR_IQT_REG);
  702. tail = ((tail >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
  703. writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG);
  704. do {
  705. if (qi->desc_status[head] == QI_IN_USE)
  706. qi->desc_status[head] = QI_ABORT;
  707. head = (head - 2 + QI_LENGTH) % QI_LENGTH;
  708. } while (head != tail);
  709. if (qi->desc_status[wait_index] == QI_ABORT)
  710. return -EAGAIN;
  711. }
  712. if (fault & DMA_FSTS_ICE)
  713. writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG);
  714. return 0;
  715. }
  716. /*
  717. * Submit the queued invalidation descriptor to the remapping
  718. * hardware unit and wait for its completion.
  719. */
  720. int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu)
  721. {
  722. int rc;
  723. struct q_inval *qi = iommu->qi;
  724. struct qi_desc *hw, wait_desc;
  725. int wait_index, index;
  726. unsigned long flags;
  727. if (!qi)
  728. return 0;
  729. hw = qi->desc;
  730. restart:
  731. rc = 0;
  732. spin_lock_irqsave(&qi->q_lock, flags);
  733. while (qi->free_cnt < 3) {
  734. spin_unlock_irqrestore(&qi->q_lock, flags);
  735. cpu_relax();
  736. spin_lock_irqsave(&qi->q_lock, flags);
  737. }
  738. index = qi->free_head;
  739. wait_index = (index + 1) % QI_LENGTH;
  740. qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE;
  741. hw[index] = *desc;
  742. wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) |
  743. QI_IWD_STATUS_WRITE | QI_IWD_TYPE;
  744. wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]);
  745. hw[wait_index] = wait_desc;
  746. __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc));
  747. __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc));
  748. qi->free_head = (qi->free_head + 2) % QI_LENGTH;
  749. qi->free_cnt -= 2;
  750. /*
  751. * update the HW tail register indicating the presence of
  752. * new descriptors.
  753. */
  754. writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG);
  755. while (qi->desc_status[wait_index] != QI_DONE) {
  756. /*
  757. * We will leave the interrupts disabled, to prevent interrupt
  758. * context to queue another cmd while a cmd is already submitted
  759. * and waiting for completion on this cpu. This is to avoid
  760. * a deadlock where the interrupt context can wait indefinitely
  761. * for free slots in the queue.
  762. */
  763. rc = qi_check_fault(iommu, index);
  764. if (rc)
  765. break;
  766. spin_unlock(&qi->q_lock);
  767. cpu_relax();
  768. spin_lock(&qi->q_lock);
  769. }
  770. qi->desc_status[index] = QI_DONE;
  771. reclaim_free_desc(qi);
  772. spin_unlock_irqrestore(&qi->q_lock, flags);
  773. if (rc == -EAGAIN)
  774. goto restart;
  775. return rc;
  776. }
  777. /*
  778. * Flush the global interrupt entry cache.
  779. */
  780. void qi_global_iec(struct intel_iommu *iommu)
  781. {
  782. struct qi_desc desc;
  783. desc.low = QI_IEC_TYPE;
  784. desc.high = 0;
  785. /* should never fail */
  786. qi_submit_sync(&desc, iommu);
  787. }
  788. void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,
  789. u64 type)
  790. {
  791. struct qi_desc desc;
  792. desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did)
  793. | QI_CC_GRAN(type) | QI_CC_TYPE;
  794. desc.high = 0;
  795. qi_submit_sync(&desc, iommu);
  796. }
  797. void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
  798. unsigned int size_order, u64 type)
  799. {
  800. u8 dw = 0, dr = 0;
  801. struct qi_desc desc;
  802. int ih = 0;
  803. if (cap_write_drain(iommu->cap))
  804. dw = 1;
  805. if (cap_read_drain(iommu->cap))
  806. dr = 1;
  807. desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw)
  808. | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE;
  809. desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih)
  810. | QI_IOTLB_AM(size_order);
  811. qi_submit_sync(&desc, iommu);
  812. }
  813. void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 qdep,
  814. u64 addr, unsigned mask)
  815. {
  816. struct qi_desc desc;
  817. if (mask) {
  818. BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1));
  819. addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1;
  820. desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
  821. } else
  822. desc.high = QI_DEV_IOTLB_ADDR(addr);
  823. if (qdep >= QI_DEV_IOTLB_MAX_INVS)
  824. qdep = 0;
  825. desc.low = QI_DEV_IOTLB_SID(sid) | QI_DEV_IOTLB_QDEP(qdep) |
  826. QI_DIOTLB_TYPE;
  827. qi_submit_sync(&desc, iommu);
  828. }
  829. /*
  830. * Disable Queued Invalidation interface.
  831. */
  832. void dmar_disable_qi(struct intel_iommu *iommu)
  833. {
  834. unsigned long flags;
  835. u32 sts;
  836. cycles_t start_time = get_cycles();
  837. if (!ecap_qis(iommu->ecap))
  838. return;
  839. spin_lock_irqsave(&iommu->register_lock, flags);
  840. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  841. if (!(sts & DMA_GSTS_QIES))
  842. goto end;
  843. /*
  844. * Give a chance to HW to complete the pending invalidation requests.
  845. */
  846. while ((readl(iommu->reg + DMAR_IQT_REG) !=
  847. readl(iommu->reg + DMAR_IQH_REG)) &&
  848. (DMAR_OPERATION_TIMEOUT > (get_cycles() - start_time)))
  849. cpu_relax();
  850. iommu->gcmd &= ~DMA_GCMD_QIE;
  851. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  852. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl,
  853. !(sts & DMA_GSTS_QIES), sts);
  854. end:
  855. spin_unlock_irqrestore(&iommu->register_lock, flags);
  856. }
  857. /*
  858. * Enable queued invalidation.
  859. */
  860. static void __dmar_enable_qi(struct intel_iommu *iommu)
  861. {
  862. u32 sts;
  863. unsigned long flags;
  864. struct q_inval *qi = iommu->qi;
  865. qi->free_head = qi->free_tail = 0;
  866. qi->free_cnt = QI_LENGTH;
  867. spin_lock_irqsave(&iommu->register_lock, flags);
  868. /* write zero to the tail reg */
  869. writel(0, iommu->reg + DMAR_IQT_REG);
  870. dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc));
  871. iommu->gcmd |= DMA_GCMD_QIE;
  872. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  873. /* Make sure hardware complete it */
  874. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts);
  875. spin_unlock_irqrestore(&iommu->register_lock, flags);
  876. }
  877. /*
  878. * Enable Queued Invalidation interface. This is a must to support
  879. * interrupt-remapping. Also used by DMA-remapping, which replaces
  880. * register based IOTLB invalidation.
  881. */
  882. int dmar_enable_qi(struct intel_iommu *iommu)
  883. {
  884. struct q_inval *qi;
  885. if (!ecap_qis(iommu->ecap))
  886. return -ENOENT;
  887. /*
  888. * queued invalidation is already setup and enabled.
  889. */
  890. if (iommu->qi)
  891. return 0;
  892. iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC);
  893. if (!iommu->qi)
  894. return -ENOMEM;
  895. qi = iommu->qi;
  896. qi->desc = (void *)(get_zeroed_page(GFP_ATOMIC));
  897. if (!qi->desc) {
  898. kfree(qi);
  899. iommu->qi = 0;
  900. return -ENOMEM;
  901. }
  902. qi->desc_status = kmalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC);
  903. if (!qi->desc_status) {
  904. free_page((unsigned long) qi->desc);
  905. kfree(qi);
  906. iommu->qi = 0;
  907. return -ENOMEM;
  908. }
  909. qi->free_head = qi->free_tail = 0;
  910. qi->free_cnt = QI_LENGTH;
  911. spin_lock_init(&qi->q_lock);
  912. __dmar_enable_qi(iommu);
  913. return 0;
  914. }
  915. /* iommu interrupt handling. Most stuff are MSI-like. */
  916. enum faulttype {
  917. DMA_REMAP,
  918. INTR_REMAP,
  919. UNKNOWN,
  920. };
  921. static const char *dma_remap_fault_reasons[] =
  922. {
  923. "Software",
  924. "Present bit in root entry is clear",
  925. "Present bit in context entry is clear",
  926. "Invalid context entry",
  927. "Access beyond MGAW",
  928. "PTE Write access is not set",
  929. "PTE Read access is not set",
  930. "Next page table ptr is invalid",
  931. "Root table address invalid",
  932. "Context table ptr is invalid",
  933. "non-zero reserved fields in RTP",
  934. "non-zero reserved fields in CTP",
  935. "non-zero reserved fields in PTE",
  936. };
  937. static const char *intr_remap_fault_reasons[] =
  938. {
  939. "Detected reserved fields in the decoded interrupt-remapped request",
  940. "Interrupt index exceeded the interrupt-remapping table size",
  941. "Present field in the IRTE entry is clear",
  942. "Error accessing interrupt-remapping table pointed by IRTA_REG",
  943. "Detected reserved fields in the IRTE entry",
  944. "Blocked a compatibility format interrupt request",
  945. "Blocked an interrupt request due to source-id verification failure",
  946. };
  947. #define MAX_FAULT_REASON_IDX (ARRAY_SIZE(fault_reason_strings) - 1)
  948. const char *dmar_get_fault_reason(u8 fault_reason, int *fault_type)
  949. {
  950. if (fault_reason >= 0x20 && (fault_reason <= 0x20 +
  951. ARRAY_SIZE(intr_remap_fault_reasons))) {
  952. *fault_type = INTR_REMAP;
  953. return intr_remap_fault_reasons[fault_reason - 0x20];
  954. } else if (fault_reason < ARRAY_SIZE(dma_remap_fault_reasons)) {
  955. *fault_type = DMA_REMAP;
  956. return dma_remap_fault_reasons[fault_reason];
  957. } else {
  958. *fault_type = UNKNOWN;
  959. return "Unknown";
  960. }
  961. }
  962. void dmar_msi_unmask(unsigned int irq)
  963. {
  964. struct intel_iommu *iommu = get_irq_data(irq);
  965. unsigned long flag;
  966. /* unmask it */
  967. spin_lock_irqsave(&iommu->register_lock, flag);
  968. writel(0, iommu->reg + DMAR_FECTL_REG);
  969. /* Read a reg to force flush the post write */
  970. readl(iommu->reg + DMAR_FECTL_REG);
  971. spin_unlock_irqrestore(&iommu->register_lock, flag);
  972. }
  973. void dmar_msi_mask(unsigned int irq)
  974. {
  975. unsigned long flag;
  976. struct intel_iommu *iommu = get_irq_data(irq);
  977. /* mask it */
  978. spin_lock_irqsave(&iommu->register_lock, flag);
  979. writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG);
  980. /* Read a reg to force flush the post write */
  981. readl(iommu->reg + DMAR_FECTL_REG);
  982. spin_unlock_irqrestore(&iommu->register_lock, flag);
  983. }
  984. void dmar_msi_write(int irq, struct msi_msg *msg)
  985. {
  986. struct intel_iommu *iommu = get_irq_data(irq);
  987. unsigned long flag;
  988. spin_lock_irqsave(&iommu->register_lock, flag);
  989. writel(msg->data, iommu->reg + DMAR_FEDATA_REG);
  990. writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG);
  991. writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG);
  992. spin_unlock_irqrestore(&iommu->register_lock, flag);
  993. }
  994. void dmar_msi_read(int irq, struct msi_msg *msg)
  995. {
  996. struct intel_iommu *iommu = get_irq_data(irq);
  997. unsigned long flag;
  998. spin_lock_irqsave(&iommu->register_lock, flag);
  999. msg->data = readl(iommu->reg + DMAR_FEDATA_REG);
  1000. msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG);
  1001. msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG);
  1002. spin_unlock_irqrestore(&iommu->register_lock, flag);
  1003. }
  1004. static int dmar_fault_do_one(struct intel_iommu *iommu, int type,
  1005. u8 fault_reason, u16 source_id, unsigned long long addr)
  1006. {
  1007. const char *reason;
  1008. int fault_type;
  1009. reason = dmar_get_fault_reason(fault_reason, &fault_type);
  1010. if (fault_type == INTR_REMAP)
  1011. printk(KERN_ERR "INTR-REMAP: Request device [[%02x:%02x.%d] "
  1012. "fault index %llx\n"
  1013. "INTR-REMAP:[fault reason %02d] %s\n",
  1014. (source_id >> 8), PCI_SLOT(source_id & 0xFF),
  1015. PCI_FUNC(source_id & 0xFF), addr >> 48,
  1016. fault_reason, reason);
  1017. else
  1018. printk(KERN_ERR
  1019. "DMAR:[%s] Request device [%02x:%02x.%d] "
  1020. "fault addr %llx \n"
  1021. "DMAR:[fault reason %02d] %s\n",
  1022. (type ? "DMA Read" : "DMA Write"),
  1023. (source_id >> 8), PCI_SLOT(source_id & 0xFF),
  1024. PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
  1025. return 0;
  1026. }
  1027. #define PRIMARY_FAULT_REG_LEN (16)
  1028. irqreturn_t dmar_fault(int irq, void *dev_id)
  1029. {
  1030. struct intel_iommu *iommu = dev_id;
  1031. int reg, fault_index;
  1032. u32 fault_status;
  1033. unsigned long flag;
  1034. spin_lock_irqsave(&iommu->register_lock, flag);
  1035. fault_status = readl(iommu->reg + DMAR_FSTS_REG);
  1036. if (fault_status)
  1037. printk(KERN_ERR "DRHD: handling fault status reg %x\n",
  1038. fault_status);
  1039. /* TBD: ignore advanced fault log currently */
  1040. if (!(fault_status & DMA_FSTS_PPF))
  1041. goto clear_rest;
  1042. fault_index = dma_fsts_fault_record_index(fault_status);
  1043. reg = cap_fault_reg_offset(iommu->cap);
  1044. while (1) {
  1045. u8 fault_reason;
  1046. u16 source_id;
  1047. u64 guest_addr;
  1048. int type;
  1049. u32 data;
  1050. /* highest 32 bits */
  1051. data = readl(iommu->reg + reg +
  1052. fault_index * PRIMARY_FAULT_REG_LEN + 12);
  1053. if (!(data & DMA_FRCD_F))
  1054. break;
  1055. fault_reason = dma_frcd_fault_reason(data);
  1056. type = dma_frcd_type(data);
  1057. data = readl(iommu->reg + reg +
  1058. fault_index * PRIMARY_FAULT_REG_LEN + 8);
  1059. source_id = dma_frcd_source_id(data);
  1060. guest_addr = dmar_readq(iommu->reg + reg +
  1061. fault_index * PRIMARY_FAULT_REG_LEN);
  1062. guest_addr = dma_frcd_page_addr(guest_addr);
  1063. /* clear the fault */
  1064. writel(DMA_FRCD_F, iommu->reg + reg +
  1065. fault_index * PRIMARY_FAULT_REG_LEN + 12);
  1066. spin_unlock_irqrestore(&iommu->register_lock, flag);
  1067. dmar_fault_do_one(iommu, type, fault_reason,
  1068. source_id, guest_addr);
  1069. fault_index++;
  1070. if (fault_index >= cap_num_fault_regs(iommu->cap))
  1071. fault_index = 0;
  1072. spin_lock_irqsave(&iommu->register_lock, flag);
  1073. }
  1074. clear_rest:
  1075. /* clear all the other faults */
  1076. fault_status = readl(iommu->reg + DMAR_FSTS_REG);
  1077. writel(fault_status, iommu->reg + DMAR_FSTS_REG);
  1078. spin_unlock_irqrestore(&iommu->register_lock, flag);
  1079. return IRQ_HANDLED;
  1080. }
  1081. int dmar_set_interrupt(struct intel_iommu *iommu)
  1082. {
  1083. int irq, ret;
  1084. /*
  1085. * Check if the fault interrupt is already initialized.
  1086. */
  1087. if (iommu->irq)
  1088. return 0;
  1089. irq = create_irq();
  1090. if (!irq) {
  1091. printk(KERN_ERR "IOMMU: no free vectors\n");
  1092. return -EINVAL;
  1093. }
  1094. set_irq_data(irq, iommu);
  1095. iommu->irq = irq;
  1096. ret = arch_setup_dmar_msi(irq);
  1097. if (ret) {
  1098. set_irq_data(irq, NULL);
  1099. iommu->irq = 0;
  1100. destroy_irq(irq);
  1101. return ret;
  1102. }
  1103. ret = request_irq(irq, dmar_fault, 0, iommu->name, iommu);
  1104. if (ret)
  1105. printk(KERN_ERR "IOMMU: can't request irq\n");
  1106. return ret;
  1107. }
  1108. int __init enable_drhd_fault_handling(void)
  1109. {
  1110. struct dmar_drhd_unit *drhd;
  1111. /*
  1112. * Enable fault control interrupt.
  1113. */
  1114. for_each_drhd_unit(drhd) {
  1115. int ret;
  1116. struct intel_iommu *iommu = drhd->iommu;
  1117. ret = dmar_set_interrupt(iommu);
  1118. if (ret) {
  1119. printk(KERN_ERR "DRHD %Lx: failed to enable fault, "
  1120. " interrupt, ret %d\n",
  1121. (unsigned long long)drhd->reg_base_addr, ret);
  1122. return -1;
  1123. }
  1124. }
  1125. return 0;
  1126. }
  1127. /*
  1128. * Re-enable Queued Invalidation interface.
  1129. */
  1130. int dmar_reenable_qi(struct intel_iommu *iommu)
  1131. {
  1132. if (!ecap_qis(iommu->ecap))
  1133. return -ENOENT;
  1134. if (!iommu->qi)
  1135. return -ENOENT;
  1136. /*
  1137. * First disable queued invalidation.
  1138. */
  1139. dmar_disable_qi(iommu);
  1140. /*
  1141. * Then enable queued invalidation again. Since there is no pending
  1142. * invalidation requests now, it's safe to re-enable queued
  1143. * invalidation.
  1144. */
  1145. __dmar_enable_qi(iommu);
  1146. return 0;
  1147. }
  1148. /*
  1149. * Check interrupt remapping support in DMAR table description.
  1150. */
  1151. int dmar_ir_support(void)
  1152. {
  1153. struct acpi_table_dmar *dmar;
  1154. dmar = (struct acpi_table_dmar *)dmar_tbl;
  1155. return dmar->flags & 0x1;
  1156. }