dmar.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. #undef PREFIX
  34. #define PREFIX "DMAR:"
  35. /* No locks are needed as DMA remapping hardware unit
  36. * list is constructed at boot time and hotplug of
  37. * these units are not supported by the architecture.
  38. */
  39. LIST_HEAD(dmar_drhd_units);
  40. static struct acpi_table_header * __initdata dmar_tbl;
  41. static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
  42. {
  43. /*
  44. * add INCLUDE_ALL at the tail, so scan the list will find it at
  45. * the very end.
  46. */
  47. if (drhd->include_all)
  48. list_add_tail(&drhd->list, &dmar_drhd_units);
  49. else
  50. list_add(&drhd->list, &dmar_drhd_units);
  51. }
  52. static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope,
  53. struct pci_dev **dev, u16 segment)
  54. {
  55. struct pci_bus *bus;
  56. struct pci_dev *pdev = NULL;
  57. struct acpi_dmar_pci_path *path;
  58. int count;
  59. bus = pci_find_bus(segment, scope->bus);
  60. path = (struct acpi_dmar_pci_path *)(scope + 1);
  61. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  62. / sizeof(struct acpi_dmar_pci_path);
  63. while (count) {
  64. if (pdev)
  65. pci_dev_put(pdev);
  66. /*
  67. * Some BIOSes list non-exist devices in DMAR table, just
  68. * ignore it
  69. */
  70. if (!bus) {
  71. printk(KERN_WARNING
  72. PREFIX "Device scope bus [%d] not found\n",
  73. scope->bus);
  74. break;
  75. }
  76. pdev = pci_get_slot(bus, PCI_DEVFN(path->dev, path->fn));
  77. if (!pdev) {
  78. printk(KERN_WARNING PREFIX
  79. "Device scope device [%04x:%02x:%02x.%02x] not found\n",
  80. segment, bus->number, path->dev, path->fn);
  81. break;
  82. }
  83. path ++;
  84. count --;
  85. bus = pdev->subordinate;
  86. }
  87. if (!pdev) {
  88. printk(KERN_WARNING PREFIX
  89. "Device scope device [%04x:%02x:%02x.%02x] not found\n",
  90. segment, scope->bus, path->dev, path->fn);
  91. *dev = NULL;
  92. return 0;
  93. }
  94. if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \
  95. pdev->subordinate) || (scope->entry_type == \
  96. ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) {
  97. pci_dev_put(pdev);
  98. printk(KERN_WARNING PREFIX
  99. "Device scope type does not match for %s\n",
  100. pci_name(pdev));
  101. return -EINVAL;
  102. }
  103. *dev = pdev;
  104. return 0;
  105. }
  106. static int __init dmar_parse_dev_scope(void *start, void *end, int *cnt,
  107. struct pci_dev ***devices, u16 segment)
  108. {
  109. struct acpi_dmar_device_scope *scope;
  110. void * tmp = start;
  111. int index;
  112. int ret;
  113. *cnt = 0;
  114. while (start < end) {
  115. scope = start;
  116. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
  117. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
  118. (*cnt)++;
  119. else
  120. printk(KERN_WARNING PREFIX
  121. "Unsupported device scope\n");
  122. start += scope->length;
  123. }
  124. if (*cnt == 0)
  125. return 0;
  126. *devices = kcalloc(*cnt, sizeof(struct pci_dev *), GFP_KERNEL);
  127. if (!*devices)
  128. return -ENOMEM;
  129. start = tmp;
  130. index = 0;
  131. while (start < end) {
  132. scope = start;
  133. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
  134. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) {
  135. ret = dmar_parse_one_dev_scope(scope,
  136. &(*devices)[index], segment);
  137. if (ret) {
  138. kfree(*devices);
  139. return ret;
  140. }
  141. index ++;
  142. }
  143. start += scope->length;
  144. }
  145. return 0;
  146. }
  147. /**
  148. * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
  149. * structure which uniquely represent one DMA remapping hardware unit
  150. * present in the platform
  151. */
  152. static int __init
  153. dmar_parse_one_drhd(struct acpi_dmar_header *header)
  154. {
  155. struct acpi_dmar_hardware_unit *drhd;
  156. struct dmar_drhd_unit *dmaru;
  157. int ret = 0;
  158. dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
  159. if (!dmaru)
  160. return -ENOMEM;
  161. dmaru->hdr = header;
  162. drhd = (struct acpi_dmar_hardware_unit *)header;
  163. dmaru->reg_base_addr = drhd->address;
  164. dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
  165. ret = alloc_iommu(dmaru);
  166. if (ret) {
  167. kfree(dmaru);
  168. return ret;
  169. }
  170. dmar_register_drhd_unit(dmaru);
  171. return 0;
  172. }
  173. static int __init dmar_parse_dev(struct dmar_drhd_unit *dmaru)
  174. {
  175. struct acpi_dmar_hardware_unit *drhd;
  176. int ret = 0;
  177. drhd = (struct acpi_dmar_hardware_unit *) dmaru->hdr;
  178. if (dmaru->include_all)
  179. return 0;
  180. ret = dmar_parse_dev_scope((void *)(drhd + 1),
  181. ((void *)drhd) + drhd->header.length,
  182. &dmaru->devices_cnt, &dmaru->devices,
  183. drhd->segment);
  184. if (ret) {
  185. list_del(&dmaru->list);
  186. kfree(dmaru);
  187. }
  188. return ret;
  189. }
  190. #ifdef CONFIG_DMAR
  191. LIST_HEAD(dmar_rmrr_units);
  192. static void __init dmar_register_rmrr_unit(struct dmar_rmrr_unit *rmrr)
  193. {
  194. list_add(&rmrr->list, &dmar_rmrr_units);
  195. }
  196. static int __init
  197. dmar_parse_one_rmrr(struct acpi_dmar_header *header)
  198. {
  199. struct acpi_dmar_reserved_memory *rmrr;
  200. struct dmar_rmrr_unit *rmrru;
  201. rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
  202. if (!rmrru)
  203. return -ENOMEM;
  204. rmrru->hdr = header;
  205. rmrr = (struct acpi_dmar_reserved_memory *)header;
  206. rmrru->base_address = rmrr->base_address;
  207. rmrru->end_address = rmrr->end_address;
  208. dmar_register_rmrr_unit(rmrru);
  209. return 0;
  210. }
  211. static int __init
  212. rmrr_parse_dev(struct dmar_rmrr_unit *rmrru)
  213. {
  214. struct acpi_dmar_reserved_memory *rmrr;
  215. int ret;
  216. rmrr = (struct acpi_dmar_reserved_memory *) rmrru->hdr;
  217. ret = dmar_parse_dev_scope((void *)(rmrr + 1),
  218. ((void *)rmrr) + rmrr->header.length,
  219. &rmrru->devices_cnt, &rmrru->devices, rmrr->segment);
  220. if (ret || (rmrru->devices_cnt == 0)) {
  221. list_del(&rmrru->list);
  222. kfree(rmrru);
  223. }
  224. return ret;
  225. }
  226. #endif
  227. static void __init
  228. dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
  229. {
  230. struct acpi_dmar_hardware_unit *drhd;
  231. struct acpi_dmar_reserved_memory *rmrr;
  232. switch (header->type) {
  233. case ACPI_DMAR_TYPE_HARDWARE_UNIT:
  234. drhd = (struct acpi_dmar_hardware_unit *)header;
  235. printk (KERN_INFO PREFIX
  236. "DRHD (flags: 0x%08x)base: 0x%016Lx\n",
  237. drhd->flags, (unsigned long long)drhd->address);
  238. break;
  239. case ACPI_DMAR_TYPE_RESERVED_MEMORY:
  240. rmrr = (struct acpi_dmar_reserved_memory *)header;
  241. printk (KERN_INFO PREFIX
  242. "RMRR base: 0x%016Lx end: 0x%016Lx\n",
  243. (unsigned long long)rmrr->base_address,
  244. (unsigned long long)rmrr->end_address);
  245. break;
  246. }
  247. }
  248. /**
  249. * dmar_table_detect - checks to see if the platform supports DMAR devices
  250. */
  251. static int __init dmar_table_detect(void)
  252. {
  253. acpi_status status = AE_OK;
  254. /* if we could find DMAR table, then there are DMAR devices */
  255. status = acpi_get_table(ACPI_SIG_DMAR, 0,
  256. (struct acpi_table_header **)&dmar_tbl);
  257. if (ACPI_SUCCESS(status) && !dmar_tbl) {
  258. printk (KERN_WARNING PREFIX "Unable to map DMAR\n");
  259. status = AE_NOT_FOUND;
  260. }
  261. return (ACPI_SUCCESS(status) ? 1 : 0);
  262. }
  263. /**
  264. * parse_dmar_table - parses the DMA reporting table
  265. */
  266. static int __init
  267. parse_dmar_table(void)
  268. {
  269. struct acpi_table_dmar *dmar;
  270. struct acpi_dmar_header *entry_header;
  271. int ret = 0;
  272. /*
  273. * Do it again, earlier dmar_tbl mapping could be mapped with
  274. * fixed map.
  275. */
  276. dmar_table_detect();
  277. dmar = (struct acpi_table_dmar *)dmar_tbl;
  278. if (!dmar)
  279. return -ENODEV;
  280. if (dmar->width < PAGE_SHIFT - 1) {
  281. printk(KERN_WARNING PREFIX "Invalid DMAR haw\n");
  282. return -EINVAL;
  283. }
  284. printk (KERN_INFO PREFIX "Host address width %d\n",
  285. dmar->width + 1);
  286. entry_header = (struct acpi_dmar_header *)(dmar + 1);
  287. while (((unsigned long)entry_header) <
  288. (((unsigned long)dmar) + dmar_tbl->length)) {
  289. /* Avoid looping forever on bad ACPI tables */
  290. if (entry_header->length == 0) {
  291. printk(KERN_WARNING PREFIX
  292. "Invalid 0-length structure\n");
  293. ret = -EINVAL;
  294. break;
  295. }
  296. dmar_table_print_dmar_entry(entry_header);
  297. switch (entry_header->type) {
  298. case ACPI_DMAR_TYPE_HARDWARE_UNIT:
  299. ret = dmar_parse_one_drhd(entry_header);
  300. break;
  301. case ACPI_DMAR_TYPE_RESERVED_MEMORY:
  302. #ifdef CONFIG_DMAR
  303. ret = dmar_parse_one_rmrr(entry_header);
  304. #endif
  305. break;
  306. default:
  307. printk(KERN_WARNING PREFIX
  308. "Unknown DMAR structure type\n");
  309. ret = 0; /* for forward compatibility */
  310. break;
  311. }
  312. if (ret)
  313. break;
  314. entry_header = ((void *)entry_header + entry_header->length);
  315. }
  316. return ret;
  317. }
  318. int dmar_pci_device_match(struct pci_dev *devices[], int cnt,
  319. struct pci_dev *dev)
  320. {
  321. int index;
  322. while (dev) {
  323. for (index = 0; index < cnt; index++)
  324. if (dev == devices[index])
  325. return 1;
  326. /* Check our parent */
  327. dev = dev->bus->self;
  328. }
  329. return 0;
  330. }
  331. struct dmar_drhd_unit *
  332. dmar_find_matched_drhd_unit(struct pci_dev *dev)
  333. {
  334. struct dmar_drhd_unit *dmaru = NULL;
  335. struct acpi_dmar_hardware_unit *drhd;
  336. list_for_each_entry(dmaru, &dmar_drhd_units, list) {
  337. drhd = container_of(dmaru->hdr,
  338. struct acpi_dmar_hardware_unit,
  339. header);
  340. if (dmaru->include_all &&
  341. drhd->segment == pci_domain_nr(dev->bus))
  342. return dmaru;
  343. if (dmar_pci_device_match(dmaru->devices,
  344. dmaru->devices_cnt, dev))
  345. return dmaru;
  346. }
  347. return NULL;
  348. }
  349. int __init dmar_dev_scope_init(void)
  350. {
  351. struct dmar_drhd_unit *drhd, *drhd_n;
  352. int ret = -ENODEV;
  353. list_for_each_entry_safe(drhd, drhd_n, &dmar_drhd_units, list) {
  354. ret = dmar_parse_dev(drhd);
  355. if (ret)
  356. return ret;
  357. }
  358. #ifdef CONFIG_DMAR
  359. {
  360. struct dmar_rmrr_unit *rmrr, *rmrr_n;
  361. list_for_each_entry_safe(rmrr, rmrr_n, &dmar_rmrr_units, list) {
  362. ret = rmrr_parse_dev(rmrr);
  363. if (ret)
  364. return ret;
  365. }
  366. }
  367. #endif
  368. return ret;
  369. }
  370. int __init dmar_table_init(void)
  371. {
  372. static int dmar_table_initialized;
  373. int ret;
  374. if (dmar_table_initialized)
  375. return 0;
  376. dmar_table_initialized = 1;
  377. ret = parse_dmar_table();
  378. if (ret) {
  379. if (ret != -ENODEV)
  380. printk(KERN_INFO PREFIX "parse DMAR table failure.\n");
  381. return ret;
  382. }
  383. if (list_empty(&dmar_drhd_units)) {
  384. printk(KERN_INFO PREFIX "No DMAR devices found\n");
  385. return -ENODEV;
  386. }
  387. #ifdef CONFIG_DMAR
  388. if (list_empty(&dmar_rmrr_units))
  389. printk(KERN_INFO PREFIX "No RMRR found\n");
  390. #endif
  391. #ifdef CONFIG_INTR_REMAP
  392. parse_ioapics_under_ir();
  393. #endif
  394. return 0;
  395. }
  396. void __init detect_intel_iommu(void)
  397. {
  398. int ret;
  399. ret = dmar_table_detect();
  400. {
  401. #ifdef CONFIG_INTR_REMAP
  402. struct acpi_table_dmar *dmar;
  403. /*
  404. * for now we will disable dma-remapping when interrupt
  405. * remapping is enabled.
  406. * When support for queued invalidation for IOTLB invalidation
  407. * is added, we will not need this any more.
  408. */
  409. dmar = (struct acpi_table_dmar *) dmar_tbl;
  410. if (ret && cpu_has_x2apic && dmar->flags & 0x1)
  411. printk(KERN_INFO
  412. "Queued invalidation will be enabled to support "
  413. "x2apic and Intr-remapping.\n");
  414. #endif
  415. #ifdef CONFIG_DMAR
  416. if (ret && !no_iommu && !iommu_detected && !swiotlb &&
  417. !dmar_disabled)
  418. iommu_detected = 1;
  419. #endif
  420. }
  421. dmar_tbl = NULL;
  422. }
  423. int alloc_iommu(struct dmar_drhd_unit *drhd)
  424. {
  425. struct intel_iommu *iommu;
  426. int map_size;
  427. u32 ver;
  428. static int iommu_allocated = 0;
  429. int agaw = 0;
  430. iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
  431. if (!iommu)
  432. return -ENOMEM;
  433. iommu->seq_id = iommu_allocated++;
  434. iommu->reg = ioremap(drhd->reg_base_addr, VTD_PAGE_SIZE);
  435. if (!iommu->reg) {
  436. printk(KERN_ERR "IOMMU: can't map the region\n");
  437. goto error;
  438. }
  439. iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
  440. iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
  441. #ifdef CONFIG_DMAR
  442. agaw = iommu_calculate_agaw(iommu);
  443. if (agaw < 0) {
  444. printk(KERN_ERR
  445. "Cannot get a valid agaw for iommu (seq_id = %d)\n",
  446. iommu->seq_id);
  447. goto error;
  448. }
  449. #endif
  450. iommu->agaw = agaw;
  451. /* the registers might be more than one page */
  452. map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
  453. cap_max_fault_reg_offset(iommu->cap));
  454. map_size = VTD_PAGE_ALIGN(map_size);
  455. if (map_size > VTD_PAGE_SIZE) {
  456. iounmap(iommu->reg);
  457. iommu->reg = ioremap(drhd->reg_base_addr, map_size);
  458. if (!iommu->reg) {
  459. printk(KERN_ERR "IOMMU: can't map the region\n");
  460. goto error;
  461. }
  462. }
  463. ver = readl(iommu->reg + DMAR_VER_REG);
  464. pr_debug("IOMMU %llx: ver %d:%d cap %llx ecap %llx\n",
  465. (unsigned long long)drhd->reg_base_addr,
  466. DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
  467. (unsigned long long)iommu->cap,
  468. (unsigned long long)iommu->ecap);
  469. spin_lock_init(&iommu->register_lock);
  470. drhd->iommu = iommu;
  471. return 0;
  472. error:
  473. kfree(iommu);
  474. return -1;
  475. }
  476. void free_iommu(struct intel_iommu *iommu)
  477. {
  478. if (!iommu)
  479. return;
  480. #ifdef CONFIG_DMAR
  481. free_dmar_iommu(iommu);
  482. #endif
  483. if (iommu->reg)
  484. iounmap(iommu->reg);
  485. kfree(iommu);
  486. }
  487. /*
  488. * Reclaim all the submitted descriptors which have completed its work.
  489. */
  490. static inline void reclaim_free_desc(struct q_inval *qi)
  491. {
  492. while (qi->desc_status[qi->free_tail] == QI_DONE) {
  493. qi->desc_status[qi->free_tail] = QI_FREE;
  494. qi->free_tail = (qi->free_tail + 1) % QI_LENGTH;
  495. qi->free_cnt++;
  496. }
  497. }
  498. static int qi_check_fault(struct intel_iommu *iommu, int index)
  499. {
  500. u32 fault;
  501. int head;
  502. struct q_inval *qi = iommu->qi;
  503. int wait_index = (index + 1) % QI_LENGTH;
  504. fault = readl(iommu->reg + DMAR_FSTS_REG);
  505. /*
  506. * If IQE happens, the head points to the descriptor associated
  507. * with the error. No new descriptors are fetched until the IQE
  508. * is cleared.
  509. */
  510. if (fault & DMA_FSTS_IQE) {
  511. head = readl(iommu->reg + DMAR_IQH_REG);
  512. if ((head >> 4) == index) {
  513. memcpy(&qi->desc[index], &qi->desc[wait_index],
  514. sizeof(struct qi_desc));
  515. __iommu_flush_cache(iommu, &qi->desc[index],
  516. sizeof(struct qi_desc));
  517. writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG);
  518. return -EINVAL;
  519. }
  520. }
  521. return 0;
  522. }
  523. /*
  524. * Submit the queued invalidation descriptor to the remapping
  525. * hardware unit and wait for its completion.
  526. */
  527. int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu)
  528. {
  529. int rc = 0;
  530. struct q_inval *qi = iommu->qi;
  531. struct qi_desc *hw, wait_desc;
  532. int wait_index, index;
  533. unsigned long flags;
  534. if (!qi)
  535. return 0;
  536. hw = qi->desc;
  537. spin_lock_irqsave(&qi->q_lock, flags);
  538. while (qi->free_cnt < 3) {
  539. spin_unlock_irqrestore(&qi->q_lock, flags);
  540. cpu_relax();
  541. spin_lock_irqsave(&qi->q_lock, flags);
  542. }
  543. index = qi->free_head;
  544. wait_index = (index + 1) % QI_LENGTH;
  545. qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE;
  546. hw[index] = *desc;
  547. wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) |
  548. QI_IWD_STATUS_WRITE | QI_IWD_TYPE;
  549. wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]);
  550. hw[wait_index] = wait_desc;
  551. __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc));
  552. __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc));
  553. qi->free_head = (qi->free_head + 2) % QI_LENGTH;
  554. qi->free_cnt -= 2;
  555. /*
  556. * update the HW tail register indicating the presence of
  557. * new descriptors.
  558. */
  559. writel(qi->free_head << 4, iommu->reg + DMAR_IQT_REG);
  560. while (qi->desc_status[wait_index] != QI_DONE) {
  561. /*
  562. * We will leave the interrupts disabled, to prevent interrupt
  563. * context to queue another cmd while a cmd is already submitted
  564. * and waiting for completion on this cpu. This is to avoid
  565. * a deadlock where the interrupt context can wait indefinitely
  566. * for free slots in the queue.
  567. */
  568. rc = qi_check_fault(iommu, index);
  569. if (rc)
  570. goto out;
  571. spin_unlock(&qi->q_lock);
  572. cpu_relax();
  573. spin_lock(&qi->q_lock);
  574. }
  575. out:
  576. qi->desc_status[index] = qi->desc_status[wait_index] = QI_DONE;
  577. reclaim_free_desc(qi);
  578. spin_unlock_irqrestore(&qi->q_lock, flags);
  579. return rc;
  580. }
  581. /*
  582. * Flush the global interrupt entry cache.
  583. */
  584. void qi_global_iec(struct intel_iommu *iommu)
  585. {
  586. struct qi_desc desc;
  587. desc.low = QI_IEC_TYPE;
  588. desc.high = 0;
  589. /* should never fail */
  590. qi_submit_sync(&desc, iommu);
  591. }
  592. int qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,
  593. u64 type, int non_present_entry_flush)
  594. {
  595. struct qi_desc desc;
  596. if (non_present_entry_flush) {
  597. if (!cap_caching_mode(iommu->cap))
  598. return 1;
  599. else
  600. did = 0;
  601. }
  602. desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did)
  603. | QI_CC_GRAN(type) | QI_CC_TYPE;
  604. desc.high = 0;
  605. return qi_submit_sync(&desc, iommu);
  606. }
  607. int qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
  608. unsigned int size_order, u64 type,
  609. int non_present_entry_flush)
  610. {
  611. u8 dw = 0, dr = 0;
  612. struct qi_desc desc;
  613. int ih = 0;
  614. if (non_present_entry_flush) {
  615. if (!cap_caching_mode(iommu->cap))
  616. return 1;
  617. else
  618. did = 0;
  619. }
  620. if (cap_write_drain(iommu->cap))
  621. dw = 1;
  622. if (cap_read_drain(iommu->cap))
  623. dr = 1;
  624. desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw)
  625. | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE;
  626. desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih)
  627. | QI_IOTLB_AM(size_order);
  628. return qi_submit_sync(&desc, iommu);
  629. }
  630. /*
  631. * Enable Queued Invalidation interface. This is a must to support
  632. * interrupt-remapping. Also used by DMA-remapping, which replaces
  633. * register based IOTLB invalidation.
  634. */
  635. int dmar_enable_qi(struct intel_iommu *iommu)
  636. {
  637. u32 cmd, sts;
  638. unsigned long flags;
  639. struct q_inval *qi;
  640. if (!ecap_qis(iommu->ecap))
  641. return -ENOENT;
  642. /*
  643. * queued invalidation is already setup and enabled.
  644. */
  645. if (iommu->qi)
  646. return 0;
  647. iommu->qi = kmalloc(sizeof(*qi), GFP_KERNEL);
  648. if (!iommu->qi)
  649. return -ENOMEM;
  650. qi = iommu->qi;
  651. qi->desc = (void *)(get_zeroed_page(GFP_KERNEL));
  652. if (!qi->desc) {
  653. kfree(qi);
  654. iommu->qi = 0;
  655. return -ENOMEM;
  656. }
  657. qi->desc_status = kmalloc(QI_LENGTH * sizeof(int), GFP_KERNEL);
  658. if (!qi->desc_status) {
  659. free_page((unsigned long) qi->desc);
  660. kfree(qi);
  661. iommu->qi = 0;
  662. return -ENOMEM;
  663. }
  664. qi->free_head = qi->free_tail = 0;
  665. qi->free_cnt = QI_LENGTH;
  666. spin_lock_init(&qi->q_lock);
  667. spin_lock_irqsave(&iommu->register_lock, flags);
  668. /* write zero to the tail reg */
  669. writel(0, iommu->reg + DMAR_IQT_REG);
  670. dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc));
  671. cmd = iommu->gcmd | DMA_GCMD_QIE;
  672. iommu->gcmd |= DMA_GCMD_QIE;
  673. writel(cmd, iommu->reg + DMAR_GCMD_REG);
  674. /* Make sure hardware complete it */
  675. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts);
  676. spin_unlock_irqrestore(&iommu->register_lock, flags);
  677. return 0;
  678. }