amd_iommu.c 46 KB

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