amd_iommu.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/gfp.h>
  21. #include <linux/bitops.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/iommu-helper.h>
  24. #include <asm/proto.h>
  25. #include <asm/iommu.h>
  26. #include <asm/gart.h>
  27. #include <asm/amd_iommu_types.h>
  28. #include <asm/amd_iommu.h>
  29. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  30. #define EXIT_LOOP_COUNT 10000000
  31. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  32. /* A list of preallocated protection domains */
  33. static LIST_HEAD(iommu_pd_list);
  34. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  35. /*
  36. * general struct to manage commands send to an IOMMU
  37. */
  38. struct iommu_cmd {
  39. u32 data[4];
  40. };
  41. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  42. struct unity_map_entry *e);
  43. /* returns !0 if the IOMMU is caching non-present entries in its TLB */
  44. static int iommu_has_npcache(struct amd_iommu *iommu)
  45. {
  46. return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
  47. }
  48. /****************************************************************************
  49. *
  50. * Interrupt handling functions
  51. *
  52. ****************************************************************************/
  53. static void iommu_print_event(void *__evt)
  54. {
  55. u32 *event = __evt;
  56. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  57. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  58. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  59. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  60. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  61. printk(KERN_ERR "AMD IOMMU: Event logged [");
  62. switch (type) {
  63. case EVENT_TYPE_ILL_DEV:
  64. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  65. "address=0x%016llx flags=0x%04x]\n",
  66. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  67. address, flags);
  68. break;
  69. case EVENT_TYPE_IO_FAULT:
  70. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  71. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  72. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  73. domid, address, flags);
  74. break;
  75. case EVENT_TYPE_DEV_TAB_ERR:
  76. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  77. "address=0x%016llx flags=0x%04x]\n",
  78. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  79. address, flags);
  80. break;
  81. case EVENT_TYPE_PAGE_TAB_ERR:
  82. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  83. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  84. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  85. domid, address, flags);
  86. break;
  87. case EVENT_TYPE_ILL_CMD:
  88. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  89. break;
  90. case EVENT_TYPE_CMD_HARD_ERR:
  91. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  92. "flags=0x%04x]\n", address, flags);
  93. break;
  94. case EVENT_TYPE_IOTLB_INV_TO:
  95. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  96. "address=0x%016llx]\n",
  97. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  98. address);
  99. break;
  100. case EVENT_TYPE_INV_DEV_REQ:
  101. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  102. "address=0x%016llx flags=0x%04x]\n",
  103. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  104. address, flags);
  105. break;
  106. default:
  107. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  108. }
  109. }
  110. static void iommu_poll_events(struct amd_iommu *iommu)
  111. {
  112. u32 head, tail;
  113. unsigned long flags;
  114. spin_lock_irqsave(&iommu->lock, flags);
  115. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  116. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  117. while (head != tail) {
  118. iommu_print_event(iommu->evt_buf + head);
  119. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  120. }
  121. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  122. spin_unlock_irqrestore(&iommu->lock, flags);
  123. }
  124. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  125. {
  126. struct amd_iommu *iommu;
  127. list_for_each_entry(iommu, &amd_iommu_list, list)
  128. iommu_poll_events(iommu);
  129. return IRQ_HANDLED;
  130. }
  131. /****************************************************************************
  132. *
  133. * IOMMU command queuing functions
  134. *
  135. ****************************************************************************/
  136. /*
  137. * Writes the command to the IOMMUs command buffer and informs the
  138. * hardware about the new command. Must be called with iommu->lock held.
  139. */
  140. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  141. {
  142. u32 tail, head;
  143. u8 *target;
  144. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  145. target = iommu->cmd_buf + tail;
  146. memcpy_toio(target, cmd, sizeof(*cmd));
  147. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  148. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  149. if (tail == head)
  150. return -ENOMEM;
  151. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  152. return 0;
  153. }
  154. /*
  155. * General queuing function for commands. Takes iommu->lock and calls
  156. * __iommu_queue_command().
  157. */
  158. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  159. {
  160. unsigned long flags;
  161. int ret;
  162. spin_lock_irqsave(&iommu->lock, flags);
  163. ret = __iommu_queue_command(iommu, cmd);
  164. if (!ret)
  165. iommu->need_sync = 1;
  166. spin_unlock_irqrestore(&iommu->lock, flags);
  167. return ret;
  168. }
  169. /*
  170. * This function is called whenever we need to ensure that the IOMMU has
  171. * completed execution of all commands we sent. It sends a
  172. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  173. * us about that by writing a value to a physical address we pass with
  174. * the command.
  175. */
  176. static int iommu_completion_wait(struct amd_iommu *iommu)
  177. {
  178. int ret = 0, ready = 0;
  179. unsigned status = 0;
  180. struct iommu_cmd cmd;
  181. unsigned long flags, i = 0;
  182. memset(&cmd, 0, sizeof(cmd));
  183. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  184. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  185. spin_lock_irqsave(&iommu->lock, flags);
  186. if (!iommu->need_sync)
  187. goto out;
  188. iommu->need_sync = 0;
  189. ret = __iommu_queue_command(iommu, &cmd);
  190. if (ret)
  191. goto out;
  192. while (!ready && (i < EXIT_LOOP_COUNT)) {
  193. ++i;
  194. /* wait for the bit to become one */
  195. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  196. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  197. }
  198. /* set bit back to zero */
  199. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  200. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  201. if (unlikely(i == EXIT_LOOP_COUNT))
  202. panic("AMD IOMMU: Completion wait loop failed\n");
  203. out:
  204. spin_unlock_irqrestore(&iommu->lock, flags);
  205. return 0;
  206. }
  207. /*
  208. * Command send function for invalidating a device table entry
  209. */
  210. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  211. {
  212. struct iommu_cmd cmd;
  213. int ret;
  214. BUG_ON(iommu == NULL);
  215. memset(&cmd, 0, sizeof(cmd));
  216. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  217. cmd.data[0] = devid;
  218. ret = iommu_queue_command(iommu, &cmd);
  219. return ret;
  220. }
  221. /*
  222. * Generic command send function for invalidaing TLB entries
  223. */
  224. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  225. u64 address, u16 domid, int pde, int s)
  226. {
  227. struct iommu_cmd cmd;
  228. int ret;
  229. memset(&cmd, 0, sizeof(cmd));
  230. address &= PAGE_MASK;
  231. CMD_SET_TYPE(&cmd, CMD_INV_IOMMU_PAGES);
  232. cmd.data[1] |= domid;
  233. cmd.data[2] = lower_32_bits(address);
  234. cmd.data[3] = upper_32_bits(address);
  235. if (s) /* size bit - we flush more than one 4kb page */
  236. cmd.data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  237. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  238. cmd.data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  239. ret = iommu_queue_command(iommu, &cmd);
  240. return ret;
  241. }
  242. /*
  243. * TLB invalidation function which is called from the mapping functions.
  244. * It invalidates a single PTE if the range to flush is within a single
  245. * page. Otherwise it flushes the whole TLB of the IOMMU.
  246. */
  247. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  248. u64 address, size_t size)
  249. {
  250. int s = 0;
  251. unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
  252. address &= PAGE_MASK;
  253. if (pages > 1) {
  254. /*
  255. * If we have to flush more than one page, flush all
  256. * TLB entries for this domain
  257. */
  258. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  259. s = 1;
  260. }
  261. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  262. return 0;
  263. }
  264. /* Flush the whole IO/TLB for a given protection domain */
  265. static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
  266. {
  267. u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  268. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
  269. }
  270. /****************************************************************************
  271. *
  272. * The functions below are used the create the page table mappings for
  273. * unity mapped regions.
  274. *
  275. ****************************************************************************/
  276. /*
  277. * Generic mapping functions. It maps a physical address into a DMA
  278. * address space. It allocates the page table pages if necessary.
  279. * In the future it can be extended to a generic mapping function
  280. * supporting all features of AMD IOMMU page tables like level skipping
  281. * and full 64 bit address spaces.
  282. */
  283. static int iommu_map(struct protection_domain *dom,
  284. unsigned long bus_addr,
  285. unsigned long phys_addr,
  286. int prot)
  287. {
  288. u64 __pte, *pte, *page;
  289. bus_addr = PAGE_ALIGN(bus_addr);
  290. phys_addr = PAGE_ALIGN(phys_addr);
  291. /* only support 512GB address spaces for now */
  292. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  293. return -EINVAL;
  294. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  295. if (!IOMMU_PTE_PRESENT(*pte)) {
  296. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  297. if (!page)
  298. return -ENOMEM;
  299. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  300. }
  301. pte = IOMMU_PTE_PAGE(*pte);
  302. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  303. if (!IOMMU_PTE_PRESENT(*pte)) {
  304. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  305. if (!page)
  306. return -ENOMEM;
  307. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  308. }
  309. pte = IOMMU_PTE_PAGE(*pte);
  310. pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
  311. if (IOMMU_PTE_PRESENT(*pte))
  312. return -EBUSY;
  313. __pte = phys_addr | IOMMU_PTE_P;
  314. if (prot & IOMMU_PROT_IR)
  315. __pte |= IOMMU_PTE_IR;
  316. if (prot & IOMMU_PROT_IW)
  317. __pte |= IOMMU_PTE_IW;
  318. *pte = __pte;
  319. return 0;
  320. }
  321. /*
  322. * This function checks if a specific unity mapping entry is needed for
  323. * this specific IOMMU.
  324. */
  325. static int iommu_for_unity_map(struct amd_iommu *iommu,
  326. struct unity_map_entry *entry)
  327. {
  328. u16 bdf, i;
  329. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  330. bdf = amd_iommu_alias_table[i];
  331. if (amd_iommu_rlookup_table[bdf] == iommu)
  332. return 1;
  333. }
  334. return 0;
  335. }
  336. /*
  337. * Init the unity mappings for a specific IOMMU in the system
  338. *
  339. * Basically iterates over all unity mapping entries and applies them to
  340. * the default domain DMA of that IOMMU if necessary.
  341. */
  342. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  343. {
  344. struct unity_map_entry *entry;
  345. int ret;
  346. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  347. if (!iommu_for_unity_map(iommu, entry))
  348. continue;
  349. ret = dma_ops_unity_map(iommu->default_dom, entry);
  350. if (ret)
  351. return ret;
  352. }
  353. return 0;
  354. }
  355. /*
  356. * This function actually applies the mapping to the page table of the
  357. * dma_ops domain.
  358. */
  359. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  360. struct unity_map_entry *e)
  361. {
  362. u64 addr;
  363. int ret;
  364. for (addr = e->address_start; addr < e->address_end;
  365. addr += PAGE_SIZE) {
  366. ret = iommu_map(&dma_dom->domain, addr, addr, e->prot);
  367. if (ret)
  368. return ret;
  369. /*
  370. * if unity mapping is in aperture range mark the page
  371. * as allocated in the aperture
  372. */
  373. if (addr < dma_dom->aperture_size)
  374. __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
  375. }
  376. return 0;
  377. }
  378. /*
  379. * Inits the unity mappings required for a specific device
  380. */
  381. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  382. u16 devid)
  383. {
  384. struct unity_map_entry *e;
  385. int ret;
  386. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  387. if (!(devid >= e->devid_start && devid <= e->devid_end))
  388. continue;
  389. ret = dma_ops_unity_map(dma_dom, e);
  390. if (ret)
  391. return ret;
  392. }
  393. return 0;
  394. }
  395. /****************************************************************************
  396. *
  397. * The next functions belong to the address allocator for the dma_ops
  398. * interface functions. They work like the allocators in the other IOMMU
  399. * drivers. Its basically a bitmap which marks the allocated pages in
  400. * the aperture. Maybe it could be enhanced in the future to a more
  401. * efficient allocator.
  402. *
  403. ****************************************************************************/
  404. /*
  405. * The address allocator core function.
  406. *
  407. * called with domain->lock held
  408. */
  409. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  410. struct dma_ops_domain *dom,
  411. unsigned int pages,
  412. unsigned long align_mask,
  413. u64 dma_mask)
  414. {
  415. unsigned long limit;
  416. unsigned long address;
  417. unsigned long boundary_size;
  418. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  419. PAGE_SIZE) >> PAGE_SHIFT;
  420. limit = iommu_device_max_index(dom->aperture_size >> PAGE_SHIFT, 0,
  421. dma_mask >> PAGE_SHIFT);
  422. if (dom->next_bit >= limit) {
  423. dom->next_bit = 0;
  424. dom->need_flush = true;
  425. }
  426. address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
  427. 0 , boundary_size, align_mask);
  428. if (address == -1) {
  429. address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
  430. 0, boundary_size, align_mask);
  431. dom->need_flush = true;
  432. }
  433. if (likely(address != -1)) {
  434. dom->next_bit = address + pages;
  435. address <<= PAGE_SHIFT;
  436. } else
  437. address = bad_dma_address;
  438. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  439. return address;
  440. }
  441. /*
  442. * The address free function.
  443. *
  444. * called with domain->lock held
  445. */
  446. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  447. unsigned long address,
  448. unsigned int pages)
  449. {
  450. address >>= PAGE_SHIFT;
  451. iommu_area_free(dom->bitmap, address, pages);
  452. if (address >= dom->next_bit)
  453. dom->need_flush = true;
  454. }
  455. /****************************************************************************
  456. *
  457. * The next functions belong to the domain allocation. A domain is
  458. * allocated for every IOMMU as the default domain. If device isolation
  459. * is enabled, every device get its own domain. The most important thing
  460. * about domains is the page table mapping the DMA address space they
  461. * contain.
  462. *
  463. ****************************************************************************/
  464. static u16 domain_id_alloc(void)
  465. {
  466. unsigned long flags;
  467. int id;
  468. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  469. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  470. BUG_ON(id == 0);
  471. if (id > 0 && id < MAX_DOMAIN_ID)
  472. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  473. else
  474. id = 0;
  475. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  476. return id;
  477. }
  478. /*
  479. * Used to reserve address ranges in the aperture (e.g. for exclusion
  480. * ranges.
  481. */
  482. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  483. unsigned long start_page,
  484. unsigned int pages)
  485. {
  486. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  487. if (start_page + pages > last_page)
  488. pages = last_page - start_page;
  489. iommu_area_reserve(dom->bitmap, start_page, pages);
  490. }
  491. static void dma_ops_free_pagetable(struct dma_ops_domain *dma_dom)
  492. {
  493. int i, j;
  494. u64 *p1, *p2, *p3;
  495. p1 = dma_dom->domain.pt_root;
  496. if (!p1)
  497. return;
  498. for (i = 0; i < 512; ++i) {
  499. if (!IOMMU_PTE_PRESENT(p1[i]))
  500. continue;
  501. p2 = IOMMU_PTE_PAGE(p1[i]);
  502. for (j = 0; j < 512; ++j) {
  503. if (!IOMMU_PTE_PRESENT(p2[j]))
  504. continue;
  505. p3 = IOMMU_PTE_PAGE(p2[j]);
  506. free_page((unsigned long)p3);
  507. }
  508. free_page((unsigned long)p2);
  509. }
  510. free_page((unsigned long)p1);
  511. }
  512. /*
  513. * Free a domain, only used if something went wrong in the
  514. * allocation path and we need to free an already allocated page table
  515. */
  516. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  517. {
  518. if (!dom)
  519. return;
  520. dma_ops_free_pagetable(dom);
  521. kfree(dom->pte_pages);
  522. kfree(dom->bitmap);
  523. kfree(dom);
  524. }
  525. /*
  526. * Allocates a new protection domain usable for the dma_ops functions.
  527. * It also intializes the page table and the address allocator data
  528. * structures required for the dma_ops interface
  529. */
  530. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  531. unsigned order)
  532. {
  533. struct dma_ops_domain *dma_dom;
  534. unsigned i, num_pte_pages;
  535. u64 *l2_pde;
  536. u64 address;
  537. /*
  538. * Currently the DMA aperture must be between 32 MB and 1GB in size
  539. */
  540. if ((order < 25) || (order > 30))
  541. return NULL;
  542. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  543. if (!dma_dom)
  544. return NULL;
  545. spin_lock_init(&dma_dom->domain.lock);
  546. dma_dom->domain.id = domain_id_alloc();
  547. if (dma_dom->domain.id == 0)
  548. goto free_dma_dom;
  549. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  550. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  551. dma_dom->domain.priv = dma_dom;
  552. if (!dma_dom->domain.pt_root)
  553. goto free_dma_dom;
  554. dma_dom->aperture_size = (1ULL << order);
  555. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  556. GFP_KERNEL);
  557. if (!dma_dom->bitmap)
  558. goto free_dma_dom;
  559. /*
  560. * mark the first page as allocated so we never return 0 as
  561. * a valid dma-address. So we can use 0 as error value
  562. */
  563. dma_dom->bitmap[0] = 1;
  564. dma_dom->next_bit = 0;
  565. dma_dom->need_flush = false;
  566. dma_dom->target_dev = 0xffff;
  567. /* Intialize the exclusion range if necessary */
  568. if (iommu->exclusion_start &&
  569. iommu->exclusion_start < dma_dom->aperture_size) {
  570. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  571. int pages = iommu_num_pages(iommu->exclusion_start,
  572. iommu->exclusion_length,
  573. PAGE_SIZE);
  574. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  575. }
  576. /*
  577. * At the last step, build the page tables so we don't need to
  578. * allocate page table pages in the dma_ops mapping/unmapping
  579. * path.
  580. */
  581. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  582. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  583. GFP_KERNEL);
  584. if (!dma_dom->pte_pages)
  585. goto free_dma_dom;
  586. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  587. if (l2_pde == NULL)
  588. goto free_dma_dom;
  589. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  590. for (i = 0; i < num_pte_pages; ++i) {
  591. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  592. if (!dma_dom->pte_pages[i])
  593. goto free_dma_dom;
  594. address = virt_to_phys(dma_dom->pte_pages[i]);
  595. l2_pde[i] = IOMMU_L1_PDE(address);
  596. }
  597. return dma_dom;
  598. free_dma_dom:
  599. dma_ops_domain_free(dma_dom);
  600. return NULL;
  601. }
  602. /*
  603. * Find out the protection domain structure for a given PCI device. This
  604. * will give us the pointer to the page table root for example.
  605. */
  606. static struct protection_domain *domain_for_device(u16 devid)
  607. {
  608. struct protection_domain *dom;
  609. unsigned long flags;
  610. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  611. dom = amd_iommu_pd_table[devid];
  612. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  613. return dom;
  614. }
  615. /*
  616. * If a device is not yet associated with a domain, this function does
  617. * assigns it visible for the hardware
  618. */
  619. static void set_device_domain(struct amd_iommu *iommu,
  620. struct protection_domain *domain,
  621. u16 devid)
  622. {
  623. unsigned long flags;
  624. u64 pte_root = virt_to_phys(domain->pt_root);
  625. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  626. << DEV_ENTRY_MODE_SHIFT;
  627. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  628. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  629. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  630. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  631. amd_iommu_dev_table[devid].data[2] = domain->id;
  632. amd_iommu_pd_table[devid] = domain;
  633. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  634. iommu_queue_inv_dev_entry(iommu, devid);
  635. }
  636. /*****************************************************************************
  637. *
  638. * The next functions belong to the dma_ops mapping/unmapping code.
  639. *
  640. *****************************************************************************/
  641. /*
  642. * This function checks if the driver got a valid device from the caller to
  643. * avoid dereferencing invalid pointers.
  644. */
  645. static bool check_device(struct device *dev)
  646. {
  647. if (!dev || !dev->dma_mask)
  648. return false;
  649. return true;
  650. }
  651. /*
  652. * In this function the list of preallocated protection domains is traversed to
  653. * find the domain for a specific device
  654. */
  655. static struct dma_ops_domain *find_protection_domain(u16 devid)
  656. {
  657. struct dma_ops_domain *entry, *ret = NULL;
  658. unsigned long flags;
  659. if (list_empty(&iommu_pd_list))
  660. return NULL;
  661. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  662. list_for_each_entry(entry, &iommu_pd_list, list) {
  663. if (entry->target_dev == devid) {
  664. ret = entry;
  665. list_del(&ret->list);
  666. break;
  667. }
  668. }
  669. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  670. return ret;
  671. }
  672. /*
  673. * In the dma_ops path we only have the struct device. This function
  674. * finds the corresponding IOMMU, the protection domain and the
  675. * requestor id for a given device.
  676. * If the device is not yet associated with a domain this is also done
  677. * in this function.
  678. */
  679. static int get_device_resources(struct device *dev,
  680. struct amd_iommu **iommu,
  681. struct protection_domain **domain,
  682. u16 *bdf)
  683. {
  684. struct dma_ops_domain *dma_dom;
  685. struct pci_dev *pcidev;
  686. u16 _bdf;
  687. *iommu = NULL;
  688. *domain = NULL;
  689. *bdf = 0xffff;
  690. if (dev->bus != &pci_bus_type)
  691. return 0;
  692. pcidev = to_pci_dev(dev);
  693. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  694. /* device not translated by any IOMMU in the system? */
  695. if (_bdf > amd_iommu_last_bdf)
  696. return 0;
  697. *bdf = amd_iommu_alias_table[_bdf];
  698. *iommu = amd_iommu_rlookup_table[*bdf];
  699. if (*iommu == NULL)
  700. return 0;
  701. *domain = domain_for_device(*bdf);
  702. if (*domain == NULL) {
  703. dma_dom = find_protection_domain(*bdf);
  704. if (!dma_dom)
  705. dma_dom = (*iommu)->default_dom;
  706. *domain = &dma_dom->domain;
  707. set_device_domain(*iommu, *domain, *bdf);
  708. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  709. "device ", (*domain)->id);
  710. print_devid(_bdf, 1);
  711. }
  712. if (domain_for_device(_bdf) == NULL)
  713. set_device_domain(*iommu, *domain, _bdf);
  714. return 1;
  715. }
  716. /*
  717. * This is the generic map function. It maps one 4kb page at paddr to
  718. * the given address in the DMA address space for the domain.
  719. */
  720. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  721. struct dma_ops_domain *dom,
  722. unsigned long address,
  723. phys_addr_t paddr,
  724. int direction)
  725. {
  726. u64 *pte, __pte;
  727. WARN_ON(address > dom->aperture_size);
  728. paddr &= PAGE_MASK;
  729. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  730. pte += IOMMU_PTE_L0_INDEX(address);
  731. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  732. if (direction == DMA_TO_DEVICE)
  733. __pte |= IOMMU_PTE_IR;
  734. else if (direction == DMA_FROM_DEVICE)
  735. __pte |= IOMMU_PTE_IW;
  736. else if (direction == DMA_BIDIRECTIONAL)
  737. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  738. WARN_ON(*pte);
  739. *pte = __pte;
  740. return (dma_addr_t)address;
  741. }
  742. /*
  743. * The generic unmapping function for on page in the DMA address space.
  744. */
  745. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  746. struct dma_ops_domain *dom,
  747. unsigned long address)
  748. {
  749. u64 *pte;
  750. if (address >= dom->aperture_size)
  751. return;
  752. WARN_ON(address & ~PAGE_MASK || address >= dom->aperture_size);
  753. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  754. pte += IOMMU_PTE_L0_INDEX(address);
  755. WARN_ON(!*pte);
  756. *pte = 0ULL;
  757. }
  758. /*
  759. * This function contains common code for mapping of a physically
  760. * contiguous memory region into DMA address space. It is used by all
  761. * mapping functions provided with this IOMMU driver.
  762. * Must be called with the domain lock held.
  763. */
  764. static dma_addr_t __map_single(struct device *dev,
  765. struct amd_iommu *iommu,
  766. struct dma_ops_domain *dma_dom,
  767. phys_addr_t paddr,
  768. size_t size,
  769. int dir,
  770. bool align,
  771. u64 dma_mask)
  772. {
  773. dma_addr_t offset = paddr & ~PAGE_MASK;
  774. dma_addr_t address, start;
  775. unsigned int pages;
  776. unsigned long align_mask = 0;
  777. int i;
  778. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  779. paddr &= PAGE_MASK;
  780. if (align)
  781. align_mask = (1UL << get_order(size)) - 1;
  782. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  783. dma_mask);
  784. if (unlikely(address == bad_dma_address))
  785. goto out;
  786. start = address;
  787. for (i = 0; i < pages; ++i) {
  788. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  789. paddr += PAGE_SIZE;
  790. start += PAGE_SIZE;
  791. }
  792. address += offset;
  793. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  794. iommu_flush_tlb(iommu, dma_dom->domain.id);
  795. dma_dom->need_flush = false;
  796. } else if (unlikely(iommu_has_npcache(iommu)))
  797. iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
  798. out:
  799. return address;
  800. }
  801. /*
  802. * Does the reverse of the __map_single function. Must be called with
  803. * the domain lock held too
  804. */
  805. static void __unmap_single(struct amd_iommu *iommu,
  806. struct dma_ops_domain *dma_dom,
  807. dma_addr_t dma_addr,
  808. size_t size,
  809. int dir)
  810. {
  811. dma_addr_t i, start;
  812. unsigned int pages;
  813. if ((dma_addr == bad_dma_address) ||
  814. (dma_addr + size > dma_dom->aperture_size))
  815. return;
  816. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  817. dma_addr &= PAGE_MASK;
  818. start = dma_addr;
  819. for (i = 0; i < pages; ++i) {
  820. dma_ops_domain_unmap(iommu, dma_dom, start);
  821. start += PAGE_SIZE;
  822. }
  823. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  824. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  825. iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
  826. dma_dom->need_flush = false;
  827. }
  828. }
  829. /*
  830. * The exported map_single function for dma_ops.
  831. */
  832. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  833. size_t size, int dir)
  834. {
  835. unsigned long flags;
  836. struct amd_iommu *iommu;
  837. struct protection_domain *domain;
  838. u16 devid;
  839. dma_addr_t addr;
  840. u64 dma_mask;
  841. if (!check_device(dev))
  842. return bad_dma_address;
  843. dma_mask = *dev->dma_mask;
  844. get_device_resources(dev, &iommu, &domain, &devid);
  845. if (iommu == NULL || domain == NULL)
  846. /* device not handled by any AMD IOMMU */
  847. return (dma_addr_t)paddr;
  848. spin_lock_irqsave(&domain->lock, flags);
  849. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
  850. dma_mask);
  851. if (addr == bad_dma_address)
  852. goto out;
  853. iommu_completion_wait(iommu);
  854. out:
  855. spin_unlock_irqrestore(&domain->lock, flags);
  856. return addr;
  857. }
  858. /*
  859. * The exported unmap_single function for dma_ops.
  860. */
  861. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  862. size_t size, int dir)
  863. {
  864. unsigned long flags;
  865. struct amd_iommu *iommu;
  866. struct protection_domain *domain;
  867. u16 devid;
  868. if (!check_device(dev) ||
  869. !get_device_resources(dev, &iommu, &domain, &devid))
  870. /* device not handled by any AMD IOMMU */
  871. return;
  872. spin_lock_irqsave(&domain->lock, flags);
  873. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  874. iommu_completion_wait(iommu);
  875. spin_unlock_irqrestore(&domain->lock, flags);
  876. }
  877. /*
  878. * This is a special map_sg function which is used if we should map a
  879. * device which is not handled by an AMD IOMMU in the system.
  880. */
  881. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  882. int nelems, int dir)
  883. {
  884. struct scatterlist *s;
  885. int i;
  886. for_each_sg(sglist, s, nelems, i) {
  887. s->dma_address = (dma_addr_t)sg_phys(s);
  888. s->dma_length = s->length;
  889. }
  890. return nelems;
  891. }
  892. /*
  893. * The exported map_sg function for dma_ops (handles scatter-gather
  894. * lists).
  895. */
  896. static int map_sg(struct device *dev, struct scatterlist *sglist,
  897. int nelems, int dir)
  898. {
  899. unsigned long flags;
  900. struct amd_iommu *iommu;
  901. struct protection_domain *domain;
  902. u16 devid;
  903. int i;
  904. struct scatterlist *s;
  905. phys_addr_t paddr;
  906. int mapped_elems = 0;
  907. u64 dma_mask;
  908. if (!check_device(dev))
  909. return 0;
  910. dma_mask = *dev->dma_mask;
  911. get_device_resources(dev, &iommu, &domain, &devid);
  912. if (!iommu || !domain)
  913. return map_sg_no_iommu(dev, sglist, nelems, dir);
  914. spin_lock_irqsave(&domain->lock, flags);
  915. for_each_sg(sglist, s, nelems, i) {
  916. paddr = sg_phys(s);
  917. s->dma_address = __map_single(dev, iommu, domain->priv,
  918. paddr, s->length, dir, false,
  919. dma_mask);
  920. if (s->dma_address) {
  921. s->dma_length = s->length;
  922. mapped_elems++;
  923. } else
  924. goto unmap;
  925. }
  926. iommu_completion_wait(iommu);
  927. out:
  928. spin_unlock_irqrestore(&domain->lock, flags);
  929. return mapped_elems;
  930. unmap:
  931. for_each_sg(sglist, s, mapped_elems, i) {
  932. if (s->dma_address)
  933. __unmap_single(iommu, domain->priv, s->dma_address,
  934. s->dma_length, dir);
  935. s->dma_address = s->dma_length = 0;
  936. }
  937. mapped_elems = 0;
  938. goto out;
  939. }
  940. /*
  941. * The exported map_sg function for dma_ops (handles scatter-gather
  942. * lists).
  943. */
  944. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  945. int nelems, int dir)
  946. {
  947. unsigned long flags;
  948. struct amd_iommu *iommu;
  949. struct protection_domain *domain;
  950. struct scatterlist *s;
  951. u16 devid;
  952. int i;
  953. if (!check_device(dev) ||
  954. !get_device_resources(dev, &iommu, &domain, &devid))
  955. return;
  956. spin_lock_irqsave(&domain->lock, flags);
  957. for_each_sg(sglist, s, nelems, i) {
  958. __unmap_single(iommu, domain->priv, s->dma_address,
  959. s->dma_length, dir);
  960. s->dma_address = s->dma_length = 0;
  961. }
  962. iommu_completion_wait(iommu);
  963. spin_unlock_irqrestore(&domain->lock, flags);
  964. }
  965. /*
  966. * The exported alloc_coherent function for dma_ops.
  967. */
  968. static void *alloc_coherent(struct device *dev, size_t size,
  969. dma_addr_t *dma_addr, gfp_t flag)
  970. {
  971. unsigned long flags;
  972. void *virt_addr;
  973. struct amd_iommu *iommu;
  974. struct protection_domain *domain;
  975. u16 devid;
  976. phys_addr_t paddr;
  977. u64 dma_mask = dev->coherent_dma_mask;
  978. if (!check_device(dev))
  979. return NULL;
  980. if (!get_device_resources(dev, &iommu, &domain, &devid))
  981. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  982. flag |= __GFP_ZERO;
  983. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  984. if (!virt_addr)
  985. return 0;
  986. paddr = virt_to_phys(virt_addr);
  987. if (!iommu || !domain) {
  988. *dma_addr = (dma_addr_t)paddr;
  989. return virt_addr;
  990. }
  991. if (!dma_mask)
  992. dma_mask = *dev->dma_mask;
  993. spin_lock_irqsave(&domain->lock, flags);
  994. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  995. size, DMA_BIDIRECTIONAL, true, dma_mask);
  996. if (*dma_addr == bad_dma_address) {
  997. free_pages((unsigned long)virt_addr, get_order(size));
  998. virt_addr = NULL;
  999. goto out;
  1000. }
  1001. iommu_completion_wait(iommu);
  1002. out:
  1003. spin_unlock_irqrestore(&domain->lock, flags);
  1004. return virt_addr;
  1005. }
  1006. /*
  1007. * The exported free_coherent function for dma_ops.
  1008. */
  1009. static void free_coherent(struct device *dev, size_t size,
  1010. void *virt_addr, dma_addr_t dma_addr)
  1011. {
  1012. unsigned long flags;
  1013. struct amd_iommu *iommu;
  1014. struct protection_domain *domain;
  1015. u16 devid;
  1016. if (!check_device(dev))
  1017. return;
  1018. get_device_resources(dev, &iommu, &domain, &devid);
  1019. if (!iommu || !domain)
  1020. goto free_mem;
  1021. spin_lock_irqsave(&domain->lock, flags);
  1022. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1023. iommu_completion_wait(iommu);
  1024. spin_unlock_irqrestore(&domain->lock, flags);
  1025. free_mem:
  1026. free_pages((unsigned long)virt_addr, get_order(size));
  1027. }
  1028. /*
  1029. * This function is called by the DMA layer to find out if we can handle a
  1030. * particular device. It is part of the dma_ops.
  1031. */
  1032. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1033. {
  1034. u16 bdf;
  1035. struct pci_dev *pcidev;
  1036. /* No device or no PCI device */
  1037. if (!dev || dev->bus != &pci_bus_type)
  1038. return 0;
  1039. pcidev = to_pci_dev(dev);
  1040. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1041. /* Out of our scope? */
  1042. if (bdf > amd_iommu_last_bdf)
  1043. return 0;
  1044. return 1;
  1045. }
  1046. /*
  1047. * The function for pre-allocating protection domains.
  1048. *
  1049. * If the driver core informs the DMA layer if a driver grabs a device
  1050. * we don't need to preallocate the protection domains anymore.
  1051. * For now we have to.
  1052. */
  1053. static void prealloc_protection_domains(void)
  1054. {
  1055. struct pci_dev *dev = NULL;
  1056. struct dma_ops_domain *dma_dom;
  1057. struct amd_iommu *iommu;
  1058. int order = amd_iommu_aperture_order;
  1059. u16 devid;
  1060. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1061. devid = (dev->bus->number << 8) | dev->devfn;
  1062. if (devid > amd_iommu_last_bdf)
  1063. continue;
  1064. devid = amd_iommu_alias_table[devid];
  1065. if (domain_for_device(devid))
  1066. continue;
  1067. iommu = amd_iommu_rlookup_table[devid];
  1068. if (!iommu)
  1069. continue;
  1070. dma_dom = dma_ops_domain_alloc(iommu, order);
  1071. if (!dma_dom)
  1072. continue;
  1073. init_unity_mappings_for_device(dma_dom, devid);
  1074. dma_dom->target_dev = devid;
  1075. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1076. }
  1077. }
  1078. static struct dma_mapping_ops amd_iommu_dma_ops = {
  1079. .alloc_coherent = alloc_coherent,
  1080. .free_coherent = free_coherent,
  1081. .map_single = map_single,
  1082. .unmap_single = unmap_single,
  1083. .map_sg = map_sg,
  1084. .unmap_sg = unmap_sg,
  1085. .dma_supported = amd_iommu_dma_supported,
  1086. };
  1087. /*
  1088. * The function which clues the AMD IOMMU driver into dma_ops.
  1089. */
  1090. int __init amd_iommu_init_dma_ops(void)
  1091. {
  1092. struct amd_iommu *iommu;
  1093. int order = amd_iommu_aperture_order;
  1094. int ret;
  1095. /*
  1096. * first allocate a default protection domain for every IOMMU we
  1097. * found in the system. Devices not assigned to any other
  1098. * protection domain will be assigned to the default one.
  1099. */
  1100. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1101. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  1102. if (iommu->default_dom == NULL)
  1103. return -ENOMEM;
  1104. ret = iommu_init_unity_mappings(iommu);
  1105. if (ret)
  1106. goto free_domains;
  1107. }
  1108. /*
  1109. * If device isolation is enabled, pre-allocate the protection
  1110. * domains for each device.
  1111. */
  1112. if (amd_iommu_isolate)
  1113. prealloc_protection_domains();
  1114. iommu_detected = 1;
  1115. force_iommu = 1;
  1116. bad_dma_address = 0;
  1117. #ifdef CONFIG_GART_IOMMU
  1118. gart_iommu_aperture_disabled = 1;
  1119. gart_iommu_aperture = 0;
  1120. #endif
  1121. /* Make the driver finally visible to the drivers */
  1122. dma_ops = &amd_iommu_dma_ops;
  1123. return 0;
  1124. free_domains:
  1125. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1126. if (iommu->default_dom)
  1127. dma_ops_domain_free(iommu->default_dom);
  1128. }
  1129. return ret;
  1130. }