amd_iommu.c 50 KB

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