amd_iommu.c 35 KB

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