amd_iommu.c 41 KB

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