amd_iommu.c 34 KB

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