amd_iommu.c 46 KB

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