dmar.c 34 KB

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