amd_iommu.c 58 KB

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